-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add gemini pro, gemini pro vision models
- Loading branch information
1 parent
3d1e8c8
commit 261e500
Showing
12 changed files
with
304 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package palm2 | ||
|
||
import ( | ||
"chat/globals" | ||
"chat/utils" | ||
"strings" | ||
) | ||
|
||
func getGeminiRole(role string) string { | ||
switch role { | ||
case globals.User: | ||
return GeminiUserType | ||
case globals.Assistant, globals.Tool, globals.System: | ||
return GeminiModelType | ||
default: | ||
return GeminiUserType | ||
} | ||
} | ||
|
||
func getMimeType(content string) string { | ||
segment := strings.Split(content, ".") | ||
if len(segment) == 0 || len(segment) == 1 { | ||
return "image/png" | ||
} | ||
|
||
suffix := strings.TrimSpace(strings.ToLower(segment[len(segment)-1])) | ||
|
||
switch suffix { | ||
case "png": | ||
return "image/png" | ||
case "jpg", "jpeg": | ||
return "image/jpeg" | ||
case "gif": | ||
return "image/gif" | ||
case "webp": | ||
return "image/webp" | ||
case "heif": | ||
return "image/heif" | ||
case "heic": | ||
return "image/heic" | ||
default: | ||
return "image/png" | ||
} | ||
} | ||
|
||
func getGeminiContent(parts []GeminiChatPart, content string, model string) []GeminiChatPart { | ||
parts = append(parts, GeminiChatPart{ | ||
Text: &content, | ||
}) | ||
|
||
if model == globals.GeminiPro { | ||
return parts | ||
} | ||
|
||
urls := utils.ExtractImageUrls(content) | ||
if len(urls) > geminiMaxImages { | ||
urls = urls[:geminiMaxImages] | ||
} | ||
|
||
for _, url := range urls { | ||
data, err := utils.ConvertToBase64(url) | ||
if err != nil { | ||
continue | ||
} | ||
|
||
parts = append(parts, GeminiChatPart{ | ||
InlineData: &GeminiInlineData{ | ||
MimeType: getMimeType(url), | ||
Data: data, | ||
}, | ||
}) | ||
} | ||
|
||
return parts | ||
} | ||
|
||
func (c *ChatInstance) GetGeminiContents(model string, message []globals.Message) []GeminiContent { | ||
// gemini role should be user-model | ||
|
||
result := make([]GeminiContent, 0) | ||
for _, item := range message { | ||
role := getGeminiRole(item.Role) | ||
if len(item.Content) == 0 { | ||
// gemini model: message must include non empty content | ||
continue | ||
} | ||
|
||
if len(result) == 0 && getGeminiRole(item.Role) == GeminiModelType { | ||
// gemini model: first message must be user | ||
continue | ||
} | ||
|
||
if len(result) > 0 && role == result[len(result)-1].Role { | ||
// gemini model: messages must alternate between authors | ||
result[len(result)-1].Parts = getGeminiContent(result[len(result)-1].Parts, item.Content, model) | ||
continue | ||
} | ||
|
||
result = append(result, GeminiContent{ | ||
Role: getGeminiRole(item.Role), | ||
Parts: getGeminiContent(make([]GeminiChatPart, 0), item.Content, model), | ||
}) | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,72 @@ | ||
package palm2 | ||
|
||
const ( | ||
GeminiUserType = "user" | ||
GeminiModelType = "model" | ||
) | ||
|
||
type PalmMessage struct { | ||
Author string `json:"author"` | ||
Content string `json:"content"` | ||
} | ||
|
||
// ChatBody is the native http request body for palm2 | ||
type ChatBody struct { | ||
Prompt Prompt `json:"prompt"` | ||
// PalmChatBody is the native http request body for palm2 | ||
type PalmChatBody struct { | ||
Prompt PalmPrompt `json:"prompt"` | ||
} | ||
|
||
type Prompt struct { | ||
type PalmPrompt struct { | ||
Messages []PalmMessage `json:"messages"` | ||
} | ||
|
||
// ChatResponse is the native http response body for palm2 | ||
type ChatResponse struct { | ||
// PalmChatResponse is the native http response body for palm2 | ||
type PalmChatResponse struct { | ||
Candidates []PalmMessage `json:"candidates"` | ||
} | ||
|
||
// GeminiChatBody is the native http request body for gemini | ||
type GeminiChatBody struct { | ||
Contents []GeminiContent `json:"contents"` | ||
GenerationConfig GeminiConfig `json:"generationConfig"` | ||
} | ||
|
||
type GeminiConfig struct { | ||
Temperature *float64 `json:"temperature,omitempty"` | ||
MaxOutputTokens *int `json:"maxOutputTokens,omitempty"` | ||
TopP *float64 `json:"topP,omitempty"` | ||
TopK *int `json:"topK,omitempty"` | ||
} | ||
|
||
type GeminiContent struct { | ||
Role string `json:"role"` | ||
Parts []GeminiChatPart `json:"parts"` | ||
} | ||
|
||
type GeminiChatPart struct { | ||
Text *string `json:"text,omitempty"` | ||
InlineData *GeminiInlineData `json:"inline_data,omitempty"` | ||
} | ||
|
||
type GeminiInlineData struct { | ||
MimeType string `json:"mime_type"` | ||
Data string `json:"data"` | ||
} | ||
|
||
type GeminiChatResponse struct { | ||
Candidates []struct { | ||
Content struct { | ||
Parts []struct { | ||
Text string `json:"text"` | ||
} `json:"parts"` | ||
Role string `json:"role"` | ||
} `json:"content"` | ||
} `json:"candidates"` | ||
} | ||
|
||
type GeminiChatErrorResponse struct { | ||
Error struct { | ||
Code int `json:"code"` | ||
Message string `json:"message"` | ||
Status string `json:"status"` | ||
} `json:"error"` | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.