Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can the toolkits "PythonTools" being reusable? #1083

Open
KevinZhang19870314 opened this issue Aug 5, 2024 · 3 comments
Open

Can the toolkits "PythonTools" being reusable? #1083

KevinZhang19870314 opened this issue Aug 5, 2024 · 3 comments

Comments

@KevinZhang19870314
Copy link

Each time the python tools will generate the code file, can we reuse it the second time?

from phi.assistant import Assistant
from phi.tools.python import PythonTools

assistant = Assistant(tools=[PythonTools()], output_model=DatetimeModel, show_tool_calls=True)
assistant.print_response("Tell me the current time", markdown=True)
INFO     Saved:                                                                
         D:\storage\python_tools\generate_time.py                                                   
INFO     Running                                                               
         D:\storage\python_tools\generate_time.py 
@jacobweiss2305
Copy link
Contributor

@KevinZhang19870314
try prompt engineering first. Add instructions = [], to the Assistant() class stating: "[save file as ...., check to see if this file exists]"

@KevinZhang19870314
Copy link
Author

@jacobweiss2305 Thanks for your response, below is my code with Assistant instructions added, but it not worked for me. You can see the logs below(Run second time), it still save the file get_start_time.py again and then run the code.

import os
import pathlib
import time

from phi.assistant import Assistant
from phi.llm.openai import OpenAIChat
from phi.tools.python import PythonTools
from pydantic import BaseModel, Field
from dotenv import load_dotenv

load_dotenv()

STORAGE_ROOT = os.getenv('STORAGE_ROOT')
PYTHON_TOOLS_STORAGE = os.getenv('PYTHON_TOOLS')
PYTHON_TOOLS_STORAGE_FOLDER_PATH = os.path.join(STORAGE_ROOT, PYTHON_TOOLS_STORAGE)


class TradesAPIParameter(BaseModel):
	startTime: str = Field(None, description="Start time for transaction creation. Use the python_tools tool to generate the code and get the value, in the format '2024-07-26T00:00:00+08:00'.")
	endTime: str = Field(None, description="End time for transaction creation. Use the python_tools tool to generate the code and get the value, in the format '2024-07-26T23:59:59+08:00'.")
	page: int = Field(1, description="Current page number for pagination.")
	size: int = Field(10, description="Number of records per page for pagination.")


start_time = time.time()

movie_assistant = Assistant(
	llm=OpenAIChat(model="gpt-4o"),
	description="You are a parameter parser for the transaction record query API, responsible for parsing the natural language input by the user into the parameter fields required by this API.",
	output_model=TradesAPIParameter,
	tools=[PythonTools(base_dir=pathlib.Path(PYTHON_TOOLS_STORAGE_FOLDER_PATH))],
	instructions=[
		"Only use the python_tools tool when the user mentions date or time-related words or text.",
		f"When use python_tools tool, save the file to the folder {PYTHON_TOOLS_STORAGE_FOLDER_PATH}. If the file already exists, just run it to get the value; otherwise, save the file and then run it.",
	],
	show_tool_calls=False,
	debug_mode=False,
)

params = movie_assistant.run("Query the today's records, and make sure 14 records are displayed per page.")

end_time = time.time()
execution_time = end_time - start_time

print("=========================")
print(params)
print(movie_assistant.llm.metrics)
print(f"Execution Time: {execution_time} seconds")
print("=========================")

log

WARNING  PythonTools can run arbitrary code, please provide human supervision. 
INFO     Saved:                                                                
         D:\storage\python_tools\get_start_time.py                                                  
INFO     Running                                                               
         D:\storage\python_tools\get_start_time.py                                                  
INFO     Saved:                                                                
         D:\storage\python_tools\get_end_time.py                                                    
INFO     Running                                                               
         D:\storage\python_tools\get_end_time.py                                                    
=========================
startTime='2024-08-06T00:00:00+08:00' endTime='2024-08-06T23:59:59+08:00' page=1 size=14
{'response_times': [3.781749599991599, 1.962295899997116], 'prompt_tokens': 1508, 'completion_tokens': 269, 'total_tokens': 1777, 'tool_call_times': {'save_to_file_and_run': [0.013642200006870553, 0.004424200000357814]}}
Execution Time: 5.766778230667114 seconds
=========================

@KevinZhang19870314
Copy link
Author

Here is another try, still not worked.

movie_assistant = Assistant(
	llm=OpenAIChat(model="gpt-4o"),
	description="You are a parameter parser for the transaction record query API, responsible for parsing the natural language input by the user into the parameter fields required by this API.",
	output_model=TradesAPIParameter,
	tools=[PythonTools(base_dir=pathlib.Path(PYTHON_TOOLS_STORAGE_FOLDER_PATH))],
	instructions=[
		"Only use the python_tools tool when the user mentions date or time-related words or text.",
		f"When use python_tools tool to obtain the startTime and endTime, name the generated python file as 'get_start_end_time.py' and save it in folder {PYTHON_TOOLS_STORAGE_FOLDER_PATH}.",
		f"Only save the python file 'get_start_end_time.py' if the file does NOT already exist in folder {PYTHON_TOOLS_STORAGE_FOLDER_PATH}.",
		# f"When use python_tools tool, check to see if python file already generated in folder {PYTHON_TOOLS_STORAGE_FOLDER_PATH} or not. If exists, just run it to get the value; otherwise, save the file and then run it.",
	],
	show_tool_calls=False,
	debug_mode=False,
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants