phidatahq / phidata

Build AI Agents with memory, knowledge, tools and reasoning. Chat with them using a beautiful Agent UI.
https://docs.phidata.com
Mozilla Public License 2.0
13.6k stars 1.91k forks source link

Why the `PythonTools` will always being called? #1084

Open KevinZhang19870314 opened 2 months ago

KevinZhang19870314 commented 2 months ago

For below code, why the 'PythonTools' will always being called? Even I am not say any date or time related words in text?

My opinion is, the python tools will be called when needed. The tool calls is a timeconsuming task, it should be better that the tool called as needed.

import os
import pathlib

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.")

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))],
    show_tool_calls=False,
    debug_mode=False,
)

params = movie_assistant.run("Query the records, and make sure 14 records are displayed per page.")
print("=========================")
print(params)
print("=========================")
jacobweiss2305 commented 2 months ago

@KevinZhang19870314 can you try adding instructions = ["Only use python tools when ..."] to the Assistant(instructions=[]) clss. Let me know if that helps.

KevinZhang19870314 commented 2 months ago

@KevinZhang19870314 can you try adding instructions = ["Only use python tools when ..."] to the Assistant(instructions=[]) clss. Let me know if that helps.

@jacobweiss2305 It worked for me, thank you very much!

instructions=["Only use the python_tools tool when the user mentions date or time-related words or text."],