-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
132 lines (114 loc) · 3.14 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package gollama
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
// apiGet sends a GET request to the specified path on the Ollama server,
// and unmarshals the response into the given interface.
//
// The URL is built by joining the server address with the path.
//
// The Ollama server must respond with a JSON object that can be
// unmarshaled into the given interface.
//
// The Verbose flag is respected, and the URL is printed if it is set.
//
// The HTTPTimeout is used as the timeout for the HTTP request.
//
// If the request fails, or the response cannot be unmarshaled, an error
// is returned.
func (c *Gollama) apiGet(ctx context.Context, path string, v interface{}) error {
url, _ := url.JoinPath(c.ServerAddr, path)
if c.Verbose {
fmt.Printf("Sending a request to GET %s\n", url)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return err
}
HTTPClient := &http.Client{
Timeout: c.HTTPTimeout,
}
resp, err := HTTPClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
return json.NewDecoder(resp.Body).Decode(v)
}
// apiPost sends a POST request to the specified path on the Ollama server,
// and unmarshals the response into the given interface.
//
// The URL is built by joining the server address with the path.
//
// The Ollama server must respond with a JSON object that can be
// unmarshaled into the given interface.
//
// The Verbose flag is respected, and the URL is printed if it is set.
//
// If the request fails, or the response cannot be unmarshaled, an error
// is returned.
//
// The HTTPTimeout is used as the timeout for the HTTP request, except for
// requests to the /api/pull endpoint, which is given the PullTimeout.
func (c *Gollama) apiPost(ctx context.Context, path string, v interface{}, data interface{}) error {
url, _ := url.JoinPath(c.ServerAddr, path)
if c.Verbose {
fmt.Printf("Sending a request to POST %s\n", url)
}
reqBytes, err := json.Marshal(data)
if err != nil {
if c.Verbose {
fmt.Printf("Failed to marshal request data: %s\n", err)
}
return err
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewBuffer(reqBytes))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
HTTPClient := &http.Client{
Timeout: c.HTTPTimeout,
}
if path == "/api/pull" {
HTTPClient.Timeout = c.PullTimeout
}
resp, err := HTTPClient.Do(req)
if err != nil {
if c.Verbose {
fmt.Printf("Failed to send request: %s\n", err)
}
return err
}
defer resp.Body.Close()
var bodyBytes []byte
bodyBytes, err = io.ReadAll(resp.Body)
if err != nil {
if c.Verbose {
fmt.Println("Failed to read response body error:", err)
}
return err
}
resp.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
err = json.NewDecoder(resp.Body).Decode(v)
if err != nil {
if c.Verbose {
fmt.Printf("Failed to unmarshal response body: %s\n", err)
}
return err
}
if c.Verbose {
if bodyBytes != nil {
fmt.Printf("Response body: %s\n", string(bodyBytes))
} else {
fmt.Println("Response body logging skipped due to error or disabled verbose mode.")
}
}
return nil
}