Feature: Add LlamaCpp model discovery API

Add GetAvailableModelsAsync() to LlamaCppVisionClient that:
- Queries llama.cpp /v1/models endpoint for available models
- Filters out multimodal projector models (mmproj-*)
- Tracks loaded/unloaded status for each model
- Returns sorted list with loaded models first

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-15 22:52:41 -05:00
parent b5406106ec
commit cba20f20fc

View File

@@ -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;
}
/// <summary>
/// Get available models from the llama.cpp server.
/// </summary>
public async Task<List<LlamaCppModel>> 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<LlamaCppModel>();
}
var json = await response.Content.ReadAsStringAsync();
var modelsResponse = JsonSerializer.Deserialize<LlamaCppModelsResponse>(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<LlamaCppModel>();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching models from llama.cpp");
return new List<LlamaCppModel>();
}
}
/// <summary>
/// Send a text-only prompt to the LLM (no image).
/// </summary>
@@ -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<LlamaCppModelData>? 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; }
}
}