redis_openai_agents.SemanticRouter#

class SemanticRouter(name, routes, redis_url='redis://localhost:6379', vectorizer=None, overwrite=True, routing_config=None)[source]#

Semantic router for classifying statements into predefined routes.

Uses vector similarity search via RedisVL to match input statements to the most appropriate route based on semantic similarity to reference phrases.

Example:

routes = [
    Route(
        name="technology",
        references=["AI news", "programming tips", "tech updates"],
    ),
    Route(
        name="sports",
        references=["football scores", "basketball highlights"],
    ),
]

async with SemanticRouter(
    name="topic-router",
    routes=routes,
    redis_url="redis://localhost:6379",
) as router:
    result = await router("What's new in machine learning?")
    print(result.name)  # "technology"

Initialize the semantic router.

Parameters:
  • name (str) – Unique name for this router (used as Redis index prefix).

  • routes (list[Route]) – List of Route objects defining classification categories.

  • redis_url (str) – Redis connection URL.

  • vectorizer (Any | None) – Optional vectorizer instance. Defaults to HuggingFace sentence-transformers/all-MiniLM-L6-v2.

  • overwrite (bool) – If True, overwrite existing index with same name. If False, raise ValueError if index exists.

  • routing_config (dict[str, Any] | None) – Optional configuration for routing behavior (max_k, aggregation_method).

Raises:

ValueError – If routes is empty, has duplicates, or index exists and overwrite=False.

__init__(name, routes, redis_url='redis://localhost:6379', vectorizer=None, overwrite=True, routing_config=None)[source]#

Initialize the semantic router.

Parameters:
  • name (str) – Unique name for this router (used as Redis index prefix).

  • routes (list[Route]) – List of Route objects defining classification categories.

  • redis_url (str) – Redis connection URL.

  • vectorizer (Any | None) – Optional vectorizer instance. Defaults to HuggingFace sentence-transformers/all-MiniLM-L6-v2.

  • overwrite (bool) – If True, overwrite existing index with same name. If False, raise ValueError if index exists.

  • routing_config (dict[str, Any] | None) – Optional configuration for routing behavior (max_k, aggregation_method).

Raises:

ValueError – If routes is empty, has duplicates, or index exists and overwrite=False.

Return type:

None

Methods

__init__(name, routes[, redis_url, ...])

Initialize the semantic router.

add_route_references(route_name, references)

Add new references to an existing route.

close()

Close the router and release resources.

delete_route_references(route_name[, ...])

Delete references from a route.

from_dict(data[, redis_url, vectorizer, ...])

Create router from dict configuration.

get_route(name)

Get route by name.

get_route_references(route_name[, reference_ids])

Get references for a route.

remove_route(route_name)

Remove an entire route and its references.

route_many(statement[, max_k, ...])

Route a statement to multiple matching routes.

to_dict()

Serialize router configuration to dict.

Attributes

name

Get router name.

route_names

Get list of all route names.