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
95f0ed18
authored
Apr 10, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: implement parameter cleaning for Gemini functions
parent
984f91d1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
99 additions
and
5 deletions
+99
-5
relay/channel/gemini/relay-gemini.go
+99
-5
No files found.
relay/channel/gemini/relay-gemini.go
View file @
95f0ed18
...
@@ -56,6 +56,7 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -56,6 +56,7 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
continue
continue
}
}
if
tool
.
Function
.
Parameters
!=
nil
{
if
tool
.
Function
.
Parameters
!=
nil
{
params
,
ok
:=
tool
.
Function
.
Parameters
.
(
map
[
string
]
interface
{})
params
,
ok
:=
tool
.
Function
.
Parameters
.
(
map
[
string
]
interface
{})
if
ok
{
if
ok
{
if
props
,
hasProps
:=
params
[
"properties"
]
.
(
map
[
string
]
interface
{});
hasProps
{
if
props
,
hasProps
:=
params
[
"properties"
]
.
(
map
[
string
]
interface
{});
hasProps
{
...
@@ -65,6 +66,9 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -65,6 +66,9 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
}
}
}
}
}
// Clean the parameters before appending
cleanedParams
:=
cleanFunctionParameters
(
tool
.
Function
.
Parameters
)
tool
.
Function
.
Parameters
=
cleanedParams
functions
=
append
(
functions
,
tool
.
Function
)
functions
=
append
(
functions
,
tool
.
Function
)
}
}
if
codeExecution
{
if
codeExecution
{
...
@@ -86,11 +90,11 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -86,11 +90,11 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
// json_data, _ := json.Marshal(geminiRequest.Tools)
// json_data, _ := json.Marshal(geminiRequest.Tools)
// common.SysLog("tools_json: " + string(json_data))
// common.SysLog("tools_json: " + string(json_data))
}
else
if
textRequest
.
Functions
!=
nil
{
}
else
if
textRequest
.
Functions
!=
nil
{
geminiRequest
.
Tools
=
[]
GeminiChatTool
{
//
geminiRequest.Tools = []GeminiChatTool{
{
//
{
FunctionDeclarations
:
textRequest
.
Functions
,
//
FunctionDeclarations: textRequest.Functions,
},
//
},
}
//
}
}
}
if
textRequest
.
ResponseFormat
!=
nil
&&
(
textRequest
.
ResponseFormat
.
Type
==
"json_schema"
||
textRequest
.
ResponseFormat
.
Type
==
"json_object"
)
{
if
textRequest
.
ResponseFormat
!=
nil
&&
(
textRequest
.
ResponseFormat
.
Type
==
"json_schema"
||
textRequest
.
ResponseFormat
.
Type
==
"json_object"
)
{
...
@@ -229,6 +233,96 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -229,6 +233,96 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
return
&
geminiRequest
,
nil
return
&
geminiRequest
,
nil
}
}
// cleanFunctionParameters recursively removes unsupported fields from Gemini function parameters.
func
cleanFunctionParameters
(
params
interface
{})
interface
{}
{
if
params
==
nil
{
return
nil
}
paramMap
,
ok
:=
params
.
(
map
[
string
]
interface
{})
if
!
ok
{
// Not a map, return as is (e.g., could be an array or primitive)
return
params
}
// Create a copy to avoid modifying the original
cleanedMap
:=
make
(
map
[
string
]
interface
{})
for
k
,
v
:=
range
paramMap
{
cleanedMap
[
k
]
=
v
}
// Clean properties
if
props
,
ok
:=
cleanedMap
[
"properties"
]
.
(
map
[
string
]
interface
{});
ok
&&
props
!=
nil
{
cleanedProps
:=
make
(
map
[
string
]
interface
{})
for
propName
,
propValue
:=
range
props
{
propMap
,
ok
:=
propValue
.
(
map
[
string
]
interface
{})
if
!
ok
{
cleanedProps
[
propName
]
=
propValue
// Keep non-map properties
continue
}
// Create a copy of the property map
cleanedPropMap
:=
make
(
map
[
string
]
interface
{})
for
k
,
v
:=
range
propMap
{
cleanedPropMap
[
k
]
=
v
}
// Remove unsupported fields
delete
(
cleanedPropMap
,
"default"
)
delete
(
cleanedPropMap
,
"exclusiveMaximum"
)
delete
(
cleanedPropMap
,
"exclusiveMinimum"
)
// Check and clean 'format' for string types
if
propType
,
typeExists
:=
cleanedPropMap
[
"type"
]
.
(
string
);
typeExists
&&
propType
==
"string"
{
if
formatValue
,
formatExists
:=
cleanedPropMap
[
"format"
]
.
(
string
);
formatExists
{
if
formatValue
!=
"enum"
&&
formatValue
!=
"date-time"
{
delete
(
cleanedPropMap
,
"format"
)
}
}
}
// Recursively clean nested properties within this property if it's an object/array
// Check the type before recursing
if
propType
,
typeExists
:=
cleanedPropMap
[
"type"
]
.
(
string
);
typeExists
&&
(
propType
==
"object"
||
propType
==
"array"
)
{
cleanedProps
[
propName
]
=
cleanFunctionParameters
(
cleanedPropMap
)
}
else
{
cleanedProps
[
propName
]
=
cleanedPropMap
// Assign the cleaned map back if not recursing
}
}
cleanedMap
[
"properties"
]
=
cleanedProps
}
// Recursively clean items in arrays if needed (e.g., type: array, items: { ... })
if
items
,
ok
:=
cleanedMap
[
"items"
]
.
(
map
[
string
]
interface
{});
ok
&&
items
!=
nil
{
cleanedMap
[
"items"
]
=
cleanFunctionParameters
(
items
)
}
// Also handle items if it's an array of schemas
if
itemsArray
,
ok
:=
cleanedMap
[
"items"
]
.
([]
interface
{});
ok
{
cleanedItemsArray
:=
make
([]
interface
{},
len
(
itemsArray
))
for
i
,
item
:=
range
itemsArray
{
cleanedItemsArray
[
i
]
=
cleanFunctionParameters
(
item
)
}
cleanedMap
[
"items"
]
=
cleanedItemsArray
}
// Recursively clean other schema composition keywords if necessary
for
_
,
field
:=
range
[]
string
{
"allOf"
,
"anyOf"
,
"oneOf"
}
{
if
nested
,
ok
:=
cleanedMap
[
field
]
.
([]
interface
{});
ok
{
cleanedNested
:=
make
([]
interface
{},
len
(
nested
))
for
i
,
item
:=
range
nested
{
cleanedNested
[
i
]
=
cleanFunctionParameters
(
item
)
}
cleanedMap
[
field
]
=
cleanedNested
}
}
return
cleanedMap
}
func
removeAdditionalPropertiesWithDepth
(
schema
interface
{},
depth
int
)
interface
{}
{
func
removeAdditionalPropertiesWithDepth
(
schema
interface
{},
depth
int
)
interface
{}
{
if
depth
>=
5
{
if
depth
>=
5
{
return
schema
return
schema
...
...
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