@@ -0,0 +1,65 @@
|
||||
package openai
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type ChatCompletionRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []map[string]any `json:"messages"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
Extra map[string]json.RawMessage `json:"-"`
|
||||
}
|
||||
|
||||
func (r *ChatCompletionRequest) UnmarshalJSON(data []byte) error {
|
||||
type alias ChatCompletionRequest
|
||||
var a alias
|
||||
if err := json.Unmarshal(data, &a); err != nil {
|
||||
return err
|
||||
}
|
||||
var raw map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &raw); err != nil {
|
||||
return err
|
||||
}
|
||||
delete(raw, "model")
|
||||
delete(raw, "messages")
|
||||
delete(raw, "stream")
|
||||
*r = ChatCompletionRequest(a)
|
||||
r.Extra = raw
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r ChatCompletionRequest) MarshalForUpstream() ([]byte, error) {
|
||||
model := map[string]string{
|
||||
"minimax-m2.5": "minimax-m25",
|
||||
"glm4.7": "glm47",
|
||||
}[r.Model]
|
||||
if model == "" {
|
||||
model = r.Model
|
||||
}
|
||||
m := map[string]any{
|
||||
"model": model,
|
||||
"messages": r.Messages,
|
||||
"temperature": 0,
|
||||
"stream": r.Stream,
|
||||
"stream_options": map[string]any{"include_usage": true},
|
||||
"max_tokens": 16000,
|
||||
"inputs": map[string]any{"aiDevQuestion": ""},
|
||||
}
|
||||
for k, v := range r.Extra {
|
||||
var anyValue any
|
||||
if err := json.Unmarshal(v, &anyValue); err == nil {
|
||||
m[k] = anyValue
|
||||
}
|
||||
}
|
||||
return json.Marshal(m)
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error ErrorBody `json:"error"`
|
||||
}
|
||||
|
||||
type ErrorBody struct {
|
||||
Message string `json:"message"`
|
||||
Type string `json:"type"`
|
||||
Param any `json:"param"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
Reference in New Issue
Block a user