75 lines
2.2 KiB
C#
75 lines
2.2 KiB
C#
using System.IO;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
namespace Szmedi.RvKits.LLMScript
|
|
{
|
|
public class OllamaProvider : ILLMProvider
|
|
{
|
|
private static readonly HttpClient _client = new HttpClient();
|
|
private readonly string _endpoint;
|
|
private readonly string _model;
|
|
|
|
public OllamaProvider(string endpoint, string model)
|
|
{
|
|
_endpoint = endpoint;
|
|
_model = model;
|
|
}
|
|
|
|
public async Task GenerateStreamAsync(
|
|
string systemPrompt,
|
|
string userPrompt,
|
|
Action<string> onTokenReceived)
|
|
{
|
|
var payload = new
|
|
{
|
|
model = _model,
|
|
messages = new[]
|
|
{
|
|
new { role = "system", content = systemPrompt },
|
|
new { role = "user", content = userPrompt }
|
|
},
|
|
stream = true
|
|
};
|
|
|
|
string json = JsonConvert.SerializeObject(payload);
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, _endpoint);
|
|
request.Content = new StringContent(json, Encoding.UTF8, "application/json");
|
|
|
|
var response = await _client.SendAsync(
|
|
request,
|
|
HttpCompletionOption.ResponseHeadersRead);
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
using (var stream = await response.Content.ReadAsStreamAsync())
|
|
using (var reader = new StreamReader(stream))
|
|
{
|
|
while (!reader.EndOfStream)
|
|
{
|
|
string line = await reader.ReadLineAsync();
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
continue;
|
|
|
|
JObject obj = JObject.Parse(line);
|
|
|
|
bool done = obj["done"]?.Value<bool>() ?? false;
|
|
|
|
var content = obj["message"]?["content"];
|
|
if (content != null)
|
|
{
|
|
onTokenReceived(content.ToString());
|
|
}
|
|
|
|
if (done)
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|