-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLMClient.cs
More file actions
122 lines (99 loc) · 3.29 KB
/
LLMClient.cs
File metadata and controls
122 lines (99 loc) · 3.29 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
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.ComponentModel;
class LLMClient
{
public static async Task<string> Ask(string systemPrompt, string userPrompt)
{
string uri = "http://localhost:1234/v1/chat/completions";
try
{
using var client = new HttpClient();
var json = new
{
model = "qwen/qwen3-1.7b",
messages = new[]
{
new { role = "system", content = systemPrompt },
new { role = "user", content = userPrompt }
},
temperature = 0.7,
max_tokens = -1,
stream = false
};
var jsonString = JsonConvert.SerializeObject(json);
var content = new StringContent(jsonString, Encoding.UTF8, "application/json");
var response = await client.PostAsync(uri, content);
if (!response.IsSuccessStatusCode)
{
return "Invalid status code";
}
var responseContent = await response.Content.ReadAsStringAsync();
var responseObject = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(responseContent);
if (responseObject?.Choices?.Count > 0)
{
if (!String.IsNullOrWhiteSpace(responseObject.Choices[0].Message?.Content))
{
var responseText = responseObject.Choices[0]!.Message!.Content!;
var thinkEnd = responseText.IndexOf("</think>")+8;
responseText = responseText[thinkEnd..].Replace("**", "❞");
return responseText;
}
else
{
return "❌ Sorry, the response was invalid";
}
}
else
{
return "❌ Sorry, failed to generate a response";
}
}
catch (Exception ex)
{
return $"An error occurred: {ex.Message}";
}
}
}
public class RootObject
{
[JsonProperty("id")]
public string? Id { get; set; }
[JsonProperty("object")]
public string? ChatObject { get; set; }
[JsonProperty("created")]
public long Created { get; set; }
[JsonProperty("model")]
public string? Model { get; set; }
[JsonProperty("choices")]
public List<Choice>? Choices { get; set; }
[JsonProperty("usage")]
public Object? Usage { get; set; }
[JsonProperty("stats")]
public Dictionary<string, object>? Stats { get; set; }
[JsonProperty("system_fingerprint")]
public string? SystemFingerPrint { get; set; }
}
public class Choice
{
[JsonProperty("index")]
public int Index { get; set; }
[JsonProperty("message")]
public Message? Message { get; set; }
}
public class Message
{
[JsonProperty("role")]
public string? Role { get; set; }
[JsonProperty("content")]
public string? Content { get; set; }
[JsonProperty("tool_calls")]
public List<ToolCall>? tool_calls { get; set; }
}
public class ToolCall
{
//Placeholder
}