feat: switch AI provider from Groq to OpenAI
Replace Groq/Llama with OpenAI API: - Text model: gpt-4o-mini - Vision model: gpt-4o - Rename GEMINI_API_KEY → OPENAI_API_KEY env var - Rename callGroq → callOpenAI, update all related constants and comments Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -11,19 +11,19 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
// groqAPIURL is the Groq OpenAI-compatible endpoint (free tier, no billing required).
|
||||
groqAPIURL = "https://api.groq.com/openai/v1/chat/completions"
|
||||
// openaiAPIURL is the OpenAI chat completions endpoint.
|
||||
openaiAPIURL = "https://api.openai.com/v1/chat/completions"
|
||||
|
||||
// groqModel is the default text generation model.
|
||||
groqModel = "llama-3.3-70b-versatile"
|
||||
// openaiModel is the default text generation model.
|
||||
openaiModel = "gpt-4o-mini"
|
||||
|
||||
// groqVisionModel supports image inputs in OpenAI vision format.
|
||||
groqVisionModel = "meta-llama/llama-4-scout-17b-16e-instruct"
|
||||
// openaiVisionModel supports image inputs.
|
||||
openaiVisionModel = "gpt-4o"
|
||||
|
||||
maxRetries = 3
|
||||
)
|
||||
|
||||
// Client is an HTTP client for the Groq LLM API (OpenAI-compatible).
|
||||
// Client is an HTTP client for the OpenAI API.
|
||||
type Client struct {
|
||||
apiKey string
|
||||
httpClient *http.Client
|
||||
@@ -39,9 +39,9 @@ func NewClient(apiKey string) *Client {
|
||||
}
|
||||
}
|
||||
|
||||
// generateContent sends text messages to the text-only model.
|
||||
// generateContent sends text messages to the text model.
|
||||
func (c *Client) generateContent(ctx context.Context, messages []map[string]string) (string, error) {
|
||||
return c.callGroq(ctx, groqModel, 0.7, messages)
|
||||
return c.callOpenAI(ctx, openaiModel, 0.7, messages)
|
||||
}
|
||||
|
||||
// generateVisionContent sends an image + text prompt to the vision model.
|
||||
@@ -68,12 +68,12 @@ func (c *Client) generateVisionContent(ctx context.Context, prompt, imageBase64,
|
||||
},
|
||||
},
|
||||
}
|
||||
return c.callGroq(ctx, groqVisionModel, 0.1, messages)
|
||||
return c.callOpenAI(ctx, openaiVisionModel, 0.1, messages)
|
||||
}
|
||||
|
||||
// callGroq is the shared HTTP transport for all Groq requests.
|
||||
// callOpenAI is the shared HTTP transport for all OpenAI requests.
|
||||
// messages can be []map[string]string (text) or []any (vision with image content).
|
||||
func (c *Client) callGroq(ctx context.Context, model string, temperature float64, messages any) (string, error) {
|
||||
func (c *Client) callOpenAI(ctx context.Context, model string, temperature float64, messages any) (string, error) {
|
||||
body := map[string]any{
|
||||
"model": model,
|
||||
"temperature": temperature,
|
||||
@@ -85,7 +85,7 @@ func (c *Client) callGroq(ctx context.Context, model string, temperature float64
|
||||
return "", fmt.Errorf("marshal request: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, groqAPIURL, bytes.NewReader(bodyBytes))
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, openaiAPIURL, bytes.NewReader(bodyBytes))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create request: %w", err)
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func (c *Client) callGroq(ctx context.Context, model string, temperature float64
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
raw, _ := io.ReadAll(resp.Body)
|
||||
return "", fmt.Errorf("groq API error %d: %s", resp.StatusCode, string(raw))
|
||||
return "", fmt.Errorf("openai API error %d: %s", resp.StatusCode, string(raw))
|
||||
}
|
||||
|
||||
var result struct {
|
||||
@@ -114,7 +114,7 @@ func (c *Client) callGroq(ctx context.Context, model string, temperature float64
|
||||
return "", fmt.Errorf("decode response: %w", err)
|
||||
}
|
||||
if len(result.Choices) == 0 {
|
||||
return "", fmt.Errorf("empty response from Groq")
|
||||
return "", fmt.Errorf("empty response from OpenAI")
|
||||
}
|
||||
return result.Choices[0].Message.Content, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user