62 lines
2.4 KiB
Go
62 lines
2.4 KiB
Go
package ai
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
openai "github.com/sashabaranov/go-openai"
|
|
"github.com/tradarr/backend/internal/summaryqa"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestTicket53OpenAIQAIsStatelessWithoutSourcesSessionOrCache(t *testing.T) {
|
|
calls := 0
|
|
cfg := openai.DefaultConfig("fixture-key")
|
|
cfg.HTTPClient = &http.Client{Transport: ticket31Transport(func(r *http.Request) (*http.Response, error) {
|
|
calls++
|
|
var body map[string]json.RawMessage
|
|
if json.NewDecoder(r.Body).Decode(&body) != nil {
|
|
t.Fatal("invalid request")
|
|
}
|
|
for _, key := range []string{"previous_response_id", "conversation", "session", "store", "prompt_cache_key"} {
|
|
if _, ok := body[key]; ok {
|
|
t.Fatalf("unexpected state/cache key %s", key)
|
|
}
|
|
}
|
|
var model string
|
|
_ = json.Unmarshal(body["model"], &model)
|
|
if model != "exact-saved-model" {
|
|
t.Fatal("selected model replaced")
|
|
}
|
|
text := string(body["messages"])
|
|
for _, expected := range []string{"OWN_SUMMARY", "EARLIER_QUESTION", "EARLIER_ANSWER", "CURRENT_QUESTION"} {
|
|
if !strings.Contains(text, expected) {
|
|
t.Fatal("missing own context", expected)
|
|
}
|
|
}
|
|
if strings.Contains(text, "PRIVATE_ARTICLE_BODY") {
|
|
t.Fatal("article sent to stateless QA")
|
|
}
|
|
return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": []string{"application/json"}}, Body: io.NopCloser(strings.NewReader(`{"choices":[{"finish_reason":"stop","message":{"content":"safe answer"}}]}`))}, nil
|
|
})}
|
|
p := statelessQAProvider{provider: &openAIProvider{model: "exact-saved-model", client: openai.NewClientWithConfig(cfg)}}
|
|
if p.Capabilities() != (summaryqa.Capabilities{}) {
|
|
t.Fatal("unsupported sessions advertised")
|
|
}
|
|
if _, ok := any(p).(summaryqa.SessionProvider); ok {
|
|
t.Fatal("stateless adapter pretends to implement sessions")
|
|
}
|
|
req := summaryqa.StatelessRequest{Summary: "OWN_SUMMARY", Language: "fr", History: []summaryqa.Exchange{{Question: "EARLIER_QUESTION", Answer: "EARLIER_ANSWER"}}, Passage: "excerpt", Question: "CURRENT_QUESTION"}
|
|
answer, err := p.Answer(context.Background(), req)
|
|
if err != nil || answer != "safe answer" || calls != 1 {
|
|
t.Fatal(answer, err, calls)
|
|
}
|
|
p = statelessQAProvider{provider: &ticket50Provider{err: errors.New("PRIVATE_ARTICLE_BODY")}}
|
|
if _, err = p.Answer(context.Background(), req); !errors.Is(err, summaryqa.ErrProvider) || strings.Contains(err.Error(), "PRIVATE") {
|
|
t.Fatal("raw provider error escaped", err)
|
|
}
|
|
}
|