CoolFace
Apppublic

Rhinox13/chatapi

sourceHugging Faceupdated 3mo agoView on Hugging Face
0likes
turn_request_tools.py51 linesDownload Raw Back to services
1from __future__ import annotations
2
3from typing import Any
4
5
6def extract_tool_names(data: dict[str, Any]) -> set[str]:
7    raw_tools = data.get("tools")
8    if not isinstance(raw_tools, list):
9        return set()
10
11    names: set[str] = set()
12    for tool in raw_tools:
13        if not isinstance(tool, dict):
14            continue
15        func = tool.get("function")
16        if isinstance(func, dict):
17            name = func.get("name")
18        else:
19            name = tool.get("name")
20        if isinstance(name, str) and name.strip():
21            names.add(name.strip())
22    return names
23
24
25def extract_tool_schemas(data: dict[str, Any]) -> dict[str, dict[str, Any]]:
26    raw_tools = data.get("tools")
27    if not isinstance(raw_tools, list):
28        return {}
29
30    schemas: dict[str, dict[str, Any]] = {}
31    for tool in raw_tools:
32        if not isinstance(tool, dict):
33            continue
34        func = tool.get("function")
35        if isinstance(func, dict):
36            name = func.get("name")
37            parameters = func.get("parameters", {})
38        else:
39            name = tool.get("name")
40            parameters = tool.get("input_schema", {})
41        if isinstance(name, str) and name.strip():
42            schemas[name.strip()] = parameters if isinstance(parameters, dict) else {}
43    return schemas
44
45
46def find_response_message_metadata(messages: list[Any], response_id: str) -> dict[str, Any]:
47    for message in reversed(messages):
48        if message.response_id == response_id and message.role == "assistant":
49            return dict(message.metadata)
50    return {}
51