package examples import ( "bytes" "context" "encoding/json" "errors" "io" "net/http" "time" ) // AskLocalOllama is for public, non-secret explanatory prompts only. func AskLocalOllama(ctx context.Context, model, prompt string) (string, error) { payload, _ := json.Marshal(map[string]any{"model": model, "messages": []map[string]string{{"role": "user", "content": prompt}}, "stream": false}) req, err := http.NewRequestWithContext(ctx, "POST", "http://127.0.0.1:11434/api/chat", bytes.NewReader(payload)) if err != nil { return "", err } req.Header.Set("Content-Type", "application/json") c := http.Client{Timeout: 15 * time.Second} res, err := c.Do(req) if err != nil { return "", err } defer res.Body.Close() if res.StatusCode != 200 { return "", errors.New("ollama request failed") } var v struct { Message struct { Content string `json:"content"` } `json:"message"` } if err = json.NewDecoder(io.LimitReader(res.Body, 1<<20)).Decode(&v); err != nil { return "", err } return v.Message.Content, nil }