Intelligent function/tool routing using FunctionGemma
Project description
FuncRoute
Intelligent Function/Tool Routing using FunctionGemma
FuncRoute is a Python package that enables smart task routing for agentic AI systems. Fine-tune Google's FunctionGemma model to intelligently route user queries to the appropriate function or tool.
๐ Features
Core Capabilities
- Easy Training: Fine-tune FunctionGemma with your own data or use synthetic data generation
- Pre-trained Models: Start with
scionoftech/functiongemma-e-commerce-tool-callingfor e-commerce - Flexible Data: Support for JSONL, CSV, pandas DataFrame, and Hugging Face Datasets
- Efficient Training: LoRA + 4-bit quantization for memory-efficient fine-tuning
- Fast Inference: Single and batch query routing with confidence scores
- Production Ready: REST API server, caching, and CLI interface
Data Generation
- Rule-based: Generate synthetic training data using pattern expansion (like the train.py example)
- LLM-based: Use any Hugging Face LLM to generate diverse training examples
Evaluation & Monitoring
- Comprehensive metrics (accuracy, precision, recall, F1)
- Confusion matrices and visualizations
- Performance dashboards
- Latency tracking
Framework Integrations
- LangGraph: Seamless integration with LangGraph workflows
- AutoGen: Compatible with Microsoft AutoGen agents
- CrewAI: Tool selection for CrewAI agents
- Google ADK: Agent Development Kit integration
๐ฆ Installation
pip install funcroute
From Source
git clone https://github.com/yourusername/funcroute.git
cd funcroute
pip install -e .
๐ฏ Quick Start
Using Pre-trained Model
from funcroute import FuncRoute
# Load pre-trained e-commerce model
router = FuncRoute.from_pretrained("scionoftech/functiongemma-e-commerce-tool-calling")
# Route a query
result = router.route("Where is my order?")
print(f"Tool: {result.tool}")
print(f"Confidence: {result.confidence:.2%}")
Training Your Own Model
from funcroute import FuncRoute, TrainingConfig
# Initialize router
router = FuncRoute()
# Configure training
config = TrainingConfig(
output_dir="./my_router",
num_epochs=3,
batch_size=4,
learning_rate=2e-4
)
# Train on your data
router.train(
train_data="train.jsonl",
val_data="val.jsonl",
config=config
)
# Use the trained model
result = router.route("Show me red dresses")
Generating Synthetic Data
from funcroute import SyntheticDataGenerator, ToolDefinition
# Define your tools
tools = [
ToolDefinition(
name="search_products",
signature="search_products(query: str, category: str) -> list",
description="Search for products",
examples=["Show me red dresses", "Find laptops under $1000"],
keywords=["show", "find", "search"]
),
# ... more tools
]
# Generate synthetic data
generator = SyntheticDataGenerator(method="rule_based")
data = generator.generate(
tools=tools,
num_variations=50,
domain_context="e-commerce"
)
# Train with synthetic data
router.train(train_data=data, config=config)
๐ ๏ธ CLI Usage
# Train a model
funcroute train --train-data train.jsonl --output-dir ./model
# Generate synthetic data
funcroute generate --tools tools.json --output synthetic.jsonl
# Evaluate model
funcroute evaluate --model ./model --test-data test.jsonl
# Serve model as REST API
funcroute serve --model ./model --port 8000
# Use pre-trained model
funcroute serve --model scionoftech/functiongemma-e-commerce-tool-calling --port 8000
# Interactive testing
funcroute interactive --model ./model
๐ Framework Integrations
LangGraph
from funcroute.integrations import LangGraphRouter
from langgraph.graph import StateGraph
router = FuncRoute.load("./my_model")
langgraph_router = LangGraphRouter(router)
workflow = StateGraph(...)
workflow.add_node("router", langgraph_router.route_node)
AutoGen
from funcroute.integrations import AutoGenRouter
router = FuncRoute.load("./my_model")
autogen_router = AutoGenRouter(router)
assistant = autogen.AssistantAgent(
name="assistant",
function_map=autogen_router.get_function_map()
)
CrewAI
from funcroute.integrations import CrewAIRouter
router = FuncRoute.load("./my_model")
crewai_router = CrewAIRouter(router)
agent = Agent(
role="Support Agent",
tools=crewai_router.get_tools(),
tool_selector=crewai_router.select_tool
)
๐ Example: E-commerce Support Routing
See examples/ecommerce for a complete working example based on the original train.py.
from funcroute import FuncRoute, TrainingConfig, ToolDefinition
from funcroute.data import SyntheticDataGenerator
# Define 7 e-commerce support tools
tools = [
ToolDefinition(
name="manage_order",
signature="manage_order(order_id: str, action: str) -> dict",
description="Track, update, or cancel customer orders",
examples=["Where's my order?", "Track package #12345"],
keywords=["order", "track", "delivery", "shipping"]
),
# ... 6 more tools
]
# Generate training data
generator = SyntheticDataGenerator(method="rule_based")
train_data = generator.generate(tools=tools, num_variations=50)
# Train model
router = FuncRoute()
router.train(
train_data=train_data,
config=TrainingConfig(output_dir="./ecommerce_router")
)
# Test
result = router.route("I want to return my shoes")
# Output: tool="process_return", confidence=0.97
๐ Documentation
- PACKAGE_PLAN.md - Complete implementation plan
- CLAUDE.md - Development environment setup
- Full API documentation (coming soon)
๐๏ธ Project Structure
funcroute/
โโโ funcroute/
โ โโโ core/ # Main FuncRoute class and configs
โ โโโ data/ # Data loading, formatting, generation
โ โโโ training/ # Training orchestration
โ โโโ inference/ # Inference and serving
โ โโโ evaluation/ # Metrics and visualization
โ โโโ integrations/ # Framework adapters
โ โโโ utils/ # Utilities
โโโ examples/ # Example implementations
โโโ tests/ # Test suite
โโโ docs/ # Documentation
๐ฏ Supported Models
- Routing Model:
google/functiongemma-270m-it(only) - Synthetic Data Generation: Any Hugging Face text generation model
- Pre-trained:
scionoftech/functiongemma-e-commerce-tool-calling
๐ง Requirements
- Python 3.9+
- PyTorch 2.0+
- transformers >= 4.36.0
- CUDA GPU recommended (CPU supported but slow)
๐ Data Format
Training Data (JSONL)
{"query": "Where is my order?", "tool": "manage_order"}
{"query": "Show me red dresses", "tool": "search_products"}
Tool Definitions (JSON)
{
"tools": [
{
"name": "search_products",
"signature": "search_products(query: str) -> list",
"description": "Search for products",
"examples": ["Show me laptops", "Find red shoes"],
"keywords": ["show", "find", "search"]
}
]
}
๐ค Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
๐ License
MIT License - see LICENSE for details.
๐ Acknowledgments
- Built on Google's FunctionGemma
- Pre-trained model by scionoftech
- Inspired by the original train.py e-commerce example
๐ง Contact
For questions or feedback, please open an issue on GitHub.
Note: FuncRoute is currently in active development (v0.1.0). APIs may change before the 1.0 release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file funcroute-0.1.0.tar.gz.
File metadata
- Download URL: funcroute-0.1.0.tar.gz
- Upload date:
- Size: 44.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8758ce5fb2f1dd4032184877d720315a195546b73fa03f97ce8f5995b176d0c1
|
|
| MD5 |
1a8e43f44d4a1f97e0f45c6d2b2d1edd
|
|
| BLAKE2b-256 |
e504ae43f6ee1d86129fcdeec82dfe3c5a7d5641cfffb7f731252677969d1afd
|
File details
Details for the file funcroute-0.1.0-py3-none-any.whl.
File metadata
- Download URL: funcroute-0.1.0-py3-none-any.whl
- Upload date:
- Size: 48.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5162be1afa99f30193a90f184312b22099da0fc431ff5250bb9bf0adf5c9d8b1
|
|
| MD5 |
d2b89362f48a46dfbfe2d8d210b13bc6
|
|
| BLAKE2b-256 |
5dd02bac2f4e29a33cd348bb2973310d6b7a04795f9821436902c845db421046
|