Compare commits
6 Commits
4ada379be9
...
edc9d3d667
| Author | SHA1 | Date |
|---|---|---|
|
|
edc9d3d667 | |
|
|
c63890b104 | |
|
|
46a4374e69 | |
|
|
2bd7333233 | |
|
|
77c0396623 | |
|
|
a0f477c9a8 |
59
Makefile
59
Makefile
|
|
@ -1,6 +1,6 @@
|
|||
# Makefile for running the Vet Clinic Chat Assistant locally with Ollama
|
||||
|
||||
.PHONY: run ollama-start ollama-stop ollama-pull ollama-status
|
||||
.PHONY: run ollama-start ollama-stop ollama-pull ollama-status curl-embed curl-translate curl-chat
|
||||
|
||||
# Start Ollama server (if not already running)
|
||||
ollama-start:
|
||||
|
|
@ -20,6 +20,15 @@ ollama-pull:
|
|||
ollama-status:
|
||||
ollama list
|
||||
|
||||
# Ollama host & models (override as needed)
|
||||
OLLAMA_HOST ?= http://localhost:11434
|
||||
# Primary chat / reasoning model (already using OPENAI_MODEL var for compatibility)
|
||||
OPENAI_MODEL ?= qwen3:latest
|
||||
# Optional separate embedding model
|
||||
OLLAMA_EMBED_MODEL ?= all-minilm
|
||||
# Translation prompt (mirrors config.yaml translate_prompt). Can override: make curl-translate PROMPT="..." TRANSLATE_PROMPT="..."
|
||||
TRANSLATE_PROMPT ?= Translate the following veterinary-related sentence to English. Input: '$(PROMPT)'. Return ONLY the English translation, no extra text, no markdown, no quotes. If already English, return as is.
|
||||
|
||||
# Database configuration (override via: make run DB_PASSWORD=secret DB_NAME=other)
|
||||
DB_HOST ?= localhost
|
||||
DB_PORT ?= 5432
|
||||
|
|
@ -48,7 +57,53 @@ print-dsn:
|
|||
@echo postgres://$(DB_USER):******@$(DB_HOST):$(DB_PORT)/$(DB_NAME)?sslmode=$(DB_SSLMODE)
|
||||
|
||||
# Run tests
|
||||
.PHONY: test
|
||||
.PHONY: test test-verbose test-race test-coverage test-coverage-html
|
||||
|
||||
# Run standard tests
|
||||
test:
|
||||
go test ./...
|
||||
|
||||
# Run tests with verbose output
|
||||
test-verbose:
|
||||
go test -v ./...
|
||||
|
||||
# Run tests with race detection
|
||||
test-race:
|
||||
go test -race ./...
|
||||
|
||||
# Run tests with coverage reporting
|
||||
test-coverage:
|
||||
go test -coverprofile=coverage.out ./...
|
||||
go tool cover -func=coverage.out
|
||||
|
||||
# Run tests with HTML coverage report
|
||||
test-coverage-html: test-coverage
|
||||
go tool cover -html=coverage.out
|
||||
|
||||
# --- Utility curl targets ---
|
||||
# Example: make curl-embed PROMPT="warm up"
|
||||
curl-embed:
|
||||
@test -n "$(PROMPT)" || { echo "Usage: make curl-embed PROMPT='text' [OLLAMA_EMBED_MODEL=model]"; exit 1; }
|
||||
@echo "[curl-embed] model=$(OLLAMA_EMBED_MODEL) prompt='$(PROMPT)'"
|
||||
@curl -sS -X POST "$(OLLAMA_HOST)/api/embeddings" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"model":"$(OLLAMA_EMBED_MODEL)","prompt":"$(PROMPT)"}' | jq . || true
|
||||
|
||||
# Example: make curl-translate PROMPT="A kutyám nem eszik"
|
||||
curl-translate:
|
||||
@test -n "$(PROMPT)" || { echo "Usage: make curl-translate PROMPT='sentence to translate'"; exit 1; }
|
||||
@echo "[curl-translate] model=$(OPENAI_MODEL)"; \
|
||||
PROMPT_JSON=$$(printf '%s' "$(TRANSLATE_PROMPT)" | jq -Rs .); \
|
||||
curl -sS -X POST "$(OLLAMA_HOST)/api/chat" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"model":"$(OPENAI_MODEL)","messages":[{"role":"user","content":'$$PROMPT_JSON'}],"stream":false}' | jq -r '.message.content' || true
|
||||
|
||||
# Generic chat invocation (raw user PROMPT)
|
||||
# Example: make curl-chat PROMPT="List 3 dog breeds"
|
||||
curl-chat:
|
||||
@test -n "$(PROMPT)" || { echo "Usage: make curl-chat PROMPT='your message'"; exit 1; }
|
||||
@echo "[curl-chat] model=$(OPENAI_MODEL)"; \
|
||||
PROMPT_JSON=$$(printf '%s' "$(PROMPT)" | jq -Rs .); \
|
||||
curl -sS -X POST "$(OLLAMA_HOST)/api/chat" \
|
||||
-H 'Content-Type: application/json' \
|
||||
-d '{"model":"$(OPENAI_MODEL)","messages":[{"role":"user","content":'$$PROMPT_JSON'}],"stream":false}' | jq -r '.message.content' || true
|
||||
|
|
|
|||
|
|
@ -83,20 +83,9 @@ func (cs *ChatService) findBestVisit(ctx context.Context, req ChatRequest, keywo
|
|||
bestID := ""
|
||||
rawDis := ""
|
||||
if len(candidates) > 0 {
|
||||
if real, ok := cs.LLM.(*LLMClient); ok {
|
||||
raw, vr, derr := real.DisambiguateBestMatchRaw(ctx, req.Message, candidates)
|
||||
rawDis = raw
|
||||
bestID = vr
|
||||
if derr != nil {
|
||||
cs.logBestID(bestID, derr)
|
||||
} else {
|
||||
cs.logBestID(bestID, nil)
|
||||
}
|
||||
} else {
|
||||
bestID, err = cs.LLM.DisambiguateBestMatch(ctx, req.Message, candidates)
|
||||
cs.logBestID(bestID, err)
|
||||
}
|
||||
}
|
||||
visit, err := cs.visitsDB.FindById(bestID)
|
||||
if err != nil {
|
||||
return nil, rawDis, fmt.Errorf("FindById: %w", err)
|
||||
|
|
@ -236,3 +225,8 @@ func (cs *ChatService) persistInteraction(ctx context.Context, correlationID str
|
|||
logrus.WithError(err).Debug("failed to save chat interaction")
|
||||
}
|
||||
}
|
||||
|
||||
// Add this at the top-level (outside any function)
|
||||
type correlationIDCtxKeyType struct{}
|
||||
|
||||
var correlationIDCtxKey = correlationIDCtxKeyType{}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,8 @@ type mockLLM struct {
|
|||
disambigID string
|
||||
keywordsErr error
|
||||
disambigErr error
|
||||
embeddings []float64
|
||||
embeddingErr error
|
||||
}
|
||||
|
||||
var _ LLMClientAPI = (*mockLLM)(nil)
|
||||
|
|
@ -27,6 +29,12 @@ func (m *mockLLM) ExtractKeywords(ctx context.Context, msg string) (map[string]i
|
|||
func (m *mockLLM) DisambiguateBestMatch(ctx context.Context, msg string, candidates []Visit) (string, error) {
|
||||
return m.disambigID, m.disambigErr
|
||||
}
|
||||
func (m *mockLLM) GetEmbeddings(ctx context.Context, input string) ([]float64, error) {
|
||||
return m.embeddings, m.embeddingErr
|
||||
}
|
||||
func (m *mockLLM) TranslateToEnglish(ctx context.Context, msg string) (string, error) {
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// --- Test VisitDB ---
|
||||
type testVisitDB struct {
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ type Config struct {
|
|||
LLM struct {
|
||||
ExtractKeywordsPrompt string `yaml:"extract_keywords_prompt"`
|
||||
DisambiguatePrompt string `yaml:"disambiguate_prompt"`
|
||||
TranslatePrompt string `yaml:"translate_prompt"`
|
||||
} `yaml:"llm"`
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
llm:
|
||||
extract_keywords_prompt: "You will extract structured data from the user input. Input text: {{.Message}}. Return ONLY valid minified JSON object with keys: translate (English translation of input), keyword (array of 3-5 concise English veterinary-related keywords derived strictly from the input), animal (animal mentioned or 'unknown'). Example: {\"translate\":\"dog has diarrhea\",\"keyword\":[\"diarrhea\",\"digestive\"],\"animal\":\"dog\"}. Do not add extra text, markdown, or quotes outside JSON."
|
||||
disambiguate_prompt: "Given candidate visit entries (JSON array): {{.Entries}} and user message: {{.Message}} choose the best matching visit's ID. Return ONLY JSON: {\"visitReason\":\"<one of the candidate IDs or empty string if none>\"}. No other text."
|
||||
translate_prompt: "Translate the following veterinary-related sentence to English. Input: '{{.Message}}'. Return ONLY the English translation as one concise sentence. IMPORTANT: Do NOT output any <think> tags, reasoning, analysis, or explanations. No markdown, no quotes. If already English, return it unchanged."
|
||||
|
|
|
|||
|
|
@ -0,0 +1,180 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestChatService_HandleChat(t *testing.T) {
|
||||
// Setup mock dependencies
|
||||
mockLLM := &MockLLMClient{
|
||||
ExtractKeywordsFunc: func(ctx context.Context, message string) (map[string]interface{}, error) {
|
||||
return map[string]interface{}{
|
||||
"translate": "test translation",
|
||||
"keyword": []string{"test", "keyword"},
|
||||
"animal": "dog",
|
||||
}, nil
|
||||
},
|
||||
DisambiguateBestMatchFunc: func(ctx context.Context, message string, candidates []Visit) (string, error) {
|
||||
return "visit1", nil
|
||||
},
|
||||
}
|
||||
|
||||
mockDB := &MockVisitDB{
|
||||
FindCandidatesFunc: func(keywords []string) ([]Visit, error) {
|
||||
return []Visit{
|
||||
{ID: "visit1", Procedures: []Procedure{{Name: "Test", Price: 100, DurationMin: 30}}},
|
||||
{ID: "visit2", Procedures: []Procedure{{Name: "Test2", Price: 200, DurationMin: 60}}},
|
||||
}, nil
|
||||
},
|
||||
FindByIdFunc: func(id string) (Visit, error) {
|
||||
return Visit{
|
||||
ID: id,
|
||||
Procedures: []Procedure{
|
||||
{Name: "Test", Price: 100, DurationMin: 30},
|
||||
},
|
||||
Notes: "Test notes",
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
|
||||
mockRepo := &MockChatRepository{}
|
||||
|
||||
// Create service with mocks
|
||||
svc := NewChatService(mockLLM, mockDB, mockRepo)
|
||||
|
||||
// Create test context
|
||||
gin.SetMode(gin.TestMode)
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := gin.CreateTestContext(w)
|
||||
|
||||
// Mock request body
|
||||
reqBody := `{"message": "I need a test visit"}`
|
||||
c.Request = httptest.NewRequest(http.MethodPost, "/chat", strings.NewReader(reqBody))
|
||||
c.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
// Call the handler
|
||||
svc.HandleChat(c)
|
||||
|
||||
// Validate response
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
|
||||
var resp ChatResponse
|
||||
err := json.Unmarshal(w.Body.Bytes(), &resp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.NotNil(t, resp.Match)
|
||||
assert.Equal(t, "visit1", *resp.Match)
|
||||
assert.Equal(t, 100, resp.TotalPrice)
|
||||
assert.Equal(t, 30, resp.TotalDuration)
|
||||
assert.Equal(t, "Test notes", resp.Notes)
|
||||
}
|
||||
|
||||
type MockVisitDB struct {
|
||||
FindCandidatesFunc func(keywords []string) ([]Visit, error)
|
||||
FindByIdFunc func(id string) (Visit, error)
|
||||
}
|
||||
|
||||
func (m *MockVisitDB) FindCandidates(keywords []string) ([]Visit, error) {
|
||||
if m.FindCandidatesFunc != nil {
|
||||
return m.FindCandidatesFunc(keywords)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockVisitDB) FindById(id string) (Visit, error) {
|
||||
if m.FindByIdFunc != nil {
|
||||
return m.FindByIdFunc(id)
|
||||
}
|
||||
return Visit{}, nil
|
||||
}
|
||||
|
||||
type MockChatRepository struct {
|
||||
SaveChatInteractionFunc func(ctx context.Context, interaction ChatInteraction) error
|
||||
ListChatInteractionsFunc func(ctx context.Context, limit, offset int) ([]ChatInteraction, error)
|
||||
SaveLLMRawEventFunc func(ctx context.Context, correlationID, phase, raw string) error
|
||||
ListLLMRawEventsFunc func(ctx context.Context, correlationID string, limit, offset int) ([]RawLLMEvent, error)
|
||||
SaveKnowledgeModelFunc func(ctx context.Context, text string) error
|
||||
ListKnowledgeModelsFunc func(ctx context.Context, limit, offset int) ([]knowledgeModelMeta, error)
|
||||
GetKnowledgeModelTextFunc func(ctx context.Context, id int64) (string, error)
|
||||
GetUserByUsernameFunc func(ctx context.Context, username string) (*User, error)
|
||||
CountUsersFunc func(ctx context.Context) (int, error)
|
||||
CreateUserFunc func(ctx context.Context, username, passwordHash string) error
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) SaveChatInteraction(ctx context.Context, interaction ChatInteraction) error {
|
||||
if m.SaveChatInteractionFunc != nil {
|
||||
return m.SaveChatInteractionFunc(ctx, interaction)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) ListChatInteractions(ctx context.Context, limit, offset int) ([]ChatInteraction, error) {
|
||||
if m.ListChatInteractionsFunc != nil {
|
||||
return m.ListChatInteractionsFunc(ctx, limit, offset)
|
||||
}
|
||||
return []ChatInteraction{}, nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) SaveLLMRawEvent(ctx context.Context, correlationID, phase, raw string) error {
|
||||
if m.SaveLLMRawEventFunc != nil {
|
||||
return m.SaveLLMRawEventFunc(ctx, correlationID, phase, raw)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) ListLLMRawEvents(ctx context.Context, correlationID string, limit, offset int) ([]RawLLMEvent, error) {
|
||||
if m.ListLLMRawEventsFunc != nil {
|
||||
return m.ListLLMRawEventsFunc(ctx, correlationID, limit, offset)
|
||||
}
|
||||
return []RawLLMEvent{}, nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) SaveKnowledgeModel(ctx context.Context, text string) error {
|
||||
if m.SaveKnowledgeModelFunc != nil {
|
||||
return m.SaveKnowledgeModelFunc(ctx, text)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) ListKnowledgeModels(ctx context.Context, limit, offset int) ([]knowledgeModelMeta, error) {
|
||||
if m.ListKnowledgeModelsFunc != nil {
|
||||
return m.ListKnowledgeModelsFunc(ctx, limit, offset)
|
||||
}
|
||||
return []knowledgeModelMeta{}, nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) GetKnowledgeModelText(ctx context.Context, id int64) (string, error) {
|
||||
if m.GetKnowledgeModelTextFunc != nil {
|
||||
return m.GetKnowledgeModelTextFunc(ctx, id)
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) GetUserByUsername(ctx context.Context, username string) (*User, error) {
|
||||
if m.GetUserByUsernameFunc != nil {
|
||||
return m.GetUserByUsernameFunc(ctx, username)
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) CountUsers(ctx context.Context) (int, error) {
|
||||
if m.CountUsersFunc != nil {
|
||||
return m.CountUsersFunc(ctx)
|
||||
}
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
func (m *MockChatRepository) CreateUser(ctx context.Context, username, passwordHash string) error {
|
||||
if m.CreateUserFunc != nil {
|
||||
return m.CreateUserFunc(ctx, username, passwordHash)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -20,6 +20,8 @@ type mockHandleChatLLM struct {
|
|||
disambigID string
|
||||
keywordsErr error
|
||||
disambigErr error
|
||||
embeddings []float64
|
||||
embeddingErr error
|
||||
}
|
||||
|
||||
func (m *mockHandleChatLLM) ExtractKeywords(ctx context.Context, msg string) (map[string]interface{}, error) {
|
||||
|
|
@ -28,6 +30,12 @@ func (m *mockHandleChatLLM) ExtractKeywords(ctx context.Context, msg string) (ma
|
|||
func (m *mockHandleChatLLM) DisambiguateBestMatch(ctx context.Context, msg string, candidates []Visit) (string, error) {
|
||||
return m.disambigID, m.disambigErr
|
||||
}
|
||||
func (m *mockHandleChatLLM) GetEmbeddings(ctx context.Context, input string) ([]float64, error) {
|
||||
return m.embeddings, m.embeddingErr
|
||||
}
|
||||
func (m *mockHandleChatLLM) TranslateToEnglish(ctx context.Context, msg string) (string, error) {
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
// mapChatRepo is an in-memory implementation of ChatRepositoryAPI for tests.
|
||||
type mapChatRepo struct {
|
||||
|
|
|
|||
347
llm.go
347
llm.go
|
|
@ -3,55 +3,74 @@ package main
|
|||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"text/template"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// LLMClient abstracts LLM API calls
|
||||
type LLMClient struct {
|
||||
APIKey string
|
||||
BaseURL string
|
||||
Model string
|
||||
Repo ChatRepositoryAPI
|
||||
// LLMClientAPI allows mocking LLMClient in other places
|
||||
type LLMClientAPI interface {
|
||||
ExtractKeywords(ctx context.Context, message string) (map[string]interface{}, error)
|
||||
DisambiguateBestMatch(ctx context.Context, message string, candidates []Visit) (string, error)
|
||||
GetEmbeddings(ctx context.Context, input string) ([]float64, error)
|
||||
TranslateToEnglish(ctx context.Context, message string) (string, error)
|
||||
}
|
||||
|
||||
// NewLLMClient constructs a new LLMClient with the given API key, base URL, model, and optional repository
|
||||
func NewLLMClient(apiKey, baseURL string, model string, repo ChatRepositoryAPI) *LLMClient {
|
||||
return &LLMClient{APIKey: apiKey, BaseURL: baseURL, Model: model, Repo: repo}
|
||||
}
|
||||
// --- Format Utilities ---
|
||||
|
||||
func (llm *LLMClient) SetRepository(r ChatRepositoryAPI) { llm.Repo = r }
|
||||
|
||||
// helper to get correlation id from context
|
||||
const correlationIDCtxKey = "corr_id"
|
||||
|
||||
func correlationIDFromCtx(ctx context.Context) string {
|
||||
v := ctx.Value(correlationIDCtxKey)
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
// GetExtractKeywordsFormat returns the format specification for keyword extraction
|
||||
func GetExtractKeywordsFormat() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"translate": map[string]interface{}{"type": "string"},
|
||||
"keyword": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}},
|
||||
"animal": map[string]interface{}{"type": "string"},
|
||||
},
|
||||
"required": []string{"translate", "keyword", "animal"},
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (llm *LLMClient) persistRaw(ctx context.Context, phase, raw string) {
|
||||
if llm == nil || llm.Repo == nil || raw == "" {
|
||||
return
|
||||
// GetDisambiguateFormat returns the format specification for disambiguation
|
||||
func GetDisambiguateFormat() map[string]interface{} {
|
||||
return map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"visitReason": map[string]interface{}{"type": "string"},
|
||||
},
|
||||
"required": []string{"visitReason"},
|
||||
}
|
||||
cid := correlationIDFromCtx(ctx)
|
||||
if cid == "" {
|
||||
return
|
||||
}
|
||||
_ = llm.Repo.SaveLLMRawEvent(ctx, cid, phase, raw)
|
||||
}
|
||||
|
||||
// renderPrompt renders a Go template with the given data
|
||||
// --- Factory ---
|
||||
|
||||
func NewLLMClientFromEnv(repo ChatRepositoryAPI) LLMClientAPI {
|
||||
provider := os.Getenv("LLM_PROVIDER")
|
||||
apiKey := os.Getenv("OPENAI_API_KEY")
|
||||
baseURL := os.Getenv("OPENAI_BASE_URL")
|
||||
model := os.Getenv("OPENAI_MODEL")
|
||||
switch strings.ToLower(provider) {
|
||||
case "openai", "openrouter":
|
||||
return NewOpenAIClient(apiKey, baseURL, model, repo)
|
||||
case "ollama", "":
|
||||
oc := NewOllamaClient(apiKey, baseURL, model, repo)
|
||||
em := os.Getenv("OLLAMA_EMBED_MODEL")
|
||||
if strings.TrimSpace(em) == "" {
|
||||
em = "all-minilm"
|
||||
logrus.Infof("No OLLAMA_EMBED_MODEL specified; defaulting embedding model to %s", em)
|
||||
}
|
||||
oc.EmbeddingModel = em
|
||||
return oc
|
||||
default:
|
||||
logrus.Warnf("Unknown LLM_PROVIDER %q, defaulting to Ollama", provider)
|
||||
return NewOllamaClient(apiKey, baseURL, model, repo)
|
||||
}
|
||||
}
|
||||
|
||||
// --- Utility ---
|
||||
|
||||
func renderPrompt(tmplStr string, data any) (string, error) {
|
||||
tmpl, err := template.New("").Parse(tmplStr)
|
||||
if err != nil {
|
||||
|
|
@ -63,263 +82,3 @@ func renderPrompt(tmplStr string, data any) (string, error) {
|
|||
}
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// ExtractKeywords calls LLM to extract keywords from user message
|
||||
func (llm *LLMClient) ExtractKeywords(ctx context.Context, message string) (map[string]interface{}, error) {
|
||||
_, parsed, err := llm.ExtractKeywordsRaw(ctx, message)
|
||||
return parsed, err
|
||||
}
|
||||
|
||||
// ExtractKeywordsRaw returns the raw JSON string and parsed map
|
||||
func (llm *LLMClient) ExtractKeywordsRaw(ctx context.Context, message string) (string, map[string]interface{}, error) {
|
||||
prompt, err := renderPrompt(appConfig.LLM.ExtractKeywordsPrompt, map[string]string{"Message": message})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("[CONFIG] Failed to render ExtractKeywords prompt")
|
||||
return "", nil, err
|
||||
}
|
||||
logrus.WithField("prompt", prompt).Info("[LLM] ExtractKeywords prompt")
|
||||
format := map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"translate": map[string]interface{}{"type": "string"},
|
||||
"keyword": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}},
|
||||
"animal": map[string]interface{}{"type": "string"},
|
||||
},
|
||||
"required": []string{"translate", "keyword", "animal"},
|
||||
}
|
||||
resp, err := llm.openAICompletion(ctx, prompt, format)
|
||||
logrus.WithFields(logrus.Fields{"response": resp, "err": err}).Info("[LLM] ExtractKeywords response")
|
||||
if err != nil {
|
||||
return resp, nil, err // return whatever raw we got (may be empty)
|
||||
}
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(resp), &result); err != nil {
|
||||
return resp, nil, err
|
||||
}
|
||||
llm.persistRaw(ctx, "extract_keywords", resp)
|
||||
return resp, result, nil
|
||||
}
|
||||
|
||||
// DisambiguateBestMatch calls LLM to pick best match from candidates
|
||||
func (llm *LLMClient) DisambiguateBestMatch(ctx context.Context, message string, candidates []Visit) (string, error) {
|
||||
_, vr, err := llm.DisambiguateBestMatchRaw(ctx, message, candidates)
|
||||
return vr, err
|
||||
}
|
||||
|
||||
// DisambiguateBestMatchRaw returns raw JSON and visitReason
|
||||
func (llm *LLMClient) DisambiguateBestMatchRaw(ctx context.Context, message string, candidates []Visit) (string, string, error) {
|
||||
format := map[string]interface{}{
|
||||
"type": "object",
|
||||
"properties": map[string]interface{}{
|
||||
"visitReason": map[string]interface{}{"type": "string"},
|
||||
},
|
||||
"required": []string{"visitReason"},
|
||||
}
|
||||
entries, _ := json.Marshal(candidates)
|
||||
prompt, err := renderPrompt(appConfig.LLM.DisambiguatePrompt, map[string]string{"Entries": string(entries), "Message": message})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("[CONFIG] Failed to render Disambiguate prompt")
|
||||
return "", "", err
|
||||
}
|
||||
logrus.WithField("prompt", prompt).Info("[LLM] DisambiguateBestMatch prompt")
|
||||
resp, err := llm.openAICompletion(ctx, prompt, format)
|
||||
logrus.WithFields(logrus.Fields{"response": resp, "err": err}).Info("[LLM] DisambiguateBestMatch response")
|
||||
if err != nil {
|
||||
return resp, "", err
|
||||
}
|
||||
var parsed map[string]string
|
||||
if err := json.Unmarshal([]byte(resp), &parsed); err != nil {
|
||||
return resp, "", fmt.Errorf("failed to unmarshal disambiguation response: %w", err)
|
||||
}
|
||||
visitReason := strings.TrimSpace(parsed["visitReason"])
|
||||
if visitReason == "" {
|
||||
return resp, "", fmt.Errorf("visitReason not found in response")
|
||||
}
|
||||
llm.persistRaw(ctx, "disambiguate", resp)
|
||||
return resp, visitReason, nil
|
||||
}
|
||||
|
||||
// openAICompletion now supports both Ollama (default local) and OpenRouter/OpenAI-compatible APIs without external branching.
|
||||
// It auto-detects by inspecting the BaseURL. If the URL contains "openrouter.ai" or "/v1/", it assumes OpenAI-style.
|
||||
func (llm *LLMClient) openAICompletion(ctx context.Context, prompt string, format map[string]interface{}) (string, error) {
|
||||
apiURL := llm.BaseURL
|
||||
if apiURL == "" {
|
||||
// Default to Ollama local chat endpoint
|
||||
apiURL = "http://localhost:11434/api/chat"
|
||||
}
|
||||
|
||||
isOpenAIStyle := strings.Contains(apiURL, "openrouter.ai") || strings.Contains(apiURL, "/v1/")
|
||||
|
||||
// Helper to stringify the expected JSON schema for instructions
|
||||
schemaDesc := func() string {
|
||||
b, _ := json.MarshalIndent(format, "", " ")
|
||||
return string(b)
|
||||
}
|
||||
|
||||
truncate := func(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
return s[:n] + "...<truncated>"
|
||||
}
|
||||
|
||||
buildBody := func() map[string]interface{} {
|
||||
if isOpenAIStyle {
|
||||
return map[string]interface{}{
|
||||
"model": llm.Model,
|
||||
"messages": []map[string]string{
|
||||
{"role": "system", "content": "You are a strict JSON generator. ONLY output valid JSON matching this schema: " + schemaDesc() + " Do not add explanations."},
|
||||
{"role": "user", "content": prompt},
|
||||
},
|
||||
"response_format": map[string]interface{}{"type": "json_object"},
|
||||
}
|
||||
}
|
||||
// Ollama style
|
||||
return map[string]interface{}{
|
||||
"model": llm.Model,
|
||||
"messages": []map[string]string{{"role": "user", "content": prompt}},
|
||||
"stream": false,
|
||||
"format": format,
|
||||
}
|
||||
}
|
||||
|
||||
body := buildBody()
|
||||
|
||||
doRequest := func(body map[string]interface{}) (raw []byte, status int, err error, dur time.Duration) {
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
bodySize := len(jsonBody)
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"event": "llm_request",
|
||||
"api_url": apiURL,
|
||||
"model": llm.Model,
|
||||
"is_openai_style": isOpenAIStyle,
|
||||
"prompt_len": len(prompt),
|
||||
"body_size": bodySize,
|
||||
}).Info("[LLM] sending request")
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewBuffer(jsonBody))
|
||||
if llm.APIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+llm.APIKey)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if strings.Contains(apiURL, "openrouter.ai") {
|
||||
req.Header.Set("Referer", "https://github.com/")
|
||||
req.Header.Set("X-Title", "vetrag-app")
|
||||
}
|
||||
start := time.Now()
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, 0, err, time.Since(start)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, rerr := io.ReadAll(resp.Body)
|
||||
return raw, resp.StatusCode, rerr, time.Since(start)
|
||||
}
|
||||
|
||||
raw, status, err, dur := doRequest(body)
|
||||
if err != nil {
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"event": "llm_response",
|
||||
"status": status,
|
||||
"latency_ms": dur.Milliseconds(),
|
||||
"error": err,
|
||||
}).Error("[LLM] request failed")
|
||||
return "", err
|
||||
}
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"event": "llm_raw_response",
|
||||
"status": status,
|
||||
"latency_ms": dur.Milliseconds(),
|
||||
"raw_trunc": truncate(string(raw), 600),
|
||||
"raw_len": len(raw),
|
||||
}).Debug("[LLM] raw response body")
|
||||
|
||||
parseVariant := "unknown"
|
||||
|
||||
// Attempt Ollama format parse
|
||||
var ollama struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &ollama); err == nil && ollama.Message.Content != "" {
|
||||
parseVariant = "ollama"
|
||||
content := ollama.Message.Content
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"event": "llm_response",
|
||||
"status": status,
|
||||
"latency_ms": dur.Milliseconds(),
|
||||
"parse_variant": parseVariant,
|
||||
"content_len": len(content),
|
||||
"content_snip": truncate(content, 300),
|
||||
}).Info("[LLM] parsed response")
|
||||
return content, nil
|
||||
}
|
||||
|
||||
// Attempt OpenAI/OpenRouter style parse
|
||||
var openAI struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &openAI); err == nil {
|
||||
if openAI.Error != nil || status >= 400 {
|
||||
parseVariant = "openai"
|
||||
var msg string
|
||||
if openAI.Error != nil {
|
||||
msg = openAI.Error.Message
|
||||
} else {
|
||||
msg = string(raw)
|
||||
}
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"event": "llm_response",
|
||||
"status": status,
|
||||
"latency_ms": dur.Milliseconds(),
|
||||
"parse_variant": parseVariant,
|
||||
"error": msg,
|
||||
}).Error("[LLM] provider error")
|
||||
return "", fmt.Errorf("provider error: %s", msg)
|
||||
}
|
||||
if len(openAI.Choices) > 0 && openAI.Choices[0].Message.Content != "" {
|
||||
parseVariant = "openai"
|
||||
content := openAI.Choices[0].Message.Content
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"event": "llm_response",
|
||||
"status": status,
|
||||
"latency_ms": dur.Milliseconds(),
|
||||
"parse_variant": parseVariant,
|
||||
"content_len": len(content),
|
||||
"content_snip": truncate(content, 300),
|
||||
}).Info("[LLM] parsed response")
|
||||
return content, nil
|
||||
}
|
||||
}
|
||||
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"event": "llm_response",
|
||||
"status": status,
|
||||
"latency_ms": dur.Milliseconds(),
|
||||
"parse_variant": parseVariant,
|
||||
"raw_snip": truncate(string(raw), 300),
|
||||
}).Error("[LLM] unrecognized response format")
|
||||
|
||||
return "", fmt.Errorf("unrecognized LLM response format: %.200s", string(raw))
|
||||
}
|
||||
|
||||
// LLMClientAPI allows mocking LLMClient in other places
|
||||
// Only public methods should be included
|
||||
|
||||
type LLMClientAPI interface {
|
||||
ExtractKeywords(ctx context.Context, message string) (map[string]interface{}, error)
|
||||
DisambiguateBestMatch(ctx context.Context, message string, candidates []Visit) (string, error)
|
||||
}
|
||||
|
||||
var _ LLMClientAPI = (*LLMClient)(nil)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,171 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// MockLLMClient implements LLMClientAPI for testing
|
||||
type MockLLMClient struct {
|
||||
ExtractKeywordsFunc func(ctx context.Context, message string) (map[string]interface{}, error)
|
||||
DisambiguateBestMatchFunc func(ctx context.Context, message string, candidates []Visit) (string, error)
|
||||
GetEmbeddingsFunc func(ctx context.Context, input string) ([]float64, error)
|
||||
}
|
||||
|
||||
func (m *MockLLMClient) ExtractKeywords(ctx context.Context, message string) (map[string]interface{}, error) {
|
||||
if m.ExtractKeywordsFunc != nil {
|
||||
return m.ExtractKeywordsFunc(ctx, message)
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"translate": "test translation",
|
||||
"keyword": []string{"test", "keywords"},
|
||||
"animal": "test animal",
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (m *MockLLMClient) DisambiguateBestMatch(ctx context.Context, message string, candidates []Visit) (string, error) {
|
||||
if m.DisambiguateBestMatchFunc != nil {
|
||||
return m.DisambiguateBestMatchFunc(ctx, message, candidates)
|
||||
}
|
||||
if len(candidates) > 0 {
|
||||
return candidates[0].ID, nil
|
||||
}
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (m *MockLLMClient) GetEmbeddings(ctx context.Context, input string) ([]float64, error) {
|
||||
if m.GetEmbeddingsFunc != nil {
|
||||
return m.GetEmbeddingsFunc(ctx, input)
|
||||
}
|
||||
return []float64{0.1, 0.2, 0.3}, nil
|
||||
}
|
||||
|
||||
func (m *MockLLMClient) TranslateToEnglish(ctx context.Context, message string) (string, error) {
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func TestNewLLMClientFromEnv(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
envVars map[string]string
|
||||
expectedType string
|
||||
}{
|
||||
{
|
||||
name: "default to ollama when no provider specified",
|
||||
envVars: map[string]string{
|
||||
"LLM_PROVIDER": "",
|
||||
},
|
||||
expectedType: "*main.OllamaClient",
|
||||
},
|
||||
{
|
||||
name: "use openai client when provider is openai",
|
||||
envVars: map[string]string{
|
||||
"LLM_PROVIDER": "openai",
|
||||
},
|
||||
expectedType: "*main.OpenAIClient",
|
||||
},
|
||||
{
|
||||
name: "use ollama client when provider is ollama",
|
||||
envVars: map[string]string{
|
||||
"LLM_PROVIDER": "ollama",
|
||||
},
|
||||
expectedType: "*main.OllamaClient",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
// Clear existing env vars
|
||||
os.Unsetenv("LLM_PROVIDER")
|
||||
os.Unsetenv("OPENAI_API_KEY")
|
||||
os.Unsetenv("OPENAI_BASE_URL")
|
||||
os.Unsetenv("OPENAI_MODEL")
|
||||
|
||||
// Set env vars for test
|
||||
for k, v := range tt.envVars {
|
||||
os.Setenv(k, v)
|
||||
}
|
||||
|
||||
client := NewLLMClientFromEnv(nil)
|
||||
assert.NotNil(t, client)
|
||||
assert.Equal(t, tt.expectedType, typeName(client))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func typeName(v interface{}) string {
|
||||
if v == nil {
|
||||
return "nil"
|
||||
}
|
||||
return fmt.Sprintf("%T", v)
|
||||
}
|
||||
|
||||
func TestOpenAIClient_GetEmbeddings(t *testing.T) {
|
||||
// Mock server to simulate OpenAI API response
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// No path check needed since we're passing the full URL as baseURL
|
||||
assert.Equal(t, "Bearer test-key", r.Header.Get("Authorization"))
|
||||
|
||||
var reqBody map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&reqBody)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test-model", reqBody["model"])
|
||||
assert.Equal(t, "test input", reqBody["input"])
|
||||
|
||||
// Respond with mock embedding data
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{
|
||||
"data": [
|
||||
{
|
||||
"embedding": [0.1, 0.2, 0.3, 0.4, 0.5]
|
||||
}
|
||||
]
|
||||
}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Pass the full URL as the baseURL parameter
|
||||
client := NewOpenAIClient("test-key", server.URL, "test-model", nil)
|
||||
embeddings, err := client.GetEmbeddings(context.Background(), "test input")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []float64{0.1, 0.2, 0.3, 0.4, 0.5}, embeddings)
|
||||
}
|
||||
|
||||
func TestOllamaClient_GetEmbeddings(t *testing.T) {
|
||||
// Mock server to simulate Ollama API response
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// The API URL for embeddings in ollama_client.go is constructed as:
|
||||
// apiURL (baseURL) = "http://localhost:11434/api/embeddings"
|
||||
// So we shouldn't expect a path suffix here
|
||||
|
||||
var reqBody map[string]interface{}
|
||||
err := json.NewDecoder(r.Body).Decode(&reqBody)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "test-model", reqBody["model"])
|
||||
assert.Equal(t, "test input", reqBody["prompt"])
|
||||
|
||||
// Respond with mock embedding data
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{
|
||||
"embedding": [0.1, 0.2, 0.3, 0.4, 0.5]
|
||||
}`))
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
// Pass the full URL as the baseURL parameter
|
||||
client := NewOllamaClient("", server.URL, "test-model", nil)
|
||||
embeddings, err := client.GetEmbeddings(context.Background(), "test input")
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, []float64{0.1, 0.2, 0.3, 0.4, 0.5}, embeddings)
|
||||
}
|
||||
12
main.go
12
main.go
|
|
@ -5,7 +5,6 @@ import (
|
|||
"database/sql"
|
||||
"html/template"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
|
|
@ -71,13 +70,10 @@ func main() {
|
|||
// defer repo.Close() // optionally enable
|
||||
|
||||
// Initialize LLM client
|
||||
llmClient := NewLLMClient(
|
||||
os.Getenv("OPENAI_API_KEY"),
|
||||
os.Getenv("OPENAI_BASE_URL"),
|
||||
os.Getenv("OPENAI_MODEL"),
|
||||
repo,
|
||||
)
|
||||
var llm LLMClientAPI = llmClient
|
||||
llm := NewLLMClientFromEnv(repo)
|
||||
|
||||
// Launch background backfill of sentence embeddings (non-blocking)
|
||||
startSentenceEmbeddingBackfill(repo, llm, &visitDB)
|
||||
|
||||
// Wrap templates for controller
|
||||
uiTmpl := &TemplateWrapper{Tmpl: uiTemplate}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,16 @@
|
|||
-- +goose Up
|
||||
-- Create sentence_embeddings table using standard Postgres types (no vector extension)
|
||||
CREATE TABLE sentence_embeddings (
|
||||
id SERIAL PRIMARY KEY,
|
||||
visit_id INTEGER NOT NULL,
|
||||
sentence TEXT NOT NULL,
|
||||
translated TEXT,
|
||||
embeddings FLOAT[] NOT NULL, -- Using standard float array instead of vector
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- Create unique index for efficient lookups and preventing duplicates
|
||||
CREATE UNIQUE INDEX idx_sentence_embeddings_visit_sentence ON sentence_embeddings (visit_id, sentence);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE IF EXISTS sentence_embeddings;
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
-- +goose Up
|
||||
-- Altering visit_id type, keeping compatibility with standard Postgres types
|
||||
ALTER TABLE sentence_embeddings
|
||||
ALTER COLUMN visit_id TYPE TEXT;
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE sentence_embeddings
|
||||
ALTER COLUMN visit_id TYPE INTEGER USING (visit_id::integer);
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
-- +goose Up
|
||||
-- The unique index was already created in migration 0003 when we switched to standard Postgres types
|
||||
-- This migration is kept for consistency in migration sequence but doesn't perform any action
|
||||
|
||||
-- +goose Down
|
||||
-- No action needed for rollback
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
-- +goose Up
|
||||
-- Update schema to support 384-dimensional embeddings using standard Postgres types
|
||||
-- No need to modify column type as we're now using a flexible FLOAT[] array
|
||||
|
||||
-- +goose Down
|
||||
-- No action needed for rollback since we're using a flexible array type
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
-- +goose Up
|
||||
-- Add separate columns for different embedding dimensions using standard Postgres FLOAT[] arrays
|
||||
ALTER TABLE sentence_embeddings
|
||||
ADD COLUMN IF NOT EXISTS embedding_384 FLOAT[],
|
||||
ADD COLUMN IF NOT EXISTS embedding_1536 FLOAT[];
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE sentence_embeddings
|
||||
DROP COLUMN IF EXISTS embedding_384,
|
||||
DROP COLUMN IF EXISTS embedding_1536;
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
-- +goose Up
|
||||
-- Drop legacy embeddings column as it's been replaced by embedding_384 and embedding_1536
|
||||
ALTER TABLE sentence_embeddings
|
||||
DROP COLUMN IF EXISTS embeddings;
|
||||
|
||||
-- +goose Down
|
||||
-- No restoration action needed as embedding_384 and embedding_1536 are preserved
|
||||
|
|
@ -0,0 +1,273 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// --- OllamaClient implementation ---
|
||||
|
||||
type OllamaClient struct {
|
||||
APIKey string
|
||||
BaseURL string
|
||||
Model string
|
||||
EmbeddingModel string
|
||||
Repo ChatRepositoryAPI
|
||||
}
|
||||
|
||||
func NewOllamaClient(apiKey, baseURL, model string, repo ChatRepositoryAPI) *OllamaClient {
|
||||
return &OllamaClient{APIKey: apiKey, BaseURL: baseURL, Model: model, Repo: repo}
|
||||
}
|
||||
|
||||
func (llm *OllamaClient) ExtractKeywords(ctx context.Context, message string) (map[string]interface{}, error) {
|
||||
_, parsed, err := llm.ExtractKeywordsRaw(ctx, message)
|
||||
return parsed, err
|
||||
}
|
||||
|
||||
func (llm *OllamaClient) ExtractKeywordsRaw(ctx context.Context, message string) (string, map[string]interface{}, error) {
|
||||
prompt, err := renderPrompt(appConfig.LLM.ExtractKeywordsPrompt, map[string]string{"Message": message})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("[CONFIG] Failed to render ExtractKeywords prompt")
|
||||
return "", nil, err
|
||||
}
|
||||
logrus.WithField("prompt", prompt).Info("[LLM] ExtractKeywords prompt")
|
||||
|
||||
// Use the utility function instead of inline format definition
|
||||
format := GetExtractKeywordsFormat()
|
||||
|
||||
resp, err := llm.ollamaCompletion(ctx, prompt, format)
|
||||
logrus.WithFields(logrus.Fields{"response": resp, "err": err}).Info("[LLM] ExtractKeywords response")
|
||||
if err != nil {
|
||||
return resp, nil, err
|
||||
}
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(resp), &result); err != nil {
|
||||
return resp, nil, err
|
||||
}
|
||||
return resp, result, nil
|
||||
}
|
||||
|
||||
func (llm *OllamaClient) DisambiguateBestMatch(ctx context.Context, message string, candidates []Visit) (string, error) {
|
||||
_, vr, err := llm.DisambiguateBestMatchRaw(ctx, message, candidates)
|
||||
return vr, err
|
||||
}
|
||||
|
||||
func (llm *OllamaClient) DisambiguateBestMatchRaw(ctx context.Context, message string, candidates []Visit) (string, string, error) {
|
||||
// Use the utility function instead of inline format definition
|
||||
format := GetDisambiguateFormat()
|
||||
|
||||
entries, _ := json.Marshal(candidates)
|
||||
prompt, err := renderPrompt(appConfig.LLM.DisambiguatePrompt, map[string]string{"Entries": string(entries), "Message": message})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("[CONFIG] Failed to render Disambiguate prompt")
|
||||
return "", "", err
|
||||
}
|
||||
logrus.WithField("prompt", prompt).Info("[LLM] DisambiguateBestMatch prompt")
|
||||
resp, err := llm.ollamaCompletion(ctx, prompt, format)
|
||||
logrus.WithFields(logrus.Fields{"response": resp, "err": err}).Info("[LLM] DisambiguateBestMatch response")
|
||||
if err != nil {
|
||||
return resp, "", err
|
||||
}
|
||||
var parsed map[string]string
|
||||
if err := json.Unmarshal([]byte(resp), &parsed); err != nil {
|
||||
return resp, "", fmt.Errorf("failed to unmarshal disambiguation response: %w", err)
|
||||
}
|
||||
visitReason := strings.TrimSpace(parsed["visitReason"])
|
||||
if visitReason == "" {
|
||||
return resp, "", fmt.Errorf("visitReason not found in response")
|
||||
}
|
||||
return resp, visitReason, nil
|
||||
}
|
||||
|
||||
func (llm *OllamaClient) ollamaCompletion(ctx context.Context, prompt string, format map[string]interface{}) (string, error) {
|
||||
apiURL := llm.BaseURL
|
||||
if apiURL == "" {
|
||||
apiURL = "http://localhost:11434/api/chat"
|
||||
}
|
||||
|
||||
messages := []map[string]string{{"role": "user", "content": prompt}}
|
||||
//if os.Getenv("DISABLE_THINK") == "1" {
|
||||
// System message to suppress chain-of-thought style outputs.
|
||||
messages = append([]map[string]string{{
|
||||
"role": "system",
|
||||
"content": "You are a concise assistant. Output ONLY the final answer requested by the user. Do not include reasoning, analysis, or <think> tags.",
|
||||
}}, messages...)
|
||||
//}
|
||||
|
||||
body := map[string]interface{}{
|
||||
"model": llm.Model,
|
||||
"messages": messages,
|
||||
"stream": false,
|
||||
"format": format,
|
||||
}
|
||||
|
||||
// Optional: Add a stop sequence to prevent <think> tags if they appear
|
||||
if os.Getenv("DISABLE_THINK") == "1" {
|
||||
body["options"] = map[string]interface{}{"stop": []string{"<think>"}}
|
||||
}
|
||||
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewBuffer(jsonBody))
|
||||
if llm.APIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+llm.APIKey)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var ollama struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &ollama); err == nil && ollama.Message.Content != "" {
|
||||
return ollama.Message.Content, nil
|
||||
}
|
||||
if ollama.Error != "" {
|
||||
return "", fmt.Errorf("provider error: %s", ollama.Error)
|
||||
}
|
||||
return "", fmt.Errorf("unrecognized LLM response format: %.200s", string(raw))
|
||||
}
|
||||
|
||||
func normalizeOllamaHost(raw string) string {
|
||||
if raw == "" {
|
||||
return "http://localhost:11434"
|
||||
}
|
||||
// strip trailing /api/* paths if user provided full endpoint
|
||||
lower := strings.ToLower(raw)
|
||||
for _, seg := range []string{"/api/chat", "/api/embeddings", "/api/generate"} {
|
||||
if strings.HasSuffix(lower, seg) {
|
||||
return raw[:len(raw)-len(seg)]
|
||||
}
|
||||
}
|
||||
return raw
|
||||
}
|
||||
|
||||
func (llm *OllamaClient) GetEmbeddings(ctx context.Context, input string) ([]float64, error) {
|
||||
host := normalizeOllamaHost(llm.BaseURL)
|
||||
apiURL := host + "/api/embeddings"
|
||||
modelName := llm.Model
|
||||
if llm.EmbeddingModel != "" {
|
||||
modelName = llm.EmbeddingModel
|
||||
}
|
||||
// retry parameters (env override OLLAMA_EMBED_ATTEMPTS)
|
||||
maxAttempts := 5
|
||||
if v := os.Getenv("OLLAMA_EMBED_ATTEMPTS"); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil && n > 0 && n < 20 {
|
||||
maxAttempts = n
|
||||
}
|
||||
}
|
||||
baseBackoff := 300 * time.Millisecond
|
||||
var lastErr error
|
||||
for attempt := 0; attempt < maxAttempts; attempt++ {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
default:
|
||||
}
|
||||
body := map[string]interface{}{
|
||||
"model": modelName,
|
||||
"prompt": input,
|
||||
}
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewBuffer(jsonBody))
|
||||
if llm.APIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+llm.APIKey)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
resp, err := (&http.Client{}).Do(req)
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
logrus.WithError(err).Warnf("[Ollama] embeddings request attempt=%d failed", attempt+1)
|
||||
} else {
|
||||
raw, rerr := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if rerr != nil {
|
||||
lastErr = rerr
|
||||
} else {
|
||||
var generic map[string]json.RawMessage
|
||||
if jerr := json.Unmarshal(raw, &generic); jerr != nil {
|
||||
lastErr = fmt.Errorf("unrecognized response (parse): %w", jerr)
|
||||
} else if embRaw, ok := generic["embedding"]; ok && len(embRaw) > 0 {
|
||||
var emb []float64
|
||||
if jerr := json.Unmarshal(embRaw, &emb); jerr != nil {
|
||||
lastErr = fmt.Errorf("failed to decode embedding: %w", jerr)
|
||||
} else if len(emb) == 0 {
|
||||
lastErr = fmt.Errorf("empty embedding returned")
|
||||
} else {
|
||||
return emb, nil
|
||||
}
|
||||
} else if drRaw, ok := generic["done_reason"]; ok {
|
||||
var reason string
|
||||
_ = json.Unmarshal(drRaw, &reason)
|
||||
if reason == "load" { // transient model loading state
|
||||
lastErr = fmt.Errorf("model loading")
|
||||
} else {
|
||||
lastErr = fmt.Errorf("unexpected done_reason=%s", reason)
|
||||
}
|
||||
} else if errRaw, ok := generic["error"]; ok {
|
||||
var errMsg string
|
||||
_ = json.Unmarshal(errRaw, &errMsg)
|
||||
if errMsg != "" {
|
||||
lastErr = fmt.Errorf("embedding error: %s", errMsg)
|
||||
} else {
|
||||
lastErr = fmt.Errorf("embedding error (empty message)")
|
||||
}
|
||||
} else {
|
||||
lastErr = fmt.Errorf("unrecognized embedding response: %.200s", string(raw))
|
||||
}
|
||||
}
|
||||
}
|
||||
if lastErr == nil {
|
||||
break
|
||||
}
|
||||
// backoff if not last attempt
|
||||
if attempt < maxAttempts-1 {
|
||||
delay := baseBackoff << attempt
|
||||
if strings.Contains(strings.ToLower(lastErr.Error()), "model loading") {
|
||||
delay += 1 * time.Second
|
||||
}
|
||||
time.Sleep(delay)
|
||||
}
|
||||
}
|
||||
if lastErr == nil {
|
||||
lastErr = fmt.Errorf("embedding retrieval failed with no error info")
|
||||
}
|
||||
return nil, lastErr
|
||||
}
|
||||
|
||||
func (llm *OllamaClient) TranslateToEnglish(ctx context.Context, message string) (string, error) {
|
||||
prompt, err := renderPrompt(appConfig.LLM.TranslatePrompt, map[string]string{"Message": message})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("[CONFIG] Failed to render Translate prompt")
|
||||
return "", err
|
||||
}
|
||||
logrus.WithField("prompt", prompt).Info("[LLM] TranslateToEnglish prompt")
|
||||
|
||||
resp, err := llm.ollamaCompletion(ctx, prompt, nil)
|
||||
logrus.WithFields(logrus.Fields{"response": resp, "err": err}).Info("[LLM] TranslateToEnglish response")
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
return strings.TrimSpace(resp), nil
|
||||
}
|
||||
|
|
@ -0,0 +1,216 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// --- OpenAIClient implementation ---
|
||||
|
||||
type OpenAIClient struct {
|
||||
APIKey string
|
||||
BaseURL string
|
||||
Model string
|
||||
Repo ChatRepositoryAPI
|
||||
}
|
||||
|
||||
func NewOpenAIClient(apiKey, baseURL, model string, repo ChatRepositoryAPI) *OpenAIClient {
|
||||
return &OpenAIClient{APIKey: apiKey, BaseURL: baseURL, Model: model, Repo: repo}
|
||||
}
|
||||
|
||||
func (llm *OpenAIClient) ExtractKeywords(ctx context.Context, message string) (map[string]interface{}, error) {
|
||||
_, parsed, err := llm.ExtractKeywordsRaw(ctx, message)
|
||||
return parsed, err
|
||||
}
|
||||
|
||||
func (llm *OpenAIClient) ExtractKeywordsRaw(ctx context.Context, message string) (string, map[string]interface{}, error) {
|
||||
prompt, err := renderPrompt(appConfig.LLM.ExtractKeywordsPrompt, map[string]string{"Message": message})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("[CONFIG] Failed to render ExtractKeywords prompt")
|
||||
return "", nil, err
|
||||
}
|
||||
logrus.WithField("prompt", prompt).Info("[LLM] ExtractKeywords prompt")
|
||||
|
||||
// Use the utility function instead of inline format definition
|
||||
format := GetExtractKeywordsFormat()
|
||||
|
||||
resp, err := llm.openAICompletion(ctx, prompt, format)
|
||||
logrus.WithFields(logrus.Fields{"response": resp, "err": err}).Info("[LLM] ExtractKeywords response")
|
||||
if err != nil {
|
||||
return resp, nil, err
|
||||
}
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal([]byte(resp), &result); err != nil {
|
||||
return resp, nil, err
|
||||
}
|
||||
return resp, result, nil
|
||||
}
|
||||
|
||||
func (llm *OpenAIClient) DisambiguateBestMatch(ctx context.Context, message string, candidates []Visit) (string, error) {
|
||||
_, vr, err := llm.DisambiguateBestMatchRaw(ctx, message, candidates)
|
||||
return vr, err
|
||||
}
|
||||
|
||||
func (llm *OpenAIClient) DisambiguateBestMatchRaw(ctx context.Context, message string, candidates []Visit) (string, string, error) {
|
||||
// Use the utility function instead of inline format definition
|
||||
format := GetDisambiguateFormat()
|
||||
|
||||
entries, _ := json.Marshal(candidates)
|
||||
prompt, err := renderPrompt(appConfig.LLM.DisambiguatePrompt, map[string]string{"Entries": string(entries), "Message": message})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("[CONFIG] Failed to render Disambiguate prompt")
|
||||
return "", "", err
|
||||
}
|
||||
logrus.WithField("prompt", prompt).Info("[LLM] DisambiguateBestMatch prompt")
|
||||
resp, err := llm.openAICompletion(ctx, prompt, format)
|
||||
logrus.WithFields(logrus.Fields{"response": resp, "err": err}).Info("[LLM] DisambiguateBestMatch response")
|
||||
if err != nil {
|
||||
return resp, "", err
|
||||
}
|
||||
var parsed map[string]string
|
||||
if err := json.Unmarshal([]byte(resp), &parsed); err != nil {
|
||||
return resp, "", fmt.Errorf("failed to unmarshal disambiguation response: %w", err)
|
||||
}
|
||||
visitReason := strings.TrimSpace(parsed["visitReason"])
|
||||
if visitReason == "" {
|
||||
return resp, "", fmt.Errorf("visitReason not found in response")
|
||||
}
|
||||
return resp, visitReason, nil
|
||||
}
|
||||
|
||||
func (llm *OpenAIClient) openAICompletion(ctx context.Context, prompt string, format map[string]interface{}) (string, error) {
|
||||
apiURL := llm.BaseURL
|
||||
if apiURL == "" {
|
||||
apiURL = "https://api.openai.com/v1/chat/completions"
|
||||
}
|
||||
// Helper to stringify the expected JSON schema for instructions
|
||||
schemaDesc := func() string {
|
||||
b, _ := json.MarshalIndent(format, "", " ")
|
||||
return string(b)
|
||||
}
|
||||
body := map[string]interface{}{
|
||||
"model": llm.Model,
|
||||
"messages": []map[string]string{
|
||||
{"role": "system", "content": "You are a strict JSON generator. ONLY output valid JSON matching this schema: " + schemaDesc() + " Do not add explanations."},
|
||||
{"role": "user", "content": prompt},
|
||||
},
|
||||
"response_format": map[string]interface{}{"type": "json_object"},
|
||||
}
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewBuffer(jsonBody))
|
||||
if llm.APIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+llm.APIKey)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if strings.Contains(apiURL, "openrouter.ai") {
|
||||
req.Header.Set("Referer", "https://github.com/")
|
||||
req.Header.Set("X-Title", "vetrag-app")
|
||||
}
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var openAI struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &openAI); err == nil {
|
||||
if openAI.Error != nil || resp.StatusCode >= 400 {
|
||||
var msg string
|
||||
if openAI.Error != nil {
|
||||
msg = openAI.Error.Message
|
||||
} else {
|
||||
msg = string(raw)
|
||||
}
|
||||
return "", fmt.Errorf("provider error: %s", msg)
|
||||
}
|
||||
if len(openAI.Choices) > 0 && openAI.Choices[0].Message.Content != "" {
|
||||
return openAI.Choices[0].Message.Content, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("unrecognized LLM response format: %.200s", string(raw))
|
||||
}
|
||||
|
||||
func (llm *OpenAIClient) GetEmbeddings(ctx context.Context, input string) ([]float64, error) {
|
||||
apiURL := llm.BaseURL
|
||||
if apiURL == "" {
|
||||
apiURL = "https://api.openai.com/v1/embeddings"
|
||||
}
|
||||
body := map[string]interface{}{
|
||||
"model": llm.Model,
|
||||
"input": input,
|
||||
}
|
||||
jsonBody, _ := json.Marshal(body)
|
||||
req, _ := http.NewRequestWithContext(ctx, http.MethodPost, apiURL, bytes.NewBuffer(jsonBody))
|
||||
if llm.APIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+llm.APIKey)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
if strings.Contains(apiURL, "openrouter.ai") {
|
||||
req.Header.Set("Referer", "https://github.com/")
|
||||
req.Header.Set("X-Title", "vetrag-app")
|
||||
}
|
||||
client := &http.Client{}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
raw, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var openAI struct {
|
||||
Data []struct {
|
||||
Embedding []float64 `json:"embedding"`
|
||||
} `json:"data"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &openAI); err == nil && len(openAI.Data) > 0 {
|
||||
return openAI.Data[0].Embedding, nil
|
||||
}
|
||||
if openAI.Error != nil {
|
||||
return nil, fmt.Errorf("embedding error: %s", openAI.Error.Message)
|
||||
}
|
||||
return nil, fmt.Errorf("unrecognized embedding response: %.200s", string(raw))
|
||||
}
|
||||
|
||||
func (llm *OpenAIClient) TranslateToEnglish(ctx context.Context, message string) (string, error) {
|
||||
prompt, err := renderPrompt(appConfig.LLM.TranslatePrompt, map[string]string{"Message": message})
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("[CONFIG] Failed to render Translate prompt")
|
||||
return "", err
|
||||
}
|
||||
logrus.WithField("prompt", prompt).Info("[LLM] TranslateToEnglish prompt")
|
||||
|
||||
resp, err := llm.openAICompletion(ctx, prompt, nil)
|
||||
logrus.WithFields(logrus.Fields{"response": resp, "err": err}).Info("[LLM] TranslateToEnglish response")
|
||||
if err != nil {
|
||||
return resp, err
|
||||
}
|
||||
return strings.TrimSpace(resp), nil
|
||||
}
|
||||
|
|
@ -18,16 +18,9 @@ func TestLLMClient_OpenRouterStyle_ExtractKeywords(t *testing.T) {
|
|||
appConfig.LLM.ExtractKeywordsPrompt = "Dummy {{.Message}}" // simple template
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path != "/v1/chat/completions" {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
// Optionally verify header presence
|
||||
if got := r.Header.Get("Authorization"); got == "" {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
// Format the response exactly as the OpenAI API would
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
resp := map[string]interface{}{
|
||||
"choices": []map[string]interface{}{
|
||||
{
|
||||
|
|
@ -35,14 +28,25 @@ func TestLLMClient_OpenRouterStyle_ExtractKeywords(t *testing.T) {
|
|||
"role": "assistant",
|
||||
"content": `{"translate":"dog has diarrhea","keyword":["diarrhea","digestive"],"animal":"dog"}`,
|
||||
},
|
||||
"index": 0,
|
||||
},
|
||||
},
|
||||
"id": "test-id",
|
||||
"object": "chat.completion",
|
||||
"created": 1717585613,
|
||||
"model": "meta-llama/test",
|
||||
"usage": map[string]interface{}{
|
||||
"prompt_tokens": 50,
|
||||
"completion_tokens": 20,
|
||||
"total_tokens": 70,
|
||||
},
|
||||
}
|
||||
json.NewEncoder(w).Encode(resp)
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
llm := NewLLMClient("test-key", ts.URL+"/v1/chat/completions", "meta-llama/test", nil)
|
||||
// Pass the server URL directly (not adding /v1 as that causes issues)
|
||||
llm := NewOpenAIClient("test-key", ts.URL, "meta-llama/test", nil)
|
||||
res, err := llm.ExtractKeywords(context.Background(), "kutya hasmenés")
|
||||
if err != nil {
|
||||
te(t, "unexpected error: %v", err)
|
||||
|
|
@ -66,18 +70,22 @@ func TestLLMClient_OpenRouterStyle_Error(t *testing.T) {
|
|||
appConfig.LLM.ExtractKeywordsPrompt = "Dummy {{.Message}}"
|
||||
|
||||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Simulate a rate limit error response from OpenAI API
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
json.NewEncoder(w).Encode(map[string]interface{}{
|
||||
"error": map[string]interface{}{
|
||||
"message": "Rate limit",
|
||||
"type": "rate_limit",
|
||||
"message": "Rate limit exceeded, please try again in 20ms",
|
||||
"type": "rate_limit_exceeded",
|
||||
"param": nil,
|
||||
"code": "rate_limit_exceeded",
|
||||
},
|
||||
})
|
||||
}))
|
||||
defer ts.Close()
|
||||
|
||||
llm := NewLLMClient("test-key", ts.URL+"/v1/chat/completions", "meta-llama/test", nil)
|
||||
// Use the same URL structure as the success test
|
||||
llm := NewOpenAIClient("test-key", ts.URL, "meta-llama/test", nil)
|
||||
_, err := llm.ExtractKeywords(context.Background(), "test")
|
||||
if err == nil || !contains(err.Error(), "Rate limit") {
|
||||
te(t, "expected rate limit error, got: %v", err)
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
|
@ -263,6 +266,69 @@ func (r *PGChatRepository) CreateUser(ctx context.Context, username, passwordHas
|
|||
return err
|
||||
}
|
||||
|
||||
// InsertSentenceEmbedding inserts a sentence embedding if not already present (unique index on visit_id,sentence)
|
||||
func (r *PGChatRepository) InsertSentenceEmbedding(ctx context.Context, visitID, sentence, translated string, embedding []float64) error {
|
||||
if r == nil || r.pool == nil {
|
||||
return nil
|
||||
}
|
||||
l := len(embedding)
|
||||
if l != 384 && l != 1536 {
|
||||
err := fmt.Errorf("unsupported embedding length %d (expected 384 or 1536)", l)
|
||||
logrus.WithError(err).Warn("skipping sentence embedding insert")
|
||||
return err
|
||||
}
|
||||
// Build array literal
|
||||
var b strings.Builder
|
||||
b.Grow(len(embedding)*8 + 2)
|
||||
b.WriteByte('{')
|
||||
for i, v := range embedding {
|
||||
if i > 0 {
|
||||
b.WriteByte(',')
|
||||
}
|
||||
b.WriteString(strconv.FormatFloat(v, 'f', -1, 64))
|
||||
}
|
||||
b.WriteByte('}')
|
||||
arrayLiteral := b.String()
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 6*time.Second)
|
||||
defer cancel()
|
||||
|
||||
var sqlStmt string
|
||||
if l == 384 {
|
||||
sqlStmt = `INSERT INTO sentence_embeddings (visit_id, sentence, translated, embedding_384)
|
||||
VALUES ($1,$2,$3,$4::float[])
|
||||
ON CONFLICT (visit_id, sentence) DO UPDATE
|
||||
SET embedding_384 = EXCLUDED.embedding_384,
|
||||
translated = COALESCE(sentence_embeddings.translated, EXCLUDED.translated)`
|
||||
} else { // 1536
|
||||
sqlStmt = `INSERT INTO sentence_embeddings (visit_id, sentence, translated, embedding_1536)
|
||||
VALUES ($1,$2,$3,$4::float[])
|
||||
ON CONFLICT (visit_id, sentence) DO UPDATE
|
||||
SET embedding_1536 = EXCLUDED.embedding_1536,
|
||||
translated = COALESCE(sentence_embeddings.translated, EXCLUDED.translated)`
|
||||
}
|
||||
_, err := r.pool.Exec(ctx, sqlStmt, visitID, sentence, translated, arrayLiteral)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Warn("failed to upsert sentence embedding (dual columns)")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// ExistsSentenceEmbedding checks if a sentence embedding exists
|
||||
func (r *PGChatRepository) ExistsSentenceEmbedding(ctx context.Context, visitID, sentence string) (bool, error) {
|
||||
if r == nil || r.pool == nil {
|
||||
return false, nil
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
var exists bool
|
||||
err := r.pool.QueryRow(ctx, `SELECT EXISTS (SELECT 1 FROM sentence_embeddings WHERE visit_id=$1 AND sentence=$2)`, visitID, sentence).Scan(&exists)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
// Close releases pool resources
|
||||
func (r *PGChatRepository) Close() {
|
||||
if r != nil && r.pool != nil {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,154 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var sentenceSplitRegex = regexp.MustCompile(`(?m)(?:[^.!?\n]+[.!?]|[^.!?\n]+$)`)
|
||||
|
||||
// configurable via env (seconds); defaults chosen for model cold start friendliness
|
||||
func envDuration(key string, def time.Duration) time.Duration {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
if d, err := time.ParseDuration(v); err == nil {
|
||||
return d
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// startSentenceEmbeddingBackfill launches a background goroutine that iterates all visits
|
||||
// and stores (visit_id, sentence, translated, embedding) records in sentence_embeddings table
|
||||
// if they do not already exist (relying on unique index ON CONFLICT DO NOTHING).
|
||||
func startSentenceEmbeddingBackfill(repo *PGChatRepository, llm LLMClientAPI, vdb *VisitDB) {
|
||||
if repo == nil || llm == nil || vdb == nil {
|
||||
logrus.Info("Sentence embedding backfill skipped (missing repo, llm or vdb)")
|
||||
return
|
||||
}
|
||||
if disable := strings.ToLower(os.Getenv("SENTENCE_BACKFILL_DISABLE")); disable == "1" || disable == "true" {
|
||||
logrus.Info("Sentence embedding backfill disabled via SENTENCE_BACKFILL_DISABLE env var")
|
||||
return
|
||||
}
|
||||
translateTimeout := envDuration("TRANSLATE_TIMEOUT", 45*time.Second)
|
||||
embeddingTimeout := envDuration("EMBEDDING_TIMEOUT", 45*time.Second)
|
||||
maxTranslateAttempts := 3
|
||||
maxEmbeddingAttempts := 3
|
||||
go func() {
|
||||
start := time.Now()
|
||||
logrus.WithFields(logrus.Fields{"translateTimeout": translateTimeout, "embeddingTimeout": embeddingTimeout}).Info("Sentence embedding backfill started")
|
||||
processed := 0
|
||||
inserted := 0
|
||||
skippedExisting := 0
|
||||
skippedDueToFailures := 0
|
||||
for _, visit := range vdb.visitsDB { // visitsDB accessible within package
|
||||
if strings.TrimSpace(visit.Visit) == "" {
|
||||
continue
|
||||
}
|
||||
sentences := extractSentences(visit.Visit)
|
||||
for _, s := range sentences {
|
||||
processed++
|
||||
trimmed := strings.TrimSpace(s)
|
||||
if len(trimmed) < 3 {
|
||||
continue
|
||||
}
|
||||
// Existence check before any LLM calls
|
||||
existsCtx, existsCancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
exists, err := repo.ExistsSentenceEmbedding(existsCtx, visit.ID, trimmed)
|
||||
existsCancel()
|
||||
if err != nil {
|
||||
logrus.WithError(err).Warnf("Exists check failed visit=%s sentence=%q", visit.ID, trimmed)
|
||||
} else if exists {
|
||||
skippedExisting++
|
||||
continue
|
||||
}
|
||||
|
||||
// Translation with retry/backoff
|
||||
var translated string
|
||||
translateErr := retry(maxTranslateAttempts, 0, func(at int) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), translateTimeout)
|
||||
defer cancel()
|
||||
resp, err := llm.TranslateToEnglish(ctx, trimmed)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Warnf("Translate attempt=%d failed visit=%s sentence=%q", at+1, visit.ID, trimmed)
|
||||
return err
|
||||
}
|
||||
translated = strings.TrimSpace(resp)
|
||||
return nil
|
||||
})
|
||||
if translateErr != nil || translated == "" {
|
||||
translated = trimmed // fallback keep original language
|
||||
}
|
||||
|
||||
// Embedding with retry/backoff (skip if translation totally failed with deadline each time)
|
||||
var emb []float64
|
||||
embErr := retry(maxEmbeddingAttempts, 0, func(at int) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), embeddingTimeout)
|
||||
defer cancel()
|
||||
vec, err := llm.GetEmbeddings(ctx, translated)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Warnf("Embeddings attempt=%d failed visit=%s sentence=%q", at+1, visit.ID, trimmed)
|
||||
return err
|
||||
}
|
||||
emb = vec
|
||||
return nil
|
||||
})
|
||||
if embErr != nil {
|
||||
skippedDueToFailures++
|
||||
continue
|
||||
}
|
||||
|
||||
persistCtx, pcancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
if err := repo.InsertSentenceEmbedding(persistCtx, visit.ID, trimmed, translated, emb); err == nil {
|
||||
inserted++
|
||||
}
|
||||
pcancel()
|
||||
// Throttle (configurable?)
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
logrus.Infof("Sentence embedding backfill complete processed=%d inserted=%d skipped_existing=%d skipped_failures=%d elapsed=%s", processed, inserted, skippedExisting, skippedDueToFailures, time.Since(start))
|
||||
}()
|
||||
}
|
||||
|
||||
// retry executes fn up to attempts times with exponential backoff starting at base (or 200ms if base==0)
|
||||
func retry(attempts int, base time.Duration, fn func(attempt int) error) error {
|
||||
if attempts <= 0 {
|
||||
return nil
|
||||
}
|
||||
if base <= 0 {
|
||||
base = 200 * time.Millisecond
|
||||
}
|
||||
var err error
|
||||
for a := 0; a < attempts; a++ {
|
||||
err = fn(a)
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
// backoff except after last attempt
|
||||
if a < attempts-1 {
|
||||
backoff := base << a // exponential
|
||||
time.Sleep(backoff)
|
||||
}
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// extractSentences splits a block of text into sentence-like units.
|
||||
func extractSentences(text string) []string {
|
||||
// First replace newlines with space to keep regex simpler, keep periods.
|
||||
normalized := strings.ReplaceAll(text, "\n", " ")
|
||||
matches := sentenceSplitRegex.FindAllString(normalized, -1)
|
||||
var out []string
|
||||
for _, m := range matches {
|
||||
m = strings.TrimSpace(m)
|
||||
if m != "" {
|
||||
out = append(out, m)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
Loading…
Reference in New Issue