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
cd6b3296
authored
Dec 16, 2024
by
CalciumIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: support gemini SystemInstructions #408
parent
c3d4de67
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
36 additions
and
29 deletions
+36
-29
relay/channel/gemini/constant.go
+1
-1
relay/channel/gemini/dto.go
+5
-4
relay/channel/gemini/relay-gemini.go
+30
-24
No files found.
relay/channel/gemini/constant.go
View file @
cd6b3296
...
@@ -5,7 +5,7 @@ const (
...
@@ -5,7 +5,7 @@ const (
)
)
var
ModelList
=
[]
string
{
var
ModelList
=
[]
string
{
"gemini-1.5-pro-latest"
,
"gemini-1.5-flash-latest"
,
"gemini-ultra"
,
"gemini-1.5-pro-latest"
,
"gemini-1.5-flash-latest"
,
"gemini-1.5-pro-exp-0827"
,
"gemini-1.5-flash-exp-0827"
,
"gemini-1.5-pro-exp-0827"
,
"gemini-1.5-flash-exp-0827"
,
"gemini-exp-1114"
,
"gemini-exp-1206"
,
"gemini-exp-1114"
,
"gemini-exp-1206"
,
"gemini-2.0-flash-exp"
,
"gemini-2.0-flash-exp"
,
...
...
relay/channel/gemini/dto.go
View file @
cd6b3296
package
gemini
package
gemini
type
GeminiChatRequest
struct
{
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
[]
GeminiChatTools
`json:"tools,omitempty"`
Tools
[]
GeminiChatTools
`json:"tools,omitempty"`
SystemInstructions
*
GeminiPart
`json:"system_instructions,omitempty"`
}
}
type
GeminiInlineData
struct
{
type
GeminiInlineData
struct
{
...
...
relay/channel/gemini/relay-gemini.go
View file @
cd6b3296
...
@@ -72,21 +72,27 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) *GeminiChatReques
...
@@ -72,21 +72,27 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) *GeminiChatReques
},
},
}
}
}
}
shouldAddDummyModelMessage
:=
false
//
shouldAddDummyModelMessage := false
for
_
,
message
:=
range
textRequest
.
Messages
{
for
_
,
message
:=
range
textRequest
.
Messages
{
if
message
.
Role
==
"system"
{
geminiRequest
.
SystemInstructions
=
&
GeminiPart
{
Text
:
message
.
StringContent
(),
}
continue
}
content
:=
GeminiChatContent
{
content
:=
GeminiChatContent
{
Role
:
message
.
Role
,
Role
:
message
.
Role
,
Parts
:
[]
GeminiPart
{
//
Parts: []GeminiPart{
{
//
{
Text
:
message
.
StringContent
(),
//
Text: message.StringContent(),
},
//
},
},
//
},
}
}
openaiContent
:=
message
.
ParseContent
()
openaiContent
:=
message
.
ParseContent
()
var
parts
[]
GeminiPart
var
parts
[]
GeminiPart
imageNum
:=
0
imageNum
:=
0
for
_
,
part
:=
range
openaiContent
{
for
_
,
part
:=
range
openaiContent
{
if
part
.
Type
==
dto
.
ContentTypeText
{
if
part
.
Type
==
dto
.
ContentTypeText
{
parts
=
append
(
parts
,
GeminiPart
{
parts
=
append
(
parts
,
GeminiPart
{
Text
:
part
.
Text
,
Text
:
part
.
Text
,
...
@@ -127,24 +133,24 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) *GeminiChatReques
...
@@ -127,24 +133,24 @@ func CovertGemini2OpenAI(textRequest dto.GeneralOpenAIRequest) *GeminiChatReques
content
.
Role
=
"model"
content
.
Role
=
"model"
}
}
// Converting system prompt to prompt from user for the same reason
// Converting system prompt to prompt from user for the same reason
if
content
.
Role
==
"system"
{
//
if content.Role == "system" {
content
.
Role
=
"user"
//
content.Role = "user"
shouldAddDummyModelMessage
=
true
//
shouldAddDummyModelMessage = true
}
//
}
geminiRequest
.
Contents
=
append
(
geminiRequest
.
Contents
,
content
)
geminiRequest
.
Contents
=
append
(
geminiRequest
.
Contents
,
content
)
//
// If a system message is the last message, we need to add a dummy model message to make gemini happy
//
//
If a system message is the last message, we need to add a dummy model message to make gemini happy
if
shouldAddDummyModelMessage
{
//
if shouldAddDummyModelMessage {
geminiRequest
.
Contents
=
append
(
geminiRequest
.
Contents
,
GeminiChatContent
{
//
geminiRequest.Contents = append(geminiRequest.Contents, GeminiChatContent{
Role
:
"model"
,
//
Role: "model",
Parts
:
[]
GeminiPart
{
//
Parts: []GeminiPart{
{
//
{
Text
:
"Okay"
,
//
Text: "Okay",
},
//
},
},
//
},
})
//
})
shouldAddDummyModelMessage
=
false
//
shouldAddDummyModelMessage = false
}
//
}
}
}
return
&
geminiRequest
return
&
geminiRequest
}
}
...
...
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