♻️ Use raw API to access ollama

This commit is contained in:
2025-01-30 14:03:11 +08:00
parent 0f8dd2a709
commit 57709487f8
3 changed files with 40 additions and 23 deletions

View File

@ -1,15 +1,15 @@
package services
import (
"context"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/llms/ollama"
"github.com/tmc/langchaingo/prompts"
)
@ -26,16 +26,18 @@ func PingOllama() error {
return nil
}
var LargeModel *ollama.LLM
func ConnectOllama() error {
model := viper.GetString("ollama.model")
llm, err := ollama.New(ollama.WithModel(model), ollama.WithServerURL(viper.GetString("ollama.url")))
if err != nil {
return err
}
LargeModel = llm
return nil
type OllamaResponse struct {
Model string `json:"model"`
CreatedAt time.Time `json:"created_at"`
Response string `json:"response"`
Done bool `json:"done"`
Context []int64 `json:"context"`
TotalDuration int64 `json:"total_duration"`
LoadDuration int64 `json:"load_duration"`
PromptEvalCount int64 `json:"prompt_eval_count"`
PromptEvalDuration int64 `json:"prompt_eval_duration"`
EvalCount int64 `json:"eval_count"`
EvalDuration int64 `json:"eval_duration"`
}
func GenerateInsights(source string) (string, error) {
@ -50,13 +52,33 @@ func GenerateInsights(source string) (string, error) {
return "", fmt.Errorf("failed to format prompt: %v", err)
}
raw, _ := json.Marshal(map[string]any{
"model": viper.GetString("ollama.model"),
"prompt": inPrompt,
"stream": false,
})
start := time.Now()
completion, err := LargeModel.Call(context.Background(), inPrompt,
llms.WithTemperature(0.8),
)
url := viper.GetString("ollama.url") + "/api/generate"
resp, err := http.Post(url, "application/json", bytes.NewBuffer(raw))
if err != nil {
return "", fmt.Errorf("failed to generate insights: %v", err)
}
outRaw, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("failed to read response body: %v", err)
}
var response OllamaResponse
err = json.Unmarshal(outRaw, &response)
if err != nil {
return "", fmt.Errorf("failed to unmarshal response: %v", err)
}
took := time.Since(start)
log.Info().Dur("took", took).Msg("Insight generated successfully...")
return completion, err
return response.Response, err
}