Skip to main content

AI-powered intent parsing system that converts natural language into structured tool calls

Project description

IntentAI

PyPI version Python 3.8+ License: MIT Code style: black Imports: isort Tests Documentation

AI-powered intent parsing system that converts natural language into structured tool calls. IntentAI understands what you want and transforms your requests into actionable commands with confidence scoring and validation.

Features

  • 🎯 Intent Detection: Parse natural language into structured tool calls
  • 🔧 Decorator-based Registration: Use @tool_call decorator to register functions as tools
  • 📊 Confidence Scoring: Get confidence scores for tool detection
  • Parameter Validation: Automatic parameter extraction and validation with Pydantic
  • 🎨 Flexible Matching: Support for multiple trigger phrases and examples
  • 📋 Schema Generation: Generate JSON Schema for your tools
  • 🚀 Lightweight: Minimal dependencies, fast performance

Installation

pip install intentai

Quick Start

Basic Usage

from intentai import detect_tool_and_params

# Detect calculator usage
result = detect_tool_and_params("Calculate 5 * 13")
print(result)
# {'tool': 'calculator', 'params': {'expression': '5 * 13'}}

# Detect weather lookup
result = detect_tool_and_params("Weather in London")
print(result)
# {'tool': 'get_weather', 'params': {'city': 'London'}}

Decorator-based Approach

from intentai import tool_call, get_tools_from_functions, detect_tool_and_params
from pydantic import BaseModel

class WeatherParams(BaseModel):
    city: str
    country: str = "US"

@tool_call(
    name="get_weather",
    description="Get current weather information for a city",
    trigger_phrases=["weather in", "weather for", "temperature in"],
    examples=[
        "weather in New York",
        "what's the temperature in London?",
        "weather for Tokyo"
    ]
)
def get_weather(city: str, country: str = "US") -> str:
    """Get weather information for a city."""
    return f"Weather in {city}, {country}: Sunny, 25°C"

# Register your functions
tools = get_tools_from_functions([get_weather])

# Detect tools in user input
result = detect_tool_and_params("What's the weather like in Paris?", tools)
print(result)
# DetectionResult(tool='get_weather', params={'city': 'Paris', 'country': 'US'}, confidence=0.85)

Advanced Features

Parameter Validation with Pydantic

from pydantic import BaseModel, Field

class CalculatorParams(BaseModel):
    expression: str = Field(..., description="Mathematical expression to evaluate")
    precision: int = Field(default=2, ge=0, le=10, description="Decimal precision")

@tool_call(
    name="calculator",
    description="Evaluate mathematical expressions",
    trigger_phrases=["calculate", "compute", "what is"],
    examples=["calculate 2 + 2", "what is 10 * 5", "compute 100 / 4"]
)
def calculator(expression: str, precision: int = 2) -> float:
    """Evaluate a mathematical expression."""
    return round(eval(expression), precision)

Schema Generation

from intentai import get_openapi_schema_for_tools

# Generate JSON Schema for your tools
schema = get_openapi_schema_for_tools(tools)
print(schema)

API Reference

Core Functions

  • detect_tool_and_params(text: str, tools: List[Tool] = None) -> DetectionResult
  • get_tools_from_functions(functions: List[Callable]) -> List[Tool]
  • get_openapi_schema_for_tools(tools: List[Tool]) -> Dict

Decorators

  • @tool_call(name: str, description: str, trigger_phrases: List[str], examples: List[str])

Data Models

  • Tool: Represents a tool with metadata and parameters
  • ToolParameter: Represents a tool parameter with type and validation
  • DetectionResult: Result of tool detection with confidence score
  • ParameterType: Enum of supported parameter types

Examples

Check out the examples directory for more comprehensive usage examples:

Development

Setup

  1. Clone the repository:

    git clone https://github.com/ameenalam/intentai.git
    cd intentai
    
  2. Install development dependencies:

    pip install -e ".[dev]"
    
  3. Install pre-commit hooks:

    pre-commit install
    

Testing

# Run all tests
pytest

# Run with coverage
pytest --cov=tool_detector --cov-report=html

# Run specific test file
pytest tests/test_detector.py

Code Quality

# Format code
black intentai tests examples

# Sort imports
isort intentai tests examples

# Type checking
mypy intentai

# Linting
flake8 intentai tests examples

Contributing

We welcome contributions! Please see our Contributing Guide for details.

Documentation

For detailed documentation, visit intentai.readthedocs.io.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Changelog

See CHANGELOG.md for a list of changes and version history.

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

intentai-0.1.0.tar.gz (17.1 kB view details)

Uploaded Source

Built Distribution

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

intentai-0.1.0-py3-none-any.whl (13.5 kB view details)

Uploaded Python 3

File details

Details for the file intentai-0.1.0.tar.gz.

File metadata

  • Download URL: intentai-0.1.0.tar.gz
  • Upload date:
  • Size: 17.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for intentai-0.1.0.tar.gz
Algorithm Hash digest
SHA256 aa45ec3b9e33995b499899f8d6d85d5c37d232cf3249b591fbdb2828cef9873f
MD5 025f98a5870737ed70cfbdc1499b5b61
BLAKE2b-256 61669a781ad9be56c34934790081a8bebaca609b5bc13eb06b50fbe7787ae291

See more details on using hashes here.

File details

Details for the file intentai-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: intentai-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 13.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for intentai-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f566cf8a166f71c0e43d7a1e0dc644e29cf4424359e04e820cc4888135b35129
MD5 ffc0849bce9cac7434d6a19e2f628abc
BLAKE2b-256 234dce2d2f8d2209a2afb2bc6dabd80d5a162e6e1fde2801dbc7527c51a0f061

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