Yess
Some checks failed
Mirror to Gitea / git-sync (push) Has been cancelled

This commit is contained in:
2026-03-28 00:26:55 +02:00
parent ea2d84f5cc
commit ae0994409a
15 changed files with 660 additions and 94 deletions

View File

@@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using System.IO;
namespace VisionAsist.Models;
public class OllamaMessage
{
public string role { get; set; } = string.Empty;
public string content { get; set; } = string.Empty;
}
public static class OllamaService
{
private static readonly HttpClient _httpClient = new() { Timeout = TimeSpan.FromMinutes(5) };
private const string BaseUrl = "http://localhost:11434/api";
public static async IAsyncEnumerable<string> SendChatStreamAsync(string model, List<OllamaMessage> messages, string? toolsJson = null)
{
var requestBody = new Dictionary<string, object>
{
{ "model", model },
{ "messages", messages },
{ "stream", true } // ВКЛЮЧАЕМ СТРИМИНГ
};
if (!string.IsNullOrEmpty(toolsJson))
{
requestBody.Add("tools", JsonDocument.Parse(toolsJson).RootElement);
}
var json = JsonSerializer.Serialize(requestBody);
var content = new StringContent(json, Encoding.UTF8, "application/json");
using var request = new HttpRequestMessage(HttpMethod.Post, $"{BaseUrl}/chat") { Content = content };
using var response = await _httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
using var stream = await response.Content.ReadAsStreamAsync();
using var reader = new StreamReader(stream);
while (!reader.EndOfStream)
{
var line = await reader.ReadLineAsync();
if (!string.IsNullOrWhiteSpace(line))
{
yield return line;
}
}
}
}