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
411d3a62
authored
Dec 24, 2024
by
Yan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: gemini func call
parent
1e22f405
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
190 additions
and
67 deletions
+190
-67
common/str.go
+1
-3
constant/finish_reason.go
+3
-0
relay/channel/gemini/dto.go
+30
-4
relay/channel/gemini/relay-gemini.go
+156
-60
No files found.
common/str.go
View file @
411d3a62
...
@@ -35,9 +35,7 @@ func StrToMap(str string) map[string]interface{} {
...
@@ -35,9 +35,7 @@ func StrToMap(str string) map[string]interface{} {
m
:=
make
(
map
[
string
]
interface
{})
m
:=
make
(
map
[
string
]
interface
{})
err
:=
json
.
Unmarshal
([]
byte
(
str
),
&
m
)
err
:=
json
.
Unmarshal
([]
byte
(
str
),
&
m
)
if
err
!=
nil
{
if
err
!=
nil
{
return
map
[
string
]
interface
{}{
return
nil
"result"
:
str
,
}
}
}
return
m
return
m
}
}
...
...
constant/finish_reason.go
View file @
411d3a62
...
@@ -3,4 +3,7 @@ package constant
...
@@ -3,4 +3,7 @@ package constant
var
(
var
(
FinishReasonStop
=
"stop"
FinishReasonStop
=
"stop"
FinishReasonToolCalls
=
"tool_calls"
FinishReasonToolCalls
=
"tool_calls"
FinishReasonLength
=
"length"
FinishReasonFunctionCall
=
"function_call"
FinishReasonContentFilter
=
"content_filter"
)
)
relay/channel/gemini/dto.go
View file @
411d3a62
...
@@ -4,7 +4,7 @@ type GeminiChatRequest struct {
...
@@ -4,7 +4,7 @@ type GeminiChatRequest struct {
Contents
[]
GeminiChatContent
`json:"contents"`
Contents
[]
GeminiChatContent
`json:"contents"`
SafetySettings
[]
GeminiChatSafetySettings
`json:"safety_settings,omitempty"`
SafetySettings
[]
GeminiChatSafetySettings
`json:"safety_settings,omitempty"`
GenerationConfig
GeminiChatGenerationConfig
`json:"generation_config,omitempty"`
GenerationConfig
GeminiChatGenerationConfig
`json:"generation_config,omitempty"`
Tools
[]
GeminiChatTool
s
`json:"tools,omitempty"`
Tools
[]
GeminiChatTool
`json:"tools,omitempty"`
SystemInstructions
*
GeminiChatContent
`json:"system_instruction,omitempty"`
SystemInstructions
*
GeminiChatContent
`json:"system_instruction,omitempty"`
}
}
...
@@ -18,9 +18,29 @@ type FunctionCall struct {
...
@@ -18,9 +18,29 @@ type FunctionCall struct {
Arguments
any
`json:"args"`
Arguments
any
`json:"args"`
}
}
type
GeminiFunctionResponseContent
struct
{
Name
string
`json:"name"`
Content
any
`json:"content"`
}
type
FunctionResponse
struct
{
type
FunctionResponse
struct
{
Name
string
`json:"name"`
Name
string
`json:"name"`
Response
any
`json:"response"`
Response
GeminiFunctionResponseContent
`json:"response"`
}
type
GeminiPartExecutableCode
struct
{
Language
string
`json:"language,omitempty"`
Code
string
`json:"code,omitempty"`
}
type
GeminiPartCodeExecutionResult
struct
{
Outcome
string
`json:"outcome,omitempty"`
Output
string
`json:"output,omitempty"`
}
type
GeminiFileData
struct
{
MimeType
string
`json:"mimeType,omitempty"`
FileUri
string
`json:"fileUri,omitempty"`
}
}
type
GeminiPart
struct
{
type
GeminiPart
struct
{
...
@@ -28,6 +48,9 @@ type GeminiPart struct {
...
@@ -28,6 +48,9 @@ type GeminiPart struct {
InlineData
*
GeminiInlineData
`json:"inlineData,omitempty"`
InlineData
*
GeminiInlineData
`json:"inlineData,omitempty"`
FunctionCall
*
FunctionCall
`json:"functionCall,omitempty"`
FunctionCall
*
FunctionCall
`json:"functionCall,omitempty"`
FunctionResponse
*
FunctionResponse
`json:"functionResponse,omitempty"`
FunctionResponse
*
FunctionResponse
`json:"functionResponse,omitempty"`
FileData
*
GeminiFileData
`json:"fileData,omitempty"`
ExecutableCode
*
GeminiPartExecutableCode
`json:"executableCode,omitempty"`
CodeExecutionResult
*
GeminiPartCodeExecutionResult
`json:"codeExecutionResult,omitempty"`
}
}
type
GeminiChatContent
struct
{
type
GeminiChatContent
struct
{
...
@@ -40,8 +63,10 @@ type GeminiChatSafetySettings struct {
...
@@ -40,8 +63,10 @@ type GeminiChatSafetySettings struct {
Threshold
string
`json:"threshold"`
Threshold
string
`json:"threshold"`
}
}
type
GeminiChatTool
s
struct
{
type
GeminiChatTool
struct
{
GoogleSearch
any
`json:"googleSearch,omitempty"`
GoogleSearch
any
`json:"googleSearch,omitempty"`
GoogleSearchRetrieval
any
`json:"googleSearchRetrieval,omitempty"`
CodeExecution
any
`json:"codeExecution,omitempty"`
FunctionDeclarations
any
`json:"functionDeclarations,omitempty"`
FunctionDeclarations
any
`json:"functionDeclarations,omitempty"`
}
}
...
@@ -54,11 +79,12 @@ type GeminiChatGenerationConfig struct {
...
@@ -54,11 +79,12 @@ type GeminiChatGenerationConfig struct {
StopSequences
[]
string
`json:"stopSequences,omitempty"`
StopSequences
[]
string
`json:"stopSequences,omitempty"`
ResponseMimeType
string
`json:"responseMimeType,omitempty"`
ResponseMimeType
string
`json:"responseMimeType,omitempty"`
ResponseSchema
any
`json:"responseSchema,omitempty"`
ResponseSchema
any
`json:"responseSchema,omitempty"`
Seed
int64
`json:"seed,omitempty"`
}
}
type
GeminiChatCandidate
struct
{
type
GeminiChatCandidate
struct
{
Content
GeminiChatContent
`json:"content"`
Content
GeminiChatContent
`json:"content"`
FinishReason
string
`json:"finishReason"`
FinishReason
*
string
`json:"finishReason"`
Index
int64
`json:"index"`
Index
int64
`json:"index"`
SafetyRatings
[]
GeminiChatSafetyRating
`json:"safetyRatings"`
SafetyRatings
[]
GeminiChatSafetyRating
`json:"safetyRatings"`
}
}
...
...
relay/channel/gemini/relay-gemini.go
View file @
411d3a62
...
@@ -18,6 +18,7 @@ import (
...
@@ -18,6 +18,7 @@ import (
// Setting safety to the lowest possible values since Gemini is already powerless enough
// Setting safety to the lowest possible values since Gemini is already powerless enough
func
CovertGemini2OpenAI
(
textRequest
dto
.
GeneralOpenAIRequest
)
(
*
GeminiChatRequest
,
error
)
{
func
CovertGemini2OpenAI
(
textRequest
dto
.
GeneralOpenAIRequest
)
(
*
GeminiChatRequest
,
error
)
{
geminiRequest
:=
GeminiChatRequest
{
geminiRequest
:=
GeminiChatRequest
{
Contents
:
make
([]
GeminiChatContent
,
0
,
len
(
textRequest
.
Messages
)),
Contents
:
make
([]
GeminiChatContent
,
0
,
len
(
textRequest
.
Messages
)),
SafetySettings
:
[]
GeminiChatSafetySettings
{
SafetySettings
:
[]
GeminiChatSafetySettings
{
...
@@ -46,16 +47,24 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -46,16 +47,24 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
Temperature
:
textRequest
.
Temperature
,
Temperature
:
textRequest
.
Temperature
,
TopP
:
textRequest
.
TopP
,
TopP
:
textRequest
.
TopP
,
MaxOutputTokens
:
textRequest
.
MaxTokens
,
MaxOutputTokens
:
textRequest
.
MaxTokens
,
Seed
:
int64
(
textRequest
.
Seed
),
},
},
}
}
// openaiContent.FuncToToolCalls()
if
textRequest
.
Tools
!=
nil
{
if
textRequest
.
Tools
!=
nil
{
functions
:=
make
([]
dto
.
FunctionCall
,
0
,
len
(
textRequest
.
Tools
))
functions
:=
make
([]
dto
.
FunctionCall
,
0
,
len
(
textRequest
.
Tools
))
googleSearch
:=
false
googleSearch
:=
false
codeExecution
:=
false
for
_
,
tool
:=
range
textRequest
.
Tools
{
for
_
,
tool
:=
range
textRequest
.
Tools
{
if
tool
.
Function
.
Name
==
"googleSearch"
{
if
tool
.
Function
.
Name
==
"googleSearch"
{
googleSearch
=
true
googleSearch
=
true
continue
continue
}
}
if
tool
.
Function
.
Name
==
"codeExecution"
{
codeExecution
=
true
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
{
...
@@ -68,25 +77,32 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -68,25 +77,32 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
}
functions
=
append
(
functions
,
tool
.
Function
)
functions
=
append
(
functions
,
tool
.
Function
)
}
}
if
len
(
functions
)
>
0
{
if
codeExecution
{
geminiRequest
.
Tools
=
[]
GeminiChatTools
{
geminiRequest
.
Tools
=
append
(
geminiRequest
.
Tools
,
GeminiChatTool
{
{
CodeExecution
:
make
(
map
[
string
]
string
),
FunctionDeclarations
:
functions
,
})
},
}
}
}
if
googleSearch
{
if
googleSearch
{
geminiRequest
.
Tools
=
append
(
geminiRequest
.
Tools
,
GeminiChatTool
s
{
geminiRequest
.
Tools
=
append
(
geminiRequest
.
Tools
,
GeminiChatTool
{
GoogleSearch
:
make
(
map
[
string
]
string
),
GoogleSearch
:
make
(
map
[
string
]
string
),
})
})
}
}
if
len
(
functions
)
>
0
{
geminiRequest
.
Tools
=
append
(
geminiRequest
.
Tools
,
GeminiChatTool
{
FunctionDeclarations
:
functions
,
})
}
// common.SysLog("tools: " + fmt.Sprintf("%+v", geminiRequest.Tools))
// json_data, _ := json.Marshal(geminiRequest.Tools)
// common.SysLog("tools_json: " + string(json_data))
}
else
if
textRequest
.
Functions
!=
nil
{
}
else
if
textRequest
.
Functions
!=
nil
{
geminiRequest
.
Tools
=
[]
GeminiChatTool
s
{
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"
)
{
geminiRequest
.
GenerationConfig
.
ResponseMimeType
=
"application/json"
geminiRequest
.
GenerationConfig
.
ResponseMimeType
=
"application/json"
...
@@ -96,20 +112,14 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -96,20 +112,14 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
}
}
}
tool_call_ids
:=
make
(
map
[
string
]
string
)
tool_call_ids
:=
make
(
map
[
string
]
string
)
var
system_content
[]
string
//shouldAddDummyModelMessage := false
//shouldAddDummyModelMessage := false
for
_
,
message
:=
range
textRequest
.
Messages
{
for
_
,
message
:=
range
textRequest
.
Messages
{
if
message
.
Role
==
"system"
{
if
message
.
Role
==
"system"
{
geminiRequest
.
SystemInstructions
=
&
GeminiChatContent
{
system_content
=
append
(
system_content
,
message
.
StringContent
())
Parts
:
[]
GeminiPart
{
{
Text
:
message
.
StringContent
(),
},
},
}
continue
continue
}
else
if
message
.
Role
==
"tool"
{
}
else
if
message
.
Role
==
"tool"
||
message
.
Role
==
"function"
{
if
len
(
geminiRequest
.
Contents
)
==
0
||
geminiRequest
.
Contents
[
len
(
geminiRequest
.
Contents
)
-
1
]
.
Role
!=
"user
"
{
if
len
(
geminiRequest
.
Contents
)
==
0
||
geminiRequest
.
Contents
[
len
(
geminiRequest
.
Contents
)
-
1
]
.
Role
==
"model
"
{
geminiRequest
.
Contents
=
append
(
geminiRequest
.
Contents
,
GeminiChatContent
{
geminiRequest
.
Contents
=
append
(
geminiRequest
.
Contents
,
GeminiChatContent
{
Role
:
"user"
,
Role
:
"user"
,
})
})
...
@@ -121,9 +131,16 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -121,9 +131,16 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
else
if
val
,
exists
:=
tool_call_ids
[
message
.
ToolCallId
];
exists
{
}
else
if
val
,
exists
:=
tool_call_ids
[
message
.
ToolCallId
];
exists
{
name
=
val
name
=
val
}
}
content
:=
common
.
StrToMap
(
message
.
StringContent
())
functionResp
:=
&
FunctionResponse
{
functionResp
:=
&
FunctionResponse
{
Name
:
name
,
Name
:
name
,
Response
:
common
.
StrToMap
(
message
.
StringContent
()),
Response
:
GeminiFunctionResponseContent
{
Name
:
name
,
Content
:
content
,
},
}
if
content
==
nil
{
functionResp
.
Response
.
Content
=
message
.
StringContent
()
}
}
*
parts
=
append
(
*
parts
,
GeminiPart
{
*
parts
=
append
(
*
parts
,
GeminiPart
{
FunctionResponse
:
functionResp
,
FunctionResponse
:
functionResp
,
...
@@ -134,26 +151,35 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -134,26 +151,35 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
content
:=
GeminiChatContent
{
content
:=
GeminiChatContent
{
Role
:
message
.
Role
,
Role
:
message
.
Role
,
}
}
isToolCall
:=
false
//
isToolCall := false
if
message
.
ToolCalls
!=
nil
{
if
message
.
ToolCalls
!=
nil
{
message
.
Role
=
"model"
//
message.Role = "model"
isToolCall
=
true
//
isToolCall = true
for
_
,
call
:=
range
message
.
ParseToolCalls
()
{
for
_
,
call
:=
range
message
.
ParseToolCalls
()
{
args
:=
map
[
string
]
interface
{}{}
if
call
.
Function
.
Arguments
!=
""
{
if
json
.
Unmarshal
([]
byte
(
call
.
Function
.
Arguments
),
&
args
)
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"invalid arguments for function %s, args: %s"
,
call
.
Function
.
Name
,
call
.
Function
.
Arguments
)
}
}
toolCall
:=
GeminiPart
{
toolCall
:=
GeminiPart
{
FunctionCall
:
&
FunctionCall
{
FunctionCall
:
&
FunctionCall
{
FunctionName
:
call
.
Function
.
Name
,
FunctionName
:
call
.
Function
.
Name
,
Arguments
:
call
.
Function
.
Parameter
s
,
Arguments
:
arg
s
,
},
},
}
}
parts
=
append
(
parts
,
toolCall
)
parts
=
append
(
parts
,
toolCall
)
tool_call_ids
[
call
.
ID
]
=
call
.
Function
.
Name
tool_call_ids
[
call
.
ID
]
=
call
.
Function
.
Name
}
}
}
}
if
!
isToolCall
{
openaiContent
:=
message
.
ParseContent
()
openaiContent
:=
message
.
ParseContent
()
imageNum
:=
0
imageNum
:=
0
for
_
,
part
:=
range
openaiContent
{
for
_
,
part
:=
range
openaiContent
{
if
part
.
Type
==
dto
.
ContentTypeText
{
if
part
.
Type
==
dto
.
ContentTypeText
{
if
part
.
Text
==
""
{
continue
}
parts
=
append
(
parts
,
GeminiPart
{
parts
=
append
(
parts
,
GeminiPart
{
Text
:
part
.
Text
,
Text
:
part
.
Text
,
})
})
...
@@ -187,7 +213,6 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -187,7 +213,6 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
}
}
}
}
}
}
content
.
Parts
=
parts
content
.
Parts
=
parts
...
@@ -197,6 +222,17 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
...
@@ -197,6 +222,17 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) (*GeminiChatReque
}
}
geminiRequest
.
Contents
=
append
(
geminiRequest
.
Contents
,
content
)
geminiRequest
.
Contents
=
append
(
geminiRequest
.
Contents
,
content
)
}
}
if
len
(
system_content
)
>
0
{
geminiRequest
.
SystemInstructions
=
&
GeminiChatContent
{
Parts
:
[]
GeminiPart
{
{
Text
:
strings
.
Join
(
system_content
,
"
\n
"
),
},
},
}
}
return
&
geminiRequest
,
nil
return
&
geminiRequest
,
nil
}
}
...
@@ -240,15 +276,15 @@ func removeAdditionalPropertiesWithDepth(schema interface{}, depth int) interfac
...
@@ -240,15 +276,15 @@ func removeAdditionalPropertiesWithDepth(schema interface{}, depth int) interfac
return
v
return
v
}
}
func
(
g
*
GeminiChatResponse
)
GetResponseText
()
string
{
//
func (g *GeminiChatResponse) GetResponseText() string {
if
g
==
nil
{
//
if g == nil {
return
""
//
return ""
}
//
}
if
len
(
g
.
Candidates
)
>
0
&&
len
(
g
.
Candidates
[
0
]
.
Content
.
Parts
)
>
0
{
//
if len(g.Candidates) > 0 && len(g.Candidates[0].Content.Parts) > 0 {
return
g
.
Candidates
[
0
]
.
Content
.
Parts
[
0
]
.
Text
//
return g.Candidates[0].Content.Parts[0].Text
}
//
}
return
""
//
return ""
}
//
}
func
getToolCall
(
item
*
GeminiPart
)
*
dto
.
ToolCall
{
func
getToolCall
(
item
*
GeminiPart
)
*
dto
.
ToolCall
{
argsBytes
,
err
:=
json
.
Marshal
(
item
.
FunctionCall
.
Arguments
)
argsBytes
,
err
:=
json
.
Marshal
(
item
.
FunctionCall
.
Arguments
)
...
@@ -298,11 +334,10 @@ func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResp
...
@@ -298,11 +334,10 @@ func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResp
Choices
:
make
([]
dto
.
OpenAITextResponseChoice
,
0
,
len
(
response
.
Candidates
)),
Choices
:
make
([]
dto
.
OpenAITextResponseChoice
,
0
,
len
(
response
.
Candidates
)),
}
}
content
,
_
:=
json
.
Marshal
(
""
)
content
,
_
:=
json
.
Marshal
(
""
)
for
i
,
candidate
:=
range
response
.
Candidates
{
is_tool_call
:=
false
// jsonData, _ := json.MarshalIndent(candidate, "", " ")
for
_
,
candidate
:=
range
response
.
Candidates
{
// common.SysLog(fmt.Sprintf("candidate: %v", string(jsonData)))
choice
:=
dto
.
OpenAITextResponseChoice
{
choice
:=
dto
.
OpenAITextResponseChoice
{
Index
:
i
,
Index
:
i
nt
(
candidate
.
Index
)
,
Message
:
dto
.
Message
{
Message
:
dto
.
Message
{
Role
:
"assistant"
,
Role
:
"assistant"
,
Content
:
content
,
Content
:
content
,
...
@@ -319,48 +354,107 @@ func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResp
...
@@ -319,48 +354,107 @@ func responseGeminiChat2OpenAI(response *GeminiChatResponse) *dto.OpenAITextResp
tool_calls
=
append
(
tool_calls
,
*
call
)
tool_calls
=
append
(
tool_calls
,
*
call
)
}
}
}
else
{
}
else
{
if
part
.
ExecutableCode
!=
nil
{
texts
=
append
(
texts
,
"```"
+
part
.
ExecutableCode
.
Language
+
"
\n
"
+
part
.
ExecutableCode
.
Code
+
"
\n
```"
)
}
else
if
part
.
CodeExecutionResult
!=
nil
{
texts
=
append
(
texts
,
"```output
\n
"
+
part
.
CodeExecutionResult
.
Output
+
"
\n
```"
)
}
else
{
// 过滤掉空行
if
part
.
Text
!=
"
\n
"
{
texts
=
append
(
texts
,
part
.
Text
)
texts
=
append
(
texts
,
part
.
Text
)
}
}
}
}
choice
.
Message
.
SetStringContent
(
strings
.
Join
(
texts
,
"
\n
"
))
}
}
if
len
(
tool_calls
)
>
0
{
choice
.
Message
.
SetToolCalls
(
tool_calls
)
choice
.
Message
.
SetToolCalls
(
tool_calls
)
is_tool_call
=
true
}
}
// 过滤掉空行
choice
.
Message
.
SetStringContent
(
strings
.
Join
(
texts
,
"
\n
"
))
}
if
candidate
.
FinishReason
!=
nil
{
switch
*
candidate
.
FinishReason
{
case
"STOP"
:
choice
.
FinishReason
=
constant
.
FinishReasonStop
case
"MAX_TOKENS"
:
choice
.
FinishReason
=
constant
.
FinishReasonLength
default
:
choice
.
FinishReason
=
constant
.
FinishReasonContentFilter
}
}
if
is_tool_call
{
choice
.
FinishReason
=
constant
.
FinishReasonToolCalls
}
fullTextResponse
.
Choices
=
append
(
fullTextResponse
.
Choices
,
choice
)
fullTextResponse
.
Choices
=
append
(
fullTextResponse
.
Choices
,
choice
)
}
}
return
&
fullTextResponse
return
&
fullTextResponse
}
}
func
streamResponseGeminiChat2OpenAI
(
geminiResponse
*
GeminiChatResponse
)
*
dto
.
ChatCompletionsStreamResponse
{
func
streamResponseGeminiChat2OpenAI
(
geminiResponse
*
GeminiChatResponse
)
(
*
dto
.
ChatCompletionsStreamResponse
,
bool
)
{
var
choice
dto
.
ChatCompletionsStreamResponseChoice
choices
:=
make
([]
dto
.
ChatCompletionsStreamResponseChoice
,
0
,
len
(
geminiResponse
.
Candidates
))
//choice.Delta.SetContentString(geminiResponse.GetResponseText())
is_stop
:=
false
if
len
(
geminiResponse
.
Candidates
)
>
0
&&
len
(
geminiResponse
.
Candidates
[
0
]
.
Content
.
Parts
)
>
0
{
for
_
,
candidate
:=
range
geminiResponse
.
Candidates
{
if
candidate
.
FinishReason
!=
nil
&&
*
candidate
.
FinishReason
==
"STOP"
{
is_stop
=
true
candidate
.
FinishReason
=
nil
}
choice
:=
dto
.
ChatCompletionsStreamResponseChoice
{
Index
:
int
(
candidate
.
Index
),
Delta
:
dto
.
ChatCompletionsStreamResponseChoiceDelta
{
Role
:
"assistant"
,
},
}
var
texts
[]
string
var
texts
[]
string
var
tool_calls
[]
dto
.
ToolCall
isTools
:=
false
for
_
,
part
:=
range
geminiResponse
.
Candidates
[
0
]
.
Content
.
Parts
{
if
candidate
.
FinishReason
!=
nil
{
// p := GeminiConvertFinishReason(*candidate.FinishReason)
switch
*
candidate
.
FinishReason
{
case
"STOP"
:
choice
.
FinishReason
=
&
constant
.
FinishReasonStop
case
"MAX_TOKENS"
:
choice
.
FinishReason
=
&
constant
.
FinishReasonLength
default
:
choice
.
FinishReason
=
&
constant
.
FinishReasonContentFilter
}
}
for
_
,
part
:=
range
candidate
.
Content
.
Parts
{
if
part
.
FunctionCall
!=
nil
{
if
part
.
FunctionCall
!=
nil
{
isTools
=
true
if
call
:=
getToolCall
(
&
part
);
call
!=
nil
{
if
call
:=
getToolCall
(
&
part
);
call
!=
nil
{
tool_calls
=
append
(
tool_c
alls
,
*
call
)
choice
.
Delta
.
ToolCalls
=
append
(
choice
.
Delta
.
ToolC
alls
,
*
call
)
}
}
}
else
{
}
else
{
if
part
.
ExecutableCode
!=
nil
{
texts
=
append
(
texts
,
"```"
+
part
.
ExecutableCode
.
Language
+
"
\n
"
+
part
.
ExecutableCode
.
Code
+
"
\n
```
\n
"
)
}
else
if
part
.
CodeExecutionResult
!=
nil
{
texts
=
append
(
texts
,
"```output
\n
"
+
part
.
CodeExecutionResult
.
Output
+
"
\n
```
\n
"
)
}
else
{
if
part
.
Text
!=
"
\n
"
{
texts
=
append
(
texts
,
part
.
Text
)
texts
=
append
(
texts
,
part
.
Text
)
}
}
}
}
if
len
(
texts
)
>
0
{
choice
.
Delta
.
SetContentString
(
strings
.
Join
(
texts
,
"
\n
"
))
}
}
if
len
(
tool_calls
)
>
0
{
choice
.
Delta
.
ToolCalls
=
tool_calls
}
}
choice
.
Delta
.
SetContentString
(
strings
.
Join
(
texts
,
"
\n
"
))
if
isTools
{
choice
.
FinishReason
=
&
constant
.
FinishReasonToolCalls
}
choices
=
append
(
choices
,
choice
)
}
}
var
response
dto
.
ChatCompletionsStreamResponse
var
response
dto
.
ChatCompletionsStreamResponse
response
.
Object
=
"chat.completion.chunk"
response
.
Object
=
"chat.completion.chunk"
response
.
Model
=
"gemini"
response
.
Model
=
"gemini"
response
.
Choices
=
[]
dto
.
ChatCompletionsStreamResponseChoice
{
choice
}
response
.
Choices
=
choices
return
&
response
return
&
response
,
is_stop
}
}
func
GeminiChatStreamHandler
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
RelayInfo
)
(
*
dto
.
OpenAIErrorWithStatusCode
,
*
dto
.
Usage
)
{
func
GeminiChatStreamHandler
(
c
*
gin
.
Context
,
resp
*
http
.
Response
,
info
*
relaycommon
.
RelayInfo
)
(
*
dto
.
OpenAIErrorWithStatusCode
,
*
dto
.
Usage
)
{
responseText
:=
""
//
responseText := ""
id
:=
fmt
.
Sprintf
(
"chatcmpl-%s"
,
common
.
GetUUID
())
id
:=
fmt
.
Sprintf
(
"chatcmpl-%s"
,
common
.
GetUUID
())
createAt
:=
common
.
GetTimestamp
()
createAt
:=
common
.
GetTimestamp
()
var
usage
=
&
dto
.
Usage
{}
var
usage
=
&
dto
.
Usage
{}
...
@@ -384,14 +478,11 @@ func GeminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycom
...
@@ -384,14 +478,11 @@ func GeminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycom
continue
continue
}
}
response
:=
streamResponseGeminiChat2OpenAI
(
&
geminiResponse
)
response
,
is_stop
:=
streamResponseGeminiChat2OpenAI
(
&
geminiResponse
)
if
response
==
nil
{
continue
}
response
.
Id
=
id
response
.
Id
=
id
response
.
Created
=
createAt
response
.
Created
=
createAt
response
.
Model
=
info
.
UpstreamModelName
response
.
Model
=
info
.
UpstreamModelName
responseText
+=
response
.
Choices
[
0
]
.
Delta
.
GetContentString
()
//
responseText += response.Choices[0].Delta.GetContentString()
if
geminiResponse
.
UsageMetadata
.
TotalTokenCount
!=
0
{
if
geminiResponse
.
UsageMetadata
.
TotalTokenCount
!=
0
{
usage
.
PromptTokens
=
geminiResponse
.
UsageMetadata
.
PromptTokenCount
usage
.
PromptTokens
=
geminiResponse
.
UsageMetadata
.
PromptTokenCount
usage
.
CompletionTokens
=
geminiResponse
.
UsageMetadata
.
CandidatesTokenCount
usage
.
CompletionTokens
=
geminiResponse
.
UsageMetadata
.
CandidatesTokenCount
...
@@ -400,12 +491,17 @@ func GeminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycom
...
@@ -400,12 +491,17 @@ func GeminiChatStreamHandler(c *gin.Context, resp *http.Response, info *relaycom
if
err
!=
nil
{
if
err
!=
nil
{
common
.
LogError
(
c
,
err
.
Error
())
common
.
LogError
(
c
,
err
.
Error
())
}
}
}
if
is_stop
{
response
:=
service
.
GenerateStopResponse
(
id
,
createAt
,
info
.
UpstreamModelName
,
constant
.
FinishReasonStop
)
response
:=
service
.
GenerateStopResponse
(
id
,
createAt
,
info
.
UpstreamModelName
,
constant
.
FinishReasonStop
)
service
.
ObjectData
(
c
,
response
)
service
.
ObjectData
(
c
,
response
)
}
}
var
response
*
dto
.
ChatCompletionsStreamResponse
usage
.
TotalTokens
=
usage
.
PromptTokens
+
usage
.
CompletionTokens
usage
.
TotalTokens
=
usage
.
PromptTokens
+
usage
.
CompletionTokens
usage
.
PromptTokensDetails
.
TextTokens
=
usage
.
PromptTokens
usage
.
CompletionTokenDetails
.
TextTokens
=
usage
.
CompletionTokens
if
info
.
ShouldIncludeUsage
{
if
info
.
ShouldIncludeUsage
{
response
=
service
.
GenerateFinalUsageResponse
(
id
,
createAt
,
info
.
UpstreamModelName
,
*
usage
)
response
=
service
.
GenerateFinalUsageResponse
(
id
,
createAt
,
info
.
UpstreamModelName
,
*
usage
)
...
...
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