Skip to content

Commit 0406889

Browse files
authored
Merge pull request #115 from soundprediction/migrate-embed
Migrate from /api/embedding to /api/embed
2 parents 5e7fb4b + 2fb4f3a commit 0406889

2 files changed

Lines changed: 14 additions & 13 deletions

File tree

embed_ollama.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
const defaultBaseURLOllama = "http://localhost:11434/api"
1515

1616
type ollamaResponse struct {
17-
Embedding []float32 `json:"embedding"`
17+
Embeddings [][]float32 `json:"embeddings"`
1818
}
1919

2020
// NewEmbeddingFuncOllama returns a function that creates embeddings for a text
@@ -39,16 +39,17 @@ func NewEmbeddingFuncOllama(model string, baseURLOllama string) EmbeddingFunc {
3939
return func(ctx context.Context, text string) ([]float32, error) {
4040
// Prepare the request body.
4141
reqBody, err := json.Marshal(map[string]string{
42-
"model": model,
43-
"prompt": text,
42+
"model": model,
43+
"input": text,
4444
})
45+
4546
if err != nil {
4647
return nil, fmt.Errorf("couldn't marshal request body: %w", err)
4748
}
4849

4950
// Create the request. Creating it with context is important for a timeout
5051
// to be possible, because the client is configured without a timeout.
51-
req, err := http.NewRequestWithContext(ctx, "POST", baseURLOllama+"/embeddings", bytes.NewBuffer(reqBody))
52+
req, err := http.NewRequestWithContext(ctx, "POST", baseURLOllama+"/embed", bytes.NewBuffer(reqBody))
5253
if err != nil {
5354
return nil, fmt.Errorf("couldn't create request: %w", err)
5455
}
@@ -78,11 +79,11 @@ func NewEmbeddingFuncOllama(model string, baseURLOllama string) EmbeddingFunc {
7879
}
7980

8081
// Check if the response contains embeddings.
81-
if len(embeddingResponse.Embedding) == 0 {
82+
if len(embeddingResponse.Embeddings) == 0 {
8283
return nil, errors.New("no embeddings found in the response")
8384
}
8485

85-
v := embeddingResponse.Embedding
86+
v := embeddingResponse.Embeddings[0]
8687
checkNormalized.Do(func() {
8788
if isNormalized(v) {
8889
checkedNormalized = true

embed_ollama_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ func TestNewEmbeddingFuncOllama(t *testing.T) {
1919
prompt := "hello world"
2020

2121
wantBody, err := json.Marshal(map[string]string{
22-
"model": model,
23-
"prompt": prompt,
22+
"model": model,
23+
"input": prompt,
2424
})
2525
if err != nil {
2626
t.Fatal("unexpected error:", err)
2727
}
28-
wantRes := []float32{-0.40824828, 0.40824828, 0.81649655} // normalized version of `{-0.1, 0.1, 0.2}`
28+
wantRes := [][]float32{{-0.40824828, 0.40824828, 0.81649655}} // normalized version of `{-0.1, 0.1, 0.2}`
2929

3030
// Mock server
3131
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
3232
// Check URL
33-
if !strings.HasSuffix(r.URL.Path, baseURLSuffix+"/embeddings") {
34-
t.Fatal("expected URL", baseURLSuffix+"/embeddings", "got", r.URL.Path)
33+
if !strings.HasSuffix(r.URL.Path, baseURLSuffix+"/embed") {
34+
t.Fatal("expected URL", baseURLSuffix+"/embed", "got", r.URL.Path)
3535
}
3636
// Check method
3737
if r.Method != "POST" {
@@ -52,7 +52,7 @@ func TestNewEmbeddingFuncOllama(t *testing.T) {
5252

5353
// Write response
5454
resp := ollamaResponse{
55-
Embedding: wantRes,
55+
Embeddings: wantRes,
5656
}
5757
w.WriteHeader(http.StatusOK)
5858
_ = json.NewEncoder(w).Encode(resp)
@@ -70,7 +70,7 @@ func TestNewEmbeddingFuncOllama(t *testing.T) {
7070
if err != nil {
7171
t.Fatal("expected nil, got", err)
7272
}
73-
if slices.Compare(wantRes, res) != 0 {
73+
if slices.Compare(wantRes[0], res) != 0 {
7474
t.Fatal("expected res", wantRes, "got", res)
7575
}
7676
}

0 commit comments

Comments
 (0)