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
100 lines (90 loc) · 3.1 KB
/
Program.cs
File metadata and controls
100 lines (90 loc) · 3.1 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
90
91
92
93
94
95
96
97
98
99
100
using Azure;
using Azure.AI.Inference;
using Azure.AI.OpenAI;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using ModelContextProtocol.Client;
using System.ClientModel;
// To run the sample, you need to set the following environment variables or user secrets:
// Using GitHub models
// "HF_API_KEY": " your HF token"
// "GITHUB_TOKEN": " your GitHub Token "
// Using Azure OpenAI models
// "endpoint": "https://<endpoint>.services.ai.azure.com/",
// "apikey": " your key ",
// "deploymentName": "a deployment name, ie: gpt-4.1-mini"
var builder = Host.CreateApplicationBuilder(args);
var config = builder.Configuration
.AddEnvironmentVariables()
.AddUserSecrets<Program>()
.Build();
var deploymentName = config["deploymentName"] ?? "gpt-4.1-mini"; // Default to gpt-4.1-mini if not specified
// create MCP Client using Hugging Face endpoint
var hfHeaders = new Dictionary<string, string>
{
{ "Authorization", $"Bearer {config["HF_API_KEY"]}" }
};
var clientTransport = new HttpClientTransport(
new HttpClientTransportOptions
{
Name = "HF Server",
Endpoint = new Uri("https://huggingface.co/mcp"),
AdditionalHeaders = hfHeaders
});
await using var mcpClient = await McpClient.CreateAsync(clientTransport);
// Display the available server tools
var tools = await mcpClient.ListToolsAsync();
foreach (var tool in tools)
{
Console.WriteLine($"Connected to server with tools: {tool.Name}");
}
Console.WriteLine("Press Enter to continue...");
Console.ReadLine();
Console.WriteLine();
// create an IChatClient using the MCP tools
IChatClient client = GetChatClient();
var chatOptions = new ChatOptions
{
Tools = [.. tools],
ModelId = deploymentName
};
// Create image
Console.WriteLine("Starting the process to generate an image of a pixelated puppy...");
var query = "Create an image of a pixelated puppy.";
var result = await client.GetResponseAsync(query, chatOptions);
Console.Write($"AI response: {result}");
Console.WriteLine();
IChatClient GetChatClient()
{
IChatClient client = null;
var githubToken = Environment.GetEnvironmentVariable("GITHUB_TOKEN");
if (string.IsNullOrEmpty(githubToken))
{
githubToken = config["GITHUB_TOKEN"];
}
// create a client if githubToken is valid string
if (!string.IsNullOrEmpty(githubToken))
{
client = new ChatCompletionsClient(
endpoint: new Uri("https://models.github.ai/inference"),
new AzureKeyCredential(githubToken))
.AsIChatClient(deploymentName)
.AsBuilder()
.UseFunctionInvocation()
.Build(); ;
}
else
{
// create an Azure OpenAI client if githubToken is not valid
var endpoint = config["endpoint"];
var apiKey = new ApiKeyCredential(config["apikey"]);
client = new AzureOpenAIClient(new Uri(endpoint), apiKey)
.GetChatClient(deploymentName)
.AsIChatClient()
.AsBuilder()
.UseFunctionInvocation()
.Build();
}
return client;
}