Signed-off-by: kaedwen <kaedwen@heinrich.blue>
This commit is contained in:
1 file changed
+16
-4
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/pheinrich/aitrade/pkg/config"
|
"github.com/pheinrich/aitrade/pkg/config"
|
||||||
"github.com/pheinrich/aitrade/pkg/model"
|
"github.com/pheinrich/aitrade/pkg/model"
|
||||||
@@ -167,7 +168,16 @@ JSON Response:`, article.Title, article.Content)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// callOllamaAPI sends request to Ollama API and parses response
|
// callOllamaAPI sends request to Ollama API and parses response
|
||||||
func (l *LLMScorer) callOllamaAPI(ctx context.Context, prompt string) (*SentimentResponse, error) {
|
func (l *LLMScorer) callOllamaAPI(ctx context.Context, prompt string) (sentiment *SentimentResponse, err error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
defer func() {
|
||||||
|
duration := time.Since(startTime)
|
||||||
|
l.logger.Info("LLM request completed",
|
||||||
|
slog.Duration("duration", duration),
|
||||||
|
slog.String("model", l.cfg.ModelName),
|
||||||
|
slog.Bool("success", err == nil))
|
||||||
|
}()
|
||||||
|
|
||||||
// Build request
|
// Build request
|
||||||
reqBody := LLMRequest{
|
reqBody := LLMRequest{
|
||||||
Model: l.cfg.ModelName,
|
Model: l.cfg.ModelName,
|
||||||
@@ -197,7 +207,8 @@ func (l *LLMScorer) callOllamaAPI(ctx context.Context, prompt string) (*Sentimen
|
|||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
if resp.StatusCode != http.StatusOK {
|
||||||
body, _ := io.ReadAll(resp.Body)
|
body, _ := io.ReadAll(resp.Body)
|
||||||
return nil, fmt.Errorf("Ollama API returned status %d: %s", resp.StatusCode, string(body))
|
err = fmt.Errorf("Ollama API returned status %d: %s", resp.StatusCode, string(body))
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read response
|
// Read response
|
||||||
@@ -207,12 +218,13 @@ func (l *LLMScorer) callOllamaAPI(ctx context.Context, prompt string) (*Sentimen
|
|||||||
}
|
}
|
||||||
|
|
||||||
var llmResp LLMResponse
|
var llmResp LLMResponse
|
||||||
if err := json.Unmarshal(body, &llmResp); err != nil {
|
if err = json.Unmarshal(body, &llmResp); err != nil {
|
||||||
return nil, fmt.Errorf("failed to parse Ollama response: %w", err)
|
return nil, fmt.Errorf("failed to parse Ollama response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse sentiment from LLM response
|
// Parse sentiment from LLM response
|
||||||
return l.parseSentiment(llmResp.Response)
|
sentiment, err = l.parseSentiment(llmResp.Response)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// parseSentiment extracts structured sentiment from LLM text response
|
// parseSentiment extracts structured sentiment from LLM text response
|
||||||
|
|||||||
Reference in new issue
Block a user