main
c8b91bc ยท 4 months ago 7 commits
 1package ai
 2
 3import (
 4	"bytes"
 5	"encoding/json"
 6	"fmt"
 7	"io"
 8	"net/http"
 9	"strings"
10	"time"
11)
12
13var client = &http.Client{
14	Timeout: 10 * time.Second,
15}
16
17func Ask(prompt string) (string, error) {
18	if cached, ok := getCached(prompt); ok {
19		return cached, nil
20	}
21
22	payload := map[string]any{
23		"model":  "llama3-chatqa:8b",
24		"prompt": prompt,
25		"stream": false,
26		"think":  false,
27	}
28
29	body, err := json.Marshal(payload)
30	if err != nil {
31		return "", fmt.Errorf("marshal: %w", err)
32	}
33
34	resp, err := client.Post(
35		"http://localhost:11434/api/generate",
36		"application/json",
37		bytes.NewReader(body),
38	)
39	if err != nil {
40		return "", fmt.Errorf("http post: %w", err)
41	}
42	defer resp.Body.Close()
43
44	respBody, err := io.ReadAll(resp.Body)
45	if err != nil {
46		return "", fmt.Errorf("read body: %w", err)
47	}
48
49	var result struct {
50		Response string `json:"response"`
51	}
52
53	if err := json.Unmarshal(respBody, &result); err != nil {
54		return "", fmt.Errorf("unmarshal: %w", err)
55	}
56	answer := strings.TrimSpace(result.Response)
57	setCached(prompt, answer)
58	return answer, nil
59}