Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions plugins/chatai/gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,31 +77,33 @@ func (g *Gemini) RequireEchoAnswer(originMessage, echoMessage []cqcode.ArrayMess
var parts []*genai.Part

for _, msg := range originMessage {
if msg.Type == "image" {
switch msg.Type {
case "image":
if url, ok := msg.Data["url"].(string); ok {
if data, imgType, err := g.ImageProcessing(essentials.GetImageData(url)); err == nil {
parts = append(parts, &genai.Part{InlineData: &genai.Blob{Data: data, MIMEType: "image/" + imgType}})
} else {
log.Printf("Image processing error: %v", err)
}
}
} else if msg.Type == "text" {
case "text":
if text, ok := msg.Data["text"].(string); ok && text != "" {
parts = append(parts, &genai.Part{Text: text})
}
}
}

for _, msg := range echoMessage {
if msg.Type == "image" {
switch msg.Type {
case "image":
if url, ok := msg.Data["url"].(string); ok {
if data, imgType, err := g.ImageProcessing(essentials.GetImageData(url)); err == nil {
parts = append(parts, &genai.Part{InlineData: &genai.Blob{Data: data, MIMEType: "image/" + imgType}})
} else {
log.Printf("Image processing error: %v", err)
}
}
} else if msg.Type == "text" {
case "text":
if text, ok := msg.Data["text"].(string); ok {
parts = append(parts, &genai.Part{Text: text})
}
Expand Down Expand Up @@ -149,14 +151,14 @@ func (g *Gemini) GetResponse(parts []*genai.Part, modelName string) ([]string, e
Category: genai.HarmCategorySexuallyExplicit,
Threshold: genai.HarmBlockThresholdBlockNone,
},
{
Category: genai.HarmCategoryCivicIntegrity,
Threshold: genai.HarmBlockThresholdBlockNone,
},
},
}

config.Tools = geminiTools(modelName)
config.Tools = geminiRequestTools(modelName)
if len(config.Tools) > 1 {
includeToolInvocations := true
config.ToolConfig = &genai.ToolConfig{IncludeServerSideToolInvocations: &includeToolInvocations}
}

if strings.Contains(modelName, "image") {
config.ResponseModalities = []string{"TEXT", "IMAGE"}
Expand Down Expand Up @@ -207,6 +209,22 @@ func geminiTools(modelName string) []*genai.Tool {
}
}

func geminiRequestTools(modelName string) []*genai.Tool {
tools := geminiTools(modelName)
if modelName != "gemini-3.1-pro-preview" {
return tools
}

// Gemini 3.1 Pro rejects Google Maps combined with Search or URL Context.
compatible := make([]*genai.Tool, 0, len(tools))
for _, tool := range tools {
if tool.GoogleMaps == nil {
compatible = append(compatible, tool)
}
}
return compatible
}

func (*Gemini) ImageProcessing(imgData *bytes.Buffer) ([]byte, string, error) {
imgBody, err := io.ReadAll(imgData)
if err != nil {
Expand Down
Loading