reformatting

This commit is contained in:
grimsace
2026-06-08 15:53:40 -05:00
parent 99df32cb1f
commit ba5e1ff154
16 changed files with 137 additions and 17 deletions
+87
View File
@@ -0,0 +1,87 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using System.Threading.Tasks;
using System.Text.Json.Serialization;
using System.Linq;
namespace finder2e_foundry_converter.Services
{
public class OllamaService : ILlmService
{
private readonly HttpClient _httpClient = new HttpClient();
public async Task<List<string>> GetModelsAsync(string baseUrl)
{
try
{
var response = await _httpClient.GetFromJsonAsync<OllamaModelsResponse>($"{baseUrl.TrimEnd('/')}/api/tags");
if (response?.Models == null) return new List<string>();
return response.Models.Select(m => m.Name).ToList();
}
catch
{
return new List<string> { "Error: Ollama not found" };
}
}
public async Task<string> GenerateResponseAsync(string baseUrl, string model, string text, List<string> imagePaths)
{
try
{
var images = new List<string>();
foreach (var path in imagePaths)
{
if (string.IsNullOrWhiteSpace(path)) continue;
var bytes = await System.IO.File.ReadAllBytesAsync(path);
images.Add(Convert.ToBase64String(bytes));
}
var requestBody = new
{
model = model,
messages = new[]
{
new { role = "user", content = text, images = images.Count > 0 ? images.ToArray() : null }
},
stream = false
};
var response = await _httpClient.PostAsJsonAsync($"{baseUrl.TrimEnd('/')}/api/chat", requestBody);
var body = await response.Content.ReadAsStringAsync();
using var doc = JsonDocument.Parse(body);
if (doc.RootElement.TryGetProperty("error", out var error))
{
return $"Error: {error.GetString()}";
}
if (doc.RootElement.TryGetProperty("message", out var message))
{
return message.GetProperty("content").GetString() ?? "No content returned";
}
return "No response returned.";
}
catch (Exception ex)
{
return $"Error: {ex.Message}";
}
}
private class OllamaModelsResponse
{
[JsonPropertyName("models")]
public List<OllamaModelData>? Models { get; set; }
}
private class OllamaModelData
{
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
}
}
}