Compare commits
3 Commits
b3d068edf9
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| bd62d666b6 | |||
| ea582b77e4 | |||
| 4cda5f0e75 |
@@ -0,0 +1 @@
|
|||||||
|
# calculator
|
||||||
@@ -16,15 +16,36 @@ class Calculator:
|
|||||||
def evaluate(self, expression):
|
def evaluate(self, expression):
|
||||||
if not expression or expression.isspace():
|
if not expression or expression.isspace():
|
||||||
return None
|
return None
|
||||||
tokens = expression.strip().split()
|
tokens = self._tokenize(expression)
|
||||||
return self._evaluate_infix(tokens)
|
return self._evaluate_infix(tokens)
|
||||||
|
|
||||||
|
def _tokenize(self, expression):
|
||||||
|
# Add spaces around operators and parentheses for easy splitting
|
||||||
|
expression = (
|
||||||
|
expression.replace("(", " ( ")
|
||||||
|
.replace(")", " ) ")
|
||||||
|
.replace("+", " + ")
|
||||||
|
.replace("-", " - ")
|
||||||
|
.replace("*", " * ")
|
||||||
|
.replace("/", " / ")
|
||||||
|
)
|
||||||
|
return [token for token in expression.split() if token]
|
||||||
|
|
||||||
def _evaluate_infix(self, tokens):
|
def _evaluate_infix(self, tokens):
|
||||||
values = []
|
values = []
|
||||||
operators = []
|
operators = []
|
||||||
|
|
||||||
for token in tokens:
|
for token in tokens:
|
||||||
if token in self.operators:
|
if token == "(":
|
||||||
|
operators.append(token)
|
||||||
|
elif token == ")":
|
||||||
|
while operators and operators[-1] != "(":
|
||||||
|
self._apply_operator(operators, values)
|
||||||
|
if operators and operators[-1] == "(":
|
||||||
|
operators.pop() # Pop the opening parenthesis
|
||||||
|
else:
|
||||||
|
raise ValueError("Mismatched parentheses")
|
||||||
|
elif token in self.operators:
|
||||||
while (
|
while (
|
||||||
operators
|
operators
|
||||||
and operators[-1] in self.operators
|
and operators[-1] in self.operators
|
||||||
@@ -39,6 +60,8 @@ class Calculator:
|
|||||||
raise ValueError(f"invalid token: {token}")
|
raise ValueError(f"invalid token: {token}")
|
||||||
|
|
||||||
while operators:
|
while operators:
|
||||||
|
if operators[-1] == "(":
|
||||||
|
raise ValueError("Mismatched parentheses")
|
||||||
self._apply_operator(operators, values)
|
self._apply_operator(operators, values)
|
||||||
|
|
||||||
if len(values) != 1:
|
if len(values) != 1:
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from google.genai import types
|
from google.genai import types
|
||||||
from functions.get_file_content import schema_get_file_content
|
from functions.get_file_content import schema_get_file_content, get_file_content
|
||||||
from functions.get_files_info import schema_get_files_info
|
from functions.get_files_info import schema_get_files_info, get_files_info
|
||||||
from functions.run_python_file import schema_run_python_file
|
from functions.run_python_file import schema_run_python_file, run_python_file
|
||||||
from functions.write_file import schema_write_file
|
from functions.write_file import schema_write_file, write_file
|
||||||
|
|
||||||
available_functions = types.Tool(
|
available_functions = types.Tool(
|
||||||
function_declarations=[
|
function_declarations=[
|
||||||
@@ -11,4 +11,42 @@ available_functions = types.Tool(
|
|||||||
schema_write_file,
|
schema_write_file,
|
||||||
schema_run_python_file
|
schema_run_python_file
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def call_function(function_call, verbose=False):
|
||||||
|
if verbose:
|
||||||
|
print(f"Calling function: {function_call.name}({function_call.args})")
|
||||||
|
print(f" - Calling function: {function_call.name}")
|
||||||
|
|
||||||
|
function_map = {
|
||||||
|
"get_file_content": get_file_content,
|
||||||
|
"get_files_info": get_files_info,
|
||||||
|
"run_python_file": run_python_file,
|
||||||
|
"write_file": write_file,
|
||||||
|
}
|
||||||
|
|
||||||
|
function_name = function_call.name or ""
|
||||||
|
if function_name == "":
|
||||||
|
return types.Content(
|
||||||
|
role="tool",
|
||||||
|
parts=[
|
||||||
|
types.Part.from_function_response(
|
||||||
|
name=function_name,
|
||||||
|
response={"error": f"Unknown function: {function_name}"},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
|
args = dict(function_call.args) if function_call.args else {}
|
||||||
|
args["working_directory"] = "./calculator"
|
||||||
|
function_result = function_map[function_name](**args)
|
||||||
|
return types.Content(
|
||||||
|
role="tool",
|
||||||
|
parts=[
|
||||||
|
types.Part.from_function_response(
|
||||||
|
name=function_name,
|
||||||
|
response={"result": function_result},
|
||||||
|
)
|
||||||
|
],
|
||||||
|
)
|
||||||
|
|
||||||
@@ -1,11 +1,12 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
|
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 prompts import system_prompt
|
from prompts import system_prompt
|
||||||
from functions.call_function import available_functions
|
from functions.call_function import available_functions, call_function
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
api_key = os.environ.get("GEMINI_API_KEY")
|
api_key = os.environ.get("GEMINI_API_KEY")
|
||||||
@@ -19,25 +20,48 @@ parser.add_argument("--verbose", action="store_true", help="Enable verbose outpu
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
messages = [types.Content(role="user", parts=[types.Part(text=args.user_prompt)])]
|
messages = [types.Content(role="user", parts=[types.Part(text=args.user_prompt)])]
|
||||||
|
|
||||||
response = client.models.generate_content(
|
for _ in range(10):
|
||||||
model="gemini-2.5-flash",
|
response = client.models.generate_content(
|
||||||
contents=messages,
|
model="gemini-2.5-flash",
|
||||||
config=types.GenerateContentConfig(
|
contents=messages,
|
||||||
system_instruction=system_prompt,
|
config=types.GenerateContentConfig(
|
||||||
tools=[available_functions],
|
system_instruction=system_prompt,
|
||||||
),
|
tools=[available_functions],
|
||||||
)
|
),
|
||||||
if not response.usage_metadata:
|
)
|
||||||
raise RuntimeError("Cannot get usage metadata")
|
if response.candidates:
|
||||||
|
for candidate in response.candidates:
|
||||||
|
if candidate.content:
|
||||||
|
messages.append(candidate.content)
|
||||||
|
|
||||||
if args.verbose:
|
if not response.usage_metadata:
|
||||||
print(f"User prompt: {args.user_prompt}")
|
raise RuntimeError("Cannot get usage metadata")
|
||||||
print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}")
|
|
||||||
print(f"Response tokens: {response.usage_metadata.candidates_token_count}")
|
if args.verbose:
|
||||||
|
print(f"User prompt: {args.user_prompt}")
|
||||||
function_calls = response.function_calls
|
print(f"Prompt tokens: {response.usage_metadata.prompt_token_count}")
|
||||||
if function_calls:
|
print(f"Response tokens: {response.usage_metadata.candidates_token_count}")
|
||||||
for function_call in function_calls:
|
|
||||||
print(f"Calling function: {function_call.name}({function_call.args})")
|
function_calls = response.function_calls
|
||||||
|
function_responses = []
|
||||||
|
if function_calls:
|
||||||
|
for function_call in function_calls:
|
||||||
|
function_call_result = call_function(function_call, args.verbose)
|
||||||
|
|
||||||
|
if not function_call_result.parts:
|
||||||
|
raise Exception("function_call_result.parts list is empty")
|
||||||
|
if function_call_result.parts[0].function_response is None:
|
||||||
|
raise Exception("FunctionResponse object is equal to None")
|
||||||
|
if function_call_result.parts[0].function_response.response is None:
|
||||||
|
raise Exception("FunctionResponse object response is equal to None")
|
||||||
|
|
||||||
|
function_responses.append(function_call_result.parts[0])
|
||||||
|
if args.verbose:
|
||||||
|
print(f"-> {function_call_result.parts[0].function_response.response}")
|
||||||
|
|
||||||
|
messages.append(types.Content(role="user", parts=function_responses))
|
||||||
|
else:
|
||||||
|
break
|
||||||
else:
|
else:
|
||||||
print(response.text)
|
print("Maximum iterations reached without a final response")
|
||||||
|
sys.exit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user