-
Notifications
You must be signed in to change notification settings - Fork 156
/
Copy pathopenai.go
229 lines (195 loc) · 6.04 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
package model
import "encoding/json"
type OpenAIChatCompletionRequest struct {
Model string `json:"model"`
Stream bool `json:"stream"`
Messages []OpenAIChatMessage `json:"messages"`
OpenAIChatCompletionExtraRequest
}
type OpenAIChatCompletionExtraRequest struct {
ChannelId *string `json:"channelId"`
}
type SessionState struct {
Models []string `json:"models"`
Layers int `json:"layers"`
Answer string `json:"answer"`
AnswerIsFinished bool `json:"answer_is_finished"`
}
type OpenAIChatMessage struct {
Role string `json:"role"`
Content interface{} `json:"content"`
IsPrompt bool `json:"is_prompt"`
SessionState *SessionState `json:"session_state"`
}
func (r *OpenAIChatCompletionRequest) AddMessage(message OpenAIChatMessage) {
r.Messages = append([]OpenAIChatMessage{message}, r.Messages...)
}
func (r *OpenAIChatCompletionRequest) PrependMessagesFromJSON(jsonString string) error {
var newMessages []OpenAIChatMessage
err := json.Unmarshal([]byte(jsonString), &newMessages)
if err != nil {
return err
}
// 查找最后一个 system role 的索引
var insertIndex int
for i := len(r.Messages) - 1; i >= 0; i-- {
if r.Messages[i].Role == "system" {
insertIndex = i + 1
break
}
}
// 将 newMessages 插入到找到的索引后面
r.Messages = append(r.Messages[:insertIndex], append(newMessages, r.Messages[insertIndex:]...)...)
return nil
}
func (r *OpenAIChatCompletionRequest) SystemMessagesProcess(model string) {
if r.Messages == nil {
return
}
if model == "deep-seek-r1" {
for i := range r.Messages {
if r.Messages[i].Role == "system" {
r.Messages[i].Role = "user"
}
if r.Messages[i].Role == "assistant" {
r.Messages[i].IsPrompt = false
r.Messages[i].SessionState = &SessionState{
Models: []string{model},
}
}
}
}
}
func (r *OpenAIChatCompletionRequest) FilterUserMessage() {
if r.Messages == nil {
return
}
// 返回最后一个role为user的元素
for i := len(r.Messages) - 1; i >= 0; i-- {
if r.Messages[i].Role == "user" {
r.Messages = r.Messages[i:]
break
}
}
}
type OpenAIErrorResponse struct {
OpenAIError OpenAIError `json:"error"`
}
type OpenAIError struct {
Message string `json:"message"`
Type string `json:"type"`
Param string `json:"param"`
Code string `json:"code"`
}
type OpenAIChatCompletionResponse struct {
ID string `json:"id"`
Object string `json:"object"`
Created int64 `json:"created"`
Model string `json:"model"`
Choices []OpenAIChoice `json:"choices"`
Usage OpenAIUsage `json:"usage"`
SystemFingerprint *string `json:"system_fingerprint"`
Suggestions []string `json:"suggestions"`
}
type OpenAIChoice struct {
Index int `json:"index"`
Message OpenAIMessage `json:"message"`
LogProbs *string `json:"logprobs"`
FinishReason *string `json:"finish_reason"`
Delta OpenAIDelta `json:"delta"`
}
type OpenAIMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type OpenAIUsage struct {
PromptTokens int `json:"prompt_tokens"`
CompletionTokens int `json:"completion_tokens"`
TotalTokens int `json:"total_tokens"`
}
type OpenAIDelta struct {
Content string `json:"content"`
Role string `json:"role"`
}
type OpenAIImagesGenerationRequest struct {
OpenAIChatCompletionExtraRequest
Model string `json:"model"`
Prompt string `json:"prompt"`
ResponseFormat string `json:"response_format"`
Image string `json:"image"`
}
type VideosGenerationRequest struct {
ResponseFormat string `json:"response_format"`
Model string `json:"model"`
AspectRatio string `json:"aspect_ratio"`
Duration int `json:"duration"`
Prompt string `json:"prompt"`
AutoPrompt bool `json:"auto_prompt"`
Image string `json:"image"`
}
type VideosGenerationResponse struct {
Created int64 `json:"created"`
Data []*VideosGenerationDataResponse `json:"data"`
}
type VideosGenerationDataResponse struct {
URL string `json:"url"`
RevisedPrompt string `json:"revised_prompt"`
B64Json string `json:"b64_json"`
}
type OpenAIImagesGenerationResponse struct {
Created int64 `json:"created"`
DailyLimit bool `json:"dailyLimit"`
Data []*OpenAIImagesGenerationDataResponse `json:"data"`
Suggestions []string `json:"suggestions"`
}
type OpenAIImagesGenerationDataResponse struct {
URL string `json:"url"`
RevisedPrompt string `json:"revised_prompt"`
B64Json string `json:"b64_json"`
}
type OpenAIGPT4VImagesReq struct {
Type string `json:"type"`
Text string `json:"text"`
ImageURL struct {
URL string `json:"url"`
} `json:"image_url"`
}
type GetUserContent interface {
GetUserContent() []string
}
type OpenAIModerationRequest struct {
Input string `json:"input"`
}
type OpenAIModerationResponse struct {
ID string `json:"id"`
Model string `json:"model"`
Results []struct {
Flagged bool `json:"flagged"`
Categories map[string]bool `json:"categories"`
CategoryScores map[string]float64 `json:"category_scores"`
} `json:"results"`
}
type OpenaiModelResponse struct {
ID string `json:"id"`
Object string `json:"object"`
//Created time.Time `json:"created"`
//OwnedBy string `json:"owned_by"`
}
// ModelList represents a list of models.
type OpenaiModelListResponse struct {
Object string `json:"object"`
Data []OpenaiModelResponse `json:"data"`
}
func (r *OpenAIChatCompletionRequest) GetUserContent() []string {
var userContent []string
for i := len(r.Messages) - 1; i >= 0; i-- {
if r.Messages[i].Role == "user" {
switch contentObj := r.Messages[i].Content.(type) {
case string:
userContent = append(userContent, contentObj)
}
break
}
}
return userContent
}