A tool routing AI package using embeddings and FAISS
Project description
Capability Tool Router
An AI-powered tool routing system that uses semantic embeddings to intelligently route user queries to the most appropriate tools. Built with Python, OpenAI embeddings, and FAISS vector search.
Features
- Semantic Tool Routing: Uses OpenAI embeddings and FAISS to match queries to tools based on semantic similarity
- Tool Registry: Flexible registry system for managing tools with descriptions and schemas
- Caching: Built-in result caching to improve performance and reduce API calls
- Feedback Learning: Tracks tool success/failure rates for continuous improvement
- OpenAPI Integration: Load tools directly from OpenAPI specifications
- Async Support: Fully asynchronous implementation for high performance
Installation
- Clone the repository:
git clone <repository-url>
cd capability-tool-router
- Install dependencies:
pip install -r requirements.txt
- Set up your OpenAI API key:
export OPENAI_API_KEY="your-api-key-here"
Quick Start
import asyncio
from tool_router_ai.registry import ToolRegistry
from tool_router_ai.router import ToolRouter
from tool_router_ai.embedder import Embedder
from tool_router_ai.models import Tool
# Define your tools
def get_weather(city: str):
return f"Weather for {city}"
def get_stock_price(symbol: str):
return f"Stock price for {symbol}"
async def main():
# Create registry and register tools
registry = ToolRegistry()
registry.register(Tool(
name="weather",
description="Get weather information for any city",
input_schema={"city": "string"},
func=get_weather
))
registry.register(Tool(
name="stocks",
description="Get stock market prices",
input_schema={"symbol": "string"},
func=get_stock_price
))
# Create embedder and router
embedder = Embedder()
router = ToolRouter(registry, embedder)
# Build the search index
await router.build_index()
# Route a query
query = "What's the weather like in San Francisco?"
tools = await router.route(query, top_k=1)
print(f"Selected tool: {tools[0].name}")
asyncio.run(main())
Architecture
Core Components
- ToolRouter: Main routing engine that builds FAISS index and performs semantic search
- ToolRegistry: Manages tool registration and discovery
- Embedder: Handles text embedding using OpenAI's API
- FeedbackStore: Tracks tool performance metrics
- ToolCache: Caches tool execution results
- OpenAPILoader: Imports tools from OpenAPI specifications
Tool Model
Each tool is defined with:
name: Unique identifierdescription: Human-readable description for embeddinginput_schema: Parameter specificationfunc: Python callable (for local tools)endpoint: API endpoint (for remote tools)
Advanced Usage
Loading Tools from OpenAPI
from tool_router_ai.openapi_loader import load_openapi_tools
# Load tools from an OpenAPI spec
tools = load_openapi_tools("https://api.example.com/openapi.json")
registry.register_many(tools)
Using Feedback for Learning
from tool_router_ai.feedback_store import FeedbackStore
feedback = FeedbackStore()
# After tool execution
try:
result = tool.func(**params)
feedback.record_success(tool.name)
except Exception as e:
feedback.record_failure(tool.name)
# Get success rate
score = feedback.score(tool.name)
Caching Results
from tool_router_ai.cache import ToolCache
cache = ToolCache()
# Check cache before execution
cached_result = cache.get(tool.name, params)
if cached_result:
return cached_result
# Execute and cache
result = tool.func(**params)
cache.set(tool.name, params, result)
Dependencies
faiss-cpu: Vector similarity searchnumpy: Numerical computingopenai: OpenAI API client
Testing
Run the test suite:
python test.py
Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
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
tool_router_ai-0.2.0.tar.gz
(4.8 kB
view details)
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 tool_router_ai-0.2.0.tar.gz.
File metadata
- Download URL: tool_router_ai-0.2.0.tar.gz
- Upload date:
- Size: 4.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
786e2f7ae11b5df7984c2babf8850f16385c9d301cd91a306925994a80b938a3
|
|
| MD5 |
2355a17c59ed4cd5afe80bdeac4eedb3
|
|
| BLAKE2b-256 |
c99dc58bd64f0d82e90a4a0f87c7b20d3b967a3eb99003f869d11cf64628d63f
|
File details
Details for the file tool_router_ai-0.2.0-py3-none-any.whl.
File metadata
- Download URL: tool_router_ai-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
92ee2fb4941b18a663a6a0ca3156e02812cebd494a86a484db198410e02f3337
|
|
| MD5 |
2e0ca57c8ee8a5924e628b372d3849df
|
|
| BLAKE2b-256 |
dcdc9347357a42a586f59d700b5350abda7b4305307f241dca701c6f660e0ffe
|