Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
ce74e94f
authored
Aug 11, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
✨
feat: Refactor Gemini tools handling to support JSON raw message format
parent
7d3bf7c5
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
44 additions
and
9 deletions
+44
-9
dto/gemini.go
+37
-1
relay/channel/gemini/relay-gemini.go
+5
-6
service/convert.go
+2
-2
No files found.
dto/gemini.go
View file @
ce74e94f
...
...
@@ -3,16 +3,52 @@ package dto
import
(
"encoding/json"
"one-api/common"
"strings"
)
type
GeminiChatRequest
struct
{
Contents
[]
GeminiChatContent
`json:"contents"`
SafetySettings
[]
GeminiChatSafetySettings
`json:"safetySettings,omitempty"`
GenerationConfig
GeminiChatGenerationConfig
`json:"generationConfig,omitempty"`
Tools
[]
GeminiChatTool
`json:"tools,omitempty"`
Tools
json
.
RawMessage
`json:"tools,omitempty"`
SystemInstructions
*
GeminiChatContent
`json:"systemInstruction,omitempty"`
}
func
(
r
*
GeminiChatRequest
)
GetTools
()
[]
GeminiChatTool
{
var
tools
[]
GeminiChatTool
if
strings
.
HasSuffix
(
string
(
r
.
Tools
),
"["
)
{
// is array
if
err
:=
common
.
Unmarshal
(
r
.
Tools
,
&
tools
);
err
!=
nil
{
common
.
LogError
(
nil
,
"error_unmarshalling_tools: "
+
err
.
Error
())
return
nil
}
}
else
if
strings
.
HasPrefix
(
string
(
r
.
Tools
),
"{"
)
{
// is object
singleTool
:=
GeminiChatTool
{}
if
err
:=
common
.
Unmarshal
(
r
.
Tools
,
&
singleTool
);
err
!=
nil
{
common
.
LogError
(
nil
,
"error_unmarshalling_single_tool: "
+
err
.
Error
())
return
nil
}
tools
=
[]
GeminiChatTool
{
singleTool
}
}
return
tools
}
func
(
r
*
GeminiChatRequest
)
SetTools
(
tools
[]
GeminiChatTool
)
{
if
len
(
tools
)
==
0
{
r
.
Tools
=
json
.
RawMessage
(
"[]"
)
return
}
// Marshal the tools to JSON
data
,
err
:=
common
.
Marshal
(
tools
)
if
err
!=
nil
{
common
.
LogError
(
nil
,
"error_marshalling_tools: "
+
err
.
Error
())
return
}
r
.
Tools
=
data
}
type
GeminiThinkingConfig
struct
{
IncludeThoughts
bool
`json:"includeThoughts,omitempty"`
ThinkingBudget
*
int
`json:"thinkingBudget,omitempty"`
...
...
relay/channel/gemini/relay-gemini.go
View file @
ce74e94f
...
...
@@ -267,24 +267,23 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest, info *relaycommon
tool
.
Function
.
Parameters
=
cleanedParams
functions
=
append
(
functions
,
tool
.
Function
)
}
geminiTools
:=
geminiRequest
.
GetTools
()
if
codeExecution
{
gemini
Request
.
Tools
=
append
(
geminiRequest
.
Tools
,
dto
.
GeminiChatTool
{
gemini
Tools
=
append
(
gemini
Tools
,
dto
.
GeminiChatTool
{
CodeExecution
:
make
(
map
[
string
]
string
),
})
}
if
googleSearch
{
gemini
Request
.
Tools
=
append
(
geminiRequest
.
Tools
,
dto
.
GeminiChatTool
{
gemini
Tools
=
append
(
gemini
Tools
,
dto
.
GeminiChatTool
{
GoogleSearch
:
make
(
map
[
string
]
string
),
})
}
if
len
(
functions
)
>
0
{
gemini
Request
.
Tools
=
append
(
geminiRequest
.
Tools
,
dto
.
GeminiChatTool
{
gemini
Tools
=
append
(
gemini
Tools
,
dto
.
GeminiChatTool
{
FunctionDeclarations
:
functions
,
})
}
// common.SysLog("tools: " + fmt.Sprintf("%+v", geminiRequest.Tools))
// json_data, _ := json.Marshal(geminiRequest.Tools)
// common.SysLog("tools_json: " + string(json_data))
geminiRequest
.
SetTools
(
geminiTools
)
}
if
textRequest
.
ResponseFormat
!=
nil
&&
(
textRequest
.
ResponseFormat
.
Type
==
"json_schema"
||
textRequest
.
ResponseFormat
.
Type
==
"json_object"
)
{
...
...
service/convert.go
View file @
ce74e94f
...
...
@@ -569,9 +569,9 @@ func GeminiToOpenAIRequest(geminiRequest *dto.GeminiChatRequest, info *relaycomm
}
// 转换工具调用
if
len
(
geminiRequest
.
Tools
)
>
0
{
if
len
(
geminiRequest
.
GetTools
()
)
>
0
{
var
tools
[]
dto
.
ToolCallRequest
for
_
,
tool
:=
range
geminiRequest
.
Tools
{
for
_
,
tool
:=
range
geminiRequest
.
GetTools
()
{
if
tool
.
FunctionDeclarations
!=
nil
{
// 将 Gemini 的 FunctionDeclarations 转换为 OpenAI 的 ToolCallRequest
functionDeclarations
,
ok
:=
tool
.
FunctionDeclarations
.
([]
dto
.
FunctionRequest
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment