Files
Shanghua Gao 9f7070ed7b update coding tools and tests system and vis (#17)
### **Visualization Suite**
- 2D/3D molecular visualization with interactive HTML output
- Protein structure rendering with multiple visualization styles
- Scientific plotting with Plotly, Matplotlib, and NetworkX

### **Coding API**
- Type-safe function calling: `from tooluniverse.tools import ToolName`
- Dynamic tool access: `tu.tools.ToolName()`
- Built-in caching and error handling

### **Health Check System**
- New `tooluniverse-doctor` CLI tool
- Automatic dependency validation and bulk fix commands

### **Expanded Tool Ecosystem**
- 50+ new scientific tools
- Clinical guidelines (NICE, WHO, Europe PMC)
2025-10-12 20:24:07 -04:00

146 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""
ToolUniverse Coding API Examples
Demonstrates various methods for calling tools using the coding API
"""
from tooluniverse import ToolUniverse
from pathlib import Path
# =============================================================================
# Method 1: Dynamic Function-Style Calling
# =============================================================================
# Description: Call tools like regular Python functions through tu.tools
# Syntax: tu.tools.ToolName(parameter=value)
tu = ToolUniverse()
tu.load_tools()
# Basic function-style calling
result1 = tu.tools.UniProt_get_entry_by_accession(accession="P05067")
# Error handling for non-existent tools
try:
tu.tools.NonExistentTool(some_param="test")
except AttributeError as e:
# Handle non-existent tool error
pass
# Caching functionality
result2 = tu.tools.UniProt_get_entry_by_accession(
accession="P05067",
use_cache=True
)
# Lifecycle management
tu.tools.refresh()
tu.tools.eager_load(["UniProt_get_entry_by_accession"])
# =============================================================================
# Method 2: Generated Tools Calling
# =============================================================================
# Description: Use pre-generated tools for type-safe tool calling
# Syntax: from tooluniverse.tools import ToolName
# Note: Tools are auto-generated: python scripts/build_tools.py
# Import generated tools
from tooluniverse.tools import UniProt_get_entry_by_accession
from tooluniverse.exceptions import ToolValidationError
# Call tool function directly
result3 = UniProt_get_entry_by_accession(accession="P05067")
# Structured error handling
try:
result4 = UniProt_get_entry_by_accession(accession="invalid")
except ToolValidationError as e:
# Handle structured validation errors
# e.next_steps contains suggested actions
# e.retriable indicates if retry is possible
pass
# =============================================================================
# Method 3: Traditional JSON-Based Calling
# =============================================================================
# Description: Use original JSON-based calling method
# Syntax: tu.run_one_function({'name': 'ToolName', 'arguments': {...}})
# Basic JSON calling
result5 = tu.run_one_function({
"name": "UniProt_get_entry_by_accession",
"arguments": {"accession": "P05067"}
})
# Error scenarios with JSON calling
error_scenarios = [
{
"name": "Non-existent tool",
"call": lambda: tu.tools.NonExistentTool(param="test")
},
{
"name": "Invalid parameters",
"call": lambda: tu.run_one_function({
"name": "UniProt_get_entry_by_accession",
"arguments": {"invalid_param": "test"}
})
},
{
"name": "Missing required parameter",
"call": lambda: tu.run_one_function({
"name": "UniProt_get_entry_by_accession",
"arguments": {}
})
}
]
# Test error scenarios
for scenario in error_scenarios:
try:
result = scenario["call"]()
# Check for dual-format error response
if isinstance(result, dict) and "error" in result:
# Handle error response format
error_details = result.get("error_details", {})
# error_details contains: type, retriable, next_steps
pass
except Exception as e:
# Handle exception-based errors
pass
# =============================================================================
# Method 4: Multiple Individual Calls
# =============================================================================
# Description: Execute multiple tools individually (alternative to batch calling)
# Syntax: Multiple tu.run_one_function() calls
result6a = tu.run_one_function({
"name": "UniProt_get_entry_by_accession",
"arguments": {"accession": "P05067"}
})
result6b = tu.run_one_function({
"name": "UniProt_get_entry_by_accession",
"arguments": {"accession": "P12345"}
})
# =============================================================================
# Method 5: Tool Discovery and Dynamic Selection
# =============================================================================
# Description: Discover and call tools dynamically
# Syntax: getattr(tu.tools, 'ToolName')(parameters)
tool_name = "UniProt_get_entry_by_accession"
if hasattr(tu.tools, tool_name):
tool_func = getattr(tu.tools, tool_name)
result7 = tool_func(accession="P05067")
# =============================================================================
# Summary of Methods
# =============================================================================
# All methods return the same type of result objects.
# Choose the method that best fits your use case:
# - Dynamic calling: Most intuitive for single tool calls
# - Generated SDK: Type-safe with IDE support
# - JSON-based: Useful for dynamic tool selection
# - Batch calling: Efficient for multiple tool calls
# - Dynamic selection: Good for programmatic tool discovery