-
Notifications
You must be signed in to change notification settings - Fork 2
/
ChatService.cs
38 lines (32 loc) · 1.51 KB
/
ChatService.cs
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
using OpenAI.Chat;
using Azure;
using Azure.AI.OpenAI;
using System.Text;
namespace BlazorIntApp1
{
public class ChatService
{
private readonly ChatClient chatClient;
public ChatService(string ApiUrl, string ApiKey)
{
string urlFromEnvironment = ApiUrl ?? Environment.GetEnvironmentVariable("AZURE_OPENAI_API_URL") ?? "";
string keyFromEnvironment = ApiKey ?? Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? "";
AzureOpenAIClient azureClient = new(new Uri(urlFromEnvironment),new AzureKeyCredential(keyFromEnvironment));
chatClient = azureClient.GetChatClient("gpt-4o");
}
public string GetResponse(string query)
{
ChatCompletion completion = chatClient.CompleteChat(
[
// System messages represent instructions or other guidance about how the assistant should behave
//new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
// User messages represent user input, whether historical or the most recen tinput
new UserChatMessage(query),
// Assistant messages in a request represent conversation history for responses
//new AssistantChatMessage("Arrr! Of course, me hearty! What can I do for ye?"),
//new UserChatMessage("What's the best way to train a parrot?"),
]);
return completion.Content[0].Text;
}
}
}