-
Notifications
You must be signed in to change notification settings - Fork 612
feat(go/plugins/googlegenai): add multi-part tools support #3975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
27ad716
57b678b
a1c141e
d59c370
9910d0a
9521303
af663bb
cf89fc7
f0eaa03
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -478,6 +478,34 @@ func toGeminiTools(inTools []*ai.ToolDefinition) ([]*genai.Tool, error) { | |
| return outTools, nil | ||
| } | ||
|
|
||
| // toGeminiFunctionResponsePart translates a slice of [ai.Part] to a slice of [genai.FunctionResponsePart] | ||
| func toGeminiFunctionResponsePart(parts []*ai.Part) ([]*genai.FunctionResponsePart, error) { | ||
| frp := []*genai.FunctionResponsePart{} | ||
| for _, p := range parts { | ||
| switch { | ||
| case p.IsData(): | ||
| contentType, data, err := uri.Data(p) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| frp = append(frp, genai.NewFunctionResponsePartFromBytes(data, contentType)) | ||
| case p.IsMedia(): | ||
| if strings.HasPrefix(p.Text, "data:") { | ||
| contentType, data, err := uri.Data(p) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| frp = append(frp, genai.NewFunctionResponsePartFromBytes(data, contentType)) | ||
| continue | ||
| } | ||
| frp = append(frp, genai.NewFunctionResponsePartFromURI(p.Text, p.ContentType)) | ||
| default: | ||
| return nil, fmt.Errorf("unsupported function response part type: %d", p.Kind) | ||
| } | ||
| } | ||
| return frp, nil | ||
| } | ||
|
|
||
| // mergeTools consolidates all FunctionDeclarations into a single Tool | ||
| // while preserving non-function tools (Retrieval, GoogleSearch, CodeExecution, etc.) | ||
| func mergeTools(ts []*genai.Tool) []*genai.Tool { | ||
|
|
@@ -808,6 +836,14 @@ func translateCandidate(cand *genai.Candidate) (*ai.ModelResponse, error) { | |
| Name: part.FunctionCall.Name, | ||
| Input: part.FunctionCall.Args, | ||
| }) | ||
| // FunctionCall parts may contain a ThoughtSignature that must be preserved | ||
| // and returned in subsequent requests for the tool call to be valid. | ||
| if len(part.ThoughtSignature) > 0 { | ||
| if p.Metadata == nil { | ||
| p.Metadata = make(map[string]any) | ||
| } | ||
| p.Metadata["signature"] = part.ThoughtSignature | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @apascal07 Wanted to ask, is #3902 related to this signature handling for tool calls? or is it something else?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's for reasoning, it's a token that the model returns that must be propagated to subsequent calls in the conversation otherwise it will error. |
||
| } | ||
| } | ||
| if part.CodeExecutionResult != nil { | ||
| partFound++ | ||
|
|
@@ -888,7 +924,7 @@ func toGeminiParts(parts []*ai.Part) ([]*genai.Part, error) { | |
| func toGeminiPart(p *ai.Part) (*genai.Part, error) { | ||
| switch { | ||
| case p.IsReasoning(): | ||
| // TODO: go-genai does not support genai.NewPartFromThought() | ||
| // NOTE: go-genai does not support genai.NewPartFromThought() | ||
| signature := []byte{} | ||
| if p.Metadata != nil { | ||
| if sig, ok := p.Metadata["signature"].([]byte); ok { | ||
|
|
@@ -928,8 +964,17 @@ func toGeminiPart(p *ai.Part) (*genai.Part, error) { | |
| "content": toolResp.Output, | ||
| } | ||
| } | ||
| fr := genai.NewPartFromFunctionResponse(toolResp.Name, output) | ||
| return fr, nil | ||
| if multiPart, ok := p.Metadata["multipart"].(bool); ok { | ||
| if multiPart { | ||
| toolRespParts, err := toGeminiFunctionResponsePart(toolResp.Content) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return genai.NewPartFromFunctionResponseWithParts(toolResp.Name, output, toolRespParts), nil | ||
| } | ||
| } | ||
| fmt.Printf("tool response: %#v\n", toolResp.Content) | ||
hugoaguirre marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return genai.NewPartFromFunctionResponse(toolResp.Name, output), nil | ||
| case p.IsToolRequest(): | ||
| toolReq := p.ToolRequest | ||
| var input map[string]any | ||
|
|
@@ -941,6 +986,12 @@ func toGeminiPart(p *ai.Part) (*genai.Part, error) { | |
| } | ||
| } | ||
| fc := genai.NewPartFromFunctionCall(toolReq.Name, input) | ||
| // Restore ThoughtSignature if present in metadata | ||
| if p.Metadata != nil { | ||
| if sig, ok := p.Metadata["signature"].([]byte); ok { | ||
| fc.ThoughtSignature = sig | ||
| } | ||
| } | ||
| return fc, nil | ||
| default: | ||
| panic("unknown part type in a request") | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.