package llm import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "strings" "time" ) const ( groqAPIURL = "https://api.groq.com/openai/v1/chat/completions" groqAPIModel = "openai/gpt-oss-120b" ) type groqProvider struct { apiKey string client *http.Client } func NewGroqProvider(apiKey string) *groqProvider { return &groqProvider{apiKey: apiKey, client: &http.Client{Timeout: 50 * time.Second}} } type groqMessage struct { Role string `json:"role"` Content string `json:"content"` } type groqAPIRequest struct { Model string `json:"model"` Messages []groqMessage `json:"messages"` Stream bool `json:"stream"` } type groqAPIResponse struct { ID string `json:"id"` Model string `json:"model"` Choices []groqChoice `json:"choices"` Usage *groqUsage `json:"usage,omitempty"` Error *groqAPIError `json:"error,omitempty"` } type groqChoice struct { FinishReason string `json:"finish_reason"` Message *groqMessage `json:"message,omitempty"` Text string `json:"text,omitempty"` } type groqUsage struct { PromptTokens int64 `json:"prompt_tokens"` CompletionTokens int64 `json:"completion_tokens"` TotalTokens int64 `json:"total_tokens"` } type groqAPIError struct { Message string `json:"message"` Code int `json:"code"` } func (p *groqProvider) Complete(ctx context.Context, systemPrompt, userPrompt string) (*Completion, error) { reqBody := groqAPIRequest{ Model: groqAPIModel, Messages: []groqMessage{ {Role: "system", Content: systemPrompt}, {Role: "user", Content: userPrompt}, }, Stream: false, } bodyBytes, err := json.Marshal(reqBody) if err != nil { return nil, fmt.Errorf("marshal request: %w", err) } req, err := http.NewRequestWithContext(ctx, http.MethodPost, groqAPIURL, bytes.NewReader(bodyBytes)) if err != nil { return nil, fmt.Errorf("build request: %w", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Authorization", "Bearer "+p.apiKey) resp, err := p.client.Do(req) if err != nil { return nil, fmt.Errorf("groq api request failed: %w", err) } defer resp.Body.Close() switch resp.StatusCode { case http.StatusOK: case http.StatusUnauthorized, http.StatusForbidden: return nil, ErrAuthFailed case http.StatusPaymentRequired, http.StatusTooManyRequests: return nil, ErrQuotaExceeded default: body, _ := io.ReadAll(io.LimitReader(resp.Body, 512)) return nil, fmt.Errorf("unexpected status code: %d, body: %s", resp.StatusCode, body) } var apiResp groqAPIResponse if err := json.NewDecoder(resp.Body).Decode(&apiResp); err != nil { return nil, fmt.Errorf("decode response: %w", err) } if apiResp.Error != nil { return nil, fmt.Errorf("groq api error (code %d): %s", apiResp.Error.Code, apiResp.Error.Message) } text := extractgroqText(apiResp) if strings.TrimSpace(text) == "" { return nil, fmt.Errorf("groq returned empty completion (model %q)", apiResp.Model) } completion := &Completion{Text: text, Model: apiResp.Model} if apiResp.Usage != nil { completion.Tokens = apiResp.Usage.TotalTokens } if completion.Model == "" { completion.Model = groqAPIModel } return completion, nil } func extractgroqText(resp groqAPIResponse) string { var sb strings.Builder for _, choice := range resp.Choices { if choice.Message != nil && choice.Message.Content != "" { sb.WriteString(choice.Message.Content) continue } if choice.Text != "" { sb.WriteString(choice.Text) } } return sb.String() }