Skip to content

Commit 25a2ab8

Browse files
committed
.call()
Signed-off-by: OEvortex <abhat8283@gmail.com>
1 parent 34e8bc0 commit 25a2ab8

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ All notable changes to the HelpingAI Python SDK will be documented in this file.
66

77
### Added
88
- **🔧 Tool Calling Framework**: New [`@tools decorator`](HelpingAI/tools/core.py:144) for effortless tool creation
9+
- **🔄 Direct Tool Execution**: New `.call()` method on HAI client for executing tools without registry manipulation
910
- **🤖 Automatic Schema Generation**: Type hint-based JSON schema creation with docstring parsing
1011
- **📝 Smart Documentation**: Multi-format docstring parsing (Google, Sphinx, NumPy styles)
1112
- **🧠 Thread-Safe Tool Registry**: Reliable tool management in multi-threaded environments
@@ -19,6 +20,7 @@ All notable changes to the HelpingAI Python SDK will be documented in this file.
1920
- **🔄 Universal Compatibility**: Seamless integration with existing OpenAI-format tools
2021
- **Updated Models**: Support for latest models (Dhanishtha-2.0-preview, Dhanishtha-2.0-preview-mini)
2122
- **Improved Model Management**: Better fallback handling and detailed model descriptions
23+
- **Simplified Tool Execution**: Direct tool calling with `client.call(tool_name, arguments)` syntax
2224
- Deprecated `get_tools_format()` in favor of `get_tools()`
2325
- Updated documentation to reflect current model names and best practices
2426

HelpingAI/client.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -883,3 +883,32 @@ def __init__(
883883
super().__init__(api_key, organization, base_url, timeout)
884884
self.chat: Chat = Chat(self)
885885
self.models: Models = Models(self)
886+
887+
def call(self, tool_name: str, arguments: Dict[str, Any]) -> Any:
888+
"""
889+
Directly call a tool by name with the given arguments.
890+
891+
This method provides a convenient way to execute tools without having to
892+
manually use get_registry() and Fn objects.
893+
894+
Args:
895+
tool_name: Name of the tool to call
896+
arguments: Arguments to pass to the tool
897+
898+
Returns:
899+
Result of the tool execution
900+
901+
Raises:
902+
ValueError: If the tool is not found
903+
ToolExecutionError: If the tool execution fails
904+
"""
905+
# Import here to avoid circular imports
906+
from .tools import get_registry
907+
908+
# Get the tool from the registry
909+
tool = get_registry().get_tool(tool_name)
910+
if not tool:
911+
raise ValueError(f"Tool '{tool_name}' not found in registry")
912+
913+
# Call the tool with the provided arguments
914+
return tool.call(arguments)

README.md

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The official Python library for the [HelpingAI](https://helpingai.co) API - Adva
1111
- **OpenAI-Compatible API**: Drop-in replacement with familiar interface
1212
- **Emotional Intelligence**: Advanced AI models with emotional understanding
1313
- **Tool Calling Made Easy**: [`@tools decorator`](HelpingAI/tools/core.py:144) for effortless function-to-tool conversion
14+
- **Direct Tool Execution**: Simple `.call()` method for executing tools without registry manipulation
1415
- **Automatic Schema Generation**: Type hint-based JSON schema creation with docstring parsing
1516
- **Universal Tool Compatibility**: Seamless integration with OpenAI-format tools
1617
- **Streaming Support**: Real-time response streaming
@@ -201,6 +202,45 @@ response = hai.chat.completions.create(
201202
print(response.choices[0].message.content)
202203
```
203204

205+
### Direct Tool Execution
206+
207+
The HAI client provides a convenient `.call()` method to directly execute tools without having to manually use the registry:
208+
209+
```python
210+
from HelpingAI import HAI
211+
from HelpingAI.tools import tools
212+
213+
@tools
214+
def search(query: str, max_results: int = 5):
215+
"""Search the web for information"""
216+
# Implementation here
217+
return {"results": [{"title": "Result 1", "url": "https://example.com"}]}
218+
219+
# Create a client instance
220+
client = HAI()
221+
222+
# Directly call a tool by name with arguments
223+
search_result = client.call("search", {"query": "python programming", "max_results": 3})
224+
print("Search results:", search_result)
225+
226+
# You can also execute tools from model responses
227+
response = client.chat.completions.create(
228+
model="Dhanishtha-2.0-preview",
229+
messages=[{"role": "user", "content": "search for quantum computing"}],
230+
tools=get_tools(),
231+
tool_choice="auto"
232+
)
233+
234+
# Extract tool name and arguments from the model's tool call
235+
tool_call = response.choices[0].message.tool_calls[0]
236+
tool_name = tool_call.function.name
237+
tool_args = json.loads(tool_call.function.arguments)
238+
239+
# Execute the tool directly
240+
tool_result = client.call(tool_name, tool_args)
241+
print(f"Result: {tool_result}")
242+
```
243+
204244
### Advanced Tool Features
205245

206246
#### Type System Support
@@ -318,11 +358,9 @@ def divide_numbers(a: float, b: float) -> float:
318358
# Handle tool execution in your application
319359
def execute_tool_safely(tool_name: str, arguments: dict):
320360
try:
321-
tool = get_registry().get_tool(tool_name)
322-
if not tool:
323-
return {"error": f"Tool '{tool_name}' not found"}
324-
325-
return tool.call(arguments)
361+
# You can use the direct call method instead of registry manipulation
362+
hai = HAI()
363+
return hai.call(tool_name, arguments)
326364

327365
except ToolExecutionError as e:
328366
print(f"Tool execution failed: {e}")

0 commit comments

Comments
 (0)