Tool Calling Issue #2134
Replies: 2 comments
-
|
This is the BaseAgent """This file contains the base agent class.""" import re from llm import get_models_manager from agent_core.context import Context, _inject_ctx_into_tools class BaseAgent: |
Beta Was this translation helpful? Give feedback.
-
|
The key insight is that you don't call the tools sequentially yourself -- you let the model decide the order by running a tool-calling loop. The model will call tools one (or several) at a time, you execute them and feed results back, and the model decides what to call next until it has enough info to generate the final SQL. Here's the standard pattern using the OpenAI Python SDK: from openai import OpenAI
import json
client = OpenAI()
# Define your tools as function schemas
tools = [
{
"type": "function",
"function": {
"name": "list_tables",
"description": "List all tables in the database",
"parameters": {"type": "object", "properties": {}, "required": []},
},
},
{
"type": "function",
"function": {
"name": "get_table_info",
"description": "Get schema info (columns, types, sample rows) for specified tables",
"parameters": {
"type": "object",
"properties": {
"tables": {"type": "array", "items": {"type": "string"}, "description": "Table names to inspect"}
},
"required": ["tables"],
},
},
},
{
"type": "function",
"function": {
"name": "check_sql",
"description": "Validate a SQL query for correctness before executing",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string", "description": "SQL query to validate"}},
"required": ["query"],
},
},
},
{
"type": "function",
"function": {
"name": "run_sql",
"description": "Execute a SQL query and return results",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string", "description": "SQL query to execute"}},
"required": ["query"],
},
},
},
]
# Map tool names to your actual implementations
tool_functions = {
"list_tables": lambda **kwargs: list_sql_database_tool(),
"get_table_info": lambda tables, **kwargs: info_sql_database_tool(tables),
"check_sql": lambda query, **kwargs: query_sql_checker_tool(query),
"run_sql": lambda query, **kwargs: query_sql_database_tool(query),
}
def run_agent(user_question: str):
messages = [
{"role": "system", "content": "You are a SQL expert. Use the tools to explore the database schema, then write and validate a SQL query to answer the user's question."},
{"role": "user", "content": user_question},
]
# Tool-calling loop
while True:
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
tools=tools,
)
msg = response.choices[0].message
messages.append(msg)
# If no tool calls, the model is done
if not msg.tool_calls:
return msg.content
# Execute each tool call and append results
for tool_call in msg.tool_calls:
fn_name = tool_call.function.name
fn_args = json.loads(tool_call.function.arguments)
result = tool_functions[fn_name](**fn_args)
messages.append({
"role": "tool",
"tool_call_id": tool_call.id,
"content": json.dumps(result) if not isinstance(result, str) else result,
})
answer = run_agent("How many orders were placed last month?")
print(answer)The model will typically follow a sequence like: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am working on a task which converts the Natural Language into SQL query, for which I have created 4 tools QuerySQLDatabaseTool(), InfoSQLDatabaseTool(), ListSQLDatabaseTool(),QuerySQLCheckerTool() similar to those which create_sql_agent is using in langchain.
But how to call these tools sequentially as required considdering examples provided in the prompt.
Beta Was this translation helpful? Give feedback.
All reactions