|
@@ -32,7 +32,7 @@ type LlamaServer interface {
|
|
Ping(ctx context.Context) error
|
|
Ping(ctx context.Context) error
|
|
WaitUntilRunning(ctx context.Context) error
|
|
WaitUntilRunning(ctx context.Context) error
|
|
Completion(ctx context.Context, req CompletionRequest, fn func(CompletionResponse)) error
|
|
Completion(ctx context.Context, req CompletionRequest, fn func(CompletionResponse)) error
|
|
- Embedding(ctx context.Context, prompt string) ([]float64, error)
|
|
|
|
|
|
+ Embeddings(ctx context.Context, prompt []string) ([][]float64, error)
|
|
Tokenize(ctx context.Context, content string) ([]int, error)
|
|
Tokenize(ctx context.Context, content string) ([]int, error)
|
|
Detokenize(ctx context.Context, tokens []int) (string, error)
|
|
Detokenize(ctx context.Context, tokens []int) (string, error)
|
|
Close() error
|
|
Close() error
|
|
@@ -736,15 +736,15 @@ func (s *llmServer) Completion(ctx context.Context, req CompletionRequest, fn fu
|
|
return fmt.Errorf("max retries exceeded")
|
|
return fmt.Errorf("max retries exceeded")
|
|
}
|
|
}
|
|
|
|
|
|
-type EmbeddingRequest struct {
|
|
|
|
- Content string `json:"content"`
|
|
|
|
|
|
+type EmbeddingsRequest struct {
|
|
|
|
+ Contents []string `json:"contents"`
|
|
}
|
|
}
|
|
|
|
|
|
-type EmbeddingResponse struct {
|
|
|
|
- Embedding []float64 `json:"embedding"`
|
|
|
|
|
|
+type EmbeddingsResponse struct {
|
|
|
|
+ Embeddings [][]float64 `json:"embeddings"`
|
|
}
|
|
}
|
|
|
|
|
|
-func (s *llmServer) Embedding(ctx context.Context, prompt string) ([]float64, error) {
|
|
|
|
|
|
+func (s *llmServer) Embeddings(ctx context.Context, prompts []string) ([][]float64, error) {
|
|
if err := s.sem.Acquire(ctx, 1); err != nil {
|
|
if err := s.sem.Acquire(ctx, 1); err != nil {
|
|
slog.Error("Failed to acquire semaphore", "error", err)
|
|
slog.Error("Failed to acquire semaphore", "error", err)
|
|
return nil, err
|
|
return nil, err
|
|
@@ -758,12 +758,12 @@ func (s *llmServer) Embedding(ctx context.Context, prompt string) ([]float64, er
|
|
return nil, fmt.Errorf("unexpected server status: %s", status.ToString())
|
|
return nil, fmt.Errorf("unexpected server status: %s", status.ToString())
|
|
}
|
|
}
|
|
|
|
|
|
- data, err := json.Marshal(TokenizeRequest{Content: prompt})
|
|
|
|
|
|
+ data, err := json.Marshal(EmbeddingsRequest{Contents: prompts})
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error marshaling embed data: %w", err)
|
|
return nil, fmt.Errorf("error marshaling embed data: %w", err)
|
|
}
|
|
}
|
|
|
|
|
|
- req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("http://127.0.0.1:%d/embedding", s.port), bytes.NewBuffer(data))
|
|
|
|
|
|
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, fmt.Sprintf("http://127.0.0.1:%d/embeddings", s.port), bytes.NewBuffer(data))
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error creating embed request: %w", err)
|
|
return nil, fmt.Errorf("error creating embed request: %w", err)
|
|
}
|
|
}
|
|
@@ -780,17 +780,19 @@ func (s *llmServer) Embedding(ctx context.Context, prompt string) ([]float64, er
|
|
return nil, fmt.Errorf("error reading embed response: %w", err)
|
|
return nil, fmt.Errorf("error reading embed response: %w", err)
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ fmt.Println("embeddings response", string(body))
|
|
|
|
+
|
|
if resp.StatusCode >= 400 {
|
|
if resp.StatusCode >= 400 {
|
|
log.Printf("llm encode error: %s", body)
|
|
log.Printf("llm encode error: %s", body)
|
|
return nil, fmt.Errorf("%s", body)
|
|
return nil, fmt.Errorf("%s", body)
|
|
}
|
|
}
|
|
|
|
|
|
- var embedding EmbeddingResponse
|
|
|
|
|
|
+ var embedding EmbeddingsResponse
|
|
if err := json.Unmarshal(body, &embedding); err != nil {
|
|
if err := json.Unmarshal(body, &embedding); err != nil {
|
|
return nil, fmt.Errorf("unmarshal tokenize response: %w", err)
|
|
return nil, fmt.Errorf("unmarshal tokenize response: %w", err)
|
|
}
|
|
}
|
|
|
|
|
|
- return embedding.Embedding, nil
|
|
|
|
|
|
+ return embedding.Embeddings, nil
|
|
}
|
|
}
|
|
|
|
|
|
type TokenizeRequest struct {
|
|
type TokenizeRequest struct {
|