package ai import ( "bytes" "context" "encoding/json" "errors" "fmt" "io" "net/http" neturl "net/url" ) type geminiProvider struct { apiKey string model string client *http.Client } func newGemini(apiKey, model string) *geminiProvider { if model == "" { model = "gemini-2.0-flash" } return &geminiProvider{ apiKey: apiKey, model: model, client: &http.Client{}, } } func (p *geminiProvider) Name() string { return "gemini" } func (p *geminiProvider) Summarize(ctx context.Context, prompt string, opts GenOptions) (string, error) { if len(opts.JSONSchema) > 0 { return "", errStructuredOutputUnsupported } url := fmt.Sprintf( "https://generativelanguage.googleapis.com/v1beta/models/%s:generateContent?key=%s", neturl.PathEscape(p.model), neturl.QueryEscape(p.apiKey), ) body := map[string]interface{}{ "contents": []map[string]interface{}{ {"parts": []map[string]string{{"text": prompt}}}, }, } if opts.MaxOutputTokens > 0 { body["generationConfig"] = map[string]int{"maxOutputTokens": opts.MaxOutputTokens} } b, _ := json.Marshal(body) req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b)) if err != nil { return "", err } req.Header.Set("Content-Type", "application/json") resp, err := p.client.Do(req) if err != nil { return "", err } defer resp.Body.Close() raw, err := readGenerationResponse(resp) if err != nil { return "", err } var result struct { Candidates []struct { FinishReason string `json:"finishReason"` Content struct { Parts []struct { Text string `json:"text"` } `json:"parts"` } `json:"content"` } `json:"candidates"` } if err := json.Unmarshal(raw, &result); err != nil { return "", err } if len(result.Candidates) == 0 || len(result.Candidates[0].Content.Parts) == 0 { return "", nil } if opts.RequireComplete && result.Candidates[0].FinishReason != "STOP" { return "", errGenerationIncomplete } return result.Candidates[0].Content.Parts[0].Text, nil } func (p *geminiProvider) ListModels(ctx context.Context) ([]string, error) { url := fmt.Sprintf("https://generativelanguage.googleapis.com/v1beta/models?key=%s", neturl.QueryEscape(p.apiKey)) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err } resp, err := p.client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() raw, _ := io.ReadAll(resp.Body) if resp.StatusCode != http.StatusOK { return nil, &summaryHTTPError{status: resp.StatusCode, err: errors.New("model discovery failed")} } var result struct { Models []struct { Name string `json:"name"` SupportedMethods []string `json:"supportedGenerationMethods"` } `json:"models"` } if err := json.Unmarshal(raw, &result); err != nil { return nil, err } var names []string for _, m := range result.Models { for _, method := range m.SupportedMethods { if method == "generateContent" { // name is "models/gemini-xxx", strip prefix id := m.Name if len(id) > 7 { id = id[7:] // strip "models/" } names = append(names, id) break } } } return names, nil }