BioGTK  6.5.0
A .NET library & program for annotating, editing various microscopy imaging formats using Bioformats supported images. Including whole slide, pyramidal, and series.
Loading...
Searching...
No Matches
BioGTK.AI.OllamaChatService Class Referencesealed

Public Member Functions

 OllamaChatService (HttpClient httpClient, string baseUrl="http://localhost:11434")
 
async Task< string > GetResponseAsync (string systemPrompt, string userPrompt, CancellationToken cancellationToken=default)
 

Detailed Description

Definition at line 102 of file AI.cs.

Constructor & Destructor Documentation

◆ OllamaChatService()

BioGTK.AI.OllamaChatService.OllamaChatService ( HttpClient httpClient,
string baseUrl = "http://localhost:11434" )

Definition at line 107 of file AI.cs.

107 ://localhost:11434")
108 {
109 _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
110 _httpClient.Timeout = TimeSpan.FromMinutes(60);
111 if (_httpClient.BaseAddress == null)
112 _httpClient.BaseAddress = new Uri(baseUrl);
113
114 // ✅ Use attributes for naming instead of a broken SnakeCase policy
115 _jsonOptions = new JsonSerializerOptions
116 {
117 PropertyNamingPolicy = null, // respect [JsonPropertyName] attributes
118 DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
119 WriteIndented = false
120 };
121 }

Member Function Documentation

◆ GetResponseAsync()

async Task< string > BioGTK.AI.OllamaChatService.GetResponseAsync ( string systemPrompt,
string userPrompt,
CancellationToken cancellationToken = default )

Definition at line 123 of file AI.cs.

127 {
128 var request = new ChatRequest(
129 Model: "gpt-oss",
130 Messages: new[]
131 {
132 new ChatMessage("system", systemPrompt),
133 new ChatMessage("user", userPrompt)
134 });
135
136 var json = JsonSerializer.Serialize(request, _jsonOptions);
137 using var content = new StringContent(json, Encoding.UTF8, "application/json");
138
139 using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(30));
140 using var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(
141 cancellationToken, timeoutCts.Token);
142
143 HttpResponseMessage httpResponse;
144 try
145 {
146 httpResponse = await _httpClient.PostAsync(
147 "/v1/chat/completions",
148 content,
149 linkedCts.Token)
150 .ConfigureAwait(false);
151 }
152 catch (Exception e) when (!cancellationToken.IsCancellationRequested)
153 {
154 throw new Exception();
155 }
156
157 if (!httpResponse.IsSuccessStatusCode)
158 {
159 var rawError = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
160 throw new HttpRequestException(
161 $"Server returned {(int)httpResponse.StatusCode} – {httpResponse.ReasonPhrase}. Body: {rawError}");
162 }
163
164 var chatResponse = await httpResponse.Content
165 .ReadFromJsonAsync<ChatResponse>(_jsonOptions, linkedCts.Token)
166 .ConfigureAwait(false);
167
168 var firstChoice = chatResponse?.Choices?.Count > 0 ? chatResponse.Choices[0] : null;
169 if (firstChoice?.Message?.Content == null)
170 throw new InvalidOperationException("The response does not contain a usable message.");
171
172 return firstChoice.Message.Content;
173 }

The documentation for this class was generated from the following file: