-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
437 lines (377 loc) · 21.2 KB
/
Program.cs
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json.Serialization;
using Force.DeepCloner;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using NetTopologySuite.IO;
using Newtonsoft.Json.Linq;
using NRedisStack.RedisStackCommands;
using OllamaSharp;
using OllamaSharp.Models.Chat;
using Org.BouncyCastle.Utilities;
using ProxyKit;
using StackExchange.Redis;
namespace Onllama.MondrianGateway
{
internal class Program
{
public static List<string> NonChatApiPathList =
["/api/generate", "/api/chat", "/api/tags", "/api/embed", "/api/show", "/api/ps", "/api/embeddings"];
public static string TargetApiUrl = "http://127.0.0.1:11434";
public static string ActionApiUrl = "http://127.0.0.1:11434";
public static bool UseToken = false;
public static bool UseTokenReplace = false;
public static bool UseModelReplace = false;
public static bool UseRiskModel = false;
public static bool UseSystemPromptTrim = false;
public static bool UseSystemPromptInject = false;
//public static string RedisDBStr = "";
//public static ConnectionMultiplexer RedisConnection;
//public static IDatabase RedisDatabase;
public static string ReplaceTokenMode = "failback";
public static List<string> ReplaceTokensList = ["sk-test"];
public static string RiskModel = "shieldgemma:2b";
public static string RiskModelPrompt = string.Empty;
public static string SystemPrompt = "你是Bob,你是一个友好的助手。";
public static Dictionary<string, string> ModelReplaceDictionary = new()
{
{"gpt-3.5-turbo", "qwen2.5:7b"}
};
public static List<string> TokensList = ["sk-test-token"];
public static List<string> RiskKeywordsList = ["YES", "UNSAFE"];
public static OllamaApiClient OllamaApi = new OllamaApiClient(new Uri(ActionApiUrl));
static void Main(string[] args)
{
try
{
var configurationRoot = new ConfigurationBuilder()
.AddEnvironmentVariables()
.Build();
if (configurationRoot.GetSection("MondrianGateway").Exists())
{
var gateConfig = configurationRoot.GetSection("MondrianGateway");
if (gateConfig.GetSection("Tokens").Exists())
{
TokensList = gateConfig.GetSection("Tokens").Get<List<string>>() ?? [];
if (TokensList.Any()) UseToken = true;
}
if (gateConfig.GetSection("ReplaceTokens").Exists())
{
ReplaceTokensList = gateConfig.GetSection("ReplaceTokens").Get<List<string>>() ?? [];
if (ReplaceTokensList.Any()) UseTokenReplace = true;
}
if (gateConfig.GetSection("ModelReplace").Exists())
{
ModelReplaceDictionary =
gateConfig.GetSection("ModelReplace").Get<Dictionary<string, string>>() ??
new Dictionary<string, string>();
if (ModelReplaceDictionary.Any()) UseModelReplace = true;
}
if (gateConfig.GetSection("SystemPrompt").Exists())
{
SystemPrompt = gateConfig.GetValue<string>("SystemPrompt") ?? string.Empty;
if (string.IsNullOrWhiteSpace(SystemPrompt))
{
UseSystemPromptInject = true;
UseSystemPromptTrim = true;
}
}
if (gateConfig.GetSection("Risks").Exists())
{
var riskConfig = gateConfig.GetSection("Risks");
if (riskConfig.GetSection("RiskModel").Exists())
RiskModel = riskConfig.GetValue<string>("RiskModel") ?? string.Empty;
if (riskConfig.GetSection("RiskModelPrompt").Exists())
RiskModelPrompt = riskConfig.GetValue<string>("RiskModelPrompt") ?? string.Empty;
if (riskConfig.GetSection("RiskKeywords").Exists())
RiskKeywordsList = riskConfig.GetSection("RiskKeywords").Get<List<string>>() ?? [];
if (!string.IsNullOrWhiteSpace(RiskModel) && RiskKeywordsList.Any()) UseRiskModel = true;
}
if (gateConfig.GetSection("UseToken").Exists())
UseToken = gateConfig.GetValue<bool>("UseToken");
if (gateConfig.GetSection("UseModelReplace").Exists())
UseModelReplace = gateConfig.GetValue<bool>("UseModelReplace");
if (gateConfig.GetSection("UseTokenReplace").Exists())
UseTokenReplace = gateConfig.GetValue<bool>("UseTokenReplace");
if (gateConfig.GetSection("UseRiskModel").Exists())
UseRiskModel = gateConfig.GetValue<bool>("UseRiskModel");
if (gateConfig.GetSection("UseSystemPromptTrim").Exists())
UseSystemPromptTrim = gateConfig.GetValue<bool>("UseSystemPromptTrim");
if (gateConfig.GetSection("UseSystemPromptInject").Exists())
UseSystemPromptInject = gateConfig.GetValue<bool>("UseSystemPromptInject");
if (gateConfig.GetSection("ReplaceTokenMode").Exists())
ReplaceTokenMode = gateConfig.GetValue<string>("ReplaceTokenMode") ?? ReplaceTokenMode;
if (gateConfig.GetSection("TargetApiUrl").Exists())
TargetApiUrl = gateConfig.GetValue<string>("TargetApiUrl") ?? TargetApiUrl;
if (gateConfig.GetSection("ActionApiUrl").Exists())
{
ActionApiUrl = gateConfig.GetValue<string>("ActionApiUrl") ?? TargetApiUrl;
OllamaApi = new OllamaApiClient(new Uri(ActionApiUrl));
}
}
//RedisConnection = ConnectionMultiplexer.Connect(RedisDBStr);
//RedisDatabase = RedisConnection.GetDatabase();
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(AppDomain.CurrentDomain.SetupInformation.ApplicationBase)
.ConfigureServices(services =>
{
services.AddRouting();
services.AddProxy(httpClientBuilder =>
httpClientBuilder.ConfigureHttpClient(client =>
client.Timeout = TimeSpan.FromMinutes(5)));
})
.ConfigureKestrel(options =>
{
options.ListenAnyIP(18080,
listenOptions => listenOptions.Protocols = HttpProtocols.Http1AndHttp2);
})
.Configure(app =>
{
app.Use(async (context, next) =>
{
var reqToken = context.Request.Headers.ContainsKey("Authorization")
? context.Request.Headers.Authorization.ToString().Split(' ').Last().ToString()
: string.Empty;
if (UseToken && !TokensList.Contains(reqToken))
{
context.Response.Headers.ContentType = "application/json";
context.Response.StatusCode = (int) HttpStatusCode.Forbidden;
await context.Response.WriteAsync(new JObject()
{
{
"error", new JObject
{
{
"message",
"Authentication Fails, You need to provide a valid API key in the Authorization header using Bearer authentication (i.e. Authorization: Bearer YOUR_KEY)."
},
{"type", "invalid_request_error"}
}
}
}.ToString());
}
else
{
context.Items["Token"] = reqToken;
await next.Invoke();
}
});
foreach (var path in NonChatApiPathList)
app.Map(path, svr =>
{
svr.RunProxy(async context =>
{
var response = new HttpResponseMessage();
try
{
response = await context.ForwardTo(new Uri(TargetApiUrl + path)).Send();
response.Headers.Add("X-Forwarder-By", "MondrianGateway/0.1");
return response;
}
catch (Exception e)
{
Console.WriteLine(e);
return response;
}
});
});
app.UseRouting().UseEndpoints(endpoint =>
{
endpoint.Map("/v1/chat/completions", async context =>
{
// 配置响应头
context.Response.ContentType = "text/plain; charset=utf-8";
context.Response.Headers.CacheControl = "no-cache";
context.Response.Headers.Connection = "keep-alive";
using var httpClient = new HttpClient();
var apiUrl = "https://api.siliconflow.cn/v1/chat/completions"; // 替换实际API地址
var apiKey = "sk-";
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
var jBody = JObject.Parse(body);
jBody["model"] = "Qwen/Qwen2.5-7B-Instruct";
var request = new HttpRequestMessage(HttpMethod.Post, apiUrl);
request.Headers.Add("Authorization", $"Bearer {apiKey}");
request.Content =
new StringContent(jBody.ToString(),
Encoding.UTF8, "application/json");
// 关键:使用 ResponseHeadersRead 模式立即获取流
var response = await httpClient.SendAsync(request,
HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
// 获取响应流
using var stream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
// 流式读取
var buffer = new char[1024];
var sb = new StringBuilder();
while (true)
{
var bytesRead = await reader.ReadAsync(buffer, 0, buffer.Length);
if (bytesRead == 0) break;
sb.Append(buffer, 0, bytesRead);
// 处理完整行
while (sb.Length > 0)
{
var newLineIndex = sb.ToString().IndexOf('\n');
if (newLineIndex < 0) break;
var line = sb.ToString(0, newLineIndex).Trim();
sb.Remove(0, newLineIndex + 1);
Console.WriteLine(line);
//if (!string.IsNullOrEmpty(line))
//{
// if (line.StartsWith("data: "))
// {
// var jsonData = line.Substring(6);
// if (jsonData == "[DONE]") return;
// Console.WriteLine($"Received: {jsonData}");
// // 这里添加JSON解析逻辑
// }
//}
await context.Response.WriteAsync(line + Environment.NewLine);
await context.Response.Body.FlushAsync();
}
}
await context.Response.CompleteAsync();
});
});
//app.Map("/v1", HandleOpenaiStyleChat);
}).Build();
host.Run();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static void HandleOpenaiStyleChat(IApplicationBuilder svr)
{
svr.RunProxy(async context =>
{
HttpResponseMessage response;
try
{
if (context.Request.Method.ToUpper() == "POST")
{
var body = await new StreamReader(context.Request.Body).ReadToEndAsync();
var jBody = JObject.Parse(body);
//RedisDatabase.JSON().Set(Ulid.NewUlid().ToGuid().ToString(), "$", body);
if (jBody.ContainsKey("model") && UseModelReplace &&
ModelReplaceDictionary.TryGetValue(jBody["model"]?.ToString()!, out var newModel))
jBody["model"] = newModel;
Console.WriteLine(jBody.ToString());
if (UseTokenReplace)
{
context.Request.Headers.Remove("Authorization");
if (ReplaceTokenMode is "first" or "failback")
{
context.Request.Headers.Authorization =
"Bearer " + ReplaceTokensList.FirstOrDefault();
}
else if (ReplaceTokenMode == "random")
{
context.Request.Headers.Authorization =
"Bearer " + ReplaceTokensList.ToArray()[
new Random().Next(ReplaceTokensList.Count - 1)];
}
}
if (jBody.ContainsKey("messages"))
{
var msgs = jBody["messages"]?.ToObject<List<Message>>();
if (msgs != null && msgs.Any())
{
if (UseSystemPromptTrim)
{
msgs.RemoveAll(x => string.Equals(x.Role, ChatRole.System.ToString(),
StringComparison.CurrentCultureIgnoreCase));
jBody["messages"] = JArray.FromObject(msgs);
}
if (UseSystemPromptInject && !string.IsNullOrWhiteSpace(SystemPrompt))
{
msgs.Insert(0, new Message { Role = ChatRole.System.ToString(), Content = SystemPrompt });
jBody["messages"] = JArray.FromObject(msgs);
Console.WriteLine(jBody.ToString());
}
if (UseRiskModel)
{
await foreach (var res in OllamaApi.ChatAsync(new ChatRequest()
{
Model = RiskModel,
Messages = msgs.Select(x =>
new OllamaSharp.Models.Chat.Message(x.Role, x.Content)),
Stream = false
}))
{
var risks = res?.Message.Content;
Console.WriteLine(risks);
if (risks != null &&
RiskKeywordsList.Any(x => risks.ToUpper().Contains(x.ToUpper())))
{
return new HttpResponseMessage(HttpStatusCode.UnavailableForLegalReasons)
{
Content = new StringContent(new JObject
{
{
"error",
new JObject
{
{
"message",
"Messages with content security risks. Unable to continue."
},
{"type", "content_risks"}, {"risk_model", res?.Model},
{"risk_raw_msg", res?.Message.Content}
}
}
}.ToString())
};
}
}
}
}
}
context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(jBody.ToString()));
context.Request.ContentLength = context.Request.Body.Length;
}
response = await context.ForwardTo(new Uri(TargetApiUrl + "/v1")).Send();
response.Headers.Add("X-Forwarder-By", "MondrianGateway/0.1");
var statusCode = Convert.ToInt32(response.StatusCode);
if (UseTokenReplace && ReplaceTokenMode == "failback" && ReplaceTokensList.Any() &&
statusCode is >= 400 and < 500)
{
ReplaceTokensList.Add(ReplaceTokensList.First());
ReplaceTokensList.RemoveAt(0);
}
Console.WriteLine(await response.Content.ReadAsStringAsync());
return response;
}
catch (Exception e)
{
Console.WriteLine(e);
response = await context.ForwardTo(new Uri(TargetApiUrl + "/v1")).Send();
return response;
}
});
}
}
public class Message
{
[JsonPropertyName("role")] public string? Role { get; set; }
[JsonPropertyName("content")] public string? Content { get; set; }
[JsonPropertyName("images")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? Images { get; set; }
[JsonPropertyName("image_url")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? ImageUrl { get; set; }
[JsonPropertyName("tool_calls")]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public object? ToolCalls { get; set; }
}
}