mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-11 02:43:44 +08:00
feat: support openai websearch models
This commit is contained in:
@@ -1,13 +1,43 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/songquanpeng/one-api/common/logger"
|
||||
)
|
||||
|
||||
// ReasoningFormat is the format of reasoning content,
|
||||
// can be set by the reasoning_format parameter in the request url.
|
||||
type ReasoningFormat string
|
||||
|
||||
const (
|
||||
ReasoningFormatUnspecified ReasoningFormat = ""
|
||||
// ReasoningFormatReasoningContent is the reasoning format used by deepseek official API
|
||||
ReasoningFormatReasoningContent ReasoningFormat = "reasoning_content"
|
||||
// ReasoningFormatReasoning is the reasoning format used by openrouter
|
||||
ReasoningFormatReasoning ReasoningFormat = "reasoning"
|
||||
|
||||
// ReasoningFormatThinkTag is the reasoning format used by 3rd party deepseek-r1 providers.
|
||||
//
|
||||
// Deprecated: I believe <think> is a very poor format, especially in stream mode, it is difficult to extract and convert.
|
||||
// Considering that only a few deepseek-r1 third-party providers use this format, it has been decided to no longer support it.
|
||||
// ReasoningFormatThinkTag ReasoningFormat = "think-tag"
|
||||
|
||||
// ReasoningFormatThinking is the reasoning format used by anthropic
|
||||
ReasoningFormatThinking ReasoningFormat = "thinking"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
Role string `json:"role,omitempty"`
|
||||
// Content is a string or a list of objects
|
||||
Content any `json:"content,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
ToolCalls []Tool `json:"tool_calls,omitempty"`
|
||||
ToolCallId string `json:"tool_call_id,omitempty"`
|
||||
Audio *messageAudio `json:"audio,omitempty"`
|
||||
Content any `json:"content,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
ToolCalls []Tool `json:"tool_calls,omitempty"`
|
||||
ToolCallId string `json:"tool_call_id,omitempty"`
|
||||
Audio *messageAudio `json:"audio,omitempty"`
|
||||
Annotation []AnnotationItem `json:"annotation,omitempty"`
|
||||
|
||||
// -------------------------------------
|
||||
// Deepseek 专有的一些字段
|
||||
// https://api-docs.deepseek.com/api/create-chat-completion
|
||||
@@ -18,11 +48,52 @@ type Message struct {
|
||||
// Prefix Completion feature as the input for the CoT in the last assistant message.
|
||||
// When using this feature, the prefix parameter must be set to true.
|
||||
ReasoningContent *string `json:"reasoning_content,omitempty"`
|
||||
|
||||
// -------------------------------------
|
||||
// Openrouter
|
||||
// -------------------------------------
|
||||
Reasoning *string `json:"reasoning,omitempty"`
|
||||
Refusal *bool `json:"refusal,omitempty"`
|
||||
|
||||
// -------------------------------------
|
||||
// Anthropic
|
||||
// -------------------------------------
|
||||
Thinking *string `json:"thinking,omitempty"`
|
||||
Signature *string `json:"signature,omitempty"`
|
||||
}
|
||||
|
||||
type AnnotationItem struct {
|
||||
Type string `json:"type" binding:"oneof=url_citation"`
|
||||
UrlCitation UrlCitation `json:"url_citation"`
|
||||
}
|
||||
|
||||
// UrlCitation is a URL citation when using web search.
|
||||
type UrlCitation struct {
|
||||
// Endpoint is the index of the last character of the URL citation in the message.
|
||||
EndIndex int `json:"end_index"`
|
||||
// StartIndex is the index of the first character of the URL citation in the message.
|
||||
StartIndex int `json:"start_index"`
|
||||
// Title is the title of the web resource.
|
||||
Title string `json:"title"`
|
||||
// Url is the URL of the web resource.
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
// SetReasoningContent sets the reasoning content based on the format
|
||||
func (m *Message) SetReasoningContent(format string, reasoningContent string) {
|
||||
switch ReasoningFormat(strings.ToLower(strings.TrimSpace(format))) {
|
||||
case ReasoningFormatReasoningContent:
|
||||
m.ReasoningContent = &reasoningContent
|
||||
// case ReasoningFormatThinkTag:
|
||||
// m.Content = fmt.Sprintf("<think>%s</think>%s", reasoningContent, m.Content)
|
||||
case ReasoningFormatThinking:
|
||||
m.Thinking = &reasoningContent
|
||||
case ReasoningFormatReasoning,
|
||||
ReasoningFormatUnspecified:
|
||||
m.Reasoning = &reasoningContent
|
||||
default:
|
||||
logger.Warnf(context.TODO(), "unknown reasoning format: %q", format)
|
||||
}
|
||||
}
|
||||
|
||||
type messageAudio struct {
|
||||
@@ -50,6 +121,7 @@ func (m Message) StringContent() string {
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
if contentMap["type"] == ContentTypeText {
|
||||
if subStr, ok := contentMap["text"].(string); ok {
|
||||
contentStr += subStr
|
||||
@@ -58,6 +130,7 @@ func (m Message) StringContent() string {
|
||||
}
|
||||
return contentStr
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -71,6 +144,7 @@ func (m Message) ParseContent() []MessageContent {
|
||||
})
|
||||
return contentList
|
||||
}
|
||||
|
||||
anyList, ok := m.Content.([]any)
|
||||
if ok {
|
||||
for _, contentItem := range anyList {
|
||||
@@ -95,8 +169,21 @@ func (m Message) ParseContent() []MessageContent {
|
||||
},
|
||||
})
|
||||
}
|
||||
case ContentTypeInputAudio:
|
||||
if subObj, ok := contentMap["input_audio"].(map[string]any); ok {
|
||||
contentList = append(contentList, MessageContent{
|
||||
Type: ContentTypeInputAudio,
|
||||
InputAudio: &InputAudio{
|
||||
Data: subObj["data"].(string),
|
||||
Format: subObj["format"].(string),
|
||||
},
|
||||
})
|
||||
}
|
||||
default:
|
||||
logger.Warnf(context.TODO(), "unknown content type: %s", contentMap["type"])
|
||||
}
|
||||
}
|
||||
|
||||
return contentList
|
||||
}
|
||||
return nil
|
||||
@@ -108,7 +195,23 @@ type ImageURL struct {
|
||||
}
|
||||
|
||||
type MessageContent struct {
|
||||
Type string `json:"type,omitempty"`
|
||||
Text string `json:"text"`
|
||||
ImageURL *ImageURL `json:"image_url,omitempty"`
|
||||
// Type should be one of the following: text/input_audio
|
||||
Type string `json:"type,omitempty"`
|
||||
Text string `json:"text"`
|
||||
ImageURL *ImageURL `json:"image_url,omitempty"`
|
||||
InputAudio *InputAudio `json:"input_audio,omitempty"`
|
||||
// -------------------------------------
|
||||
// Anthropic
|
||||
// -------------------------------------
|
||||
Thinking *string `json:"thinking,omitempty"`
|
||||
Signature *string `json:"signature,omitempty"`
|
||||
}
|
||||
|
||||
type InputAudio struct {
|
||||
// Data is the base64 encoded audio data
|
||||
Data string `json:"data" binding:"required"`
|
||||
// Format is the audio format, should be one of the
|
||||
// following: mp3/mp4/mpeg/mpga/m4a/wav/webm/pcm16.
|
||||
// When stream=true, format should be pcm16
|
||||
Format string `json:"format"`
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user