Skip to main content

An adaptive router for LLM model selection

Project description

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, create_route_metadata
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings, ChatOpenAI

# Initialize LLM models
gpt_3_5_turbo = ChatOpenAI("gpt-3.5-turbo", temperature=0)
codex = ChatOpenAI("codex", temperature=0)
gpt_4 = ChatOpenAI("gpt-4", temperature=0)

# Define routes
routes = {
    "general": create_route_metadata(
        model="gpt-3.5-turbo",
        invoker=gpt_3_5_turbo,
        capabilities=["general knowledge"],
        cost=0.002,
        example_sentences=["What is the capital of France?", "Explain photosynthesis."]
    ),
    "code": create_route_metadata(
        model="codex",
        invoker=codex,
        capabilities=["code generation", "debugging"],
        cost=0.005,
        example_sentences=["Write a Python function to sort a list.", "Debug this JavaScript code."]
    ),
    "math": create_route_metadata(
        model="gpt-4",
        invoker=gpt_4,
        capabilities=["advanced math", "problem solving"],
        cost=0.01,
        example_sentences=["Solve this differential equation.", "Prove the Pythagorean theorem."]
    )
}

# Initialize router
router = AdaptiveRouter(
    vectorstore=Chroma(embedding_function=OpenAIEmbeddings()),
    llm=ChatOpenAI("gpt-3.5-turbo", temperature=0),
    embeddings=OpenAIEmbeddings(),
    routes=routes
)

# Use the router
query = "Write a Python function to calculate the Fibonacci sequence"
selected_model_route = router.route(query)
selected_model_name = selected_model_route.model
invoker = selected_model_route.invoker
response = invoker.invoke(query)

print(f"Selected model: {selected_model_name}")
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(
    model="model_name",
    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)

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

llm_adaptive_router-0.1.1.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

llm_adaptive_router-0.1.1-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file llm_adaptive_router-0.1.1.tar.gz.

File metadata

  • Download URL: llm_adaptive_router-0.1.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.0

File hashes

Hashes for llm_adaptive_router-0.1.1.tar.gz
Algorithm Hash digest
SHA256 e389b9822d703aaf34a0feaa0efd38b7343968ac04c8f1b8b8d78c32c3b5df1b
MD5 0a06985e87f41506aec33759478a4477
BLAKE2b-256 9d2732a58ac44b7f59a742f69daad4562eeabbe9a6d0276de3269d784e56544d

See more details on using hashes here.

File details

Details for the file llm_adaptive_router-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_adaptive_router-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f932eacd53abbd7e79778d9f51168fa37b641280f3b36e8aff1491dc2334a8d8
MD5 7d2b49382856d7df813c9757b0327123
BLAKE2b-256 f736be9c2632c94bf8a3f952b8a74a0803d08ad4633df2e8c034172a5964c6c7

See more details on using hashes here.

Supported by

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