Okay, now our agent can choose which function to call; it's time to actually call the function.
def call_function(function_call_part, verbose=False):
function_call_part is a types.FunctionCall object that has, most importantly:
name property (the name of the function, a string)args property (a dict of named arguments to the function)If verbose is specified, print the function name and args:
print(f"Calling function: {function_call_part.name}({function_call_part.args})")
Otherwise, just print the name:
print(f" - Calling function: {function_call_part.name}")
working_directory argument to the given dictionary of keyword arguments, because the LLM doesn't provide that one. The working directory should be ./calculator.some_function(**some_args).return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_name,
response={"error": f"Unknown function: {function_name}"},
)
],
)
return types.Content(
role="tool",
parts=[
types.Part.from_function_response(
name=function_name,
response={"result": function_result},
)
],
)
Note that from_function_response requires the response to be a dictionary, so we just shove the string result into a "result" field.
types.Content that we return from call_function should have a .parts[0].function_response.response within.raise a fatal exception of some sort..parts[0]) to a list – we'll use this later.verbose was set, print the result of the function call like this:
print(f"-> {function_call_result.parts[0].function_response.response}")
tests.py)We aren't passing the function call results back to the LLM just yet.
Run and submit the CLI tests.