0
0
LangchainHow-ToBeginner ยท 3 min read

How to Create Chain in LangChain: Simple Guide with Example

To create a chain in LangChain, import the LLMChain class and combine it with a language model and a prompt template. Then instantiate LLMChain with these components to build a chain that processes input and generates output.
๐Ÿ“

Syntax

Creating a chain in LangChain involves three main parts:

  • LLM: The language model that generates text.
  • PromptTemplate: A template that formats the input prompt.
  • LLMChain: The chain that connects the prompt and the model.

The basic syntax is:

from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

llm = OpenAI()
prompt = PromptTemplate(input_variables=["input"], template="Your prompt: {input}")
chain = LLMChain(llm=llm, prompt=prompt)
python
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

llm = OpenAI()
prompt = PromptTemplate(input_variables=["input"], template="Your prompt: {input}")
chain = LLMChain(llm=llm, prompt=prompt)
๐Ÿ’ป

Example

This example shows how to create a simple chain that asks the language model to repeat a given input with a message.

python
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

# Initialize the language model
llm = OpenAI(temperature=0)

# Define the prompt template with an input variable
prompt = PromptTemplate(
    input_variables=["text"],
    template="Repeat after me: {text}"
)

# Create the chain with the model and prompt
chain = LLMChain(llm=llm, prompt=prompt)

# Run the chain with input
result = chain.run({"text": "Hello, LangChain!"})
print(result)
Output
Repeat after me: Hello, LangChain!
โš ๏ธ

Common Pitfalls

Common mistakes when creating chains in LangChain include:

  • Not matching input_variables in the prompt template with the keys in the input dictionary.
  • Forgetting to instantiate the language model before passing it to the chain.
  • Passing input as a plain string instead of a dictionary with keys matching input_variables.

Example of wrong and right usage:

python
# Wrong: input_variables expect 'text' but input is a string
from langchain import LLMChain, PromptTemplate
from langchain.llms import OpenAI

llm = OpenAI()
prompt = PromptTemplate(input_variables=["text"], template="Say: {text}")
chain = LLMChain(llm=llm, prompt=prompt)

# This will cause an error
# result = chain.run("Hello")  # Wrong

# Right: input is a dict matching input_variables
result = chain.run({"text": "Hello"})  # Correct
print(result)
Output
Say: Hello
๐Ÿ“Š

Quick Reference

Tips for creating chains in LangChain:

  • Always define input_variables in PromptTemplate matching your input keys.
  • Use LLMChain to connect your prompt and language model.
  • Pass inputs as dictionaries with keys matching input_variables.
  • Set model parameters like temperature to control output randomness.
โœ…

Key Takeaways

Create a chain by combining an LLM and a PromptTemplate using LLMChain.
Ensure input keys match the prompt template's input_variables exactly.
Pass inputs as dictionaries, not plain strings, to the chain's run method.
Initialize the language model before creating the chain.
Adjust model parameters like temperature to control output style.