Go: implement provider: Voyage AI (#14811)
### What problem does this PR solve?
Add a Go driver for Voyage AI (https://voyageai.com), one of the
unchecked providers on the umbrella tracking issue #14736. Voyage AI is
**embed + rerank only** — no chat, no streaming, no `/v1/models`
endpoint. It's the first provider in the Go layer of this shape.
Until this PR, a tenant who configured `voyage` as a model provider in
the Go layer fell through to the default branch of
`internal/entity/models/factory.go` and got the dummy driver.
### What this PR includes
- New `internal/entity/models/voyage.go` with a `VoyageModel`
implementing the `ModelDriver` interface.
- New `conf/models/voyage.json` with 6 embedding models (`voyage-3.5`,
`voyage-3.5-lite`, `voyage-3-large`, `voyage-code-3`, `voyage-law-2`,
`voyage-finance-2`) and 2 rerank models (`rerank-2`, `rerank-2-lite`).
- `factory.go`: route `"voyage"` to `NewVoyageModel`.
- `internal/entity/models/voyage_test.go`: 19 unit tests.
### How the driver works
- **Embed**: `POST /v1/embeddings`. Response is OpenAI-shaped (`{data:
[{embedding, index, object, text}], model, usage}`). Driver reorders by
`index`, rejects duplicate / out-of-range / missing slots, and
short-circuits empty input without an HTTP call.
- **Rerank**: `POST /v1/rerank`. Voyage uses **`top_k`** as the request
param name (not `top_n` like Aliyun/SiliconFlow); the driver translates
`RerankConfig.TopN` → `top_k`. Response is Cohere-shaped (`{data:
[{relevance_score, index}], model}`), so the existing
`RerankResponse{Data: []RerankResult{Index, RelevanceScore}}` shape fits
cleanly.
- **`ListModels`**: returns a hardcoded list of `voyageKnownModels`.
Voyage does **not** expose `/v1/models` (probed live, returns 404), so
the driver synthesizes the list from the same set the config ships. New
upstream models are added by extending one slice.
- **`CheckConnection`**: pings a 1-input embed call against
`voyage-3.5`. Without `/v1/models`, this is the cheapest way to verify
the API key + network path before a tenant tries a real workload.
- **`ChatWithMessages` / `ChatStreamlyWithSender` / `Balance` /
`TranscribeAudio` / `AudioSpeech` / `OCRFile`**: all return `"no such
method"`. Voyage does not host any of these surfaces.
No interface change. No new dependencies.
### How was this tested?
**19 unit tests** in `internal/entity/models/voyage_test.go` — all pass
on go 1.25:
```
$ go test -vet=off -run TestVoyage -count=1 ./internal/entity/models/...
ok ragflow/internal/entity/models 0.036s
```
Coverage: Name; Embed (happy path, reorder, empty-input, missing
key/model, duplicate index, out-of-range index, missing slot); Rerank
(happy path with `top_k` assertion, default-to-len-documents, empty
documents, out-of-range index); ListModels (static list, missing key);
CheckConnection (happy, 401); chat methods sentinels; Balance sentinel;
audio/OCR sentinels.
`go build ./internal/entity/models/...` exits 0.
**Live integration test** against `api.voyageai.com`:
```
=== RUN TestVoyageLiveSmoke
[OK] Name() = "voyage"
[OK] ListModels (static): 8 models -> [voyage-3.5 voyage-3.5-lite voyage-3-large voyage-code-3 voyage-law-2 voyage-finance-2 rerank-2 rerank-2-lite]
[OK] CheckConnection
[OK] Embed vectors=3 dim=1024 indices=[0 1 2]
[OK] Embed(empty) -> 0 vectors
[OK] Rerank results=3 scores=[0.8125 0.59765625 0.39453125]
[OK] ChatWithMessages returns voyage, no such method
[OK] Balance returns voyage, no such method
VOYAGE LIVE SMOKE PASSED
--- PASS: TestVoyageLiveSmoke (0.81s)
```
What the live run proves on the wire:
- Auth (`Bearer <key>`) accepted by `api.voyageai.com`.
- Embed `voyage-3.5` on 3 inputs returns 3 vectors at dim 1024 with
`index` field preserved as `[0, 1, 2]` — the reorder-by-index code is
exercised on real data.
- Empty input short-circuits without an HTTP call (mock server would
have been hit if it did).
- Rerank `rerank-2` on 3 docs returns 3 real `relevance_score` floats
`[0.8125, 0.598, 0.395]`. The `top_k` translation works on the live
wire.
- All sentinel methods return the documented `"no such method"` strings.
### Note on PR history
This branch was previously named for LocalAI Embed work which is now
consolidated into PR #14813. The branch was reset to `upstream/main` and
rebuilt for Voyage. Diff against `main` is a clean +838 lines across 4
files.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
Tracking: #14736
---------
Co-authored-by: Jin Hai <haijin.chn@gmail.com>
2026-05-13 15:46:54 -10:00
|
|
|
package models
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"io"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func newVoyageServer(t *testing.T, expectedPath string, handler func(t *testing.T, body map[string]interface{}, w http.ResponseWriter)) *httptest.Server {
|
|
|
|
|
t.Helper()
|
|
|
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
if r.URL.Path != expectedPath {
|
|
|
|
|
t.Errorf("expected path=%s, got %s", expectedPath, r.URL.Path)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
|
|
|
|
|
t.Errorf("expected Authorization=Bearer test-key, got %q", got)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if got := r.Header.Get("Content-Type"); got != "application/json" {
|
|
|
|
|
t.Errorf("expected Content-Type=application/json, got %q", got)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
raw, err := io.ReadAll(r.Body)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Errorf("read body: %v", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var body map[string]interface{}
|
|
|
|
|
if err := json.Unmarshal(raw, &body); err != nil {
|
|
|
|
|
t.Errorf("unmarshal: %v\nraw=%s", err, string(raw))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
handler(t, body, w)
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newVoyageForTest(baseURL string) *VoyageModel {
|
|
|
|
|
return NewVoyageModel(
|
|
|
|
|
map[string]string{"default": baseURL},
|
|
|
|
|
URLSuffix{Embedding: "v1/embeddings", Rerank: "v1/rerank"},
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageName(t *testing.T) {
|
2026-07-09 10:19:10 +08:00
|
|
|
if got := newVoyageForTest("http://unused").Name(); got != "Voyage AI" {
|
|
|
|
|
t.Errorf("Name()=%q, want %q", got, "Voyage AI")
|
Go: implement provider: Voyage AI (#14811)
### What problem does this PR solve?
Add a Go driver for Voyage AI (https://voyageai.com), one of the
unchecked providers on the umbrella tracking issue #14736. Voyage AI is
**embed + rerank only** — no chat, no streaming, no `/v1/models`
endpoint. It's the first provider in the Go layer of this shape.
Until this PR, a tenant who configured `voyage` as a model provider in
the Go layer fell through to the default branch of
`internal/entity/models/factory.go` and got the dummy driver.
### What this PR includes
- New `internal/entity/models/voyage.go` with a `VoyageModel`
implementing the `ModelDriver` interface.
- New `conf/models/voyage.json` with 6 embedding models (`voyage-3.5`,
`voyage-3.5-lite`, `voyage-3-large`, `voyage-code-3`, `voyage-law-2`,
`voyage-finance-2`) and 2 rerank models (`rerank-2`, `rerank-2-lite`).
- `factory.go`: route `"voyage"` to `NewVoyageModel`.
- `internal/entity/models/voyage_test.go`: 19 unit tests.
### How the driver works
- **Embed**: `POST /v1/embeddings`. Response is OpenAI-shaped (`{data:
[{embedding, index, object, text}], model, usage}`). Driver reorders by
`index`, rejects duplicate / out-of-range / missing slots, and
short-circuits empty input without an HTTP call.
- **Rerank**: `POST /v1/rerank`. Voyage uses **`top_k`** as the request
param name (not `top_n` like Aliyun/SiliconFlow); the driver translates
`RerankConfig.TopN` → `top_k`. Response is Cohere-shaped (`{data:
[{relevance_score, index}], model}`), so the existing
`RerankResponse{Data: []RerankResult{Index, RelevanceScore}}` shape fits
cleanly.
- **`ListModels`**: returns a hardcoded list of `voyageKnownModels`.
Voyage does **not** expose `/v1/models` (probed live, returns 404), so
the driver synthesizes the list from the same set the config ships. New
upstream models are added by extending one slice.
- **`CheckConnection`**: pings a 1-input embed call against
`voyage-3.5`. Without `/v1/models`, this is the cheapest way to verify
the API key + network path before a tenant tries a real workload.
- **`ChatWithMessages` / `ChatStreamlyWithSender` / `Balance` /
`TranscribeAudio` / `AudioSpeech` / `OCRFile`**: all return `"no such
method"`. Voyage does not host any of these surfaces.
No interface change. No new dependencies.
### How was this tested?
**19 unit tests** in `internal/entity/models/voyage_test.go` — all pass
on go 1.25:
```
$ go test -vet=off -run TestVoyage -count=1 ./internal/entity/models/...
ok ragflow/internal/entity/models 0.036s
```
Coverage: Name; Embed (happy path, reorder, empty-input, missing
key/model, duplicate index, out-of-range index, missing slot); Rerank
(happy path with `top_k` assertion, default-to-len-documents, empty
documents, out-of-range index); ListModels (static list, missing key);
CheckConnection (happy, 401); chat methods sentinels; Balance sentinel;
audio/OCR sentinels.
`go build ./internal/entity/models/...` exits 0.
**Live integration test** against `api.voyageai.com`:
```
=== RUN TestVoyageLiveSmoke
[OK] Name() = "voyage"
[OK] ListModels (static): 8 models -> [voyage-3.5 voyage-3.5-lite voyage-3-large voyage-code-3 voyage-law-2 voyage-finance-2 rerank-2 rerank-2-lite]
[OK] CheckConnection
[OK] Embed vectors=3 dim=1024 indices=[0 1 2]
[OK] Embed(empty) -> 0 vectors
[OK] Rerank results=3 scores=[0.8125 0.59765625 0.39453125]
[OK] ChatWithMessages returns voyage, no such method
[OK] Balance returns voyage, no such method
VOYAGE LIVE SMOKE PASSED
--- PASS: TestVoyageLiveSmoke (0.81s)
```
What the live run proves on the wire:
- Auth (`Bearer <key>`) accepted by `api.voyageai.com`.
- Embed `voyage-3.5` on 3 inputs returns 3 vectors at dim 1024 with
`index` field preserved as `[0, 1, 2]` — the reorder-by-index code is
exercised on real data.
- Empty input short-circuits without an HTTP call (mock server would
have been hit if it did).
- Rerank `rerank-2` on 3 docs returns 3 real `relevance_score` floats
`[0.8125, 0.598, 0.395]`. The `top_k` translation works on the live
wire.
- All sentinel methods return the documented `"no such method"` strings.
### Note on PR history
This branch was previously named for LocalAI Embed work which is now
consolidated into PR #14813. The branch was reset to `upstream/main` and
rebuilt for Voyage. Diff against `main` is a clean +838 lines across 4
files.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
Tracking: #14736
---------
Co-authored-by: Jin Hai <haijin.chn@gmail.com>
2026-05-13 15:46:54 -10:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 03:46:58 -05:00
|
|
|
func TestVoyageNewModelWithCustomDefaultTransport(t *testing.T) {
|
|
|
|
|
original := http.DefaultTransport
|
|
|
|
|
http.DefaultTransport = roundTripperFunc(func(*http.Request) (*http.Response, error) {
|
|
|
|
|
return nil, nil
|
|
|
|
|
})
|
|
|
|
|
t.Cleanup(func() {
|
|
|
|
|
http.DefaultTransport = original
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if model := NewVoyageModel(map[string]string{"default": "http://unused"}, URLSuffix{}); model == nil {
|
|
|
|
|
t.Fatal("NewVoyageModel returned nil")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Go: implement provider: Voyage AI (#14811)
### What problem does this PR solve?
Add a Go driver for Voyage AI (https://voyageai.com), one of the
unchecked providers on the umbrella tracking issue #14736. Voyage AI is
**embed + rerank only** — no chat, no streaming, no `/v1/models`
endpoint. It's the first provider in the Go layer of this shape.
Until this PR, a tenant who configured `voyage` as a model provider in
the Go layer fell through to the default branch of
`internal/entity/models/factory.go` and got the dummy driver.
### What this PR includes
- New `internal/entity/models/voyage.go` with a `VoyageModel`
implementing the `ModelDriver` interface.
- New `conf/models/voyage.json` with 6 embedding models (`voyage-3.5`,
`voyage-3.5-lite`, `voyage-3-large`, `voyage-code-3`, `voyage-law-2`,
`voyage-finance-2`) and 2 rerank models (`rerank-2`, `rerank-2-lite`).
- `factory.go`: route `"voyage"` to `NewVoyageModel`.
- `internal/entity/models/voyage_test.go`: 19 unit tests.
### How the driver works
- **Embed**: `POST /v1/embeddings`. Response is OpenAI-shaped (`{data:
[{embedding, index, object, text}], model, usage}`). Driver reorders by
`index`, rejects duplicate / out-of-range / missing slots, and
short-circuits empty input without an HTTP call.
- **Rerank**: `POST /v1/rerank`. Voyage uses **`top_k`** as the request
param name (not `top_n` like Aliyun/SiliconFlow); the driver translates
`RerankConfig.TopN` → `top_k`. Response is Cohere-shaped (`{data:
[{relevance_score, index}], model}`), so the existing
`RerankResponse{Data: []RerankResult{Index, RelevanceScore}}` shape fits
cleanly.
- **`ListModels`**: returns a hardcoded list of `voyageKnownModels`.
Voyage does **not** expose `/v1/models` (probed live, returns 404), so
the driver synthesizes the list from the same set the config ships. New
upstream models are added by extending one slice.
- **`CheckConnection`**: pings a 1-input embed call against
`voyage-3.5`. Without `/v1/models`, this is the cheapest way to verify
the API key + network path before a tenant tries a real workload.
- **`ChatWithMessages` / `ChatStreamlyWithSender` / `Balance` /
`TranscribeAudio` / `AudioSpeech` / `OCRFile`**: all return `"no such
method"`. Voyage does not host any of these surfaces.
No interface change. No new dependencies.
### How was this tested?
**19 unit tests** in `internal/entity/models/voyage_test.go` — all pass
on go 1.25:
```
$ go test -vet=off -run TestVoyage -count=1 ./internal/entity/models/...
ok ragflow/internal/entity/models 0.036s
```
Coverage: Name; Embed (happy path, reorder, empty-input, missing
key/model, duplicate index, out-of-range index, missing slot); Rerank
(happy path with `top_k` assertion, default-to-len-documents, empty
documents, out-of-range index); ListModels (static list, missing key);
CheckConnection (happy, 401); chat methods sentinels; Balance sentinel;
audio/OCR sentinels.
`go build ./internal/entity/models/...` exits 0.
**Live integration test** against `api.voyageai.com`:
```
=== RUN TestVoyageLiveSmoke
[OK] Name() = "voyage"
[OK] ListModels (static): 8 models -> [voyage-3.5 voyage-3.5-lite voyage-3-large voyage-code-3 voyage-law-2 voyage-finance-2 rerank-2 rerank-2-lite]
[OK] CheckConnection
[OK] Embed vectors=3 dim=1024 indices=[0 1 2]
[OK] Embed(empty) -> 0 vectors
[OK] Rerank results=3 scores=[0.8125 0.59765625 0.39453125]
[OK] ChatWithMessages returns voyage, no such method
[OK] Balance returns voyage, no such method
VOYAGE LIVE SMOKE PASSED
--- PASS: TestVoyageLiveSmoke (0.81s)
```
What the live run proves on the wire:
- Auth (`Bearer <key>`) accepted by `api.voyageai.com`.
- Embed `voyage-3.5` on 3 inputs returns 3 vectors at dim 1024 with
`index` field preserved as `[0, 1, 2]` — the reorder-by-index code is
exercised on real data.
- Empty input short-circuits without an HTTP call (mock server would
have been hit if it did).
- Rerank `rerank-2` on 3 docs returns 3 real `relevance_score` floats
`[0.8125, 0.598, 0.395]`. The `top_k` translation works on the live
wire.
- All sentinel methods return the documented `"no such method"` strings.
### Note on PR history
This branch was previously named for LocalAI Embed work which is now
consolidated into PR #14813. The branch was reset to `upstream/main` and
rebuilt for Voyage. Diff against `main` is a clean +838 lines across 4
files.
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
Tracking: #14736
---------
Co-authored-by: Jin Hai <haijin.chn@gmail.com>
2026-05-13 15:46:54 -10:00
|
|
|
func TestVoyageEmbedHappyPath(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/embeddings", func(t *testing.T, body map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
if body["model"] != "voyage-3.5" {
|
|
|
|
|
t.Errorf("model=%v", body["model"])
|
|
|
|
|
}
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"object": "list",
|
|
|
|
|
"data": []map[string]interface{}{
|
|
|
|
|
{"object": "embedding", "embedding": []float64{0.1, 0.2}, "index": 0},
|
|
|
|
|
{"object": "embedding", "embedding": []float64{0.3, 0.4}, "index": 1},
|
|
|
|
|
{"object": "embedding", "embedding": []float64{0.5, 0.6}, "index": 2},
|
|
|
|
|
},
|
|
|
|
|
"model": "voyage-3.5",
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
vecs, err := v.Embed(&model, []string{"a", "b", "c"}, &APIConfig{ApiKey: &apiKey}, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Embed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(vecs) != 3 {
|
|
|
|
|
t.Fatalf("len=%d want 3", len(vecs))
|
|
|
|
|
}
|
|
|
|
|
if vecs[1].Embedding[0] != 0.3 || vecs[1].Index != 1 {
|
|
|
|
|
t.Errorf("vecs[1]=%+v", vecs[1])
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestVoyageEmbedPropagatesOutputDimension pins the docs-spelled
|
|
|
|
|
// param name. Voyage 400s on any other key (live-verified — sending
|
|
|
|
|
// "dimensions" returns "Argument 'dimensions' is not supported by our
|
|
|
|
|
// API"), so this name matters and must not regress.
|
|
|
|
|
func TestVoyageEmbedPropagatesOutputDimension(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/embeddings", func(t *testing.T, body map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
if got, ok := body["output_dimension"].(float64); !ok || got != 256 {
|
|
|
|
|
t.Errorf("output_dimension=%v want 256", body["output_dimension"])
|
|
|
|
|
}
|
|
|
|
|
for _, wrong := range []string{"dimensions", "output_dimensions", "dimension"} {
|
|
|
|
|
if _, present := body[wrong]; present {
|
|
|
|
|
t.Errorf("must not send %q (Voyage rejects unknown fields)", wrong)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{{"embedding": []float64{0.1}, "index": 0}},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
_, err := v.Embed(&model, []string{"x"}, &APIConfig{ApiKey: &apiKey},
|
|
|
|
|
&EmbeddingConfig{Dimension: 256})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Embed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// And when Dimension is zero/unset, the field MUST be absent — Voyage
|
|
|
|
|
// would default the vector length, but only if we don't send the key
|
|
|
|
|
// at all (sending output_dimension: 0 is a 400).
|
|
|
|
|
func TestVoyageEmbedOmitsOutputDimensionWhenUnset(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/embeddings", func(t *testing.T, body map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
if _, present := body["output_dimension"]; present {
|
|
|
|
|
t.Errorf("output_dimension must be absent when Dimension is unset, got %v", body["output_dimension"])
|
|
|
|
|
}
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{{"embedding": []float64{0.1}, "index": 0}},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
_, err := v.Embed(&model, []string{"x"}, &APIConfig{ApiKey: &apiKey}, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Embed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageEmbedReordersByIndex(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/embeddings", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{
|
|
|
|
|
{"embedding": []float64{2}, "index": 2},
|
|
|
|
|
{"embedding": []float64{0}, "index": 0},
|
|
|
|
|
{"embedding": []float64{1}, "index": 1},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
vecs, err := v.Embed(&model, []string{"a", "b", "c"}, &APIConfig{ApiKey: &apiKey}, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Embed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
for i, vec := range vecs {
|
|
|
|
|
if vec.Index != i || vec.Embedding[0] != float64(i) {
|
|
|
|
|
t.Errorf("slot %d=%+v", i, vec)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageEmbedEmptyInputShortCircuits(t *testing.T) {
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
|
|
|
|
|
t.Error("Embed([]) made an unexpected HTTP call")
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
vecs, err := v.Embed(&model, []string{}, &APIConfig{ApiKey: &apiKey}, nil)
|
|
|
|
|
if err != nil || len(vecs) != 0 {
|
|
|
|
|
t.Errorf("Embed([])=(%v,%v)", vecs, err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageEmbedRequiresAPIKey(t *testing.T) {
|
|
|
|
|
v := newVoyageForTest("http://unused")
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
_, err := v.Embed(&model, []string{"a"}, &APIConfig{}, nil)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "api key is required") {
|
|
|
|
|
t.Errorf("expected api-key error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageEmbedRequiresModelName(t *testing.T) {
|
|
|
|
|
v := newVoyageForTest("http://unused")
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
_, err := v.Embed(nil, []string{"a"}, &APIConfig{ApiKey: &apiKey}, nil)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "model name is required") {
|
|
|
|
|
t.Errorf("expected model-name error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageEmbedRejectsDuplicateIndex(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/embeddings", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{
|
|
|
|
|
{"embedding": []float64{1}, "index": 0},
|
|
|
|
|
{"embedding": []float64{2}, "index": 0},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
_, err := v.Embed(&model, []string{"a", "b"}, &APIConfig{ApiKey: &apiKey}, nil)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "duplicate embedding index 0") {
|
|
|
|
|
t.Errorf("expected duplicate error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageEmbedRejectsOutOfRangeIndex(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/embeddings", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{
|
|
|
|
|
{"embedding": []float64{1}, "index": 7},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
_, err := v.Embed(&model, []string{"a", "b"}, &APIConfig{ApiKey: &apiKey}, nil)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "out of range") {
|
|
|
|
|
t.Errorf("expected out-of-range error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageEmbedRejectsMissingSlot(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/embeddings", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{
|
|
|
|
|
{"embedding": []float64{1}, "index": 0},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
_, err := v.Embed(&model, []string{"a", "b"}, &APIConfig{ApiKey: &apiKey}, nil)
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "missing embedding for input index 1") {
|
|
|
|
|
t.Errorf("expected missing-slot error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageRerankHappyPath(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/rerank", func(t *testing.T, body map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
// Voyage's request key is top_k (not top_n).
|
|
|
|
|
if body["top_k"] != float64(3) {
|
|
|
|
|
t.Errorf("top_k=%v want 3", body["top_k"])
|
|
|
|
|
}
|
|
|
|
|
if body["query"] != "x" {
|
|
|
|
|
t.Errorf("query=%v", body["query"])
|
|
|
|
|
}
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"object": "list",
|
|
|
|
|
"data": []map[string]interface{}{
|
|
|
|
|
{"relevance_score": 0.8, "index": 2},
|
|
|
|
|
{"relevance_score": 0.5, "index": 0},
|
|
|
|
|
{"relevance_score": 0.3, "index": 1},
|
|
|
|
|
},
|
|
|
|
|
"model": "rerank-2",
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "rerank-2"
|
|
|
|
|
resp, err := v.Rerank(&model, "x", []string{"a", "b", "c"},
|
|
|
|
|
&APIConfig{ApiKey: &apiKey}, &RerankConfig{TopN: 3})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Rerank: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(resp.Data) != 3 {
|
|
|
|
|
t.Fatalf("len=%d want 3", len(resp.Data))
|
|
|
|
|
}
|
|
|
|
|
want := map[int]float64{0: 0.5, 1: 0.3, 2: 0.8}
|
|
|
|
|
for _, r := range resp.Data {
|
|
|
|
|
if got, ok := want[r.Index]; !ok || got != r.RelevanceScore {
|
|
|
|
|
t.Errorf("unexpected result index=%d score=%v", r.Index, r.RelevanceScore)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageRerankTopKDefaultsToLenDocuments(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/rerank", func(t *testing.T, body map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
if body["top_k"] != float64(4) {
|
|
|
|
|
t.Errorf("top_k=%v want 4 (len(documents))", body["top_k"])
|
|
|
|
|
}
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{"data": []map[string]interface{}{}})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "rerank-2"
|
|
|
|
|
_, err := v.Rerank(&model, "x", []string{"a", "b", "c", "d"},
|
|
|
|
|
&APIConfig{ApiKey: &apiKey}, &RerankConfig{})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Rerank: %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageRerankEmptyDocuments(t *testing.T) {
|
|
|
|
|
v := newVoyageForTest("http://unused")
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "rerank-2"
|
|
|
|
|
resp, err := v.Rerank(&model, "x", nil,
|
|
|
|
|
&APIConfig{ApiKey: &apiKey}, &RerankConfig{TopN: 0})
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("Rerank: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if len(resp.Data) != 0 {
|
|
|
|
|
t.Errorf("expected empty Data, got %d", len(resp.Data))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageRerankRejectsOutOfRangeIndex(t *testing.T) {
|
|
|
|
|
srv := newVoyageServer(t, "/v1/rerank", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{
|
|
|
|
|
{"relevance_score": 0.9, "index": 7},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "rerank-2"
|
|
|
|
|
_, err := v.Rerank(&model, "x", []string{"a", "b"},
|
|
|
|
|
&APIConfig{ApiKey: &apiKey}, &RerankConfig{TopN: 2})
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "out of range") {
|
|
|
|
|
t.Errorf("expected out-of-range error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestVoyageRerankRejectsDuplicateIndex(t *testing.T) {
|
|
|
|
|
// A duplicate index would silently overwrite an earlier slot, which
|
|
|
|
|
// is the same failure mode Embed already guards against. Make sure
|
|
|
|
|
// Rerank fails loudly too.
|
|
|
|
|
srv := newVoyageServer(t, "/v1/rerank", func(t *testing.T, _ map[string]interface{}, w http.ResponseWriter) {
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{
|
|
|
|
|
{"relevance_score": 0.9, "index": 0},
|
|
|
|
|
{"relevance_score": 0.8, "index": 0},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := newVoyageForTest(srv.URL)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "rerank-2"
|
|
|
|
|
_, err := v.Rerank(&model, "x", []string{"a", "b"},
|
|
|
|
|
&APIConfig{ApiKey: &apiKey}, &RerankConfig{TopN: 2})
|
|
|
|
|
if err == nil || !strings.Contains(err.Error(), "duplicate rerank index 0") {
|
|
|
|
|
t.Errorf("expected duplicate-index error, got %v", err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TestVoyageEmbedTrimsTrailingSlashInBaseURL guards against a
|
|
|
|
|
// misconfigured baseURL ending in "/" producing a double-slash path
|
|
|
|
|
// (e.g. `.../v1//embeddings`). Rerank already trims, so Embed must
|
|
|
|
|
// trim too; CodeRabbit flagged the inconsistency.
|
|
|
|
|
func TestVoyageEmbedTrimsTrailingSlashInBaseURL(t *testing.T) {
|
|
|
|
|
var sawPath string
|
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
sawPath = r.URL.Path
|
|
|
|
|
_ = json.NewEncoder(w).Encode(map[string]interface{}{
|
|
|
|
|
"data": []map[string]interface{}{{"embedding": []float64{1}, "index": 0}},
|
|
|
|
|
})
|
|
|
|
|
}))
|
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
|
|
v := NewVoyageModel(
|
|
|
|
|
map[string]string{"default": srv.URL + "/"}, // trailing slash
|
|
|
|
|
URLSuffix{Embedding: "v1/embeddings", Rerank: "v1/rerank"},
|
|
|
|
|
)
|
|
|
|
|
apiKey := "test-key"
|
|
|
|
|
model := "voyage-3.5"
|
|
|
|
|
if _, err := v.Embed(&model, []string{"x"}, &APIConfig{ApiKey: &apiKey}, nil); err != nil {
|
|
|
|
|
t.Fatalf("Embed: %v", err)
|
|
|
|
|
}
|
|
|
|
|
if sawPath != "/v1/embeddings" {
|
|
|
|
|
t.Errorf("path=%q want %q (no double slash)", sawPath, "/v1/embeddings")
|
|
|
|
|
}
|
|
|
|
|
}
|