LangChain helps you build smart apps that use language models easily. It organizes parts so you can connect and control them well.
0
0
LangChain architecture overview
Introduction
When you want to create a chatbot that can remember past talks.
When you need to combine language models with other tools like search or databases.
When building apps that process and understand text step-by-step.
When you want to manage complex language tasks by breaking them into smaller parts.
When you want to reuse and connect different language-related components smoothly.
Syntax
LangChain
from langchain import LLMChain, PromptTemplate, OpenAI # Create a prompt template prompt = PromptTemplate(input_variables=["topic"], template="Write a short story about {topic}.") # Create a language model instance llm = OpenAI(temperature=0.7) # Combine prompt and model into a chain story_chain = LLMChain(llm=llm, prompt=prompt) # Run the chain result = story_chain.run({"topic": "a friendly robot"}) print(result)
LangChain uses chains to connect prompts and models.
Each part has a clear role: prompt templates create questions, LLMs answer, chains link them.
Examples
This example makes the model say hello to a given name.
LangChain
from langchain import LLMChain, PromptTemplate, OpenAI prompt = PromptTemplate(input_variables=["name"], template="Say hello to {name}.") llm = OpenAI(temperature=0) chain = LLMChain(llm=llm, prompt=prompt) print(chain.run({"name": "Alice"}))
This example shows a simple conversation that remembers what you said before.
LangChain
from langchain import ConversationChain, OpenAI llm = OpenAI(temperature=0) conversation = ConversationChain(llm=llm) print(conversation.predict(input="Hi!"))
Sample Program
This program asks the language model to share a fun fact about an animal you choose. It shows how LangChain connects prompts and models simply.
LangChain
from langchain import LLMChain, PromptTemplate, OpenAI # Define a prompt template with a variable prompt = PromptTemplate(input_variables=["animal"], template="Describe a fun fact about a {animal}.") # Create an OpenAI language model instance llm = OpenAI(temperature=0) # Create a chain that links the prompt and the model fact_chain = LLMChain(llm=llm, prompt=prompt) # Run the chain with input output = fact_chain.run({"animal": "dolphin"}) print(output)
OutputSuccess
Important Notes
LangChain breaks down language tasks into parts you can connect and reuse.
Chains can be simple or complex, depending on your app needs.
Using prompt templates helps keep your questions clear and easy to change.
Summary
LangChain organizes language model apps into parts called chains.
Prompt templates create questions, LLMs answer, and chains link them.
This makes building smart language apps easier and clearer.