Wednesday, August 2, 2023

What is LLMChain, PromptTemplate

A LLMChain is the most common type of chain. It consists of a PromptTemplate, a model (either an LLM or a ChatModel), and an optional output parser. This chain takes multiple input variables, uses the PromptTemplate to format them into a prompt. It then passes that to the model.


Language models take text as input - that text is commonly referred to as a prompt. Typically this is not simply a hardcoded string but rather a combination of a template, some examples, and user input. LangChain provides several classes and functions to make constructing and working with prompts easy.


A prompt template refers to a reproducible way to generate a prompt. It contains a text string ("the template"), that can take in a set of parameters from the end user and generates a prompt.


A prompt template can contain:

instructions to the language model,

a set of few shot examples to help the language model generate a better response,

a question to the language model.

Here's the simplest example:


from langchain import PromptTemplate

template = """\

You are a naming consultant for new companies.

What is a good name for a company that makes {product}?

"""


prompt = PromptTemplate.from_template(template)

prompt.format(product="colorful socks")


References:

https://docs.langchain.com/docs/components/chains/llm-chain#:~:text=A%20LLMChain%20is%20the%20most,passes%20that%20to%20the%20model.



No comments:

Post a Comment