0
0
LangChainframework~5 mins

What is LangChain

Choose your learning style9 modes available
Introduction

LangChain helps you build smart apps that use language models easily. It connects language models with other tools and data.

You want to create a chatbot that can answer questions using your own documents.
You need to combine a language model with a database or API to get updated information.
You want to build an app that can understand and generate text based on complex workflows.
You want to quickly prototype language model applications without handling all details yourself.
Syntax
LangChain
from langchain.chains import LLMChain

chain = LLMChain(llm=your_llm, prompt=your_prompt)
result = chain.run(input_text)
LLMChain connects a language model (LLM) with a prompt template to generate text.
You can combine chains to build more complex workflows.
Examples
This example creates a chain that translates English text to French using an OpenAI model.
LangChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

prompt = PromptTemplate(template="Translate '{text}' to French.", input_variables=["text"])
llm = OpenAI()
chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run("Hello")
This shows how to run two chains one after another to process text step-by-step.
LangChain
from langchain.chains import SimpleSequentialChain

chain1 = LLMChain(llm=llm, prompt=prompt1)
chain2 = LLMChain(llm=llm, prompt=prompt2)
sequential_chain = SimpleSequentialChain(chains=[chain1, chain2])
output = sequential_chain.run("Input text")
Sample Program

This program uses LangChain to create a simple app that says hello to a given name using a language model.

LangChain
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain

# Define a prompt template
prompt = PromptTemplate(template="Say hello to {name}.", input_variables=["name"])

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

# Create the chain
chain = LLMChain(llm=llm, prompt=prompt)

# Run the chain with input
result = chain.run("Alice")
print(result)
OutputSuccess
Important Notes

LangChain makes it easier to connect language models with other tools like databases, APIs, and user input.

It supports many language models, not just OpenAI.

Understanding how to create prompts and chains is key to using LangChain well.

Summary

LangChain helps build apps that use language models with ease.

It connects models to prompts and other tools in workflows called chains.

Great for chatbots, translators, and smart text apps.