-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
214 lines (183 loc) · 6.45 KB
/
Program.cs
File metadata and controls
214 lines (183 loc) · 6.45 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using Microsoft.Extensions.AI;
using OllamaSharp;
using OllamaWithExtensionAndFunctionCalling;
using Scalar.AspNetCore;
using System.ComponentModel;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateBuilder(args);
// Configuration setup
var ollamaEndpoint = builder.Configuration["AI__Ollama__Endpoint"] ?? "http://localhost:11434";
var chatModel = builder.Configuration["AI__Ollama__ChatModel"] ?? "llama3.2";
// Create OllamaSharp client
var uri = new Uri(ollamaEndpoint);
var ollama = new OllamaApiClient(uri)
{
SelectedModel = chatModel
};
// Listing all models that are available locally
var models = await OllamaUtils.ListLocalModelsAsync(ollama);
// Pulling a model and reporting progress
await OllamaUtils.PullModelWithProgressAsync(ollama, chatModel);
// Generating a completion directly into the console
await OllamaUtils.TestOllamaCompletion(ollama);
// save current location for weather queries
double lat = 0, lon = 0;
IChatClient client = new OllamaChatClient(ollamaEndpoint, modelId: chatModel)
.AsBuilder()
.UseFunctionInvocation()
.Build();
// Define functions AI will call when needed
[Description("Gets the weather")]
string GetWeather()
{
using var httpClient = new HttpClient();
var url = $"https://api.open-meteo.com/v1/forecast?latitude={lat}&longitude={lon}¤t_weather=true";
var response = httpClient.GetAsync(url).Result;
if (!response.IsSuccessStatusCode)
return "Unknown weather";
var json = response.Content.ReadAsStringAsync().Result;
var weather = System.Text.Json.JsonSerializer.Deserialize<WeatherInfo>(json);
return weather?.CurrentWeather != null
? $"Temperature: {weather.CurrentWeather.Temperature}°C, Wind: {weather.CurrentWeather.Windspeed} km/h"
: "Unknown weather";
}
[Description("Gets the location")]
string GetLocation()
{
using var httpClient = new HttpClient();
var response = httpClient.GetAsync("http://ip-api.com/json/").Result;
if (!response.IsSuccessStatusCode)
return "Unknown location";
var json = response.Content.ReadAsStringAsync().Result;
var location = System.Text.Json.JsonSerializer.Deserialize<IpApiLocation>(json);
lat = location?.Latitude ?? 10.822;
lon = location?.Longitude ?? 106.6257;
return location is not null
? $"{location.City}, {location.RegionName}, {location.Country}"
: "Unknown location";
}
[Description("Gets the weather and location")]
string GetWeatherAndLocation() => $"{GetLocation()} has {GetWeather()}";
// Register functions AI will call into chatOptions
var chatOptions = new ChatOptions
{
Tools =
[
AIFunctionFactory.Create(GetWeather),
AIFunctionFactory.Create(GetLocation),
AIFunctionFactory.Create(GetWeatherAndLocation)
]
};
// adding caching for frequent queries
builder.Services.AddOutputCache(options =>
{
options.AddBasePolicy(builder =>
builder.Cache()
.Expire(TimeSpan.FromMinutes(5)));
});
// Register client AI into DI container
builder.Services.AddChatClient(client);
// Register OpenAPI for API documentation
builder.Services.AddOpenApi();
var app = builder.Build();
// endpoint for openAPI and cache results
app.MapOpenApi().CacheOutput();
// endpoint for scalar API
app.MapScalarApiReference();
// endpoint GET "/" redirects to scalar API reference
app.MapGet("/", () => Results.Redirect("/scalar/v1")).ExcludeFromDescription();
// Endpoint POST "/chat" for chat completion
app.MapPost("/chat", async (IChatClient client, ChatRequest req) =>
{
try
{
Console.Write($"You: {req.Message}");
Console.WriteLine();
// Clone chatOptions and override with request params
var options = new ChatOptions
{
Tools = chatOptions.Tools,
Temperature = req.Temperature ?? chatOptions.Temperature,
TopP = req.TopP ?? chatOptions.TopP,
TopK = req.TopK ?? chatOptions.TopK,
MaxOutputTokens = req.MaxOutputTokens ?? chatOptions.MaxOutputTokens
};
var textResult = "";
if (req.Stream)
{
var responseStream = client.GetStreamingResponseAsync(
req.Message,
options,
cancellationToken: default);
Console.Write("Chatbot: ");
await foreach (var update in responseStream)
{
if (!string.IsNullOrEmpty(update.Text))
{
Console.Write(update.Text);
textResult += update.Text;
}
}
}
else
{
var response = client.GetResponseAsync(
req.Message,
options,
cancellationToken: default);
textResult = response.Result.Text;
Console.Write($"Chatbot: {textResult}");
}
Console.WriteLine();
return Results.Ok(new { textResult });
}
catch (Exception ex)
{
return Results.Problem(
title: "Chat completion failed",
detail: ex.Message);
}
});
app.Run();
//var webTask = app.RunAsync();
// Building interactive chats
//var chatTask = OllamaUtils.RunInteractiveChat(ollama, chatOptions);
//await Task.WhenAny(webTask, chatTask);
// Class to deserialize JSON results
namespace OllamaWithExtensionAndFunctionCalling
{
public class IpApiLocation
{
[JsonPropertyName("city")]
public string? City { get; set; }
[JsonPropertyName("regionName")]
public string? RegionName { get; set; }
[JsonPropertyName("country")]
public string? Country { get; set; }
[JsonPropertyName("lat")]
public double? Latitude { get; set; }
[JsonPropertyName("lon")]
public double? Longitude { get; set; }
}
public class WeatherInfo
{
[JsonPropertyName("current_weather")]
public CurrentWeather? CurrentWeather { get; set; }
}
public class CurrentWeather
{
[JsonPropertyName("temperature")]
public double? Temperature { get; set; }
[JsonPropertyName("windspeed")]
public double? Windspeed { get; set; }
}
public class ChatRequest
{
public string Message { get; set; } = string.Empty;
public bool Stream { get; set; } = true;
public float? Temperature { get; set; } = 0.1f;
public float? TopP { get; set; } = 0.1f;
public int? TopK { get; set; } = 4;
public int? MaxOutputTokens { get; set; } = 50;
}
}