Adapt to different tasks by conditionally navigating to various LLMs and tools.
A workflow where user input is classified and directed to a specific task (can be a specific LLM, specific custom prompt, different tool calls etc.). This allows you to handle for many different inputs and handle them with the appropriate set of calls.
from pydantic import BaseModel, Fieldfrom typing import Literal, Dictdef router_workflow(input_query: str, routes: Dict[str, str]) -> str: """Given a `input_query` and a dictionary of `routes` containing options and details for each. Selects the best model for the task and return the response from the model. """ ROUTER_PROMPT = """Given a user prompt/query: {user_query}, select the best option out of the following routes: {routes}. Answer only in JSON format.""" # Create a schema from the routes dictionary class Schema(BaseModel): route: Literal[tuple(routes.keys())] reason: str = Field( description="Short one-liner explanation why this route was selected for the task in the prompt/query." ) # Call LLM to select route selected_route = JSON_llm( ROUTER_PROMPT.format(user_query=input_query, routes=routes), Schema ) print( f"Selected route:{selected_route['route']}\nReason: {selected_route['reason']}\n" ) # Use LLM on selected route. # Could also have different prompts that need to be used for each route. response = run_llm(user_prompt=input_query, model=selected_route["route"]) print(f"Response: {response}\n") return response
prompt_list = [ "Produce python snippet to check to see if a number is prime or not.", "Plan and provide a short itenary for a 2 week vacation in Europe.", "Write a short story about a dragon and a knight.",]model_routes = { "Qwen/Qwen2.5-Coder-32B-Instruct": "Best model choice for code generation tasks.", "Gryphe/MythoMax-L2-13b": "Best model choice for story-telling, role-playing and fantasy tasks.", "Qwen/QwQ-32B-Preview": "Best model for reasoning, planning and multi-step tasks",}for i, prompt in enumerate(prompt_list): print(f"Task {i+1}: {prompt}\n") print(20 * "==") router_workflow(prompt, model_routes)
Routing easy/common questions to smaller models like Llama 3.1 8B and hard/unusual questions to more capable models like Deepseek v3 and Llama 3.3 70B to optimize cost and speed.
Directing different types of customer service queries (general questions, refund requests, technical support) into different downstream processes, prompts, and tools.
Different LLMs or model configurations excel at different tasks (e.g., writing summaries vs. generating code). Using a router, you can automatically detect the user’s intent and send the input to the best-fit model.
Evaluating whether a request meets certain guidelines or triggers specific filters (e.g., checking if content is disallowed). Based on the classification, forward it to the appropriate next LLM call or step.
If one model’s output doesn’t meet a certain confidence threshold or fails for some reason, route automatically to a fallback model.