|
| 1 | +package groq |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "os" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/dfanso/commit-msg/pkg/types" |
| 13 | +) |
| 14 | + |
| 15 | +type chatMessage struct { |
| 16 | + Role string `json:"role"` |
| 17 | + Content string `json:"content"` |
| 18 | +} |
| 19 | + |
| 20 | +type chatRequest struct { |
| 21 | + Model string `json:"model"` |
| 22 | + Messages []chatMessage `json:"messages"` |
| 23 | + Temperature float64 `json:"temperature"` |
| 24 | + MaxTokens int `json:"max_tokens"` |
| 25 | +} |
| 26 | + |
| 27 | +type chatChoice struct { |
| 28 | + Message chatMessage `json:"message"` |
| 29 | +} |
| 30 | + |
| 31 | +type chatResponse struct { |
| 32 | + Choices []chatChoice `json:"choices"` |
| 33 | +} |
| 34 | + |
| 35 | +// defaultModel uses Groq's recommended general-purpose model as of Oct 2025. |
| 36 | +// If Groq updates their defaults again, override via GROQ_MODEL. |
| 37 | +const defaultModel = "llama-3.3-70b-versatile" |
| 38 | + |
| 39 | +var ( |
| 40 | + // allow overrides in tests |
| 41 | + baseURL = "https://api.groq.com/openai/v1/chat/completions" |
| 42 | + httpClient = &http.Client{Timeout: 30 * time.Second} |
| 43 | +) |
| 44 | + |
| 45 | +// GenerateCommitMessage calls Groq's OpenAI-compatible chat completions API. |
| 46 | +func GenerateCommitMessage(_ *types.Config, changes string, apiKey string) (string, error) { |
| 47 | + if changes == "" { |
| 48 | + return "", fmt.Errorf("no changes provided for commit message generation") |
| 49 | + } |
| 50 | + |
| 51 | + prompt := fmt.Sprintf("%s\n\n%s", types.CommitPrompt, changes) |
| 52 | + |
| 53 | + model := os.Getenv("GROQ_MODEL") |
| 54 | + if model == "" { |
| 55 | + model = defaultModel |
| 56 | + } |
| 57 | + |
| 58 | + payload := chatRequest{ |
| 59 | + Model: model, |
| 60 | + Temperature: 0.2, |
| 61 | + MaxTokens: 200, |
| 62 | + Messages: []chatMessage{ |
| 63 | + {Role: "system", Content: "You are an assistant that writes clear, concise git commit messages."}, |
| 64 | + {Role: "user", Content: prompt}, |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + body, err := json.Marshal(payload) |
| 69 | + if err != nil { |
| 70 | + return "", fmt.Errorf("failed to marshal Groq request: %w", err) |
| 71 | + } |
| 72 | + |
| 73 | + endpoint := baseURL |
| 74 | + if customEndpoint := os.Getenv("GROQ_API_URL"); customEndpoint != "" { |
| 75 | + endpoint = customEndpoint |
| 76 | + } |
| 77 | + |
| 78 | + req, err := http.NewRequest(http.MethodPost, endpoint, bytes.NewBuffer(body)) |
| 79 | + if err != nil { |
| 80 | + return "", fmt.Errorf("failed to create Groq request: %w", err) |
| 81 | + } |
| 82 | + |
| 83 | + req.Header.Set("Content-Type", "application/json") |
| 84 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiKey)) |
| 85 | + |
| 86 | + resp, err := httpClient.Do(req) |
| 87 | + if err != nil { |
| 88 | + return "", fmt.Errorf("failed to call Groq API: %w", err) |
| 89 | + } |
| 90 | + defer resp.Body.Close() |
| 91 | + |
| 92 | + responseBody, err := io.ReadAll(resp.Body) |
| 93 | + if err != nil { |
| 94 | + return "", fmt.Errorf("failed to read Groq response: %w", err) |
| 95 | + } |
| 96 | + |
| 97 | + if resp.StatusCode != http.StatusOK { |
| 98 | + return "", fmt.Errorf("groq API returned status %d: %s", resp.StatusCode, string(responseBody)) |
| 99 | + } |
| 100 | + |
| 101 | + var completion chatResponse |
| 102 | + if err := json.Unmarshal(responseBody, &completion); err != nil { |
| 103 | + return "", fmt.Errorf("failed to decode Groq response: %w", err) |
| 104 | + } |
| 105 | + |
| 106 | + if len(completion.Choices) == 0 || completion.Choices[0].Message.Content == "" { |
| 107 | + return "", fmt.Errorf("groq API returned empty response") |
| 108 | + } |
| 109 | + |
| 110 | + return completion.Choices[0].Message.Content, nil |
| 111 | +} |
0 commit comments