changing to use openrouter
This commit is contained in:
@@ -11,15 +11,23 @@ import (
|
||||
"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")
|
||||
func (s *llmService) Send(systemPrompt string, userPrompt string) (string, error) {
|
||||
apiURL := getEnvConfig("OPENAI_API_URL")
|
||||
if apiURL == "" {
|
||||
return "", errors.New("OPENAI_API_URL not found in environment")
|
||||
}
|
||||
|
||||
apiBase := "https://api.openai.com/v1"
|
||||
token := getEnvConfig("OPENAI_TOKEN")
|
||||
if token == "" {
|
||||
return "", errors.New("OPENAI_TOKEN not found in environment")
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/chat/completions", strings.TrimRight(apiBase, "/"))
|
||||
model := getEnvConfig("OPENAI_MODEL")
|
||||
if model == "" {
|
||||
return "", errors.New("OPENAI_MODEL not found in environment")
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/chat/completions", strings.TrimRight(apiURL, "/"))
|
||||
|
||||
reqBody := map[string]interface{}{
|
||||
"model": model,
|
||||
@@ -42,7 +50,7 @@ func (s *llmService) SendOpenAIRequest(systemPrompt string, userPrompt string, m
|
||||
return "", err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+apiKey)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{Timeout: 120 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
@@ -62,7 +70,7 @@ func (s *llmService) SendOpenAIRequest(systemPrompt string, userPrompt string, m
|
||||
}
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
lastErr = fmt.Errorf("OpenAI API error status %d: %s", resp.StatusCode, string(bodyBytes))
|
||||
lastErr = fmt.Errorf("API error status %d: %s", resp.StatusCode, string(bodyBytes))
|
||||
time.Sleep(time.Second * time.Duration(1<<i))
|
||||
continue
|
||||
}
|
||||
@@ -81,8 +89,8 @@ func (s *llmService) SendOpenAIRequest(systemPrompt string, userPrompt string, m
|
||||
if len(result.Choices) > 0 {
|
||||
return result.Choices[0].Message.Content, nil
|
||||
}
|
||||
return "", errors.New("empty response from OpenAI API")
|
||||
return "", errors.New("empty response from API")
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("failed to get OpenAI response after 5 attempts. Last error: %v", lastErr)
|
||||
return "", fmt.Errorf("failed to get response after 5 attempts. Last error: %v", lastErr)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user