-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathclient.go
171 lines (145 loc) · 4.16 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
160
161
162
163
164
165
166
167
168
169
170
171
package deepseek
import (
"bufio"
"context"
"fmt"
utils "github.com/cohesion-org/deepseek-go/utils"
)
// CreateChatCompletion sends a chat completion request and returns the generated response.
func (c *Client) CreateChatCompletion(
ctx context.Context,
request *ChatCompletionRequest,
) (*ChatCompletionResponse, error) {
if request == nil {
return nil, fmt.Errorf("request cannot be nil")
}
ctx, tcancel, err := getTimeoutContext(ctx, c.Timeout)
if err != nil {
return nil, err
}
defer tcancel()
req, err := utils.NewRequestBuilder(c.AuthToken).
SetBaseURL(c.BaseURL).
SetPath(c.Path).
SetBodyFromStruct(request).
Build(ctx)
if err != nil {
return nil, fmt.Errorf("error building request: %w", err)
}
resp, err := HandleSendChatCompletionRequest(*c, req)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, HandleAPIError(resp)
}
updatedResp, err := HandleChatCompletionResponse(resp)
if err != nil {
return nil, fmt.Errorf("error decoding response: %w", err)
}
return updatedResp, err
}
// CreateChatCompletionStream sends a chat completion request with stream = true and returns the delta
func (c *Client) CreateChatCompletionStream(
ctx context.Context,
request *StreamChatCompletionRequest,
) (ChatCompletionStream, error) {
if request == nil {
return nil, fmt.Errorf("request cannot be nil")
}
ctx, _, err := getTimeoutContext(ctx, c.Timeout)
if err != nil {
return nil, err
}
request.Stream = true
req, err := utils.NewRequestBuilder(c.AuthToken).
SetBaseURL(c.BaseURL).
SetPath(c.Path).
SetBodyFromStruct(request).
BuildStream(ctx)
if err != nil {
return nil, fmt.Errorf("error building request: %w", err)
}
resp, err := HandleSendChatCompletionRequest(*c, req)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, HandleAPIError(resp)
}
ctx, cancel := context.WithCancel(ctx)
stream := &chatCompletionStream{
ctx: ctx,
cancel: cancel,
resp: resp,
reader: bufio.NewReader(resp.Body),
}
return stream, nil
}
// CreateFIMCompletion is a beta feature. It sends a FIM completion request and returns the generated response.
// the base URL is set to "https://api.deepseek.com/beta/"
func (c *Client) CreateFIMCompletion(
ctx context.Context,
request *FIMCompletionRequest,
) (*FIMCompletionResponse, error) {
if request.MaxTokens > 4000 {
return nil, fmt.Errorf("max tokens must be <= 4000")
}
baseURL := "https://api.deepseek.com/beta/"
if request == nil {
return nil, fmt.Errorf("request cannot be nil")
}
req, err := utils.NewRequestBuilder(c.AuthToken).
SetBaseURL(baseURL).
SetPath("/completions").
SetBodyFromStruct(request).
Build(ctx)
if err != nil {
return nil, fmt.Errorf("error building request: %w", err)
}
resp, err := HandleSendChatCompletionRequest(*c, req)
if err != nil {
return nil, fmt.Errorf("error sending request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, HandleAPIError(resp)
}
updatedResp, err := HandleFIMCompletionRequest(resp)
if err != nil {
return nil, fmt.Errorf("error decoding response: %w", err)
}
return updatedResp, err
}
// CreateFIMStreamCompletion sends a FIM completion request with stream = true and returns the delta
func (c *Client) CreateFIMStreamCompletion(
ctx context.Context,
request *FIMStreamCompletionRequest,
) (FIMChatCompletionStream, error) {
baseURL := "https://api.deepseek.com/beta/"
request.Stream = true
req, err := utils.NewRequestBuilder(c.AuthToken).
SetBaseURL(baseURL).
SetPath("/completions"). //Note to maintianer: This is a really bad implementation with manual path insertion. Please create an issue.
SetBodyFromStruct(request).
BuildStream(ctx)
if err != nil {
return nil, fmt.Errorf("error building request: %w", err)
}
resp, err := HandleSendChatCompletionRequest(*c, req)
if err != nil {
return nil, err
}
if resp.StatusCode >= 400 {
return nil, HandleAPIError(resp)
}
ctx, cancel := context.WithCancel(ctx)
stream := &fimCompletionStream{
ctx: ctx,
cancel: cancel,
resp: resp,
reader: bufio.NewReader(resp.Body),
}
return stream, nil
}