initial
This commit is contained in:
88
mindforge.cronjob/internal/llm/openai.go
Normal file
88
mindforge.cronjob/internal/llm/openai.go
Normal file
@@ -0,0 +1,88 @@
|
||||
package llm
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func (s *llmService) SendOpenAIRequest(systemPrompt string, userPrompt string, model string) (string, error) {
|
||||
apiKey := getEnvConfig("OPENAI_API_KEY")
|
||||
if apiKey == "" {
|
||||
return "", errors.New("OPENAI_API_KEY not found in .env or environment")
|
||||
}
|
||||
|
||||
apiBase := "https://api.openai.com/v1"
|
||||
|
||||
url := fmt.Sprintf("%s/chat/completions", strings.TrimRight(apiBase, "/"))
|
||||
|
||||
reqBody := map[string]interface{}{
|
||||
"model": model,
|
||||
"messages": []map[string]string{
|
||||
{"role": "system", "content": systemPrompt},
|
||||
{"role": "user", "content": userPrompt},
|
||||
},
|
||||
}
|
||||
|
||||
jsonBody, err := json.Marshal(reqBody)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
var lastErr error
|
||||
// Retry up to 5 times (total 5 attempts)
|
||||
for i := 0; i < 5; i++ {
|
||||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonBody))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
|
||||
client := &http.Client{Timeout: 120 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
time.Sleep(time.Second * time.Duration(1<<i))
|
||||
continue
|
||||
}
|
||||
|
||||
bodyBytes, err := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if err != nil {
|
||||
lastErr = err
|
||||
time.Sleep(time.Second * time.Duration(1<<i))
|
||||
continue
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
lastErr = fmt.Errorf("OpenAI API error status %d: %s", resp.StatusCode, string(bodyBytes))
|
||||
time.Sleep(time.Second * time.Duration(1<<i))
|
||||
continue
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Choices []struct {
|
||||
Message struct {
|
||||
Content string `json:"content"`
|
||||
} `json:"message"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
if err := json.Unmarshal(bodyBytes, &result); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(result.Choices) > 0 {
|
||||
return result.Choices[0].Message.Content, nil
|
||||
}
|
||||
return "", errors.New("empty response from OpenAI API")
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("failed to get OpenAI response after 5 attempts. Last error: %v", lastErr)
|
||||
}
|
||||
Reference in New Issue
Block a user