rony-llm-agent/pkg/rag/embeddings/ollama.go

87 lines
1.9 KiB
Go
Raw Normal View History

package embeddings
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
// Config holds the settings for the Ollama embedder.
type Config struct {
BaseURL string // e.g. "http://localhost:11434"
Model string // e.g. "nomic-embed-text"
}
// Ollama implements Embedder using Ollama's embedding API.
type Ollama struct {
baseURL string
model string
http *http.Client
}
// NewOllama creates a new Ollama embedder.
func NewOllama(cfg Config) (*Ollama, error) {
baseURL := cfg.BaseURL
if baseURL == "" {
baseURL = "http://localhost:11434"
}
model := cfg.Model
if model == "" {
model = "nomic-embed-text"
}
return &Ollama{
baseURL: baseURL,
model: model,
http: http.DefaultClient,
}, nil
}
func (e *Ollama) Embed(ctx context.Context, text string) ([]float32, error) {
endpoint := e.baseURL + "/api/embed"
reqBody, err := json.Marshal(map[string]interface{}{
"model": e.model,
"input": text,
})
if err != nil {
return nil, fmt.Errorf("marshaling request: %w", err)
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, strings.NewReader(string(reqBody)))
if err != nil {
return nil, fmt.Errorf("creating request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
resp, err := e.http.Do(httpReq)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
body, _ := io.ReadAll(resp.Body)
return nil, fmt.Errorf("API error %d: %s", resp.StatusCode, string(body))
}
var apiResp ollamaEmbedResponse
if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil {
return nil, fmt.Errorf("decoding response: %w", err)
}
return apiResp.Embedding, nil
}
func (e *Ollama) Dimensions() int {
// Default for nomic-embed-text
return 768
}
type ollamaEmbedResponse struct {
Embedding []float32 `json:"embedding"`
}