Tools
Auto tool discovery and helper utilities.
Tool utilities for automatic schema generation from Python functions.
auto_tools(functions)
Automatically create both tool schema and tool map from functions.
| Parameters: |
|
|---|
| Returns: |
|
|---|
Example
def get_weather(location: str) -> str:
'''Get weather information for a location'''
return f"The weather in {location} is sunny and 75°F"
def calculate_math(expression: str) -> str:
'''Calculate a mathematical expression'''
try:
result = eval(expression)
return f"The result of {expression} is {result}"
except Exception:
return "Invalid mathematical expression"
tools_schema, tool_map = auto_tools([get_weather, calculate_math])
response = get_llm_response(
"What's the weather in Paris and what's 15 * 23?",
model='gpt-4o-mini',
provider='openai',
tools=tools_schema,
tool_map=tool_map
)
Source code in npcpy/tools.py
create_tool_map(functions)
create_tool_schema(functions)
Create OpenAI-style tool schema from a list of functions.
Source code in npcpy/tools.py
extract_function_info(func)
Extract function information including name, description, and parameters.
Source code in npcpy/tools.py
flatten_tool_messages(messages)
Convert tool_calls/tool messages to plain text for non-tool-capable models.
Keeps the information but in a format that won't break models that don't support the tool calling protocol.
Source code in npcpy/tools.py
python_type_to_json_schema(py_type)
Convert Python type hints to JSON schema types.