forked from dotnet/dev-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOllamaLanguageModelClient.cs
More file actions
245 lines (209 loc) · 8.73 KB
/
OllamaLanguageModelClient.cs
File metadata and controls
245 lines (209 loc) · 8.73 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Net.Http.Json;
using DevProxy.Abstractions.Prompty;
using Microsoft.Extensions.Logging;
namespace DevProxy.Abstractions.LanguageModel;
public sealed class OllamaLanguageModelClient(
HttpClient httpClient,
LanguageModelConfiguration configuration,
ILogger<OllamaLanguageModelClient> logger) : BaseLanguageModelClient(configuration, logger)
{
private readonly HttpClient _httpClient = httpClient;
private readonly Dictionary<string, OllamaLanguageModelCompletionResponse> _cacheCompletion = [];
private readonly Dictionary<IEnumerable<ILanguageModelChatCompletionMessage>, OllamaLanguageModelChatCompletionResponse> _cacheChatCompletion = [];
protected override async Task<ILanguageModelCompletionResponse?> GenerateCompletionCoreAsync(string prompt, CompletionOptions? options, CancellationToken cancellationToken)
{
using var scope = Logger.BeginScope(nameof(OllamaLanguageModelClient));
if (Configuration.CacheResponses && _cacheCompletion.TryGetValue(prompt, out var cachedResponse))
{
Logger.LogDebug("Returning cached response for prompt: {Prompt}", prompt);
return cachedResponse;
}
var response = await GenerateCompletionInternalAsync(prompt, options, cancellationToken);
if (response == null)
{
return null;
}
if (response.Error is not null)
{
Logger.LogError("{Error}", response.Error);
return null;
}
if (Configuration.CacheResponses && response.Response is not null)
{
_cacheCompletion[prompt] = response;
}
return response;
}
protected override async Task<ILanguageModelCompletionResponse?> GenerateChatCompletionCoreAsync(IEnumerable<ILanguageModelChatCompletionMessage> messages, CompletionOptions? options = null, CancellationToken cancellationToken = default)
{
using var scope = Logger.BeginScope(nameof(OllamaLanguageModelClient));
if (Configuration.CacheResponses && _cacheChatCompletion.TryGetCacheValue(messages, out var cachedResponse))
{
Logger.LogDebug("Returning cached response for message: {LastMessage}", messages.Last().Content);
return cachedResponse;
}
var response = await GenerateChatCompletionInternalAsync(messages, options);
if (response == null)
{
return null;
}
if (response.Error is not null)
{
Logger.LogError("{Error}", response.Error);
return null;
}
else
{
if (Configuration.CacheResponses && response.Response is not null)
{
_cacheChatCompletion[messages] = response;
}
return response;
}
}
protected override IEnumerable<ILanguageModelChatCompletionMessage> ConvertMessages(IEnumerable<ChatMessage> messages)
{
return messages.Select(m => new OllamaLanguageModelChatCompletionMessage
{
Role = m.Role ?? "user",
Content = m.Text ?? string.Empty
});
}
protected override async Task<bool> IsEnabledCoreAsync(CancellationToken cancellationToken)
{
if (Configuration is null || !Configuration.Enabled)
{
return false;
}
if (string.IsNullOrEmpty(Configuration.Url))
{
Logger.LogError("URL is not set. Language model will be disabled");
return false;
}
if (string.IsNullOrEmpty(Configuration.Model))
{
Logger.LogError("Model is not set. Language model will be disabled");
return false;
}
Logger.LogDebug("Checking LM availability at {Url}...", Configuration.Url);
try
{
var testCompletion = await GenerateCompletionInternalAsync("Are you there? Reply with a yes or no.", null, cancellationToken);
if (testCompletion?.Error is not null)
{
Logger.LogError("Error: {Error}", testCompletion.Error);
return false;
}
return true;
}
catch (Exception ex)
{
Logger.LogError(ex, "Couldn't reach language model at {Url}", Configuration.Url);
return false;
}
}
private async Task<OllamaLanguageModelCompletionResponse?> GenerateCompletionInternalAsync(string prompt, CompletionOptions? options, CancellationToken cancellationToken)
{
Debug.Assert(Configuration != null, "Configuration is null");
try
{
var url = $"{Configuration.Url?.TrimEnd('/')}/api/generate";
Logger.LogDebug("Requesting completion. Prompt: {Prompt}", prompt);
var response = await _httpClient.PostAsJsonAsync(url,
new
{
prompt,
model = Configuration.Model,
stream = false,
options
},
cancellationToken
);
Logger.LogDebug("Response status: {Response}", response.StatusCode);
if (!response.IsSuccessStatusCode)
{
var errorResponse = await response.Content.ReadAsStringAsync(cancellationToken);
Logger.LogDebug("LM error: {ErrorResponse}", errorResponse);
return null;
}
var res = await response.Content.ReadFromJsonAsync<OllamaLanguageModelCompletionResponse>(cancellationToken);
if (res is null)
{
Logger.LogDebug("Response: null");
return res;
}
Logger.LogDebug("Response: {Response}", res.Response);
res.RequestUrl = url;
return res;
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to generate completion");
return null;
}
}
private async Task<OllamaLanguageModelChatCompletionResponse?> GenerateChatCompletionInternalAsync(IEnumerable<ILanguageModelChatCompletionMessage> messages, CompletionOptions? options)
{
Debug.Assert(Configuration != null, "Configuration is null");
try
{
var url = $"{Configuration.Url?.TrimEnd('/')}/api/chat";
Logger.LogDebug("Requesting chat completion. Message: {LastMessage}", messages.Last().Content);
var response = await _httpClient.PostAsJsonAsync(url,
new
{
messages,
model = Configuration.Model,
stream = false,
options
}
);
Logger.LogDebug("Response: {Response}", response.StatusCode);
if (!response.IsSuccessStatusCode)
{
var errorResponse = await response.Content.ReadAsStringAsync();
Logger.LogDebug("LM error: {ErrorResponse}", errorResponse);
return null;
}
var res = await response.Content.ReadFromJsonAsync<OllamaLanguageModelChatCompletionResponse>();
if (res is null)
{
Logger.LogDebug("Response: null");
return res;
}
res.RequestUrl = url;
return res;
}
catch (Exception ex)
{
Logger.LogError(ex, "Failed to generate chat completion");
return null;
}
}
}
internal static class OllamaCacheChatCompletionExtensions
{
public static IEnumerable<ILanguageModelChatCompletionMessage>? GetKey(
this Dictionary<IEnumerable<ILanguageModelChatCompletionMessage>, OllamaLanguageModelChatCompletionResponse> cache,
IEnumerable<ILanguageModelChatCompletionMessage> messages)
{
return cache.Keys.FirstOrDefault(k => k.SequenceEqual(messages));
}
public static bool TryGetCacheValue(
this Dictionary<IEnumerable<ILanguageModelChatCompletionMessage>, OllamaLanguageModelChatCompletionResponse> cache,
IEnumerable<ILanguageModelChatCompletionMessage> messages, out OllamaLanguageModelChatCompletionResponse? value)
{
var key = cache.GetKey(messages);
if (key is null)
{
value = null;
return false;
}
value = cache[key];
return true;
}
}