giving functions to use for AI

This commit is contained in:
2025-09-09 09:57:03 +03:00
parent fb424dfa19
commit 3e18a04d55
6 changed files with 116 additions and 6 deletions

50
main.py
View File

@@ -3,6 +3,39 @@ import sys
from dotenv import load_dotenv
from google import genai
from google.genai import types
from functions.get_files_info import schema_get_files_info
from functions.get_file_content import schema_get_file_content
from functions.run_python_file import schema_run_python_file
from functions.write_file import schema_write_file
available_functions = types.Tool(
function_declarations=[
schema_get_files_info,
schema_get_file_content,
schema_run_python_file,
schema_write_file,
]
)
system_prompt = """
You are a helpful AI coding agent.
When a user asks a question or makes a request, make a function call plan.
You can perform the following operations:
- List files and directories
- Read file contents
- Execute Python files with optional arguments
- Write or overwrite files
All paths you provide should be relative to the working directory.
You do not need to specify the working directory in your function calls
as it is automatically injected for security reasons.
"""
config=types.GenerateContentConfig(
tools=[available_functions], system_instruction=system_prompt
)
messages = []
@@ -35,15 +68,22 @@ def main():
response = client.models.generate_content(
model="gemini-2.0-flash-001",
contents=messages
contents=messages,
config=config,
)
reply_text = response.text
if response.candidates[0].content.parts:
for part in response.candidates[0].content.parts:
if part.function_call:
function_call_part = part.function_call
print(f"Calling function: {function_call_part.name}({function_call_part.args})")
elif part.text:
reply_text = part.text
# Add AI response to history
add_message("model", reply_text)
# Add AI response to history
add_message("model", reply_text)
print(reply_text)
print(reply_text)
if verbose:
print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}")