|
| 1 | +// Package llm contains utilities for working with GitHub Models API |
| 2 | +package llm |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + _ "embed" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "net/http" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | + |
| 14 | + "github.com/cli/go-gh/v2/pkg/auth" |
| 15 | + "gopkg.in/yaml.v3" |
| 16 | +) |
| 17 | + |
| 18 | +//go:embed commitmsg.prompt.yml |
| 19 | +var standupPromptYAML []byte |
| 20 | + |
| 21 | +// PromptConfig represents the structure of the prompt configuration file |
| 22 | +// It includes the model parameters and the messages to be sent to the model. |
| 23 | +type PromptConfig struct { |
| 24 | + Name string `yaml:"name"` |
| 25 | + Description string `yaml:"description"` |
| 26 | + Model string `yaml:"model"` |
| 27 | + ModelParameters ModelParameters `yaml:"modelParameters"` |
| 28 | + Messages []PromptMessage `yaml:"messages"` |
| 29 | +} |
| 30 | + |
| 31 | +// ModelParameters defines the parameters for the model |
| 32 | +type ModelParameters struct { |
| 33 | + Temperature float64 `yaml:"temperature"` |
| 34 | + TopP float64 `yaml:"topP"` |
| 35 | +} |
| 36 | + |
| 37 | +// PromptMessage represents a single message in the prompt configuration |
| 38 | +type PromptMessage struct { |
| 39 | + Role string `yaml:"role"` |
| 40 | + Content string `yaml:"content"` |
| 41 | +} |
| 42 | + |
| 43 | +// Request represents the structure of the request to the GitHub Models API |
| 44 | +type Request struct { |
| 45 | + Messages []Message `json:"messages"` |
| 46 | + Model string `json:"model"` |
| 47 | + Temperature float64 `json:"temperature"` |
| 48 | + TopP float64 `json:"top_p"` |
| 49 | + Stream bool `json:"stream"` |
| 50 | +} |
| 51 | + |
| 52 | +// Message represents a single message in the request to the GitHub Models API |
| 53 | +type Message struct { |
| 54 | + Role string `json:"role"` |
| 55 | + Content string `json:"content"` |
| 56 | +} |
| 57 | + |
| 58 | +// Response represents the structure of the response from the GitHub Models API |
| 59 | +type Response struct { |
| 60 | + Choices []struct { |
| 61 | + Message struct { |
| 62 | + Content string `json:"content"` |
| 63 | + } `json:"message"` |
| 64 | + } `json:"choices"` |
| 65 | +} |
| 66 | + |
| 67 | +// Client is a wrapper around the GitHub Models API client |
| 68 | +type Client struct { |
| 69 | + token string |
| 70 | +} |
| 71 | + |
| 72 | +// NewClient initializes a new GitHub Models API client |
| 73 | +// It retrieves the GitHub token from the environment installed by the `gh` CLI tool. |
| 74 | +func NewClient() (*Client, error) { |
| 75 | + fmt.Print(" Checking GitHub token... ") |
| 76 | + |
| 77 | + host, _ := auth.DefaultHost() |
| 78 | + token, _ := auth.TokenForHost(host) // check GH_TOKEN, GITHUB_TOKEN, keychain, etc |
| 79 | + |
| 80 | + if token == "" { |
| 81 | + fmt.Println("Failed") |
| 82 | + return nil, fmt.Errorf("no GitHub token found, please run 'gh auth login' to authenticate") |
| 83 | + } |
| 84 | + fmt.Println("Done") |
| 85 | + |
| 86 | + return &Client{token: token}, nil |
| 87 | +} |
| 88 | + |
| 89 | +// GenerateCommitMessage generates a commit message based on the provided changes summary |
| 90 | +func (c *Client) GenerateCommitMessage(changesSummary string) (string, error) { |
| 91 | + fmt.Print(" Loading prompt configuration... ") |
| 92 | + promptConfig, err := loadPromptConfig() |
| 93 | + if err != nil { |
| 94 | + fmt.Println("Failed") |
| 95 | + return "", err |
| 96 | + } |
| 97 | + fmt.Println("Done") |
| 98 | + |
| 99 | + selectedModel := promptConfig.Model |
| 100 | + |
| 101 | + // Build messages from the prompt config, replacing template variables |
| 102 | + messages := make([]Message, len(promptConfig.Messages)) |
| 103 | + for i, msg := range promptConfig.Messages { |
| 104 | + content := msg.Content |
| 105 | + // Replace the {{changes}} template variable |
| 106 | + content = strings.ReplaceAll(content, "{{changes}}", changesSummary) |
| 107 | + |
| 108 | + messages[i] = Message{ |
| 109 | + Role: msg.Role, |
| 110 | + Content: content, |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + request := Request{ |
| 115 | + Messages: messages, |
| 116 | + Model: selectedModel, |
| 117 | + Temperature: promptConfig.ModelParameters.Temperature, |
| 118 | + TopP: promptConfig.ModelParameters.TopP, |
| 119 | + Stream: false, |
| 120 | + } |
| 121 | + |
| 122 | + fmt.Printf(" Calling GitHub Models API (%s)... ", selectedModel) |
| 123 | + response, err := c.callGitHubModels(request) |
| 124 | + if err != nil { |
| 125 | + fmt.Println("Failed") |
| 126 | + return "", err |
| 127 | + } |
| 128 | + fmt.Println("Done") |
| 129 | + |
| 130 | + if len(response.Choices) == 0 { |
| 131 | + return "", fmt.Errorf("no response generated from the model") |
| 132 | + } |
| 133 | + |
| 134 | + return strings.TrimSpace(response.Choices[0].Message.Content), nil |
| 135 | +} |
| 136 | + |
| 137 | +func loadPromptConfig() (*PromptConfig, error) { |
| 138 | + var config PromptConfig |
| 139 | + err := yaml.Unmarshal(standupPromptYAML, &config) |
| 140 | + if err != nil { |
| 141 | + return nil, fmt.Errorf("failed to parse prompt configuration: %w", err) |
| 142 | + } |
| 143 | + return &config, nil |
| 144 | +} |
| 145 | + |
| 146 | +// callGitHubModels makes the API call to GitHub Models |
| 147 | +func (c *Client) callGitHubModels(request Request) (*Response, error) { |
| 148 | + jsonData, err := json.Marshal(request) |
| 149 | + if err != nil { |
| 150 | + return nil, fmt.Errorf("failed to marshal request: %w", err) |
| 151 | + } |
| 152 | + |
| 153 | + req, err := http.NewRequest("POST", "https://models.github.ai/inference/chat/completions", bytes.NewBuffer(jsonData)) |
| 154 | + if err != nil { |
| 155 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 156 | + } |
| 157 | + |
| 158 | + req.Header.Set("Content-Type", "application/json") |
| 159 | + req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token)) |
| 160 | + |
| 161 | + client := &http.Client{Timeout: 30 * time.Second} |
| 162 | + resp, err := client.Do(req) |
| 163 | + if err != nil { |
| 164 | + return nil, fmt.Errorf("failed to make request: %w", err) |
| 165 | + } |
| 166 | + defer resp.Body.Close() |
| 167 | + |
| 168 | + body, err := io.ReadAll(resp.Body) |
| 169 | + if err != nil { |
| 170 | + return nil, fmt.Errorf("failed to read response: %w", err) |
| 171 | + } |
| 172 | + |
| 173 | + if resp.StatusCode != http.StatusOK { |
| 174 | + return nil, fmt.Errorf("API request failed with status %d: %s", resp.StatusCode, string(body)) |
| 175 | + } |
| 176 | + |
| 177 | + var response Response |
| 178 | + err = json.Unmarshal(body, &response) |
| 179 | + if err != nil { |
| 180 | + return nil, fmt.Errorf("failed to unmarshal response: %w", err) |
| 181 | + } |
| 182 | + |
| 183 | + return &response, nil |
| 184 | +} |
0 commit comments