Use Function Calling (Tools)
mainFunction calling allows the model to request execution of specific tools. This is currently supported only by the vertex-ai-api service and the gemini-pro model.
- Define tools using
function_declarationsin thetoolskey, providing a name, description, and JSON Schema for parameters. - The model returns a
functionCallin its response. - You must execute the function locally and then send the result back to the model using a message with
role: 'function'and thefunctionResponsekey.
# 1. Define tool and request
input = {
tools: { function_declarations: [{ name: 'get_time', parameters: { ... } }] },
contents: [{ role: 'user', parts: { text: 'What time is it?' } }]
}
result = client.stream_generate_content(input)
# 2. Handle functionCall and prepare response
# ... (execute local code) ...
# 3. Send response back
input[:contents] << result.dig(0, 'candidates', 0, 'content')
input[:contents] << { role: 'function', parts: [{ functionResponse: { name: 'get_time', response: { content: '...' } } }] }
# 4. Final request
final_result = client.stream_generate_content(input)