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 SendChatStreamAsync(string model, List messages, string? toolsJson = null) { var requestBody = new Dictionary { { "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; } } } }