mirror of
https://github.com/hanzoai/ai.git
synced 2026-11-20 14:13:23 +00:00
hanzo-inc/cloud#460 (moved from #116) listed eight defects at c7f85eed. Seven
were still on main; this fixes them.
A LocalModelProvider of type Zen, Enso or Hanzo had no client, and neither did
DeepSeek with a model other than chat or reasoner, so the first call
dereferenced nil. dialect() now names each type's client and flush once, with
typed values in place of an `any` asserted in each loop, and QueryText refuses
a type with no client. That also ends the assertion panics: OpenAI completion
models asserted the answer-only flush while holding the think one, and so did
Local and DigitalOcean completion models in local.go. Both completion loops
now skip a chunk with no choice, and local.go's no longer returns a nil result
with no error when the count fails.
DeepSeek, Mistral, SiliconFlow, Baichuan and BaiduCloud priced the total token
count at both the input and the output rate. The prompt is now priced at the
input rate and the answer at the output rate. gpt-4.5's case sat after
gpt-4's, which also matches it, so gpt-4.5 billed as gpt-4; it now comes first
and bills at the rate the pricing table publishes.
GetMessagesForChats bound the chat names to a bare "{:p0}" WHERE; it is
dbx.In("chat", ...). updateErrorText's branches were inverted, so a record
with no id updated WHERE id = 0. The Guacamole handshake returned nil on a
failed write, and UploadVideo returned "", nil on each failure; both return
the error.
Item 7 (gateway frames) is left: the gateway's own errors are gateway frames
now, but its chat, models and balance fast paths still answer with cloud
frames. That needs hanzoai/gateway's side confirmed first.
74 lines
2.4 KiB
Go
74 lines
2.4 KiB
Go
// Copyright 2026 Hanzo AI Inc. All Rights Reserved.
|
|
|
|
package model
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hanzoai/ai/proxy"
|
|
)
|
|
|
|
// A completion (non-chat) model streams its answer to the writer. The loop used
|
|
// to assert its flush to the answer-only shape while holding the think one, so
|
|
// every completion model panicked on its first piece. A chunk with no choice
|
|
// (usage arrives on one) is skipped rather than indexed.
|
|
func TestACompletionModelStreamsItsAnswer(t *testing.T) {
|
|
stream := []string{
|
|
`{"id":"c","object":"text_completion","created":1,"model":"davinci","choices":[{"index":0,"text":"Hello","finish_reason":""}]}`,
|
|
`{"id":"c","object":"text_completion","created":1,"model":"davinci","choices":[]}`,
|
|
`{"id":"c","object":"text_completion","created":1,"model":"davinci","choices":[{"index":0,"text":" world","finish_reason":"stop"}],"usage":{"prompt_tokens":3,"completion_tokens":2,"total_tokens":5}}`,
|
|
`[DONE]`,
|
|
}
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !strings.HasSuffix(r.URL.Path, "/completions") {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
for _, data := range stream {
|
|
fmt.Fprintf(w, "data: %s\n\n", data)
|
|
}
|
|
}))
|
|
t.Cleanup(srv.Close)
|
|
|
|
previous := proxy.ProxyHttpClient
|
|
proxy.ProxyHttpClient = srv.Client()
|
|
t.Cleanup(func() { proxy.ProxyHttpClient = previous })
|
|
|
|
p, err := NewOpenAiModelProvider("davinci", "k", srv.URL, 0, 1, 0, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := getOpenAiModelType("davinci"); got != "Completion" {
|
|
t.Fatalf("davinci is a %q model; this test needs a completion one", got)
|
|
}
|
|
|
|
w := httptest.NewRecorder()
|
|
result, err := func() (result *ModelResult, err error) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
t.Fatalf("QueryText panicked: %v", r)
|
|
}
|
|
}()
|
|
return p.QueryText("say hello", w, nil, "", nil, nil, "en")
|
|
}()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
body := w.Body.String()
|
|
for _, piece := range []string{"event: message\ndata: Hello\n\n", "event: message\ndata: world\n\n"} {
|
|
if !strings.Contains(body, piece) {
|
|
t.Errorf("stream %q does not carry %q", body, piece)
|
|
}
|
|
}
|
|
if result.PromptTokenCount != 3 || result.ResponseTokenCount != 2 || result.TotalTokenCount != 5 {
|
|
t.Errorf("counts = %d/%d/%d, want 3/2/5 from the upstream's usage",
|
|
result.PromptTokenCount, result.ResponseTokenCount, result.TotalTokenCount)
|
|
}
|
|
}
|