Port LEM scoring/training pipeline into CoreGo as pkg/ml with: - Inference abstraction with HTTP, llama-server, and Ollama backends - 3-tier scoring engine (heuristic, exact, LLM judge) - Capability and content probes for model evaluation - GGUF/safetensors format converters, MLX to PEFT adapter conversion - DuckDB integration for training data pipeline - InfluxDB metrics for lab dashboard - Training data export (JSONL + Parquet) - Expansion generation pipeline with distributed workers - 10 CLI commands under 'core ml' (score, probe, export, expand, status, gguf, convert, agent, worker) - 5 MCP tools (ml_generate, ml_score, ml_probe, ml_status, ml_backends) All 37 ML tests passing. Binary builds at 138MB with all commands. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package ml
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
func TestHTTPBackend_Generate_Good(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/v1/chat/completions" {
|
|
t.Errorf("unexpected path: %s", r.URL.Path)
|
|
}
|
|
|
|
var req chatRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
t.Fatalf("decode request: %v", err)
|
|
}
|
|
|
|
if len(req.Messages) != 1 || req.Messages[0].Content != "hello" {
|
|
t.Errorf("unexpected messages: %+v", req.Messages)
|
|
}
|
|
|
|
resp := chatResponse{
|
|
Choices: []chatChoice{{Message: Message{Role: "assistant", Content: "world"}}},
|
|
}
|
|
json.NewEncoder(w).Encode(resp)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
b := NewHTTPBackend(srv.URL, "test-model")
|
|
result, err := b.Generate(context.Background(), "hello", DefaultGenOpts())
|
|
if err != nil {
|
|
t.Fatalf("Generate: %v", err)
|
|
}
|
|
if result != "world" {
|
|
t.Errorf("got %q, want %q", result, "world")
|
|
}
|
|
}
|
|
|
|
func TestHTTPBackend_Generate_Bad(t *testing.T) {
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
w.Write([]byte("bad request"))
|
|
}))
|
|
defer srv.Close()
|
|
|
|
b := NewHTTPBackend(srv.URL, "test-model")
|
|
_, err := b.Generate(context.Background(), "hello", DefaultGenOpts())
|
|
if err == nil {
|
|
t.Fatal("expected error for 400 response")
|
|
}
|
|
}
|
|
|
|
func TestHTTPBackend_Retry_Ugly(t *testing.T) {
|
|
attempts := 0
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
attempts++
|
|
if attempts < 3 {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
w.Write([]byte("internal error"))
|
|
return
|
|
}
|
|
resp := chatResponse{
|
|
Choices: []chatChoice{{Message: Message{Role: "assistant", Content: "recovered"}}},
|
|
}
|
|
json.NewEncoder(w).Encode(resp)
|
|
}))
|
|
defer srv.Close()
|
|
|
|
b := NewHTTPBackend(srv.URL, "test-model")
|
|
result, err := b.Generate(context.Background(), "test", DefaultGenOpts())
|
|
if err != nil {
|
|
t.Fatalf("Generate after retry: %v", err)
|
|
}
|
|
if result != "recovered" {
|
|
t.Errorf("got %q, want %q", result, "recovered")
|
|
}
|
|
if attempts != 3 {
|
|
t.Errorf("expected 3 attempts, got %d", attempts)
|
|
}
|
|
}
|
|
|
|
func TestHTTPBackend_Name(t *testing.T) {
|
|
b := NewHTTPBackend("http://localhost", "model")
|
|
if b.Name() != "http" {
|
|
t.Errorf("Name() = %q, want %q", b.Name(), "http")
|
|
}
|
|
}
|
|
|
|
func TestHTTPBackend_Available(t *testing.T) {
|
|
b := NewHTTPBackend("http://localhost", "model")
|
|
if !b.Available() {
|
|
t.Error("Available() should be true when baseURL is set")
|
|
}
|
|
|
|
b2 := NewHTTPBackend("", "model")
|
|
if b2.Available() {
|
|
t.Error("Available() should be false when baseURL is empty")
|
|
}
|
|
}
|