-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
159 lines (134 loc) · 3.37 KB
/
client.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package glu
import (
"bufio"
"bytes"
"encoding/json"
"io"
"net/http"
"strings"
)
type MessageDecoder interface {
Decode([]byte) Message
}
type StreamMessageDecoder interface {
Decode([]byte) StreamMessage
}
type ClientOption func(c *Client)
func WithAPIKey(apiKey string) ClientOption {
return func(c *Client) {
c.config.APIKey = apiKey
}
}
func WithJSONResponse() ClientOption {
return func(c *Client) {
c.config.ResponseFormat = "json"
}
}
func WithTemperature(tmp float32) ClientOption {
return func(c *Client) {
c.config.Temperature = tmp
}
}
type ClientConfig struct {
RequestURL string
APIKey string
Model string
ResponseFormat string
Stream bool
Temperature float32
}
type RequestBuilder func(config ClientConfig, thread *Thread) (*http.Request, error)
type EmbeddingRequestBuilder func(config ClientConfig, text string) (*http.Request, error)
type EmbeddingDecoder interface {
Decode([]byte) Embedding
}
type Client struct {
config ClientConfig
BuildRequest RequestBuilder
BuildEmbeddingRequest EmbeddingRequestBuilder
MessageDecoder MessageDecoder
EmbeddingDecoder EmbeddingDecoder
StreamingMessageDecoder StreamMessageDecoder
}
func NewClient(options ...ClientOption) *Client {
c := &Client{}
for _, option := range options {
option(c)
}
return c
}
func (c *Client) Generate(thread *Thread) error {
req, err := c.BuildRequest(c.config, thread)
if err != nil {
return err
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
thread.AddMessages(c.MessageDecoder.Decode(respBody))
return nil
}
func (c *Client) GenerateStream(thread *Thread, handler StreamMessageHandler) error {
c.config.Stream = true
req, err := c.BuildRequest(c.config, thread)
if err != nil {
return err
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
var sb strings.Builder
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Bytes()
line = bytes.TrimPrefix(line, []byte("data: "))
// Attempt to parse the line as JSON
var rawMessage json.RawMessage
err := json.Unmarshal(line, &rawMessage)
if err != nil {
continue // Skip lines that cannot be parsed as JSON
}
streamMessage := c.StreamingMessageDecoder.Decode(rawMessage)
sb.WriteString(streamMessage.Message.Content)
if err := handler(StreamMessage{Message: streamMessage.Message, Done: streamMessage.Done}); err != nil {
return err // Handle errors from the handler
}
}
if err := scanner.Err(); err != nil {
return err // Handle scanner errors
}
thread.AddMessages(AssistantMessage(sb.String()))
return nil
}
type StreamMessage struct {
Message Message `json:"message"`
Done bool `json:"done"`
}
type StreamMessageHandler func(StreamMessage) error
func (c *Client) GenerateEmbedding(text string) (Embedding, error) {
req, err := c.BuildEmbeddingRequest(c.config, text)
if err != nil {
return Embedding{}, err
}
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
return Embedding{}, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return Embedding{}, err
}
return c.EmbeddingDecoder.Decode(respBody), nil
}