Skip to content

Commit 0fd94e1

Browse files
committed
made tool calls more robust and allowed tool call template customization
1 parent 4291e15 commit 0fd94e1

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

koboldcpp.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1964,7 +1964,7 @@ def transform_genparams(genparams, api_format):
19641964
# tools handling
19651965
tools_array = genparams.get('tools', [])
19661966
chosen_tool = genparams.get('tool_choice', "auto")
1967-
tool_json_formatting_instruction = " Use this style of JSON object formatting to give your answer if you think the user is asking you to perform an action: " + json.dumps([{"id": "insert an id for the response", "type": "function", "function": {"name": "insert the name of the function you want to call", "arguments": "{\"first property key\": \"first property value\", \"second property key\": \"second property value\"}"}}], indent=0)
1967+
tool_json_formatting_instruction = "\nUse this style of JSON object formatting to give your answer if you think the user is asking you to perform an action: " + json.dumps([{"id": "insert an id for the response", "type": "function", "function": {"name": "insert the name of the function you want to call", "arguments": {"first property key": "first property value", "second property key": "second property value"}}}], indent=0)
19681968
if tools_array and len(tools_array) > 0 and chosen_tool is not None:
19691969
try:
19701970
specified_function = ""
@@ -1981,7 +1981,7 @@ def transform_genparams(genparams, api_format):
19811981
if located_tooljson:
19821982
tools_array = []
19831983
tools_array.append(located_tooljson)
1984-
tool_json_formatting_instruction = f"The user is asking you to use the style of this JSON object formatting to complete the parameters for the specific function named {specified_function} in the following format: " + json.dumps([{"id": "insert an id for the response", "type": "function", "function": {"name": f"{specified_function}", "arguments": "{\"first property key\": \"first property value\", \"second property key\": \"second property value\"}"}}], indent=0)
1984+
tool_json_formatting_instruction = f"\nThe user is asking you to use the style of this JSON object formatting to complete the parameters for the specific function named {specified_function} in the following format: " + json.dumps([{"id": "insert an id for the response", "type": "function", "function": {"name": f"{specified_function}", "arguments": {"first property key": "first property value", "second property key": "second property value"}}}], indent=0)
19851985
except Exception:
19861986
# In case of any issues, just revert back to no specified function
19871987
pass
@@ -2018,11 +2018,13 @@ def transform_genparams(genparams, api_format):
20182018
#if auto mode, determine whether a tool is needed
20192019
tools_string = json.dumps(tools_array, indent=0)
20202020
should_use_tools = True
2021-
user_start = user_message_start
20222021
user_end = assistant_message_start
20232022
if chosen_tool=="auto":
2023+
# if you want a different template, you can set 'custom_tools_prompt' in the chat completions adapter as follows
2024+
custom_tools_prompt = adapter_obj.get("custom_tools_prompt", "Can the user query be answered by a listed tool? (One word response: yes or no):")
2025+
# note: message string already contains the instruct start tag!
20242026
temp_poll = {
2025-
"prompt": f"{user_start}User query:\n\n{messages_string}\n\nTool Code:\n{tools_string}Determine from the provided tool code if the user query would be best answered by a listed tool (One word: yes / no):{user_end}",
2027+
"prompt": f"{messages_string}\n\nTool List:\n{tools_string}\n\n{custom_tools_prompt}{user_end}",
20262028
"max_length":4,
20272029
"temperature":0.1,
20282030
"top_k":1,
@@ -2266,6 +2268,10 @@ def run_blocking(): # api format 1=basic,2=kai,3=oai,4=oai-chat
22662268
if using_openai_tools:
22672269
tool_calls = extract_json_from_string(recvtxt)
22682270
if tool_calls and len(tool_calls)>0:
2271+
for tc in tool_calls:
2272+
tcarg = tc.get("function",{}).get("arguments",None)
2273+
if tcarg and not isinstance(tcarg, str):
2274+
tc["function"]["arguments"] = json.dumps(tcarg)
22692275
recvtxt = None
22702276
currfinishreason = "tool_calls"
22712277
res = {"id": "chatcmpl-A1", "object": "chat.completion", "created": int(time.time()), "model": friendlymodelname,

0 commit comments

Comments
 (0)