Skip to main content

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.

License: MIT Python 3.9+

๐Ÿš€ 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-calling for 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

๐Ÿ“ง 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

funcroute-0.1.0.tar.gz (44.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

funcroute-0.1.0-py3-none-any.whl (48.4 kB view details)

Uploaded Python 3

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

Hashes for funcroute-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8758ce5fb2f1dd4032184877d720315a195546b73fa03f97ce8f5995b176d0c1
MD5 1a8e43f44d4a1f97e0f45c6d2b2d1edd
BLAKE2b-256 e504ae43f6ee1d86129fcdeec82dfe3c5a7d5641cfffb7f731252677969d1afd

See more details on using hashes here.

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

Hashes for funcroute-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5162be1afa99f30193a90f184312b22099da0fc431ff5250bb9bf0adf5c9d8b1
MD5 d2b89362f48a46dfbfe2d8d210b13bc6
BLAKE2b-256 5dd02bac2f4e29a33cd348bb2973310d6b7a04795f9821436902c845db421046

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page