-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathMiddleware.cs
More file actions
50 lines (42 loc) · 1.71 KB
/
Middleware.cs
File metadata and controls
50 lines (42 loc) · 1.71 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
using System.ComponentModel;
using System.Diagnostics;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
using OpenTelemetry.Trace;
public partial class AbstractionSamples
{
public static async Task Middleware()
{
// Configure OpenTelemetry Exporter
var sourceName = Guid.NewGuid().ToString();
var activities = new List<Activity>();
var tracerProvider = OpenTelemetry.Sdk.CreateTracerProviderBuilder()
.AddSource(sourceName)
.AddInMemoryExporter(activities)
.Build();
// Configure cache
var options = Options.Create(new MemoryDistributedCacheOptions());
IDistributedCache cache = new MemoryDistributedCache(options);
// Configure tool calling
[Description("Gets the weather")]
string GetWeather() => Random.Shared.NextDouble() > 0.5 ? "It's sunny" : "It's raining";
var chatOptions = new ChatOptions
{
Tools = [AIFunctionFactory.Create(GetWeather)]
};
IChatClient client = new SampleChatClient(new Uri("http://coolsite.ai"), "my-custom-model")
.AsBuilder()
.UseFunctionInvocation()
.UseOpenTelemetry(sourceName: sourceName, configure: o => o.EnableSensitiveData = true)
.UseDistributedCache(cache)
.Build();
List<ChatMessage> conversation =
[
new(ChatRole.System, "You are a helpful AI assistant"),
new(ChatRole.User, "Do I need an umbrella?")
];
Console.WriteLine(await client.GetResponseAsync("Do I need an umbrella?", chatOptions));
}
}