I have been working on a text to SQL prompt and the LLM made 73 tool calls in one request and 70 were the EXCACT same call. The LLM should only need one call per user question. I am curious if there are any ideas of what is going on… my best guess is something in the prompt confusing the LLM
Here is the process flow:
- Take a user question and create an SQL statement
- Use a tool call to run the SQL in the DB
- Tool definition
tools = [
{
"type": "function",
"function": {
"name": "_run_query",
"description": "Run SQL in database. Will return a list of dictionaries where each element represents a row.",
"strict": True,
"parameters": {
"type": "object",
"properties": {
"sql_query": {
"type": "string",
"description": "syntactically correct SQL statement"
}
},
"additionalProperties": False,
"required": ["sql_query"]
}
}
}
]
- Model Call
client.chat.completions.create(
model="gpt-4o-mini",
messages=messages,
temperature=0,
seed=20,
tools=tools,
tool_choice={"type": "function", "function": {"name": "_run_query"}},
parallel_tool_calls= False
)
- Turn the result into a natural language answer
Here’s an example:
user_question = "How many apples have been sold?"
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content" :"You are a SQL expert..."}, # the actual prompt is about 100 lines
{"role": "user", "content" :user_question}
],
temperature=0,
seed=20,
tools=tools,
tool_choice={"type": "function", "function": {"name": "_run_query"}},
parallel_tool_calls= False
)
# There will be 73 tool calls, and they will have the exact same argument
print(
len(response_message.tool_calls)
)
# [ChatCompletionMessageToolCall(id='call_id1, function=Function(arguments='{"SELECT COUNT(1) FROM SALES WHERE PRODUCT = "apple"}', name='_run_query'), type='function'),
#ChatCompletionMessageToolCall(id='call_id2, function=Function(arguments='{"SELECT COUNT(1) FROM SALES WHERE PRODUCT = "apple"}', name='_run_query'), type='function'),
# ...
# ChatCompletionMessageToolCall(id='call_id73, function=Function(arguments='{"SELECT COUNT(1) FROM SALES WHERE PRODUCT = "apple"}', name='_run_query'), type='function')]
print(
response_message.tool_calls
)