Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Azure OpenAI SDK #66

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -420,3 +420,11 @@ local.settings.json

# Ignore hidden folder for AI Hub Installation
**/.aihub

# appsettings.json
appsettings.*.json
!appsettings.json
!appsettings.Development.json
!appsettings.Production.json
!appsettings.Staging.json
!appsettings.Test.json
14 changes: 7 additions & 7 deletions src/AIHub/AIHub.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -9,11 +9,11 @@
<ItemGroup>
<PackageReference Include="Azure.AI.ContentSafety" Version="1.0.0" />
<PackageReference Include="Azure.AI.FormRecognizer" Version="4.1.0" />
<PackageReference Include="Azure.AI.OpenAI" Version="1.0.0-beta.13" />
<PackageReference Include="Azure.AI.Vision.ImageAnalysis" Version="1.0.0-beta.2" />
<PackageReference Include="Azure.Identity" Version="1.11.4" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.18.0" />
<PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.36.0" />
<PackageReference Include="Azure.AI.OpenAI" Version="2.0.0-beta.2" />
<PackageReference Include="Azure.AI.Vision.ImageAnalysis" Version="1.0.0-beta.3" />
<PackageReference Include="Azure.Identity" Version="1.12.0" />
<PackageReference Include="Azure.Storage.Blobs" Version="12.20.0" />
<PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.38.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

Expand All @@ -24,7 +24,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MinVer" Version="4.3.0">
<PackageReference Include="MinVer" Version="5.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
16 changes: 4 additions & 12 deletions src/AIHub/Controllers/AudioTranscriptionController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,14 @@ public IActionResult Error()
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

private bool CheckNullValues(IFormFile? imageFile)
private static bool CheckNullValues(IFormFile? imageFile)
{
if (imageFile == null)
{
return true;
}
return false;
return imageFile == null;
}

private bool CheckImageExtension(string blobUri)
private static bool CheckImageExtension(string blobUri)
{
string uri_lower = blobUri;
if (uri_lower.Contains(".mp3", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return true;
return !uri_lower.Contains(@".mp3", StringComparison.OrdinalIgnoreCase);
}
}
65 changes: 27 additions & 38 deletions src/AIHub/Controllers/BrandAnalyzerController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using OpenAI.Chat;

namespace MVCWeb.Controllers;

public class BrandAnalyzerController : Controller
Expand Down Expand Up @@ -76,42 +78,33 @@ public async Task<IActionResult> AnalyzeCompany()

try
{
Uri aoaiEndpointUri = new(AOAIendpoint);

AzureOpenAIClient azureClient = string.IsNullOrEmpty(AOAIsubscriptionKey)
? new(aoaiEndpointUri, new DefaultAzureCredential())
: new(aoaiEndpointUri, new AzureKeyCredential(AOAIsubscriptionKey));

ChatClient chatClient = azureClient.GetChatClient(AOAIDeploymentName);

OpenAIClient aoaiClient;
if (string.IsNullOrEmpty(AOAIsubscriptionKey))
var messages = new ChatMessage[]
{
aoaiClient = new OpenAIClient(
new Uri(AOAIendpoint),
new DefaultAzureCredential());
}
else
new SystemChatMessage($"I will provide a list results from opinions on the Internet about {model.CompanyName} Bing search. If {model.CompanyName} is not a company what the user is asking for, answer to provide a new Company name. The user will ask you what they want to get from the company opinions. \n Results: {input_context}"),
new UserChatMessage(model.Prompt),
};

ChatCompletionOptions chatCompletionOptions = new()
{
aoaiClient = new OpenAIClient(
new Uri(AOAIendpoint),
new AzureKeyCredential(AOAIsubscriptionKey));
}

Temperature = 0.7f,
MaxTokens = 1000,
TopP = 0.95f,
FrequencyPenalty = 0,
PresencePenalty = 0,
};

ChatCompletion completion = await chatClient.CompleteChatAsync(messages, chatCompletionOptions);

// If streaming is not selected
Response<ChatCompletions> responseWithoutStream = await aoaiClient.GetChatCompletionsAsync(
new ChatCompletionsOptions()
{
DeploymentName = AOAIDeploymentName,
Messages =
{
new ChatRequestSystemMessage(@"I will provide a list results from opinons on the internet about "+model.CompanyName+" Bing search. If "+model.CompanyName+" is not a company what the user is asking for, answer to provide a new Company name. The user will ask you what they want to get from the compay opinions. \n Results: "+ input_context),
new ChatRequestUserMessage(model.Prompt),
},
Temperature = (float)0.7,
MaxTokens = 1000,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
});

ChatCompletions completions = responseWithoutStream.Value;
ChatChoice results_analisis = completions.Choices[0];
model.Message = results_analisis.Message.Content;
ViewBag.Message = results_analisis.Message.Content;
ViewBag.Message = completion.Content[0].Text;
}
catch (RequestFailedException)
{
Expand All @@ -127,12 +120,8 @@ public IActionResult Error()
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

private bool CheckNullValues(string? companyName)
private static bool CheckNullValues(string? companyName)
{
if (string.IsNullOrEmpty(companyName))
{
return true;
}
return false;
return string.IsNullOrEmpty(companyName);
}
}
63 changes: 27 additions & 36 deletions src/AIHub/Controllers/CallCenterController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using OpenAI.Chat;

namespace MVCWeb.Controllers;

