feat: version ai result schemas
This commit is contained in:
@@ -38,11 +38,14 @@ type Usage struct {
|
||||
TotalTokens int `json:"total_tokens"`
|
||||
}
|
||||
|
||||
const ChatResultSchemaVersion = "ai.chat_result.v1"
|
||||
|
||||
type ChatResult struct {
|
||||
Content string `json:"content"`
|
||||
Model string `json:"model"`
|
||||
Usage *Usage `json:"usage,omitempty"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
Content string `json:"content"`
|
||||
Model string `json:"model"`
|
||||
Usage *Usage `json:"usage,omitempty"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
}
|
||||
|
||||
type chatRequest struct {
|
||||
@@ -137,10 +140,11 @@ func (c *Client) Chat(ctx context.Context, in ChatInput) (*ChatResult, error) {
|
||||
modelName = c.model
|
||||
}
|
||||
return &ChatResult{
|
||||
Content: out.Choices[0].Message.Content,
|
||||
Model: modelName,
|
||||
Usage: out.Usage,
|
||||
DurationMS: duration.Milliseconds(),
|
||||
SchemaVersion: ChatResultSchemaVersion,
|
||||
Content: out.Choices[0].Message.Content,
|
||||
Model: modelName,
|
||||
Usage: out.Usage,
|
||||
DurationMS: duration.Milliseconds(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
43
internal/llm/client_test.go
Normal file
43
internal/llm/client_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestChatResultIncludesSchemaVersion(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/chat/completions" {
|
||||
t.Fatalf("path = %q, want /v1/chat/completions", r.URL.Path)
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||
"model": "qwen2.5-14b",
|
||||
"choices": []map[string]any{
|
||||
{"message": map[string]string{"role": "assistant", "content": `{"ok":true}`}},
|
||||
},
|
||||
"usage": map[string]int{
|
||||
"prompt_tokens": 10,
|
||||
"completion_tokens": 2,
|
||||
"total_tokens": 12,
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := New(server.URL, "", "fallback-model", 0)
|
||||
got, err := client.Chat(t.Context(), ChatInput{User: "test", MaxTokens: 32})
|
||||
if err != nil {
|
||||
t.Fatalf("Chat: %v", err)
|
||||
}
|
||||
if got.SchemaVersion != ChatResultSchemaVersion {
|
||||
t.Fatalf("schema_version = %q, want %q", got.SchemaVersion, ChatResultSchemaVersion)
|
||||
}
|
||||
if got.Content != `{"ok":true}` {
|
||||
t.Fatalf("content = %q", got.Content)
|
||||
}
|
||||
if got.Usage == nil || got.Usage.TotalTokens != 12 {
|
||||
t.Fatalf("usage = %#v", got.Usage)
|
||||
}
|
||||
}
|
||||
@@ -60,15 +60,18 @@ type Segment struct {
|
||||
Speaker string `json:"speaker,omitempty"`
|
||||
}
|
||||
|
||||
const ResultSchemaVersion = "ai.transcription_result.v1"
|
||||
|
||||
type Result struct {
|
||||
Provider string `json:"provider,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Attempts []Attempt `json:"attempts,omitempty"`
|
||||
Language string `json:"language"`
|
||||
Segments []Segment `json:"segments"`
|
||||
DiarizeError *string `json:"diarize_error,omitempty"`
|
||||
AlignError *string `json:"align_error,omitempty"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
SchemaVersion string `json:"schema_version"`
|
||||
Provider string `json:"provider,omitempty"`
|
||||
Model string `json:"model,omitempty"`
|
||||
Attempts []Attempt `json:"attempts,omitempty"`
|
||||
Language string `json:"language"`
|
||||
Segments []Segment `json:"segments"`
|
||||
DiarizeError *string `json:"diarize_error,omitempty"`
|
||||
AlignError *string `json:"align_error,omitempty"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
}
|
||||
|
||||
type Attempt struct {
|
||||
@@ -199,11 +202,12 @@ func (c *Client) transcribeWithProvider(ctx context.Context, provider ProviderCo
|
||||
attempt.Text = text
|
||||
attempt.Segments = segments
|
||||
return &Result{
|
||||
Provider: provider.Name,
|
||||
Model: resp.Model,
|
||||
Language: firstNonEmpty(resp.Language, in.Language, "unknown"),
|
||||
Segments: segments,
|
||||
DurationMS: duration.Milliseconds(),
|
||||
SchemaVersion: ResultSchemaVersion,
|
||||
Provider: provider.Name,
|
||||
Model: resp.Model,
|
||||
Language: firstNonEmpty(resp.Language, in.Language, "unknown"),
|
||||
Segments: segments,
|
||||
DurationMS: duration.Milliseconds(),
|
||||
}, attempt, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -84,6 +84,9 @@ func TestWhisperUsesAudioTranscriptionsEndpoint(t *testing.T) {
|
||||
if len(got.Segments) != 2 || got.Segments[0].Text != "Алло, тест." || got.Segments[1].Start != 1.2 {
|
||||
t.Fatalf("segments = %#v", got.Segments)
|
||||
}
|
||||
if got.SchemaVersion != ResultSchemaVersion {
|
||||
t.Fatalf("schema_version = %q, want %q", got.SchemaVersion, ResultSchemaVersion)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWhisperFallsBackToJSONWhenVerboseJSONUnsupported(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user