-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
89 lines (74 loc) · 2.68 KB
/
Program.cs
File metadata and controls
89 lines (74 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
//--- Program.cs ----
#nullable disable
using System.Text.Json;
using OpenAI.Chat;
using static System.Net.Mime.MediaTypeNames;
using static System.Environment;
using Azure;
using OpenAI;
using Azure.Core;
using Azure.Security.KeyVault.Secrets;
namespace MyAzure.OpenAI;
internal class Program
{
static async Task Main(string[] args)
{
Console.WriteLine($"Start the OpenAI service\n\r");
string questText =
" 1) How much is 7+ 5*5 ?\n\r" +
" 2) Jaka jest stolica Włoch?\n\r" +
" 3) Ist BMW ein deutsches Auto?\n\r";
var cOpenAIUse = new OpenAIUse();
await cOpenAIUse.BasicChat(questText);
// Output: Assistant:
// 1) The sum of 7 and 5 multiplied by 5 is 32.
// 2) Stolica Włoch to Rzym.
// 3) Ja, BMW ist ein deutsches Auto.
}
}
/// <summary>
/// OpenAI on Azure Usage Class
/// </summary>
public class OpenAIUse
{
//class CAzureOpenAIUtils contains sensitive connection strings to the OpenAI service on Azure
private readonly CAzureOpenAIUtils _openAIUtils;
public OpenAIUse()
{
_openAIUtils = new CAzureOpenAIUtils();
}
public async Task BasicChat(string userQuestion)
{
string systemText = "You are a helpful assistant. Answer in languages like questions.";
// Prepare messages
var messList = new List<ChatMessage>();
messList.Add(new SystemChatMessage(systemText));
messList.Add(new UserChatMessage(userQuestion));
var options = new ChatCompletionOptions { MaxOutputTokenCount = 800 };
try
{
// Create a chat completion request
ChatCompletion completion = await _openAIUtils.AskHistoryQuestionComp(messList, options);
Console.WriteLine($"Question: \n\r{userQuestion}");
// Print answer
if (completion != null)
{
Console.WriteLine($"\n\rThe answer AI:");
Console.WriteLine($"{completion.Role}: \n\r{completion.Content[0].Text}");
Console.WriteLine($"\n\rThe answer params:");
Console.WriteLine(JsonSerializer.Serialize(completion, new JsonSerializerOptions()
{ WriteIndented = true }));
//ChatCompletion completion = await this.chatClient.CompleteChatAsync(myQuest); // CompleteChat(myQuest);
//Console.WriteLine($"{completion.Role}: {completion.Content[0].Text}");
}
else
{
Console.WriteLine("No response received.");
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}
}