# `LangChain.Chains.RoutingChain`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/chains/routing_chain.ex#L1)

Run a router based on a user's initial prompt to determine what category best
matches from the given options. If there is no good match, the value "DEFAULT"
is returned.

Here's an example:

    routes = [
      PromptRoute.new!(%{
        name: "marketing_email",
        description: "Create a marketing focused email",
        chain: marketing_email_chain
      }),
      PromptRoute.new!(%{
        name: "blog_post",
        description: "Create a blog post that will be linked from the company's landing page",
        chain: blog_post_chain
      }),
    ]

    selected_route =
      RoutingChain.new!(%{
        llm: ChatOpenAI.new!(%{model: "gpt-40-mini", stream: false}),
        input_text: "Let's create a marketing blog post about our new product 'Fuzzy Furies'",
        routes: routes,
        default_route: PromptRoute.new!(%{name: "DEFAULT", chain: fallback_chain})
      })
      |> RoutingChain.evaluate()

    # The PromptRoute for the `blog_post` should be returned as the `selected_route`.

The `llm` is the model used to make the determination of which route is the
best match. A smaller, faster LLM may be a great choice for the routing
decision, then a more complex LLM may be used for a selected route.

The `default_route` is required and is used as a fallback if the user's prompt
doesn't match any of the specified routes. It may also be used in some
fallback error situations as well.

## Callbacks

The `LLMChain` used to make the routing decision is built internally, so
handlers registered on the `llm` itself are not used. Pass `callbacks` to
observe the run instead:

    RoutingChain.new!(%{
      llm: llm,
      input_text: user_text,
      routes: routes,
      default_route: default_route,
      callbacks: [%{on_llm_token_usage: fn _chain, usage -> log_usage(usage) end}]
    })

Handlers are registered on the internally run `LLMChain`, so the full set of
`LangChain.Chains.ChainCallbacks` events is available.

# `t`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/chains/routing_chain.ex#L81)

```elixir
@type t() :: %LangChain.Chains.RoutingChain{
  callbacks: term(),
  default_route: term(),
  input_text: term(),
  llm: term(),
  routes: term(),
  verbose: term()
}
```

# `evaluate`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/chains/routing_chain.ex#L167)

```elixir
@spec evaluate(t(), Keyword.t()) :: LangChain.Routing.PromptRoute.t()
```

Runs the RoutingChain and evaluates the result to return the selected chain.

# `new`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/chains/routing_chain.ex#L90)

```elixir
@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}
```

Start a new RoutingChain.

# `new!`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/chains/routing_chain.ex#L101)

```elixir
@spec new!(attrs :: map()) :: t() | no_return()
```

Start a new RoutingChain and return it or raise an error if invalid.

# `run`
[🔗](https://github.com/brainlid/langchain/blob/v0.9.3/lib/chains/routing_chain.ex#L129)

```elixir
@spec run(t(), Keyword.t()) ::
  {:ok, LangChain.Chains.LLMChain.t()}
  | {:error, LangChain.Chains.LLMChain.t(), LangChain.LangChainError.t()}
```

Run a simple RoutingChain to analyze the input_text and determine which of the
given routes is the best match.

A simpler, faster LLM may be a great fit for running the analysis. If it fails
to find a good match, the `default_route` is used. The `default_route`'s name
is supplied to the LLM as well. The name "DEFAULT" is suggested for this
route.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
