我正在使用谷歌的gemini-1.5-pro-002模型来构建一个聊天机器人。该模型部署在VertexAI上。我需要机器人能够自动调用函数。如果函数参数是简单的类型,如字符串或整数,它可以正常工作。但当我尝试使用复杂类型时,遇到了问题。
以下请求无法工作:
curl -X POST \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Content-Type: application/json" \https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${PROJECT_ID}/locations/${LOCATION}/publishers/google/models/${MODEL_ID}:generateContent \-d '{ "contents": [ { "role": "user", "parts": [ { "text": "Search for German people with the last name of Greene" } ] } ], "tools": [ { "functionDeclarations": [ { "name": "SearchPlugin_SearchPerson", "description": "Find a person given a search request", "parameters": { "type": "object", "properties": { "search_request": { "type": "object", "properties": { "first_name": { "type": "string", "description": "The person first_name" }, "last_name": { "type": "string", "description": "The person last_name" }, "nationality": { "type": "string", "description": "The person nationality" } } } }, "required": [ "search_request" ] } } ] } ]}'
Gemini的回复是
{ "candidates": [ { "content": { "role": "model", "parts": [ { "functionCall": { "name": "SearchPlugin_SearchPerson", "args": { "search_request": "unknown" } } } ] }, "finishReason": "STOP" } ], "usageMetadata": { "promptTokenCount": 54, "candidatesTokenCount": 9, "totalTokenCount": 63 }, "modelVersion": "gemini-1.5-pro-002"}
如果我这样定义工具,Gemini可以正确使用它:
{ "functionDeclarations": [ { "name": "SearchPlugin_SearchPerson", "description": "Find a person given a search request.", "parameters": { "type": "object", "properties": { "first_name": { "type": "string", "description": "The person first_name." }, "last_name": { "type": "string", "description": "The person last_name." }, "nationality": { "type": "string", "description": "The person nationality." } }, "required": [ "last_name" ] } } ]}
Gemini能否调用具有复杂参数的函数?
回答:
关于你的问题Can Gemini call functions with complex parameters?
,答案是肯定的。但在当前阶段,结果取决于模型。
当我用几个模型测试你展示的请求体时,我注意到结果取决于模型。似乎在当前阶段(2024年9月30日),得到了以下结果。因此,作为当前的解决方案,如果你想直接使用你展示的请求体,可以考虑将模型从gemini-1.5-pro-002
更改为其他模型吗?
结果
模型 | 结果 |
---|---|
gemini-1.5-pro | 失败 |
gemini-1.5-pro-001 | 失败 |
gemini-1.5-pro-latest | 失败 |
gemini-1.5-pro-exp-0827 | 失败 |
gemini-1.5-pro-002 | 失败 |
gemini-1.5-flash | 成功 |
gemini-1.5-flash-001 | 成功 |
gemini-1.5-flash-latest | 成功 |
gemini-1.5-flash-8b-exp-0827 | 失败 |
gemini-1.5-flash-002 | 失败 |
gemini-1.5-flash-8b-exp-0924 | 成功 |
失败
:从你的请求体中得到了"search_request": "unknown"
。成功
:从你的请求体中得到了"search_request": {"last_name": "Greene","nationality": "German"}
。
注意事项:
- Gemini现在正在成长中。因此,我相信这个结果会在未来的更新中得到更新。例如,目前的
成功
结果在更新中也可能会发生变化。所以,请将这个答案视为2024年9月30日的情况。