mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-16 13:13:41 +08:00
♻️ refactor: provider refactor (#41)
* ♻️ refactor: provider refactor
* 完善百度/讯飞的函数调用,现在可以在`lobe-chat`中正常调用函数了
This commit is contained in:
@@ -26,3 +26,8 @@ type AudioResponse struct {
|
||||
Segments any `json:"segments,omitempty"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type AudioResponseWrapper struct {
|
||||
Headers map[string]string
|
||||
Body []byte
|
||||
}
|
||||
|
||||
119
types/chat.go
119
types/chat.go
@@ -5,16 +5,33 @@ const (
|
||||
ContentTypeImageURL = "image_url"
|
||||
)
|
||||
|
||||
const (
|
||||
FinishReasonStop = "stop"
|
||||
FinishReasonLength = "length"
|
||||
FinishReasonFunctionCall = "function_call"
|
||||
FinishReasonToolCalls = "tool_calls"
|
||||
FinishReasonContentFilter = "content_filter"
|
||||
FinishReasonNull = "null"
|
||||
)
|
||||
|
||||
const (
|
||||
ChatMessageRoleSystem = "system"
|
||||
ChatMessageRoleUser = "user"
|
||||
ChatMessageRoleAssistant = "assistant"
|
||||
ChatMessageRoleFunction = "function"
|
||||
ChatMessageRoleTool = "tool"
|
||||
)
|
||||
|
||||
type ChatCompletionToolCallsFunction struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Arguments string `json:"arguments,omitempty"`
|
||||
Arguments string `json:"arguments"`
|
||||
}
|
||||
|
||||
type ChatCompletionToolCalls struct {
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Function ChatCompletionToolCallsFunction `json:"function"`
|
||||
Index int `json:"index"`
|
||||
Id string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Function *ChatCompletionToolCallsFunction `json:"function"`
|
||||
Index int `json:"index"`
|
||||
}
|
||||
|
||||
type ChatCompletionMessage struct {
|
||||
@@ -123,6 +140,8 @@ type ChatCompletionRequest struct {
|
||||
Seed *int `json:"seed,omitempty"`
|
||||
FrequencyPenalty float64 `json:"frequency_penalty,omitempty"`
|
||||
LogitBias any `json:"logit_bias,omitempty"`
|
||||
LogProbs bool `json:"logprobs,omitempty"`
|
||||
TopLogProbs int `json:"top_logprobs,omitempty"`
|
||||
User string `json:"user,omitempty"`
|
||||
Functions []*ChatCompletionFunction `json:"functions,omitempty"`
|
||||
FunctionCall any `json:"function_call,omitempty"`
|
||||
@@ -151,19 +170,89 @@ type ChatCompletionTool struct {
|
||||
}
|
||||
|
||||
type ChatCompletionChoice struct {
|
||||
Index int `json:"index"`
|
||||
Message ChatCompletionMessage `json:"message"`
|
||||
FinishReason any `json:"finish_reason,omitempty"`
|
||||
Index int `json:"index"`
|
||||
Message ChatCompletionMessage `json:"message"`
|
||||
FinishReason any `json:"finish_reason,omitempty"`
|
||||
ContentFilterResults any `json:"content_filter_results,omitempty"`
|
||||
FinishDetails any `json:"finish_details,omitempty"`
|
||||
}
|
||||
|
||||
type ChatCompletionResponse struct {
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Created int64 `json:"created"`
|
||||
Model string `json:"model"`
|
||||
Choices []ChatCompletionChoice `json:"choices"`
|
||||
Usage *Usage `json:"usage,omitempty"`
|
||||
SystemFingerprint string `json:"system_fingerprint,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Object string `json:"object"`
|
||||
Created int64 `json:"created"`
|
||||
Model string `json:"model"`
|
||||
Choices []ChatCompletionChoice `json:"choices"`
|
||||
Usage *Usage `json:"usage,omitempty"`
|
||||
SystemFingerprint string `json:"system_fingerprint,omitempty"`
|
||||
PromptFilterResults any `json:"prompt_filter_results,omitempty"`
|
||||
}
|
||||
|
||||
func (c ChatCompletionStreamChoice) ConvertOpenaiStream() []ChatCompletionStreamChoice {
|
||||
var function *ChatCompletionToolCallsFunction
|
||||
var functions []*ChatCompletionToolCallsFunction
|
||||
var choices []ChatCompletionStreamChoice
|
||||
var stopFinish string
|
||||
if c.Delta.FunctionCall != nil {
|
||||
function = c.Delta.FunctionCall
|
||||
stopFinish = FinishReasonFunctionCall
|
||||
} else {
|
||||
function = c.Delta.ToolCalls[0].Function
|
||||
stopFinish = FinishReasonToolCalls
|
||||
}
|
||||
|
||||
if function.Name == "" {
|
||||
c.FinishReason = stopFinish
|
||||
choices = append(choices, c)
|
||||
return choices
|
||||
}
|
||||
|
||||
functions = append(functions, &ChatCompletionToolCallsFunction{
|
||||
Name: function.Name,
|
||||
Arguments: "",
|
||||
})
|
||||
|
||||
if function.Arguments == "" || function.Arguments == "{}" {
|
||||
functions = append(functions, &ChatCompletionToolCallsFunction{
|
||||
Arguments: "{}",
|
||||
})
|
||||
} else {
|
||||
functions = append(functions, &ChatCompletionToolCallsFunction{
|
||||
Arguments: function.Arguments,
|
||||
})
|
||||
}
|
||||
|
||||
// 循环functions, 生成choices
|
||||
for _, function := range functions {
|
||||
choice := ChatCompletionStreamChoice{
|
||||
Index: 0,
|
||||
Delta: ChatCompletionStreamChoiceDelta{
|
||||
Role: c.Delta.Role,
|
||||
},
|
||||
}
|
||||
if stopFinish == FinishReasonFunctionCall {
|
||||
choice.Delta.FunctionCall = function
|
||||
} else {
|
||||
choice.Delta.ToolCalls = []*ChatCompletionToolCalls{
|
||||
{
|
||||
Id: c.Delta.ToolCalls[0].Id,
|
||||
Index: 0,
|
||||
Type: "function",
|
||||
Function: function,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
choices = append(choices, choice)
|
||||
}
|
||||
|
||||
choices = append(choices, ChatCompletionStreamChoice{
|
||||
Index: c.Index,
|
||||
Delta: ChatCompletionStreamChoiceDelta{},
|
||||
FinishReason: stopFinish,
|
||||
})
|
||||
|
||||
return choices
|
||||
}
|
||||
|
||||
type ChatCompletionStreamChoiceDelta struct {
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package types
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Usage struct {
|
||||
PromptTokens int `json:"prompt_tokens"`
|
||||
CompletionTokens int `json:"completion_tokens"`
|
||||
@@ -14,6 +16,16 @@ type OpenAIError struct {
|
||||
InnerError any `json:"innererror,omitempty"`
|
||||
}
|
||||
|
||||
func (e *OpenAIError) Error() string {
|
||||
response := &OpenAIErrorResponse{
|
||||
Error: *e,
|
||||
}
|
||||
|
||||
// 转换为JSON
|
||||
bytes, _ := json.Marshal(response)
|
||||
return string(bytes)
|
||||
}
|
||||
|
||||
type OpenAIErrorWithStatusCode struct {
|
||||
OpenAIError
|
||||
StatusCode int `json:"status_code"`
|
||||
|
||||
@@ -8,9 +8,9 @@ type EmbeddingRequest struct {
|
||||
}
|
||||
|
||||
type Embedding struct {
|
||||
Object string `json:"object"`
|
||||
Embedding []float64 `json:"embedding"`
|
||||
Index int `json:"index"`
|
||||
Object string `json:"object"`
|
||||
Embedding any `json:"embedding"`
|
||||
Index int `json:"index"`
|
||||
}
|
||||
|
||||
type EmbeddingResponse struct {
|
||||
|
||||
Reference in New Issue
Block a user