agent-system / tools /tool_generator.py
sp103107's picture
Upload folder using huggingface_hub
e20bd8e verified
Raw History Blame Contribute Delete
1.71 kB
import logging
from pathlib import Path
from datetime import datetime
from tools.smol_manager import get_smol_manager
import os
logger = logging.getLogger(__name__)
class ToolGenerator:
def __init__(self):
self.smol = get_smol_manager()
self.base_path = Path(__file__).parent.parent
def generate_tool(self, name: str, capabilities: list) -> str:
"""Generate tool code using HuggingFace API"""
try:
token = os.getenv("HUGGINGFACE_TOKEN")
if not token:
raise ValueError("HUGGINGFACE_TOKEN not found")
headers = {"Authorization": f"Bearer {token}"}
# Create tool prompt
prompt = f"""
Create a Python tool with these capabilities: {capabilities}
Requirements:
1. Include proper error handling
2. Implement core functionality
3. Follow Python best practices
4. Include documentation
The tool should be a complete, working Python module.
"""
# Generate code using SMOL
tool_code = self.smol.generate_text(prompt)
# Save tool code
tool_path = self.base_path / "tools" / f"{name}.py"
with open(tool_path, "w") as f:
f.write(tool_code)
return tool_path
except Exception as e:
logger.error(f"Error generating tool: {str(e)}")
raise
_instance = None
def get_tool_generator():
global _instance
if _instance is None:
_instance = ToolGenerator()
return _instance