diff --git a/conf/models/lmstudio.json b/conf/models/lmstudio.json index a22cbb982f..a5293ffb9d 100644 --- a/conf/models/lmstudio.json +++ b/conf/models/lmstudio.json @@ -2,7 +2,8 @@ "name": "lmstudio", "url_suffix": { "chat": "chat/completions", - "models": "models" + "models": "models", + "embedding": "embeddings" }, "class": "local" } \ No newline at end of file diff --git a/internal/entity/models/lmstudio.go b/internal/entity/models/lmstudio.go index 89a40e4685..ba55cf7247 100644 --- a/internal/entity/models/lmstudio.go +++ b/internal/entity/models/lmstudio.go @@ -3,6 +3,7 @@ package models import ( "bufio" "bytes" + "context" "encoding/json" "fmt" "io" @@ -361,8 +362,113 @@ func (l *LmStudioModel) ChatStreamlyWithSender(modelName string, messages []Mess return scanner.Err() } +type lmstudioEmbeddingResponse struct { + Data []struct { + Index int `json:"index"` + Embedding []interface{} `json:"embedding"` + } `json:"data"` +} + func (l *LmStudioModel) Encode(modelName *string, texts []string, apiConfig *APIConfig, embeddingConfig *EmbeddingConfig) ([][]float64, error) { - return nil, fmt.Errorf("no such method") + if len(texts) == 0 { + return [][]float64{}, nil + } + + if modelName == nil || *modelName == "" { + return nil, fmt.Errorf("model name is required") + } + + region := "default" + if apiConfig != nil && apiConfig.Region != nil && *apiConfig.Region != "" { + region = *apiConfig.Region + } + + baseURL := l.BaseURL[region] + if baseURL == "" { + baseURL = l.BaseURL["default"] + } + if baseURL == "" { + return nil, fmt.Errorf("missing base URL: please configure the local access address for LM Studio (e.g., http://127.0.0.1:1234/v1)") + } + + url := fmt.Sprintf("%s/%s", strings.TrimSuffix(baseURL, "/"), l.URLSuffix.Embedding) + + reqBody := map[string]interface{}{ + "model": *modelName, + "input": texts, + } + if embeddingConfig != nil && embeddingConfig.Dimension > 0 { + reqBody["dimensions"] = embeddingConfig.Dimension + } + + jsonData, err := json.Marshal(reqBody) + if err != nil { + return nil, fmt.Errorf("failed to marshal request: %w", err) + } + + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData)) + if err != nil { + return nil, fmt.Errorf("failed to create request: %w", err) + } + + req.Header.Set("Content-Type", "application/json") + if apiConfig != nil && apiConfig.ApiKey != nil && *apiConfig.ApiKey != "" { + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *apiConfig.ApiKey)) + } + + resp, err := l.httpClient.Do(req) + if err != nil { + return nil, fmt.Errorf("failed to send request: %w", err) + } + defer resp.Body.Close() + + body, err := io.ReadAll(resp.Body) + if err != nil { + return nil, fmt.Errorf("failed to read response: %w", err) + } + + if resp.StatusCode != http.StatusOK { + return nil, fmt.Errorf("LM Studio embeddings API error: %s, body: %s", resp.Status, string(body)) + } + + var parsed lmstudioEmbeddingResponse + if err = json.Unmarshal(body, &parsed); err != nil { + return nil, fmt.Errorf("failed to parse response: %w", err) + } + + if len(parsed.Data) != len(texts) { + return nil, fmt.Errorf("lmstudio embeddings: expected %d results, got %d", len(texts), len(parsed.Data)) + } + + embeddings := make([][]float64, len(texts)) + for _, item := range parsed.Data { + if item.Index < 0 || item.Index >= len(texts) { + return nil, fmt.Errorf("unexpected embedding index %d for %d inputs", item.Index, len(texts)) + } + vec := make([]float64, len(item.Embedding)) + for j, v := range item.Embedding { + switch val := v.(type) { + case float64: + vec[j] = val + case float32: + vec[j] = float64(val) + default: + return nil, fmt.Errorf("unexpected embedding value type at item %d index %d", item.Index, j) + } + } + embeddings[item.Index] = vec + } + + for i, vec := range embeddings { + if vec == nil { + return nil, fmt.Errorf("missing embedding for input at index %d", i) + } + } + + return embeddings, nil } func (l *LmStudioModel) Rerank(modelName *string, query string, documents []string, apiConfig *APIConfig, rerankConfig *RerankConfig) (*RerankResponse, error) {