actually calling functions with AI
This commit is contained in:
1
calculator/README.md
Normal file
1
calculator/README.md
Normal file
@@ -0,0 +1 @@
|
|||||||
|
# calculator
|
||||||
1
calculator/lorem.txt
Normal file
1
calculator/lorem.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
wait, this isn't lorem ipsum
|
||||||
1
calculator/test_output.txt
Normal file
1
calculator/test_output.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
hello world
|
||||||
57
functions/call_function.py
Normal file
57
functions/call_function.py
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
import os
|
||||||
|
from google.genai import types
|
||||||
|
|
||||||
|
from functions.get_files_info import get_files_info
|
||||||
|
from functions.get_file_content import get_file_content
|
||||||
|
from functions.write_file import write_file
|
||||||
|
from functions.run_python_file import run_python_file
|
||||||
|
|
||||||
|
|
||||||
|
def call_function(function_call_part, verbose=False):
|
||||||
|
|
||||||
|
function_name = function_call_part.name
|
||||||
|
args = dict(function_call_part.args or {})
|
||||||
|
|
||||||
|
args["working_directory"] = "./calculator"
|
||||||
|
|
||||||
|
function_map = {
|
||||||
|
"get_files_info": get_files_info,
|
||||||
|
"get_file_content": get_file_content,
|
||||||
|
"write_file": write_file,
|
||||||
|
"run_python_file": run_python_file,
|
||||||
|
}
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print(f"Calling function: {function_name}({args})")
|
||||||
|
else:
|
||||||
|
print(f" - Calling function: {function_name}")
|
||||||
|
|
||||||
|
if function_name not in function_map:
|
||||||
|
return types.Content(
|
||||||
|
role="tool",
|
||||||
|
parts=[
|
||||||
|
types.Part.from_function_response(
|
||||||
|
name=function_name,
|
||||||
|
response={"error": f"Unknown function: {function_name}"},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
function_result = function_map[function_name](**args)
|
||||||
|
except Exception as e:
|
||||||
|
function_result = f"Error executing function: {e}"
|
||||||
|
|
||||||
|
tool_response = types.Content(
|
||||||
|
role="tool",
|
||||||
|
parts=[
|
||||||
|
types.Part.from_function_response(
|
||||||
|
name=function_name, response={"result": function_result}
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
if verbose:
|
||||||
|
print(f"-> {tool_response.parts[0].function_response.response}")
|
||||||
|
|
||||||
|
return tool_response
|
||||||
14
main.py
14
main.py
@@ -3,6 +3,7 @@ import sys
|
|||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from google import genai
|
from google import genai
|
||||||
from google.genai import types
|
from google.genai import types
|
||||||
|
from functions.call_function import call_function
|
||||||
from functions.get_files_info import schema_get_files_info
|
from functions.get_files_info import schema_get_files_info
|
||||||
from functions.get_file_content import schema_get_file_content
|
from functions.get_file_content import schema_get_file_content
|
||||||
from functions.run_python_file import schema_run_python_file
|
from functions.run_python_file import schema_run_python_file
|
||||||
@@ -75,14 +76,17 @@ def main():
|
|||||||
if response.candidates[0].content.parts:
|
if response.candidates[0].content.parts:
|
||||||
for part in response.candidates[0].content.parts:
|
for part in response.candidates[0].content.parts:
|
||||||
if part.function_call:
|
if part.function_call:
|
||||||
function_call_part = part.function_call
|
function_result = call_function(part.function_call, verbose=verbose)
|
||||||
print(f"Calling function: {function_call_part.name}({function_call_part.args})")
|
# validate that function call produced a response
|
||||||
|
if not (
|
||||||
|
function_result.parts
|
||||||
|
and function_result.parts[0].function_response
|
||||||
|
and function_result.parts[0].function_response.response
|
||||||
|
):
|
||||||
|
raise RuntimeError("Fatal: Function call returned no response.")
|
||||||
elif part.text:
|
elif part.text:
|
||||||
reply_text = part.text
|
reply_text = part.text
|
||||||
|
|
||||||
# Add AI response to history
|
|
||||||
add_message("model", reply_text)
|
add_message("model", reply_text)
|
||||||
|
|
||||||
print(reply_text)
|
print(reply_text)
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
|
|||||||
Reference in New Issue
Block a user