-
Notifications
You must be signed in to change notification settings - Fork 807
/
openai.go
230 lines (199 loc) · 5.58 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
/*
Copyright 2023 The K8sGPT Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Some parts of this file have been modified to make it functional in Zadig
package llm
import (
"context"
"encoding/base64"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/pkoukk/tiktoken-go"
"github.com/sashabaranov/go-openai"
"github.com/koderover/zadig/pkg/tool/cache"
"github.com/koderover/zadig/pkg/tool/log"
)
const (
DefaultOpenAIModel = openai.GPT3Dot5Turbo
DefaultOpenAIModelTokenLimit = "4096"
)
type OpenAIClient struct {
name string
model string
client *openai.Client
apiType string
}
func (c *OpenAIClient) Configure(config LLMConfig) error {
token := config.GetToken()
var defaultConfig openai.ClientConfig
if config.GetAPIType() == "AZURE" || config.GetAPIType() == "AZURE_AD" {
c.apiType = "AZURE"
baseURL := config.GetBaseURL()
defaultConfig = openai.DefaultAzureConfig(token, baseURL)
if config.GetAPIType() == "AZURE_AD" {
c.apiType = "AZURE_AD"
defaultConfig.APIType = openai.APITypeAzureAD
}
} else {
// config.GetAPIType() == "OPEN_AI"
c.apiType = "OPEN_AI"
defaultConfig = openai.DefaultConfig(token)
}
if config.GetProxy() != "" {
proxyUrl, err := url.Parse(config.GetProxy())
if err != nil {
return fmt.Errorf("invalid proxy url %s", config.GetProxy())
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyUrl),
}
defaultConfig.HTTPClient = &http.Client{
Transport: transport,
}
}
client := openai.NewClientWithConfig(defaultConfig)
if client == nil {
return errors.New("error creating OpenAI client")
}
c.client = client
c.name = config.GetName()
c.model = config.GetModel()
return nil
}
// @todo add ability to supply multiple messages
func (c *OpenAIClient) GetCompletion(ctx context.Context, prompt string, options ...ParamOption) (string, error) {
opts := ParamOptions{}
for _, opt := range options {
opt(&opts)
}
opts = ValidOptions(opts)
model := opts.Model
if model == "" {
if c.model == "" {
model = DefaultOpenAIModel
} else {
model = c.model
}
}
messages := []openai.ChatCompletionMessage{
{
Role: "user",
Content: prompt,
},
}
var resp openai.ChatCompletionResponse
var err error
if opts.MaxTokens == 0 {
resp, err = c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Model: model,
Messages: messages,
Temperature: opts.Temperature,
Stop: opts.StopWords,
LogitBias: opts.LogitBias,
})
} else {
resp, err = c.client.CreateChatCompletion(ctx, openai.ChatCompletionRequest{
Model: model,
Messages: messages,
MaxTokens: opts.MaxTokens,
Temperature: opts.Temperature,
Stop: opts.StopWords,
LogitBias: opts.LogitBias,
})
}
if err != nil {
return "", fmt.Errorf("create chat completion failed: %v", err)
}
return resp.Choices[0].Message.Content, nil
}
func (a *OpenAIClient) Parse(ctx context.Context, prompt string, cache cache.ICache, options ...ParamOption) (string, error) {
// Check for cached data
cacheKey := GetCacheKey(a.GetName(), prompt)
if !cache.IsCacheDisabled() && cache.Exists(cacheKey) {
response, err := cache.Load(cacheKey)
if err != nil {
return "", err
}
if response != "" {
output, err := base64.StdEncoding.DecodeString(response)
if err != nil {
log.Errorf("error decoding cached data: %v", err)
return "", nil
}
return string(output), nil
}
}
response, err := a.GetCompletion(ctx, prompt, options...)
if err != nil {
return "", err
}
err = cache.Store(cacheKey, base64.StdEncoding.EncodeToString([]byte(response)))
if err != nil {
log.Errorf("error storing value to cache: %v", err)
return "", nil
}
return response, nil
}
func (a *OpenAIClient) GetName() string {
if a.name == "" {
if a.apiType == "AZURE" || a.apiType == "AZURE_AD" {
return "azureopenai"
}
return "openai"
}
return a.name
}
func NumTokensFromMessages(messages []openai.ChatCompletionMessage, model string) (num_tokens int, err error) {
tkm, err := tiktoken.EncodingForModel(model)
if err != nil {
err = fmt.Errorf("EncodingForModel error: %w", err)
return
}
var tokens_per_message int
var tokens_per_name int
if model == "gpt-3.5-turbo-0301" || model == "gpt-3.5-turbo" {
tokens_per_message = 4
tokens_per_name = -1
} else if model == "gpt-4-0314" || model == "gpt-4" {
tokens_per_message = 3
tokens_per_name = 1
} else {
tokens_per_message = 3
tokens_per_name = 1
log.Warnf("Warning: model not found. Using cl100k_base encoding.")
}
for _, message := range messages {
num_tokens += tokens_per_message
num_tokens += len(tkm.Encode(message.Content, nil, nil))
num_tokens += len(tkm.Encode(message.Role, nil, nil))
num_tokens += len(tkm.Encode(message.Name, nil, nil))
if message.Name != "" {
num_tokens += tokens_per_name
}
}
num_tokens += 3
return num_tokens, nil
}
func NumTokensFromPrompt(prompt string, model string) (num_tokens int, err error) {
messages := []openai.ChatCompletionMessage{
{
Role: "user",
Content: prompt,
},
}
if model == "" {
model = DefaultOpenAIModel
}
return NumTokensFromMessages(messages, model)
}