Support more common tool variables in templates (tools, message.tool_calls) (#308)

* Add non-JSON version of `tools` and `functions` to `template_vars`.

Increase the compatibility with VLLM templates which use a non-JSON tools object.

* Add list of tool template variables to the documentation

* Use Jinja templates to provide `tools_json` and `functions_json`

This should be functionally equivelant, but the JSON won't be produced
unless it's needed.

* Make message.tool_calls match the JSON from ToolCallProcessor

* Log something when generating tool calls

* Add template for Qwen QwQ 32b

* Only log if tool calls have been detected

* API: Fix tool call variable assignments

Jinja functions do not run when variables are called. Use json.dumps
instead. In addition, log the request ID when stating that a tool
call was fired.

Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com>

* Add `ToolCallProcessor.dump()` to get the list of processed dicts

* Remove qwen_qwq_32b.jinja

This will be added to the following repository at a later date:
https://github.com/theroyallab/llm-prompt-templates

---------

Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com>
Co-authored-by: kingbri <8082010+kingbri1@users.noreply.github.com>
This commit is contained in:
Andrew Phillips
2025-03-23 14:23:00 -03:00
committed by GitHub
parent ccf23243c1
commit 436ce752da
3 changed files with 44 additions and 10 deletions

View File

@@ -18,6 +18,28 @@ class ToolCallProcessor:
return [ToolCall(**tool_call) for tool_call in tool_calls]
@staticmethod
def dump(tool_calls: List[ToolCall]) -> List[dict]:
"""
Convert ToolCall objects to a list of dictionaries.
Args:
tool_calls (List[ToolCall]): List of ToolCall objects to convert
Returns:
List[dict]: List of dictionaries representing the tool calls
"""
# Don't use list comprehension here
# as that will fail rather than warn
dumped_tool_calls = []
for tool_call_obj in tool_calls:
try:
dumped_tool_calls.append(tool_call_obj.model_dump())
except (json.JSONDecodeError, AttributeError) as e:
logger.warning(f"Error processing tool call: {e}")
return dumped_tool_calls
@staticmethod
def to_json(tool_calls: List[ToolCall]) -> str:
"""
@@ -33,14 +55,8 @@ class ToolCallProcessor:
if not tool_calls:
return ""
# Don't use list comprehension here
# as that will fail rather than warn
dumped_tool_calls = []
for tool_call_obj in tool_calls:
try:
dumped_tool_calls.append(tool_call_obj.model_dump())
except (json.JSONDecodeError, AttributeError) as e:
logger.warning(f"Error processing tool call: {e}")
# Use the dump method to get the list of dictionaries
dumped_tool_calls = ToolCallProcessor.dump(tool_calls)
# Serialize the dumped array
return json.dumps(dumped_tool_calls, indent=2)