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

A convenience chain for turning a user's prompt text into a summarized title
for the anticipated conversation.

## Basic Examples
A basic example that generates a title

    llm = ChatOpenAI.new!(%{model: "gpt-3.5-turbo", stream: false, seed: 0})
    user_text = "Let's start a new blog post about the magical properties of pineapple cookies."

    %{
      llm: llm,
      input_text: user_text
    }
    |> TextToTitleChain.new!()
    |> TextToTitleChain.evaluate()

    #=> "Magical Properties of Pineapple Cookies Blog Post"

## Examples using Title Examples
Want to get more consistent titles?

LLMs are pretty bad at following instructions for text length. However, we can
provide examples titles for the LLM to follow in format style and length. We
get the added benefit of getting more consistently formatted titles.

This is the same example, however now we provide other title examples to the
LLM to follow for consistency.

   llm = ChatOpenAI.new!(%{model: "gpt-3.5-turbo", stream: false, seed: 0})
    user_text = "Let's start a new blog post about the magical properties of
    pineapple cookies."

    %{
      llm: llm,
      input_text: user_text,
      examples: [
        "Blog Post: Making Delicious and Healthy Smoothies",
        "System Email: Notifying Users of Planned Downtime"
      ]
    }
    |> TextToTitleChain.new!()
    |> TextToTitleChain.evaluate()

    #=> "Blog Post: Exploring the Magic of Pineapple Cookies"

## Overriding the System Prompt
For more explicit control of how titles are generated, an `override_system_prompt` can be provided.

    %{
      llm: llm,
      input_text: user_text,
      override_system_prompt: ~s|
        You expertly summarize the User Text into a short 3 or 4 word title to represent a conversation in a positive way.|
    }
    |> TextToTitleChain.new!()
    |> TextToTitleChain.evaluate()

## Using a Fallback
If the primary LLM fails to respond successfully, one or more fallback LLMs can be specified.

    %{
      llm: primary_llm,
      input_text: user_text
    }
    |> TextToTitleChain.new!()
    |> TextToTitleChain.evaluate(with_fallbacks: [fallback_llm])

## Callbacks
The `LLMChain` that generates the title is built internally, so handlers
registered on the `llm` itself are not used. Pass `callbacks` to observe the
run instead:

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

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/text_to_title_chain.ex#L109)

```elixir
@type t() :: %LangChain.Chains.TextToTitleChain{
  callbacks: term(),
  examples: term(),
  fallback_title: term(),
  input_text: term(),
  llm: term(),
  override_system_prompt: term(),
  verbose: term()
}
```

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

```elixir
@spec evaluate(t(), Keyword.t()) :: String.t()
```

Runs the TextToTitleChain and evaluates the result to return the final answer.

If unable to generate a title, the `fallback_title` is returned.

## Option
- `:with_fallbacks` - Supports the `with_fallbacks: [fallback_llm]` where one or more additional LLMs can be specified as a backup when the preferred LLM fails.

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

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

Start a new TextToTitleChain configuration.

    {:ok, chain} = TextToTitleChain.new(%{
      llm: %ChatOpenAI{model: "gpt-3.5-turbo", stream: false},
      input_text: "Let's create a marketing blog post about our new product 'Fuzzy Furries'"
    })

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

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

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

    chain = TextToTitleChain.new!(%{
      llm: %ChatOpenAI{model: "gpt-3.5-turbo", stream: false},
      input_text: "Let's create a marketing blog post about our new product 'Fuzzy Furries'"
    })

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

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

Run a simple LLMChain to summarize the user's prompt into a title for the
conversation. Uses the provided model. Recommend faster, simpler LLMs without
streaming.

If it fails to summarize to a title, it returns the default text.

    new_title = TextToTitleChain.new!(%{
      llm: %ChatOpenAI{model: "gpt-3.5-turbo", stream: false},
      input_text: "Let's create a marketing blog post about our new product 'Fuzzy Furries'"
    })
    |> TextToTitleChain.run()

---

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