public class CallCenterController : Controller
Expand Down Expand Up @@ -34,43 +36,32 @@ public async Task<IActionResult> AnalyzeCall()
}
try
{
OpenAIClient aoaiClient;
if (string.IsNullOrEmpty(subscriptionKey))
Uri aoaiEndpointUri = new(endpoint);

AzureOpenAIClient azureClient = string.IsNullOrEmpty(subscriptionKey)
? new(aoaiEndpointUri, new DefaultAzureCredential())
: new(aoaiEndpointUri, new AzureKeyCredential(subscriptionKey));

ChatClient chatClient = azureClient.GetChatClient(AOAIDeploymentName);

var messages = new ChatMessage[]
{
aoaiClient = new OpenAIClient(
new Uri(endpoint),
new DefaultAzureCredential());
}
else
new SystemChatMessage(model.Prompt),
new UserChatMessage(@"Call transcript: "+model.Transcript),
};

ChatCompletionOptions chatCompletionOptions = new()
{
aoaiClient = new OpenAIClient(
new Uri(endpoint),
new AzureKeyCredential(subscriptionKey));
}

// If streaming is not selected
Response<ChatCompletions> responseWithoutStream = await aoaiClient.GetChatCompletionsAsync(
new ChatCompletionsOptions()
{
DeploymentName = AOAIDeploymentName,
Messages =
{
new ChatRequestSystemMessage(model.Prompt),
new ChatRequestUserMessage(@"Call transcript: "+model.Transcript),
},
Temperature = (float)0.1,
MaxTokens = 1000,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
});

ChatCompletions completions = responseWithoutStream.Value;
ChatChoice results_analisis = completions.Choices[0];
System.Console.WriteLine(results_analisis);
ViewBag.Message =
results_analisis.Message.Content
;
MaxTokens = 1000,
Temperature = 0.1f,
FrequencyPenalty = 0,
PresencePenalty = 0,
TopP = 0.95f,
};

ChatCompletion completion = await chatClient.CompleteChatAsync(messages, chatCompletionOptions);

ViewBag.Message = completion.Content[0].Text;
}
catch (RequestFailedException)
{
Expand All @@ -86,7 +77,7 @@ public IActionResult Error()
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

private bool CheckNullValues(string? companyName, string? prompt)
private static bool CheckNullValues(string? companyName, string? prompt)
{
if (string.IsNullOrEmpty(companyName))
{
Expand Down
77 changes: 29 additions & 48 deletions src/AIHub/Controllers/DocumentComparisonController.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using OpenAI.Chat;

namespace MVCWeb.Controllers;

public class DocumentComparisonController : Controller
Expand Down Expand Up @@ -60,41 +62,33 @@ public async Task<IActionResult> DocumentComparison(string[] document_urls, stri

try
{
OpenAIClient aoaiClient;
if (string.IsNullOrEmpty(AOAIsubscriptionKey))
Uri aoaiEndpointUri = new(AOAIendpoint);

AzureOpenAIClient azureClient = string.IsNullOrEmpty(AOAIsubscriptionKey)
? new(aoaiEndpointUri, new DefaultAzureCredential())
: new(aoaiEndpointUri, new AzureKeyCredential(AOAIsubscriptionKey));

ChatClient chatClient = azureClient.GetChatClient(AOAIDeploymentName);

var messages = new ChatMessage[]
{
aoaiClient = new OpenAIClient(
new Uri(AOAIendpoint),
new DefaultAzureCredential());
}
else
new SystemChatMessage($@"You are specialized in analyze different versions of the same PDF document. The first Document OCR result is: <<<{output_result[0]}>>> and the second Document OCR result is: <<<{output_result[1]}>>>"),
new UserChatMessage($@"User question: {prompt}"),
};

ChatCompletionOptions chatCompletionOptions = new()
{
aoaiClient = new OpenAIClient(
new Uri(AOAIendpoint),
new AzureKeyCredential(AOAIsubscriptionKey));
}
MaxTokens = 1000,
Temperature = 0.7f,
FrequencyPenalty = 0,
PresencePenalty = 0,
TopP = 0.95f
};

// ### If streaming is not selected
Response<ChatCompletions> responseWithoutStream = await aoaiClient.GetChatCompletionsAsync(
new ChatCompletionsOptions()
{
DeploymentName = AOAIDeploymentName,
Messages =
{
new ChatRequestSystemMessage(@"You are specialized in analyze different versions of the same PDF document. The first Document OCR result is: <<<"+output_result[0]+">>> and the second Document OCR result is: <<<"+output_result[1]+">>>"),
new ChatRequestUserMessage(@"User question: "+prompt ),
},
Temperature = (float)0.7,
MaxTokens = 1000,
NucleusSamplingFactor = (float)0.95,
FrequencyPenalty = 0,
PresencePenalty = 0,
});

ChatCompletions completions = responseWithoutStream.Value;
ChatChoice results_analisis = completions.Choices[0];
model.Message = results_analisis.Message.Content;
ViewBag.Message = results_analisis.Message.Content;
ChatCompletion completion = await chatClient.CompleteChatAsync(messages, chatCompletionOptions);

model.Message = completion.Content[0].Text;
ViewBag.Message = completion.Content[0].Text;
}
catch (RequestFailedException)
{
Expand Down Expand Up @@ -122,7 +116,7 @@ public async Task<IActionResult> UploadFile()
{
if (CheckImageExtension(documentFile.FileName.ToString()))
{
ViewBag.Message = "You must upload pdf documpents only";
ViewBag.Message = "You must upload pdf documents only";
return View("DocumentComparison", model);
}
}
Expand Down Expand Up @@ -156,21 +150,8 @@ public IActionResult Error()
return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
}

private bool CheckNullValues(IFormFile filename)
{
if (filename == null)
{
return true;
}
return false;
}

private bool CheckImageExtension(string filename)
private static bool CheckImageExtension(string filename)
{
if (filename.Contains(".pdf", StringComparison.OrdinalIgnoreCase))
{
return false;
}
return true;
return !filename.Contains(@".pdf", StringComparison.OrdinalIgnoreCase);
}
}
Loading
Loading