Upload a recording (audio, or video with the camera off), get a private Swedish transcript, and chat about it. - ADR-033: RLS-scoped private transcript store; ffprobe/ffmpeg intake; gateway transcription (iguana kb-whisper, berget fallback); in-process single-replica worker; chat over the transcript (local models only). - ADR-034: optional two-engine mode (pianissimo + kb-whisper) reconciled by a LOCAL model; off unless TAPIR_RECONCILE_BASE_MODEL is set. - ADR-035: real progress (phases, running/quiet/lost), phase logging, every judged spot stored, merged segments. - Hardening from four adversarial reviews: errors logged and stored as kinds never text; per-engine deadlines; converted-length check; bounded queue; client hang-ups are not errors; strict local-alias check. - Dockerfile: static ffmpeg/ffprobe 7.0.2 pinned by sha256 (#34). Built and inspected by hand; CI does not build the image on a PR, so its first CI build is this merge. Uploads stay OFF until TAPIR_UPLOADS_ENABLED is set in the deployment. Closes #33, #34, #42, #43, #44. Co-authored-by: Mathias <mthbqv@gmail.com>
This commit was merged in pull request #39.
This commit is contained in:
@@ -25,12 +25,16 @@ import (
|
||||
// docs/homelab-integration.md.
|
||||
const defaultMaxTokens = 8192
|
||||
|
||||
// defaultTemperature is what every caller got before WithTemperature existed.
|
||||
const defaultTemperature = 0.2
|
||||
|
||||
// Client calls an OpenAI-compatible chat completions endpoint.
|
||||
type Client struct {
|
||||
baseURL string
|
||||
apiKey string
|
||||
model string
|
||||
maxTokens int
|
||||
temp float64
|
||||
httpClient *http.Client
|
||||
usageHook func(model string, prompt, completion int)
|
||||
}
|
||||
@@ -51,6 +55,35 @@ func WithMaxTokens(n int) Option {
|
||||
}
|
||||
}
|
||||
|
||||
// maxErrorBody bounds how much of a failing response ends up in an error message.
|
||||
const maxErrorBody = 512
|
||||
|
||||
// HTTPError is a non-200 answer from the gateway. Its text keeps the form callers that
|
||||
// log it already rely on, with the body bounded; its Code is what code may branch on and
|
||||
// what errkind turns into a kind. The body is provider text and can echo the prompt, so
|
||||
// nothing private may persist or display Error() (ADR-035 review F3).
|
||||
type HTTPError struct {
|
||||
Code int
|
||||
Body string
|
||||
}
|
||||
|
||||
func (e *HTTPError) Error() string {
|
||||
body := e.Body
|
||||
if len(body) > maxErrorBody {
|
||||
body = body[:maxErrorBody] + "…"
|
||||
}
|
||||
return fmt.Sprintf("LLM returned %d: %s", e.Code, body)
|
||||
}
|
||||
|
||||
// HTTPStatus implements errkind.HTTPStatuser.
|
||||
func (e *HTTPError) HTTPStatus() int { return e.Code }
|
||||
|
||||
// WithTemperature overrides the sampling temperature. Zero IS a valid value and is sent
|
||||
// as zero: the reconciler's accuracy was measured at temperature 0 and must run there.
|
||||
func WithTemperature(t float64) Option {
|
||||
return func(c *Client) { c.temp = t }
|
||||
}
|
||||
|
||||
// WithUsageHook registers a callback fired after a successful completion with the
|
||||
// model and the prompt/completion token counts from the response usage block. It
|
||||
// keeps this copied, stdlib-only package (ADR-004) decoupled from metrics: the
|
||||
@@ -66,6 +99,7 @@ func New(baseURL, apiKey, model string, timeout time.Duration, opts ...Option) *
|
||||
apiKey: apiKey,
|
||||
model: model,
|
||||
maxTokens: defaultMaxTokens,
|
||||
temp: defaultTemperature,
|
||||
httpClient: &http.Client{Timeout: timeout},
|
||||
}
|
||||
for _, opt := range opts {
|
||||
@@ -105,7 +139,7 @@ func (c *Client) Complete(ctx context.Context, system, user string) (string, err
|
||||
{Role: "system", Content: system},
|
||||
{Role: "user", Content: user},
|
||||
},
|
||||
Temperature: 0.2,
|
||||
Temperature: c.temp,
|
||||
MaxTokens: c.maxTokens,
|
||||
}
|
||||
b, err := json.Marshal(body)
|
||||
@@ -155,7 +189,7 @@ func (c *Client) Complete(ctx context.Context, system, user string) (string, err
|
||||
return "", fmt.Errorf("read response: %w", err)
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", fmt.Errorf("LLM returned %d: %s", resp.StatusCode, out)
|
||||
return "", &HTTPError{Code: resp.StatusCode, Body: string(out)}
|
||||
}
|
||||
|
||||
var cr chatResponse
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"git.d-ma.be/mathias/tapir/internal/errkind"
|
||||
)
|
||||
|
||||
// A non-200 from the gateway is an HTTP status error: its kind can be logged and stored
|
||||
// without its body, which can echo the prompt (adversarial review F3).
|
||||
|
||||
func failingGateway(t *testing.T, status int, body string) *httptest.Server {
|
||||
t.Helper()
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
||||
w.WriteHeader(status)
|
||||
_, _ = w.Write([]byte(body))
|
||||
}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
func TestClient_ANon200IsAnHTTPStatusErrorWhoseKindCarriesNoBody(t *testing.T) {
|
||||
srv := failingGateway(t, http.StatusBadRequest, `{"error":"prompt was: Anders ansvarar för virket"}`)
|
||||
|
||||
_, err := New(srv.URL, "", "m", 5*time.Second).Complete(context.Background(), "s", "u")
|
||||
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
if got := errkind.Of(err); got != "http_400" {
|
||||
t.Errorf("errkind.Of = %q, want http_400", got)
|
||||
}
|
||||
var he *HTTPError
|
||||
if !errors.As(err, &he) || he.Code != http.StatusBadRequest {
|
||||
t.Errorf("error is not an *HTTPError with the status code: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_TheErrorTextIsUnchangedForCallersThatLogIt(t *testing.T) {
|
||||
srv := failingGateway(t, http.StatusBadGateway, "upstream down")
|
||||
|
||||
_, err := New(srv.URL, "", "m", 5*time.Second).Complete(context.Background(), "s", "u")
|
||||
|
||||
if err == nil || err.Error() != "LLM returned 502: upstream down" {
|
||||
t.Errorf("error text = %v: callers that log it already rely on this form", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_AnEnormousErrorBodyIsBoundedInTheErrorText(t *testing.T) {
|
||||
srv := failingGateway(t, http.StatusInternalServerError, strings.Repeat("x", 100_000))
|
||||
|
||||
_, err := New(srv.URL, "", "m", 5*time.Second).Complete(context.Background(), "s", "u")
|
||||
|
||||
if err == nil || len(err.Error()) > 1024 {
|
||||
t.Errorf("error text is %d bytes: a gateway's whole body must not become an error message", len(err.Error()))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
// capturedTemperature runs one Complete against a fake gateway and returns the
|
||||
// "temperature" field the request carried, and whether it was present at all.
|
||||
func capturedTemperature(t *testing.T, opts ...Option) (float64, bool) {
|
||||
t.Helper()
|
||||
var body map[string]any
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
raw, _ := io.ReadAll(r.Body)
|
||||
_ = json.Unmarshal(raw, &body)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"choices": []map[string]any{{"message": map[string]any{"role": "assistant", "content": "ok"}}}})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if _, err := New(srv.URL, "", "m", 5*time.Second, opts...).Complete(context.Background(), "s", "u"); err != nil {
|
||||
t.Fatalf("Complete: %v", err)
|
||||
}
|
||||
v, ok := body["temperature"].(float64)
|
||||
return v, ok
|
||||
}
|
||||
|
||||
func TestClient_DefaultTemperatureIsUnchanged(t *testing.T) {
|
||||
got, ok := capturedTemperature(t)
|
||||
|
||||
if !ok || got != 0.2 {
|
||||
t.Fatalf("default temperature = %v (present %v), want 0.2: existing callers must not change behaviour", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_WithTemperatureZeroSendsExactlyZero(t *testing.T) {
|
||||
// The reconciler's accuracy (92%/97%) was measured at temperature 0. Zero must be
|
||||
// SENT, not treated as "unset" and replaced by the default.
|
||||
got, ok := capturedTemperature(t, WithTemperature(0))
|
||||
|
||||
if !ok || got != 0 {
|
||||
t.Fatalf("temperature = %v (present %v), want an explicit 0", got, ok)
|
||||
}
|
||||
}
|
||||
|
||||
func TestClient_WithTemperatureSendsTheGivenValue(t *testing.T) {
|
||||
got, ok := capturedTemperature(t, WithTemperature(0.7))
|
||||
|
||||
if !ok || got != 0.7 {
|
||||
t.Fatalf("temperature = %v (present %v), want 0.7", got, ok)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user