-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathProgram.cs
More file actions
44 lines (36 loc) · 1.51 KB
/
Program.cs
File metadata and controls
44 lines (36 loc) · 1.51 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
using System;
using System.ClientModel;
using Microsoft.Extensions.AI;
using Microsoft.Agents.AI;
using OpenAI;
using DotNetEnv;
// Load environment variables from .env file
Env.Load("../../../../.env");
// Get Foundry Local configuration from environment variables
var foundryLocalEndpoint = Environment.GetEnvironmentVariable("FOUNDRYLOCAL_ENDPOINT")
?? throw new InvalidOperationException("FOUNDRYLOCAL_ENDPOINT is not set.");
var foundryLocalModelId = Environment.GetEnvironmentVariable("FOUNDRYLOCAL_MODEL_DEPLOYMENT_NAME")
?? throw new InvalidOperationException("FOUNDRYLOCAL_MODEL_DEPLOYMENT_NAME is not set.");
Console.WriteLine($"Endpoint: {foundryLocalEndpoint}");
Console.WriteLine($"Model: {foundryLocalModelId}");
// Configure OpenAI client for Foundry Local (no API key required)
var openAIOptions = new OpenAIClientOptions()
{
Endpoint = new Uri(foundryLocalEndpoint)
};
var openAIClient = new OpenAIClient(new ApiKeyCredential("nokey"), openAIOptions);
// Create AI Agent
AIAgent agent = openAIClient
.GetChatClient(foundryLocalModelId)
.AsIChatClient()
.AsAIAgent(instructions: "You are a helpful assistant.", name: "FoundryLocalAgent");
// Run agent
Console.WriteLine("\n=== Response ===");
Console.WriteLine(await agent.RunAsync("Can you introduce yourself?"));
// Run agent with streaming response
Console.WriteLine("\n=== Streaming Response ===");
await foreach (var update in agent.RunStreamingAsync("What can you help me with?"))
{
Console.Write(update);
}
Console.WriteLine();