forked from cohesion-org/deepseek-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat_stream.go
243 lines (215 loc) · 9.2 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package deepseek
import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
// StreamChatCompletionMessage represents a single message in a chat completion stream.
type StreamChatCompletionMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
// ChatCompletionStream is an interface for receiving streaming chat completion responses.
type ChatCompletionStream interface {
Recv() (*StreamChatCompletionResponse, error)
Close() error
}
// chatCompletionStream implements the ChatCompletionStream interface.
type chatCompletionStream struct {
ctx context.Context // Context for cancellation.
cancel context.CancelFunc // Cancel function for the context.
resp *http.Response // HTTP response from the API call.
reader *bufio.Reader // Reader for the response body.
}
// StreamOptions provides options for streaming chat completion responses.
type StreamOptions struct {
IncludeUsage bool `json:"include_usage"` // Whether to include usage information in the stream. The API returns the usage sometimes even if this is set to false.
}
// StreamUsage represents token usage statistics for a streaming chat completion response. You will get {0 0 0} up until the last stream delta.
type StreamUsage struct {
PromptTokens int `json:"prompt_tokens"` // Number of tokens in the prompt.
CompletionTokens int `json:"completion_tokens"` // Number of tokens in the completion.
TotalTokens int `json:"total_tokens"` // Total number of tokens used.
}
// StreamDelta represents a delta in the chat completion stream.
type StreamDelta struct {
Role string `json:"role,omitempty"` // Role of the message.
Content string `json:"content"` // Content of the message.
ReasoningContent string `json:"reasoning_content,omitempty"` // Reasoning content of the message.
}
// StreamChoices represents a choice in the chat completion stream.
type StreamChoices struct {
Index int `json:"index"` // Index of the choice.
Delta StreamDelta // Delta information for the choice.
FinishReason string `json:"finish_reason,omitempty"` // Reason for finishing the generation.
Logprobs Logprobs `json:"logprobs,omitempty"` // Log probabilities for the generated tokens.
}
// StreamChatCompletionResponse represents a single response from a streaming chat completion API call.
type StreamChatCompletionResponse struct {
ID string `json:"id"` // ID of the response.
Object string `json:"object"` // Type of object.
Created int64 `json:"created"` // Creation timestamp.
Model string `json:"model"` // Model used.
Choices []StreamChoices `json:"choices"` // Choices generated.
Usage *StreamUsage `json:"usage,omitempty"` // Usage statistics (optional).
}
// StreamChatCompletionRequest represents the request body for a streaming chat completion API call.
type StreamChatCompletionRequest struct {
Stream bool `json:"stream,omitempty"` //Comments: Defaults to true, since it's "STREAM"
StreamOptions StreamOptions `json:"stream_options,omitempty"` // Optional: Stream options for the request.
Model string `json:"model"` // Required: Model ID, e.g., "deepseek-chat"
Messages []ChatCompletionMessage `json:"messages"` // Required: List of messages
FrequencyPenalty float32 `json:"frequency_penalty,omitempty"` // Optional: Frequency penalty, >= -2 and <= 2
MaxTokens int `json:"max_tokens,omitempty"` // Optional: Maximum tokens, > 1
PresencePenalty float32 `json:"presence_penalty,omitempty"` // Optional: Presence penalty, >= -2 and <= 2
Temperature float32 `json:"temperature,omitempty"` // Optional: Sampling temperature, <= 2
TopP float32 `json:"top_p,omitempty"` // Optional: Nucleus sampling parameter, <= 1
ResponseFormat *ResponseFormat `json:"response_format,omitempty"` // Optional: Custom response format: just don't try, it breaks rn ;)
Stop []string `json:"stop,omitempty"` // Optional: Stop signals
Tools []Tool `json:"tools,omitempty"` // Optional: List of tools
LogProbs bool `json:"logprobs,omitempty"` // Optional: Enable log probabilities
TopLogProbs int `json:"top_logprobs,omitempty"` // Optional: Number of top tokens with log probabilities, <= 20
}
type StreamChatCompletionRequestOption func(*StreamChatCompletionRequest)
func NewDefaultStreamChatCompletionRequest(model string, messages []ChatCompletionMessage,
opts ...StreamChatCompletionRequestOption) *StreamChatCompletionRequest {
locChatCompletionReq := &StreamChatCompletionRequest{
Stream: true,
Model: model,
Messages: messages,
MaxTokens: ConstChatMaxTokensDefault,
Temperature: ConstChatTemperatureDefault,
TopP: ConstChatTopPDefault,
PresencePenalty: ConstChatPresencePenaltyDefault,
FrequencyPenalty: ConstChatFrequencyPenaltyDefault,
}
for _, o := range opts {
o(locChatCompletionReq)
}
return locChatCompletionReq
}
func WithStreamChatUpdate(update bool) StreamChatCompletionRequestOption {
return func(req *StreamChatCompletionRequest) {
req.Stream = update
}
}
func WithStreamChatModel(model string, messages []ChatCompletionMessage) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.Model = model
r.Messages = messages
}
}
func WithStreamChatMaxTokens(maxTokens int) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.MaxTokens = maxTokens
}
}
func WithStreamChatTemperature(temperature float32) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.Temperature = temperature
}
}
func WithStreamChatTopP(topP float32) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.TopP = topP
}
}
func WithStreamChatPresencePenalty(presencePenalty float32) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.PresencePenalty = presencePenalty
}
}
func WithStreamChatFrequencyPenalty(frequencyPenalty float32) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.FrequencyPenalty = frequencyPenalty
}
}
func WithStreamChatStop(stop []string) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.Stop = stop
}
}
func WithStreamChatLogprobs(logprobs bool) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.LogProbs = logprobs
}
}
func WithCStreamhatTopLogProbs(topLogProbs int) StreamChatCompletionRequestOption {
return func(r *StreamChatCompletionRequest) {
r.TopLogProbs = topLogProbs
}
}
func CheckStreamChatCompletionRequest(request *StreamChatCompletionRequest) error {
if request == nil {
return fmt.Errorf("request cannot be nil")
}
if request.Model == "" {
return fmt.Errorf("model cannot be empty")
}
if len(request.Messages) == 0 {
return fmt.Errorf("messages cannot be empty")
}
if request.MaxTokens <= ConstChatMaxTokensMin {
return fmt.Errorf("max tokens must be > 1")
}
if request.MaxTokens > ConstChatMaxTokensMax {
return fmt.Errorf("max tokens must be <= 4000")
}
if request.FrequencyPenalty < ConstChatFrequencyPenaltyMin {
return fmt.Errorf("frequency penalty must be >= %v", ConstChatFrequencyPenaltyMin)
}
if request.FrequencyPenalty > ConstChatFrequencyPenaltyMax {
return fmt.Errorf("frequency penalty must be < =%v", ConstChatFrequencyPenaltyMax)
}
if request.TopLogProbs > ConstChatTopLogprobsMax {
return fmt.Errorf("logprobs must be <= %v", ConstChatTopLogprobsMax)
}
if request.Temperature > ConstChatTemperatureMax {
return fmt.Errorf("temperature must be <= %v", ConstChatTemperatureMax)
}
if request.TopP > ConstChatTopPMax {
return fmt.Errorf("top p must be <= %v", ConstChatTopPMax)
}
return nil
}
// Recv receives the next response from the stream.
func (s *chatCompletionStream) Recv() (*StreamChatCompletionResponse, error) {
reader := s.reader
for {
line, err := reader.ReadString('\n') // Read until newline
if err != nil {
if err == io.EOF {
return nil, io.EOF
}
return nil, fmt.Errorf("error reading stream: %w", err)
}
line = strings.TrimSpace(line)
if line == "data: [DONE]" {
return nil, io.EOF // End of stream
}
if len(line) > 6 && line[:6] == "data: " {
trimmed := line[6:] // Trim the "data: " prefix
var response StreamChatCompletionResponse
if err := json.Unmarshal([]byte(trimmed), &response); err != nil {
return nil, fmt.Errorf("unmarshal error: %w, raw data: %s", err, trimmed)
}
if response.Usage == nil {
response.Usage = &StreamUsage{}
}
return &response, nil
}
}
}
// Close terminates the stream.
func (s *chatCompletionStream) Close() error {
s.cancel()
err := s.resp.Body.Close()
if err != nil {
return fmt.Errorf("failed to close response body: %w", err)
}
return nil
}