admin 管理员组文章数量: 1086019
I am using LiteLLM and Gemini-2.0-Flash to make an agent that is used to solve simple terminal queries (e.g. Find and list the contents of the file "cat.txt").
I've defined the tools, and the goal is for the LLM agent to make use of them to construct function calls, which would then be executed by my code, with their result used to continue the conversation until the solution is found.
My agent seems to severely underuse the defined tools (for example, never uses the 'cat' function, but outputs that the user should use 'cat' to list the contents of the file.
Is the underuse of the defined tools a system prompt issue, or a tool definition issue?
# Tool definitions in LiteLLM format
tools = [
{
"type": "function",
"function": {
"name": "ls",
"description": "List files in a given directory.",
"parameters": {
"type": "object",
"properties": {
"directory": {
"type": "string",
"description": "The directory whose contents are to be listed e.g. /home/",
}
}
},
},
},
{
"type": "function",
"function": {
"name": "cd",
"description": "Change the current directory. The path given to change must be absolute starting from root or relative from the current directory.",
"parameters": {
"type": "object",
"properties": {
"directory": {
"type": "string",
"description": "The target directory to change to.",
}
}
},
},
},
{
"type": "function",
"function": {
"name": "cat",
"description": "Read the content of a file. The file must be readable and not a directory. If the file doesn't exist in the current directory, this command will return an error message.",
"parameters": {
"type": "object",
"properties": {
"filename": {
"type": "string",
"description": "The target file to be read.",
}
}
},
},
},
]
# Function to handle tool execution
def execute_tool(tool_name, args):
if tool_name == "ls":
return ls(**args)
elif tool_name == "cd":
return cd(**args)
elif tool_name == "cat":
return cat(**args)
else:
return json.dumps({"error": "Unknown tool."})
# Function to interact with the agent
def run_agent(prompt):
messages = [{"role": "system", "content": """You are a Linux terminal assistant.
Use 'ls', 'cd', and 'cat' to locate files.
You will be prompted to solve a simple terminal query.
Use the provided tools do to so.
Always first list the CURRENT directory using the 'ls' tool to find out which files and directories are present inside it.
Once the query is solved, do not use any tool and respond with 'solved'.
Do not request further input from the user.
When you are asked to print the contents of a file, execute the cat command to retrieve its contents and then return those.
Do NOT list the root directory or any parent directories. Work from the current directory and its children directories."""}]
messages = [{"role": "user", "content": prompt}]
lap = 1
while True:
print(f"\nLap: {lap}")
lap+=1
response = litellmpletion(
model="gemini/gemini-2.0-flash", # Replace with an appropriate model
messages=messages,
tools=tools,
tool_choice="auto",
temperature=0.8,
)
print(response) # Debugging: See full response structure
# response = response.choices[0].message
# print(response.choices[0].message.tool_calls[0].function.name)
# print(response.choices[0].message.tool_calls[0].function.arguments)
# Ensure tool_calls exists in response
if hasattr(response.choices[0].message, "tool_calls"):
for tool_call in response.choices[0].message.tool_calls:
tool_name = tool_call.function.name
tool_args = json.loads(tool_call.function.arguments)
assert isinstance(tool_name, str), f"Expected str, got {type(tool_name)}"
assert isinstance(tool_args, dict), f"Expected dict, got {type(tool_args)}"
result = execute_tool(tool_name, tool_args)
print(result)
messages.append({"role": "assistant", "content": f"{tool_name}({tool_args}) -> {result}"})
else:
print(f"No tool calls here...")
# No more tool calls, so return the final response
return response["choices"][0]["message"]["content"]
# Example usage
print(run_agent("Find the file 'key.txt' and print its contents."))
本文标签: pythonAgentsFacilitating tool calling and underuse of tools with LiteLLM and GeminiStack Overflow
版权声明:本文标题:python - Agents - Facilitating tool calling and underuse of tools with LiteLLM and Gemini - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744096835a2533054.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论