forked from appleboy/CodeGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai.go
343 lines (314 loc) · 11.1 KB
/
openai.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package openai
import (
"context"
"crypto/tls"
"fmt"
"github.com/carsonfeng/ZCode/ollama"
"net/http"
"net/url"
"github.com/carsonfeng/ZCode/groq"
openai "github.com/sashabaranov/go-openai"
"golang.org/x/net/proxy"
)
// DefaultModel is the default OpenAI model to use if one is not provided.
var DefaultModel = openai.GPT3Dot5Turbo
// modelMaps maps model names to their corresponding model ID strings.
var modelMaps = map[string]string{
"gpt-4-32k-0613": openai.GPT432K0613,
"gpt-4-32k-0314": openai.GPT432K0314,
"gpt-4-32k": openai.GPT432K,
"gpt-4-0613": openai.GPT40613,
"gpt-4-0314": openai.GPT40314,
"gpt-4-turbo": openai.GPT4Turbo,
"gpt-4-turbo-2024-04-09": openai.GPT4Turbo20240409,
"gpt-4-0125-preview": openai.GPT4Turbo0125,
"gpt-4-1106-preview": openai.GPT4Turbo1106,
"gpt-4-turbo-preview": openai.GPT4TurboPreview,
"gpt-4-vision-preview": openai.GPT4VisionPreview,
"gpt-4": openai.GPT4,
"gpt-3.5-turbo-0125": openai.GPT3Dot5Turbo0125,
"gpt-3.5-turbo-1106": openai.GPT3Dot5Turbo1106,
"gpt-3.5-turbo-0613": openai.GPT3Dot5Turbo0613,
"gpt-3.5-turbo-0301": openai.GPT3Dot5Turbo0301,
"gpt-3.5-turbo-16k": openai.GPT3Dot5Turbo16K,
"gpt-3.5-turbo-16k-0613": openai.GPT3Dot5Turbo16K0613,
"gpt-3.5-turbo": openai.GPT3Dot5Turbo,
"gpt-3.5-turbo-instruct": openai.GPT3Dot5TurboInstruct,
"davinci": openai.GPT3Davinci,
"davinci-002": openai.GPT3Davinci002,
"curie": openai.GPT3Curie,
"curie-002": openai.GPT3Curie002,
"ada": openai.GPT3Ada,
"ada-002": openai.GPT3Ada002,
"babbage": openai.GPT3Babbage,
"babbage-002": openai.GPT3Babbage002,
ollama.LLaMA3_70b.String(): ollama.LLaMA3_70b.GetModel(),
groq.LLaMA370bChat.String(): groq.LLaMA370bChat.GetModel(),
groq.LLaMA270bChat.String(): groq.LLaMA270bChat.GetModel(),
groq.Mixtral8x7bInstructV01.String(): groq.Mixtral8x7bInstructV01.GetModel(),
groq.Gemma7bIt.String(): groq.Gemma7bIt.GetModel(),
}
// GetModel returns the model ID corresponding to the given model name.
// If the model name is not recognized, it returns the default model ID.
func GetModel(model string) string {
v, ok := modelMaps[model]
if !ok {
return DefaultModel
}
return v
}
// Client is a struct that represents an OpenAI client.
type Client struct {
client *openai.Client
model string
maxTokens int
temperature float32
isFuncCall bool
// An alternative to sampling with temperature, called nucleus sampling,
// where the model considers the results of the tokens with top_p probability mass.
// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
topP float32
// Number between -2.0 and 2.0.
// Positive values penalize new tokens based on whether they appear in the text so far,
// increasing the model's likelihood to talk about new topics.
presencePenalty float32
// Number between -2.0 and 2.0.
// Positive values penalize new tokens based on their existing frequency in the text so far,
// decreasing the model's likelihood to repeat the same line verbatim.
frequencyPenalty float32
}
type Response struct {
Content string
Usage openai.Usage
}
// CreateChatCompletion is an API call to create a function call for a chat message.
func (c *Client) CreateFunctionCall(
ctx context.Context,
content string,
funcs ...openai.FunctionDefinition,
) (resp openai.ChatCompletionResponse, err error) {
req := openai.ChatCompletionRequest{
Model: c.model,
MaxTokens: c.maxTokens,
Temperature: c.temperature,
TopP: c.topP,
FrequencyPenalty: c.frequencyPenalty,
PresencePenalty: c.presencePenalty,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant.",
},
{
Role: openai.ChatMessageRoleUser,
Content: content,
},
},
Functions: funcs,
FunctionCall: "auto",
}
return c.client.CreateChatCompletion(ctx, req)
}
// CreateChatCompletion is an API call to create a completion for a chat message.
func (c *Client) CreateChatCompletion(
ctx context.Context,
content string,
) (resp openai.ChatCompletionResponse, err error) {
req := openai.ChatCompletionRequest{
Model: c.model,
MaxTokens: c.maxTokens,
Temperature: c.temperature,
TopP: c.topP,
FrequencyPenalty: c.frequencyPenalty,
PresencePenalty: c.presencePenalty,
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are a helpful assistant.",
},
{
Role: openai.ChatMessageRoleUser,
Content: content,
},
},
}
return c.client.CreateChatCompletion(ctx, req)
}
// CreateCompletion is an API call to create a completion.
// This is the main endpoint of the API. It returns new text, as well as, if requested,
// the probabilities over each alternative token at each position.
//
// If using a fine-tuned model, simply provide the model's ID in the CompletionRequest object,
// and the server will use the model's parameters to generate the completion.
func (c *Client) CreateCompletion(
ctx context.Context,
content string,
) (resp openai.CompletionResponse, err error) {
req := openai.CompletionRequest{
Model: c.model,
MaxTokens: c.maxTokens,
Temperature: c.temperature,
TopP: c.topP,
FrequencyPenalty: c.frequencyPenalty,
PresencePenalty: c.presencePenalty,
Prompt: content,
}
return c.client.CreateCompletion(ctx, req)
}
// Completion is a method on the Client struct that takes a context.Context and a string argument
// and returns a string and an error.
func (c *Client) Completion(
ctx context.Context,
content string,
) (*Response, error) {
resp := &Response{}
switch c.model {
case openai.GPT3Dot5Turbo,
openai.GPT3Dot5Turbo0301,
openai.GPT3Dot5Turbo0613,
openai.GPT3Dot5Turbo16K,
openai.GPT3Dot5Turbo16K0613,
openai.GPT3Dot5Turbo1106,
openai.GPT3Dot5Turbo0125,
openai.GPT4,
openai.GPT40314,
openai.GPT40613,
openai.GPT432K,
openai.GPT432K0314,
openai.GPT432K0613,
openai.GPT4Turbo1106,
openai.GPT4Turbo0125,
openai.GPT4TurboPreview,
openai.GPT4VisionPreview,
openai.GPT4Turbo,
openai.GPT4Turbo20240409,
ollama.LLaMA3_70b.GetModel(),
groq.LLaMA370bChat.GetModel(),
groq.LLaMA270bChat.GetModel(),
groq.Mixtral8x7bInstructV01.GetModel(),
groq.Gemma7bIt.GetModel():
r, err := c.CreateChatCompletion(ctx, content)
if err != nil {
return nil, err
}
resp.Content = r.Choices[0].Message.Content
resp.Usage = r.Usage
default:
r, err := c.CreateCompletion(ctx, content)
if err != nil {
return nil, err
}
resp.Content = r.Choices[0].Text
resp.Usage = r.Usage
}
return resp, nil
}
// New creates a new OpenAI API client with the given options.
func New(opts ...Option) (*Client, error) {
// Create a new config object with the given options.
cfg := newConfig(opts...)
// Validate the config object, returning an error if it is invalid.
if err := cfg.valid(); err != nil {
return nil, err
}
// Create a new client instance with the necessary fields.
engine := &Client{
model: modelMaps[cfg.model],
maxTokens: cfg.maxTokens,
temperature: cfg.temperature,
}
// Create a new OpenAI config object with the given API token and other optional fields.
c := openai.DefaultConfig(cfg.token)
if cfg.orgID != "" {
c.OrgID = cfg.orgID
}
if cfg.baseURL != "" {
c.BaseURL = cfg.baseURL
}
// Create a new HTTP transport.
tr := &http.Transport{}
if cfg.skipVerify {
tr.TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
}
// Create a new HTTP client with the specified timeout and proxy, if any.
httpClient := &http.Client{
Timeout: cfg.timeout,
}
if cfg.proxyURL != "" {
proxyURL, _ := url.Parse(cfg.proxyURL)
tr.Proxy = http.ProxyURL(proxyURL)
} else if cfg.socksURL != "" {
dialer, err := proxy.SOCKS5("tcp", cfg.socksURL, nil, proxy.Direct)
if err != nil {
return nil, fmt.Errorf("can't connect to the proxy: %s", err)
}
tr.DialContext = dialer.(proxy.ContextDialer).DialContext
}
// Set the HTTP client to use the default header transport with the specified headers.
httpClient.Transport = &DefaultHeaderTransport{
Origin: tr,
Header: NewHeaders(cfg.headers),
}
// Set the OpenAI client to use the default configuration with Azure-specific options, if the provider is Azure.
if cfg.provider == AZURE {
defaultAzureConfig := openai.DefaultAzureConfig(cfg.token, cfg.baseURL)
defaultAzureConfig.AzureModelMapperFunc = func(model string) string {
return cfg.modelName
}
// Set the API version to the one with the specified options.
if cfg.apiVersion != "" {
defaultAzureConfig.APIVersion = cfg.apiVersion
}
// Set the HTTP client to the one with the specified options.
defaultAzureConfig.HTTPClient = httpClient
engine.client = openai.NewClientWithConfig(
defaultAzureConfig,
)
} else {
// Otherwise, set the OpenAI client to use the HTTP client with the specified options.
c.HTTPClient = httpClient
if cfg.apiVersion != "" {
c.APIVersion = cfg.apiVersion
}
engine.client = openai.NewClientWithConfig(c)
}
engine.isFuncCall = engine.allowFuncCall(cfg)
// Return the resulting client engine.
return engine, nil
}
// allowFuncCall returns true if the model supports function calls.
// https://learn.microsoft.com/en-us/azure/ai-services/openai/how-to/function-calling
// https://platform.openai.com/docs/guides/function-calling/supported-models
// Not all model versions are trained with function calling data.
// Function calling is supported with the following models:
// gpt-4, gpt-4-turbo-preview, gpt-4-0125-preview, gpt-4-1106-preview, gpt-4-0613,
// gpt-3.5-turbo, gpt-3.5-turbo-0125, gpt-3.5-turbo-1106, and gpt-3.5-turbo-0613
// In addition, parallel function calls is supported on the following models:
// gpt-4-turbo-preview, gpt-4-0125-preview, gpt-4-1106-preview,
// gpt-3.5-turbo-0125, and gpt-3.5-turbo-1106
func (c *Client) allowFuncCall(cfg *config) bool {
if cfg.provider == AZURE && cfg.apiVersion == "2023-07-01-preview" {
return true
}
switch c.model {
case openai.GPT4Turbo,
openai.GPT4Turbo20240409,
openai.GPT4TurboPreview,
openai.GPT4Turbo0125,
openai.GPT4Turbo1106,
openai.GPT40613,
openai.GPT3Dot5Turbo,
openai.GPT3Dot5Turbo0125,
openai.GPT3Dot5Turbo0613,
openai.GPT3Dot5Turbo1106:
return true
default:
return false
}
}
// AllowFuncCall returns true if the model supports function calls.
// In an API call, you can describe functions to gpt-3.5-turbo-0613 and gpt-4-0613
// https://platform.openai.com/docs/guides/gpt/chat-completions-api
func (c *Client) AllowFuncCall() bool {
return c.isFuncCall
}