SendPrompt sends a prompt to the GitHub Models API.
(ctx context.Context, prompt string, history []models.Message, maxTokens int)
| 85 | |
| 86 | // SendPrompt sends a prompt to the GitHub Models API. |
| 87 | func (c *GitHubModelsClient) SendPrompt(ctx context.Context, prompt string, history []models.Message, maxTokens int) (string, error) { |
| 88 | effectiveMaxTokens := maxTokens |
| 89 | if effectiveMaxTokens <= 0 { |
| 90 | effectiveMaxTokens = c.getMaxTokens() |
| 91 | } |
| 92 | |
| 93 | messages := []map[string]interface{}{} |
| 94 | for _, msg := range history { |
| 95 | role := strings.ToLower(strings.TrimSpace(msg.Role)) |
| 96 | switch role { |
| 97 | case "system", "user", "assistant": |
| 98 | default: |
| 99 | role = "user" |
| 100 | } |
| 101 | messages = append(messages, map[string]interface{}{ |
| 102 | "role": role, |
| 103 | "content": visionwire.OpenAIContent(msg.Content, msg.Images), |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | if len(history) == 0 || history[len(history)-1].Role != "user" || history[len(history)-1].Content != prompt { |
| 108 | if strings.TrimSpace(prompt) != "" { |
| 109 | messages = append(messages, map[string]interface{}{ |
| 110 | "role": "user", |
| 111 | "content": prompt, |
| 112 | }) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | payload := map[string]interface{}{ |
| 117 | "model": c.model, |
| 118 | "messages": messages, |
| 119 | "max_tokens": effectiveMaxTokens, |
| 120 | } |
| 121 | |
| 122 | jsonValue, err := json.Marshal(payload) |
| 123 | if err != nil { |
| 124 | return "", fmt.Errorf("%s: %w", i18n.T("llm.error.marshal_payload_for", "GitHub Models"), err) |
| 125 | } |
| 126 | |
| 127 | start := time.Now() |
| 128 | client.LogRequestStart(c.logger, "GITHUB_MODELS", c.model, |
| 129 | zap.Int("payload_bytes", len(jsonValue)), |
| 130 | zap.Int("history_len", len(history)), |
| 131 | zap.Int("max_tokens", effectiveMaxTokens), |
| 132 | ) |
| 133 | |
| 134 | response, err := utils.Retry(ctx, c.logger, c.maxAttempts, c.backoff, func(ctx context.Context) (string, error) { |
| 135 | resp, err := auth.DoWithRefresh(ctx, c.provider, func(token string) (*http.Response, error) { |
| 136 | req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.getAPIURL(), utils.NewJSONReader(jsonValue)) |
| 137 | if err != nil { |
| 138 | return nil, fmt.Errorf("%s: %w", i18n.T("llm.error.create_request_for", "GitHub Models"), err) |
| 139 | } |
| 140 | req.Header.Set("Content-Type", "application/json") |
| 141 | req.Header.Set("Authorization", "Bearer "+token) |
| 142 | return c.client.Do(req) |
| 143 | }) |
| 144 | if err != nil { |