Skip to main content

LLM Adaptive Router

LLM Adaptive Router is a Python package that enables dynamic model selection based on query content. It uses efficient vector search for initial categorization and LLM-based fine-grained selection for complex cases. The router can adapt and learn from feedback, making it suitable for a wide range of applications.

Features

  • Dynamic model selection based on query content
  • Efficient vector search for initial categorization
  • LLM-based fine-grained selection for complex cases
  • Adaptive learning from feedback
  • Flexible configuration of routes and models
  • Easy integration with LangChain and various LLM providers

Installation

You can install LLM Adaptive Router using pip:

pip3 install llm-adaptive-router

Quick Start

Here's a basic example of how to use LLM Adaptive Router:

from llm_adaptive_router import AdaptiveRouter, RouteMetadata
from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings, ChatOpenAI
from dotenv import load_dotenv
load_dotenv()

gpt_3_5_turbo = ChatOpenAI(model="gpt-3.5-turbo")
mini = ChatOpenAI(model="gpt-4o-mini")
gpt_4 = ChatOpenAI(model="gpt-4")

routes = {
    "general": RouteMetadata(
        invoker=gpt_3_5_turbo,
        capabilities=["general knowledge"],
        cost=0.002,
        example_sentences=["What is the capital of France?", "Explain photosynthesis."]
    ),
    "mini": RouteMetadata(
        invoker=mini,
        capabilities=["general knowledge"],
        cost=0.002,
        example_sentences=["What is the capital of France?", "Explain photosynthesis."]
        
    ),
    "math": RouteMetadata(
        invoker=gpt_4,
        capabilities=["advanced math", "problem solving"],
        cost=0.01,
        example_sentences=["Solve this differential equation.", "Prove the Pythagorean theorem."]
    )
}

llm = ChatOpenAI(model="gpt-3.5-turbo")

router = AdaptiveRouter(
    vectorstore=Chroma(embedding_function=OpenAIEmbeddings()),
    llm=llm,
    embeddings=OpenAIEmbeddings(),
    routes=routes
)

query = "How are you"
query2 = "Write a Python function to hello world"
selected_model_route = router.route(query)
selected_model_name = selected_model_route
print(selected_model_name)
invoker = selected_model_route.invoker
response = invoker.invoke(query)

print(f"Response: {response}")

Detailed Usage

Creating Route Metadata

Use the create_route_metadata function to define routes:

from llm_adaptive_router import create_route_metadata

route = create_route_metadata(
    invoker=model_function,
    capabilities=["capability1", "capability2"],
    cost=0.01,
    example_sentences=["Example query 1", "Example query 2"],
    additional_info={"key": "value"}
)

Initializing the AdaptiveRouter

Create an instance of AdaptiveRouter with your configured routes:

router = AdaptiveRouter(
    vectorstore=your_vectorstore,
    llm=your_llm,
    embeddings=your_embeddings,
    routes=your_routes
)

Routing Queries

Use the route method to select the appropriate model for a query:

selected_model_route = router.route("Your query here")
selected_model_name = selected_model_route.model
invoker = selected_model_route.invoker
response = invoker.invoke("Your query here")

Adding Feedback

Improve the router's performance by providing feedback:

router.add_feedback(query, selected_model, performance_score)

Advanced Features

  • Custom Vector Stores: LLM Adaptive Router supports various vector stores. You can use any vector store that implements the VectorStore interface from LangChain.
  • Dynamic Route Updates: You can add or remove routes dynamically:
router.add_route("new_route", new_route_metadata)
router.remove_route("old_route")
  • Adjusting Router Behavior: Fine-tune the router's behavior:
router.set_complexity_threshold(0.8)
router.set_update_frequency(200)

Metadata

Release files for llm-adaptive-router 0.1.13

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llm-adaptive-router 0.1.13
File Size Uploaded Provenance
llm_adaptive_router-0.1.13.tar.gz 10.1 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-adaptive-router 0.1.13
File Interpreter ABI Platform
llm_adaptive_router-0.1.13-py3-none-any.whl Python 3 none any Details

Total release size: 21.7 kB

Release files / llm_adaptive_router-0.1.13.tar.gz

Download URL llm_adaptive_router-0.1.13.tar.gz
Size 10.1 kB
Tags Source
SHA-256 checksum
How to use checksums
99201bc35b184eaf060ae5a7ad52f2a302994b28a51e4bc245bcdf5791109723
BLAKE2b-256 checksum
How to use checksums
a95bd69ab9caedfa5b11e9b7e72907a6d73e8d123a7eea7158c10d9328e5f0af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.0

Release files / llm_adaptive_router-0.1.13-py3-none-any.whl

Download URL llm_adaptive_router-0.1.13-py3-none-any.whl
Size 11.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f7c5add71d88c6b2a66437e1979b2e665c7cfdd8a3c2fc7dbf8f728029a1fcf3
BLAKE2b-256 checksum
How to use checksums
76b71aa4bf6f77ed92370a5b664b4eedcb640e80e367ca882863b51695fed12d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.12.0

Security for llm-adaptive-router 0.1.13

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page