forked from microsoft/Generative-AI-for-beginners-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
76 lines (66 loc) · 2.23 KB
/
Program.cs
File metadata and controls
76 lines (66 loc) · 2.23 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
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Text;
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
var endpoint = config["endpoint"];
var endpointClaude = config["endpointClaude"];
var apiKey = config["apikey"];
var deploymentName = config["deploymentName"];
// 1. create custom http client that will handle Claude endpoint in Azure
var customHttpMessageHandler = new ClaudeToOpenAIMessageHandler
{
AzureClaudeDeploymentUrl = endpointClaude,
ApiKey = apiKey, // Pass the API key to the handler
Model = deploymentName // Pass the model name to the handler
};
HttpClient customHttpClient = new(customHttpMessageHandler);
// 2. Wrap HttpClient in the NEW pipeline transport
var transport = new HttpClientPipelineTransport(customHttpClient);
// 3. Client options (generational)
var clientOptions = new AzureOpenAIClientOptions
{
Transport = transport
};
// 4. Credential type for generational client
var apiKeyCredential = new ApiKeyCredential(apiKey);
// 5. Create the client with the custom transport and credential
IChatClient client = new AzureOpenAIClient(
endpoint: new Uri(endpoint),
credential: apiKeyCredential,
options: clientOptions)
.GetChatClient(deploymentName)
.AsIChatClient()
.AsBuilder()
.Build();
var history = new List<ChatMessage>
{
new(ChatRole.System, "You are a useful chatbot.")
};
while (true)
{
Console.Write("Q: ");
var userQ = Console.ReadLine();
if (string.IsNullOrEmpty(userQ))
{
break;
}
history.Add(new ChatMessage(ChatRole.User, userQ));
var sb = new StringBuilder();
var result = client.GetStreamingResponseAsync(history);
Console.Write($"AI [{deploymentName}]: ");
await foreach (var item in result)
{
// validate if the item is null or has no contents
if (item == null || item.Contents.Count == 0)
{
continue; // skip to the next item if it's null or empty
}
sb.Append(item);
Console.Write(item.Contents[0].ToString());
}
Console.WriteLine();
history.Add(new ChatMessage(ChatRole.Assistant, sb.ToString()));
}