You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
improvements to tool calling logic (merged changes from old PR branch) (#1855)
* improvements to tool calling logic (merged changes from old PR branch)
* added some tweaks for improved tool calls to reuse old ctx, but needs testing. refer to PR.
* fixes to some stuff that concedo's modifications broke
* fixed error in reasoning
* extremely hacky way to cache tool list please fix
* oops forgot to add this
* slightly less hacky way to preserve the tool list in context
* prevented unintended toolcalls from happening when LLM states something irrelevant to toolcall decision
* fixed something that broke koboldlite
* fixed bug added by concedo that broke jinja tools
* experimental further compression of tools array, needs testing
* reverted experimental further compression of tools array
* final cleanup
* add newline after memory insert
* changed tool reasoning to always be in json format to enforce including final decision
* used new json format to skip extra llm call when not necessary
* more catching of possible bad llm output
* further cleanup
* got it down to just one llm call!
* better json format
* even better json format
* further refinement to json format
* further refinement to json format
* fixed broken tool calling
* single-call enforced json method now seems to work well. removed fallbacks as they are no longer required.
---------
Co-authored-by: Concedo <[email protected]>
# tools handling: Check if user is passing a openai tools array, if so add to end of prompt before assistant prompt unless tool_choice has been set to None
2528
2528
tools_array=genparams.get('tools', [])
2529
2529
chosen_tool=genparams.get('tool_choice', "auto")
2530
+
messages=genparams.get('messages',[])
2531
+
toolmem=genparams.get("memory","")
2532
+
2530
2533
# first handle auto mode, determine whether a tool is needed
2531
2534
used_tool_json=None
2532
2535
ifnotcurr_ctx:
2533
2536
returnNone
2537
+
2538
+
# get user's last message and last tool call results
2539
+
last_user_message=""
2540
+
tool_call_results=""
2541
+
2542
+
ifmessages:
2543
+
reversed_messages=list(reversed(messages))
2544
+
formessageinreversed_messages:
2545
+
ifmessage["role"] =="user":
2546
+
last_user_message=message["content"]
2547
+
last_user_message=f"\n\nUser's current request: {last_user_message}"
# if you want a different template, you can set 'custom_tools_prompt' in the chat completions adapter as follows
2539
-
custom_tools_prompt="Can the user query be answered by a listed tool above? (One word response: yes or no):"
2540
-
ifis_followup_tool:
2541
-
custom_tools_prompt="Can the user query be further answered by another listed tool above? (If response is already complete, reply NO) (One word response: yes or no):"
2561
+
ifchosen_tool=="auto"orchosen_tool=="required":
2542
2562
# note: message string already contains the instruct start tag!
custom_tools_prompt_json_format="Respond with a JSON object using this structure:\r\n{\r\n\"tool_name\": \"exact_tool_name_here\"\r\n}\r\n\r\nRules:\r\n- You must pick one of the tools to use, pick the most suitable tool."
custom_tools_prompt_json_format="Respond with a JSON object using this structure:\r\n{\r\n\"reasoning\": \"Your reasoning here\",\r\n\"final_decision\": \"yes\" or \"no\",\r\n\"tool_name\": \"exact_tool_name_here\" or \"null\"\r\n}\r\n\r\nRules:\r\n- Output only the JSON object. Do NOT add anything before or after the json object.\r\n- final_decision must be exactly \"yes\" or \"no\"\r\n- tool_name must be either an exact tool name, or if no tool is required, an empty string: \"\"\r\n- Keep reasoning short, maximum one or two sentences.\r\n- No unnecessary comments"
decide_tool_prompt="Which of the listed tools should be used next? Pick exactly one. If no tool is suitable, reply no_tool. (Reply directly with the selected tool's name):"
tool_json_formatting_instruction=f"\nPlease use the provided schema to fill the parameters to create a function call for {toolname}, in the following format: "+json.dumps([{"id": "call_001", "type": "function", "function": {"name": f"{toolname}", "arguments": {"first property key": "first property value", "second property key": "second property value"}}}], indent=0)
0 commit comments