Find answers from the community

Updated 3 months ago

Funcyuon

Hi, I like to have an agent that can query a SQL database for the required values then run these values through a custom formula. I gave it two tools
Plain Text
sql_database = SQLDatabase(engine, include_tables=table_names)
sql_query_engine = NLSQLTableQueryEngine(sql_database=sql_database)
sql_tool = QueryEngineTool.from_defaults(
    query_engine=sql_query_engine,
    description=(
        "Useful for translating a natural language query into an SQL query over tables containing:"
        "exam_scores, containing information about student's grades in an exam. "
        # and descriptions of more tables...
    ),
)

and a custom formula
Plain Text
def alpha(num_unique_subjects: int) -> int:
    return num_unique_subjects * num_unique_subjects
alpha_tool = FunctionTool.from_defaults(fn=alpha)

and used a gpt-4 agent
Plain Text
query_engine_tools = [sql_tool, alpha_tool]
agent = OpenAIAgent.from_tools(query_engine_tools, verbose=True)
agent.chat_repl()

However, when I ask the agent to give the alpha value of a particular student, it appear to immediately use the alpha formula passing in a hallucinated value for num_unique_subjects .
The agent has no problem answering a question on the number of unique subjects for a particular student.
How do you get the agent to do this in 2 steps? First query the SQL database, then pass those values into the alpha FunctionTool?

Thank you
L
r
2 comments
Probably the function tool needs a description -- you can write the description as a docstring or pass it into the description=".." kwarg for FunctionTool
The QueryEngineTool class's from_defaults method does accept a query_engine parameter, but it does not accept a description parameter. The from_defaults method is a class method that creates a QueryEngineTool instance with default metadata.

Here is the correct usage of the method:

Plain Text
sql_tool = QueryEngineTool.from_defaults(query_engine=sql_query_engine)


If you want to add a description, you should create a ToolMetadata instance with the description and pass it to the QueryEngineTool constructor:

Plain Text
metadata = ToolMetadata(name="SQL Tool", description="Your description here")
sql_tool = QueryEngineTool(query_engine=sql_query_engine, metadata=metadata)


Please replace "Your description here" with your actual description.
Add a reply
Sign up and join the conversation on Discord