package app import ( "bytes" "context" "encoding/json" "fmt" "io" "net/http" "strings" "time" ) func completeChat(ctx context.Context, base, key, model, prompt string) (string, error) { body, _ := json.Marshal(map[string]any{ "model": model, "messages": []map[string]string{ {"role": "user", "content": prompt}, }, }) req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(base, "/")+"/chat/completions", bytes.NewReader(body)) if err != nil { return "", err } req.Header.Set("Content-Type", "application/json") if key != "" { req.Header.Set("Authorization", "Bearer "+key) } client := &http.Client{Timeout: 60 * time.Second} resp, err := client.Do(req) if err != nil { return "", err } defer resp.Body.Close() raw, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20)) if resp.StatusCode >= 300 { return "", fmt.Errorf("llm %d: %s", resp.StatusCode, truncate(string(raw), 200)) } var out struct { Choices []struct { Message struct { Content string `json:"content"` } `json:"message"` } `json:"choices"` } if err := json.Unmarshal(raw, &out); err != nil { return "", err } if len(out.Choices) == 0 { return "", fmt.Errorf("llm: empty choices") } return out.Choices[0].Message.Content, nil } func truncate(s string, n int) string { if len(s) <= n { return s } return s[:n] }