Skip to content

Commit

Permalink
fix ollama embed error
Browse files Browse the repository at this point in the history
  • Loading branch information
henomis committed Nov 2, 2024
1 parent 458255a commit d9648bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
9 changes: 8 additions & 1 deletion embedder/ollama/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (r *request) ContentType() string {
type response struct {
HTTPStatusCode int `json:"-"`
acceptContentType string `json:"-"`
RawBody []byte `json:"-"`
Embedding []float64 `json:"embedding"`
CreatedAt string `json:"created_at"`
}
Expand All @@ -46,7 +47,13 @@ func (r *response) Decode(body io.Reader) error {
return json.NewDecoder(body).Decode(r)
}

func (r *response) SetBody(_ io.Reader) error {
func (r *response) SetBody(body io.Reader) error {
rawBody, err := io.ReadAll(body)
if err != nil {
return err
}

r.RawBody = rawBody
return nil
}

Expand Down
17 changes: 17 additions & 0 deletions embedder/ollama/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package ollamaembedder

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/henomis/restclientgo"

Expand All @@ -14,6 +17,14 @@ const (
defaultEndpoint = "http://localhost:11434/api"
)

type OllamaEmbedError struct {
Err error
}

func (e *OllamaEmbedError) Error() string {
return fmt.Sprintf("Error embedding text: %v", e.Err)
}

type Embedder struct {
model string
restClient *restclientgo.RestClient
Expand Down Expand Up @@ -88,5 +99,11 @@ func (e *Embedder) embed(ctx context.Context, text string) (embedder.Embedding,
return nil, err
}

if resp.HTTPStatusCode >= http.StatusBadRequest {
return nil, &OllamaEmbedError{
Err: errors.New(string(resp.RawBody)),
}
}

return resp.Embedding, nil
}

0 comments on commit d9648bb

Please sign in to comment.