-
Notifications
You must be signed in to change notification settings - Fork 20
/
chat_stream.go
171 lines (138 loc) · 4.25 KB
/
chat_stream.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
160
161
162
163
164
165
166
167
168
169
170
171
package openai
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/r3labs/sse/v2"
"glide/pkg/providers/clients"
"go.uber.org/zap"
"glide/pkg/api/schemas"
)
var streamDoneMarker = []byte("[DONE]")
func (c *Client) SupportChatStream() bool {
return true
}
func (c *Client) ChatStream(ctx context.Context, request *schemas.ChatRequest, responseC chan<- schemas.ChatResponse) error {
// Create a new chat request
resp, err := c.initChatStream(ctx, request)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
c.tel.Logger.Error(
"Failed to read stream chat response error",
zap.String("provider", c.Provider()),
zap.Int("statusCode", resp.StatusCode),
zap.Error(err),
)
}
// TODO: handle all specter of errors like in a sync chat API
c.tel.L().Error(
"Failed to start chat stream",
zap.String("provider", c.Provider()),
zap.Int("statusCode", resp.StatusCode),
zap.String("response", string(bodyBytes)),
zap.Any("headers", resp.Header),
)
return clients.ErrProviderUnavailable
}
reader := sse.NewEventStreamReader(resp.Body, 4096) // TODO: should we expose maxBufferSize?
var completionChunk ChatCompletionChunk
for {
rawEvent, err := reader.ReadEvent()
if err != nil {
if err == io.EOF {
c.tel.L().Debug("Chat stream is over", zap.String("provider", c.Provider()))
return nil
}
c.tel.L().Warn(
"Chat stream is unexpectedly disconnected",
zap.String("provider", c.Provider()),
)
return clients.ErrProviderUnavailable
}
c.tel.L().Debug(
"Raw chat stream chunk",
zap.String("provider", c.Provider()),
zap.ByteString("rawChunk", rawEvent),
)
event, err := clients.ParseSSEvent(rawEvent)
if bytes.Equal(event.Data, streamDoneMarker) {
return nil
}
if err != nil {
return fmt.Errorf("failed to parse chat stream message: %v", err)
}
if !event.HasContent() {
c.tel.Logger.Debug(
"Received an empty message in chat stream, skipping it",
zap.String("provider", c.Provider()),
zap.Any("msg", event),
)
continue
}
err = json.Unmarshal(event.Data, &completionChunk)
if err != nil {
return fmt.Errorf("failed to unmarshal chat stream message: %v", err)
}
c.tel.L().Debug(
"Chat response chunk",
zap.String("provider", c.Provider()),
zap.Any("chunk", completionChunk),
)
// TODO: use objectpool here
chatResponse := schemas.ChatResponse{
ID: completionChunk.ID,
Created: completionChunk.Created,
Provider: providerName,
Cached: false,
ModelName: completionChunk.ModelName,
ModelResponse: schemas.ModelResponse{
SystemID: map[string]string{
"system_fingerprint": completionChunk.SystemFingerprint,
},
Message: schemas.ChatMessage{
Role: completionChunk.Choices[0].Delta.Role,
Content: completionChunk.Choices[0].Delta.Content,
},
},
// TODO: Pass info if this is the final message
}
responseC <- chatResponse
}
}
// initChatStream establishes a new chat stream
func (c *Client) initChatStream(ctx context.Context, request *schemas.ChatRequest) (*http.Response, error) {
chatRequest := *c.createChatRequestSchema(request)
chatRequest.Stream = true
rawPayload, err := json.Marshal(chatRequest)
if err != nil {
return nil, fmt.Errorf("unable to marshal openAI chat stream request payload: %w", err)
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.chatURL, bytes.NewBuffer(rawPayload))
if err != nil {
return nil, fmt.Errorf("unable to create OpenAI stream chat request: %w", err)
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %v", string(c.config.APIKey)))
req.Header.Set("Cache-Control", "no-cache")
req.Header.Set("Accept", "text/event-stream")
req.Header.Set("Connection", "keep-alive")
// TODO: this could leak information from messages which may not be a desired thing to have
c.tel.Logger.Debug(
"Stream chat request",
zap.String("chatURL", c.chatURL),
zap.Any("payload", chatRequest),
)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, fmt.Errorf("failed to send OpenAI stream chat request: %w", err)
}
return resp, nil
}