OpenAi in Azure using C#
Program.cs located in OpenAiTest solution
Requires Secrets.cs to be created with read-only AiModelName, WebsiteUri and Key properties.
Assumes: 1) Azure account 2) OpenAi account, 3) Azure OpenAi account and the Secrets.cs reflects the correct answers.
An alternative to this approach is to hit the OpenAi API directly.
using Azure.AI.OpenAI;
using OpenAiTest;
var options = new ChatCompletionsOptions();
options.Messages.Add(new ChatMessage(ChatRole.System, "You are friendly. You will be giving advice to Texas lawyers, so only use Texas state laws when quoting state laws."));
options.MaxTokens = 2000;
Console.Write("Welcome to the OpenAI Copilot Test Console App!");
string prompt = string.Empty;
var client = new OpenAIClient(new Uri(Secrets.WebsiteUri), new AzureKeyCredential(Secrets.Key));
while (true)
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write(">> ");
prompt = Console.ReadLine()!;
if (prompt.ToLower() == "exit")
{
break;
}
options.Messages.Add(new ChatMessage(ChatRole.User, prompt));
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine();
var response = await client.GetChatCompletionsAsync(Secrets.AiModelName, options);
Console.WriteLine(response.Value.Choices[0].Message.Content);
Console.WriteLine();
var currentResponse = string.Empty;
options.Messages.Add(new ChatMessage(ChatRole.Assistant, currentResponse));
}
Comments
Post a Comment