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
66 lines (57 loc) · 2.05 KB
/
Program.cs
File metadata and controls
66 lines (57 loc) · 2.05 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
#pragma warning disable SKEXP0001, SKEXP0070
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.SemanticKernel.ChatCompletion;
using ModelContextProtocol.Client;
using OllamaSharp;
// To run the sample, you need to set the following environment variables or user secrets:
// "HF_API_KEY": " your HF token"
// "deploymentName" : "llama3.2" // Optional, defaults to "llama3.2" if not specified
var builder = Host.CreateApplicationBuilder(args);
var config = builder.Configuration
.AddEnvironmentVariables()
.AddUserSecrets<Program>()
.Build();
var deploymentName = config["deploymentName"] ?? "llama3.2"; // Default to llama3.2 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
var uri = new Uri("http://localhost:11434");
var client = new OllamaApiClient(uri, deploymentName)
.AsChatCompletionService()
.AsChatClient()
.AsBuilder()
.UseFunctionInvocation()
.Build();
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();