-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
65 lines (56 loc) · 1.08 KB
/
utils.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
package ollama
import (
json2 "encoding/json"
"strings"
)
func bodyTo[T any](body []byte) (*T, error) {
var response T
err := json2.Unmarshal(body, &response)
if err != nil {
return nil, err
}
return &response, nil
}
func pointer[T any](t T) *T {
return &t
}
func buildUrl(baseUrl, path string) string {
url := baseUrl
if !strings.HasSuffix(url, "/") && !strings.HasPrefix(path, "/") {
url += "/"
} else if strings.HasSuffix(url, "/") && strings.HasPrefix(path, "/") {
url = url[0 : len(url)-1]
}
url += path
return url
}
func splitJSONObjects(data []byte) [][]byte {
var results [][]byte
var stack []byte
var start, end int
var inString bool
for i := 0; i < len(data); i++ {
switch data[i] {
case '{':
if !inString {
if len(stack) == 0 {
start = i
}
stack = append(stack, '{')
}
case '}':
if !inString {
stack = stack[:len(stack)-1]
if len(stack) == 0 {
end = i + 1
results = append(results, data[start:end])
}
}
case '"':
if i == 0 || data[i-1] != '\\' {
inString = !inString
}
}
}
return results
}