mirror of
https://github.com/LostRuins/koboldcpp.git
synced 2026-09-18 08:44:57 +02:00
tool calling params have been ported over to KAI api and can be used, same syntax as OAI endpoint
This commit is contained in:
+27
-2
@@ -2249,6 +2249,8 @@ def determine_tool_json_to_use(genparams, curr_ctx, assistant_message_start, is_
|
||||
chosen_tool = genparams.get('tool_choice', "auto")
|
||||
# first handle auto mode, determine whether a tool is needed
|
||||
used_tool_json = None
|
||||
if not curr_ctx:
|
||||
return None
|
||||
if tools_array and len(tools_array) > 0 and chosen_tool is not None and chosen_tool!="none":
|
||||
tools_string = json.dumps(tools_array, indent=0)
|
||||
should_use_tools = True
|
||||
@@ -2313,7 +2315,6 @@ def determine_tool_json_to_use(genparams, curr_ctx, assistant_message_start, is_
|
||||
|
||||
return used_tool_json
|
||||
|
||||
|
||||
def transform_genparams(genparams, api_format):
|
||||
global chatcompl_adapter, maxctx
|
||||
|
||||
@@ -2363,7 +2364,31 @@ ws ::= | " " | "\n" [ \t]{0,20}
|
||||
genparams["max_length"] = int(genparams.get('max', args.defaultgenamt))
|
||||
|
||||
elif api_format==2:
|
||||
pass
|
||||
#tool calls only possible if forced, or if ending with assistant tag
|
||||
adapter_obj = {} if chatcompl_adapter is None else chatcompl_adapter
|
||||
assistant_message_start = adapter_obj.get("assistant_start", "\n### Response:\n")
|
||||
used_tool_json = determine_tool_json_to_use(genparams, genparams.get('prompt', ""), assistant_message_start, True)
|
||||
if used_tool_json and not genparams.get('grammar', ""):
|
||||
toolparamjson = None
|
||||
toolname = None
|
||||
# Set temperature lower automatically if function calling, cannot exceed 0.5
|
||||
genparams["temperature"] = (1.0 if genparams.get("temperature", 0.5) > 1.0 else genparams.get("temperature", 0.5))
|
||||
genparams["using_openai_tools"] = True
|
||||
# Set grammar to llamacpp example grammar to force json response (see https://github.com/ggerganov/llama.cpp/blob/master/grammars/json_arr.gbnf)
|
||||
genparams["grammar"] = jsongrammar
|
||||
try:
|
||||
toolname = used_tool_json.get('function').get('name')
|
||||
toolparamjson = used_tool_json.get('function').get('parameters')
|
||||
bettergrammarjson = {"type":"array","items":{"type":"object","properties":{"id":{"type":"string","enum":["call_001"]},"type":{"type":"string","enum":["function"]},"function":{"type":"object","properties":{"name":{"type":"string"},"arguments":{}},"required":["name","arguments"],"additionalProperties":False}},"required":["id","type","function"],"additionalProperties":False}}
|
||||
bettergrammarjson["items"]["properties"]["function"]["properties"]["arguments"] = toolparamjson
|
||||
decoded = convert_json_to_gbnf(bettergrammarjson)
|
||||
if decoded:
|
||||
genparams["grammar"] = decoded
|
||||
except Exception:
|
||||
pass
|
||||
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)
|
||||
genparams["prompt"] += f"\n\nJSON Schema:\n{used_tool_json}\n\n{tool_json_formatting_instruction}{assistant_message_start}"
|
||||
|
||||
|
||||
elif api_format==3 or api_format==4 or api_format==7:
|
||||
default_adapter = {} if chatcompl_adapter is None else chatcompl_adapter
|
||||
|
||||
Reference in New Issue
Block a user