agents.components.semantic_router#
Module Contents#
Classes#
Enum representing the operational modes of the SemanticRouter. Modes: |
|
A unified component that routes semantic information from input topics to output topics. |
API#
- class agents.components.semantic_router.RouterMode(*args, **kwds)#
Bases:
enum.EnumEnum representing the operational modes of the SemanticRouter. Modes:
LLM: Agentic mode using LLM for intent analysis and routing
VECTOR: Vector mode using embeddings and vector database for routing
DECISION: Decision mode using a decision model to choose the route
- name()#
The name of the Enum member.
- value()#
The value of the Enum member.
- class agents.components.semantic_router.SemanticRouter(*, inputs: List[agents.ros.Topic], routes: List[agents.ros.Route], config: Optional[Union[agents.config.SemanticRouterConfig, agents.config.LLMConfig]] = None, db_client: Optional[agents.clients.db_base.DBClient] = None, model_client: Optional[agents.clients.model_base.ModelClient] = None, default_route: Optional[agents.ros.Route] = None, component_name: str, **kwargs)#
Bases:
agents.components.llm.LLMA unified component that routes semantic information from input topics to output topics.
This component can operate in three modes:
Vector Mode (Standard): Uses a vector database to route inputs based on embedding similarity to route samples.
LLM Mode (Agentic): Uses an LLM to intelligently analyze intent and route inputs via function calling.
Decision Mode: Uses a decision model to choose the route in one forward pass, without generating text. The model reads the intent of the input, as an LLM does, and gives a confidence for its choice, which works as the distance threshold does in vector mode.
The mode is determined automatically based on the client provided (
db_clientvsmodel_client, and for amodel_client, whether its model is an LLM or a decision model).- Parameters:
inputs (list[Topic]) – A list of input text topics that this component will subscribe to.
routes (list[Route]) – A list of pre-defined routes that publish incoming input to the routed output topics.
default_route (Optional[Route]) – An optional route that specifies the default behavior when no specific route matches. In Vector Mode, this is used based on distance threshold. In LLM Mode, this is used if the model fails to select a route. In Decision Mode, this is used when the confidence of the model’s choice is below
minimum_confidence.config (Union[SemanticRouterConfig, LLMConfig]) – The configuration object. accepts
SemanticRouterConfig(for vector and decision mode parameters) orLLMConfig(if specific LLM settings are needed). Defaults to SemanticRouterConfig.db_client (Optional[DBClient]) – (Vector Mode) A database client used to store and retrieve routing information.
model_client (Optional[ModelClient]) – (LLM Mode) A model client used for intelligent intent analysis and tool calling. (Decision Mode) A model client with a decision model: GenericHTTPClient with a GenericDecisionModel, or OllamaClient with a decision model tag.
component_name (str) – The name of this Semantic Router component (default: “router_component”).
kwargs – Additional keyword arguments.
Example usage (Vector Mode):
# ... define topics and routes ... config = SemanticRouterConfig(router_name="my_vector_router") db_client = HTTPDBClient(db=ChromaDB(host='localhost', port=8080)) router = SemanticRouter( inputs=[input_text], routes=[route1, route2], db_client=db_client, config=config, component_name="router" )
Example usage (LLM Mode):
# ... define topics and routes ... model_client = OllamaClient(model_name="llama3", checkpoint=llama3.1:latest, init_params={"temperature": 0.0}) router = SemanticRouter( inputs=[input_text], routes=[route1, route2], model_client=model_client, component_name="smart_router" )
Example usage (Decision Mode):
# ... define topics and routes ... model_client = GenericHTTPClient(GenericDecisionModel(name="lev", checkpoint="lev"), port=8090) router = SemanticRouter( inputs=[input_text], routes=[route1, route2], default_route=route1, model_client=model_client, config=SemanticRouterConfig(router_name="my_decision_router", minimum_confidence=0.3), component_name="decision_router" )
- custom_on_deactivate()#
Deactivate component.
- abstractmethod set_component_prompt(template: Union[str, pathlib.Path]) None#
LOCKED: The SemanticRouter does not use component prompts.
- abstractmethod set_topic_prompt(input_topic: agents.ros.Topic, template: Union[str, pathlib.Path]) None#
LOCKED: Input topics are routed as-is.
- abstractmethod register_tool(tool, tool_description, send_tool_response_to_model=False) None#
LOCKED: Tools are automatically generated from ‘Route’ objects.
- abstractmethod add_documents(ids, metadatas, documents) None#
LOCKED: Document storage is managed via Route samples.