diff --git a/MoneyMap/Services/AIVisionClient.cs b/MoneyMap/Services/AIVisionClient.cs index 21909f5..30bf059 100644 --- a/MoneyMap/Services/AIVisionClient.cs +++ b/MoneyMap/Services/AIVisionClient.cs @@ -1,5 +1,6 @@ using System.Text; using System.Text.Json; +using System.Text.Json.Serialization; namespace MoneyMap.Services { @@ -234,6 +235,44 @@ namespace MoneyMap.Services _logger = logger; } + /// + /// Get available models from the llama.cpp server. + /// + public async Task> GetAvailableModelsAsync() + { + var baseUrl = _configuration["LlamaCpp:BaseUrl"] ?? "http://athena.lan:11434"; + + try + { + var response = await _httpClient.GetAsync($"{baseUrl.TrimEnd('/')}/v1/models"); + + if (!response.IsSuccessStatusCode) + { + _logger.LogWarning("Failed to fetch models: {StatusCode}", response.StatusCode); + return new List(); + } + + var json = await response.Content.ReadAsStringAsync(); + var modelsResponse = JsonSerializer.Deserialize(json); + + return modelsResponse?.Data? + .Where(m => !m.Id.StartsWith("mmproj-")) // Filter out multimodal projectors + .Select(m => new LlamaCppModel + { + Id = m.Id, + IsLoaded = m.Status?.Value == "loaded" + }) + .OrderByDescending(m => m.IsLoaded) + .ThenBy(m => m.Id) + .ToList() ?? new List(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error fetching models from llama.cpp"); + return new List(); + } + } + /// /// Send a text-only prompt to the LLM (no image). /// @@ -468,4 +507,32 @@ namespace MoneyMap.Services return trimmed; } } + + // Models for llama.cpp /v1/models endpoint + public class LlamaCppModel + { + public string Id { get; set; } = ""; + public bool IsLoaded { get; set; } + } + + public class LlamaCppModelsResponse + { + [JsonPropertyName("data")] + public List? Data { get; set; } + } + + public class LlamaCppModelData + { + [JsonPropertyName("id")] + public string Id { get; set; } = ""; + + [JsonPropertyName("status")] + public LlamaCppModelStatus? Status { get; set; } + } + + public class LlamaCppModelStatus + { + [JsonPropertyName("value")] + public string? Value { get; set; } + } }