tools[].function_declarations 里声明函数,模型决定调用时会返回 functionCall(函数名和参数),你执行后把结果用 functionResponse 放回下一轮 contents。Google 搜索:加 {"google_search": {}},模型自动搜索,并在 groundingMetadata 里返回搜索词和引用来源;同一个请求里搜索工具不要和函数声明混用,需要时拆成两次请求。官方文档:https://ai.google.dev/gemini-api/docs/function-calling 、https://ai.google.dev/gemini-api/docs/google-search{
"contents": [
{
"role": "user",
"parts": [
{
"text": "伦敦现在多少度?"
}
]
}
],
"tools": [
{
"function_declarations": [
{
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. San Francisco"
}
},
"required": [
"location"
]
}
}
]
}
]
}curl --location --globoff 'https://接口地址/v1beta/models/gemini-4-neon:generateContent' \
--header 'x-goog-api-key: sk-xxxx' \
--header 'Content-Type: application/json' \
--data '{
"contents": [
{
"role": "user",
"parts": [
{
"text": "伦敦现在多少度?"
}
]
}
],
"tools": [
{
"function_declarations": [
{
"name": "get_current_temperature",
"description": "Gets the current temperature for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. San Francisco"
}
},
"required": [
"location"
]
}
}
]
}
]
}'{
"candidates": [
{
"content": {
"role": "model",
"parts": [
{
"functionCall": {
"name": "get_current_temperature",
"args": {
"location": "London"
}
}
}
]
},
"finishReason": "STOP"
}
],
"usageMetadata": {
"promptTokenCount": 62,
"candidatesTokenCount": 14,
"totalTokenCount": 76
}
}