-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
55 lines (47 loc) · 1.34 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
// Query Ollama using HTTP.
func queryAPI(prompt, server, model string, temperature float64) (string, error) {
// Prepare the request body.
body := map[string]interface{}{
"model": model,
"prompt": prompt,
"options": map[string]float64{
"temperature": float64(temperature),
},
"stream": false,
}
jsonBody, err := json.Marshal(body)
if err != nil {
return "", fmt.Errorf("error marshalling JSON body: %s", err.Error())
}
req, err := http.NewRequest("POST", server+"/api/generate", bytes.NewBuffer(jsonBody))
if err != nil {
return "", fmt.Errorf("error creating request: %s", err.Error())
}
req.Header.Set("Content-Type", "application/json")
// Send the request and get a response back.
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", fmt.Errorf("error sending request to server: %s", err.Error())
}
defer resp.Body.Close()
// Read the response body.
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %s", err.Error())
}
// Parse the response JSON body.
var respData map[string]interface{}
err = json.Unmarshal(respBody, &respData)
if err != nil {
return "", fmt.Errorf("error unmarshalling response: %s", err.Error())
}
return respData["response"].(string), nil
}