Introduction
LangChain helps you build smart apps that use language models easily. It connects language models with other tools and data.
Jump into concepts and practice - no test required
LangChain helps you build smart apps that use language models easily. It connects language models with other tools and data.
from langchain.chains import LLMChain chain = LLMChain(llm=your_llm, prompt=your_prompt) result = chain.run(input_text)
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")
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")
This program uses LangChain to create a simple app that says hello to a given name using a language model.
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)
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.
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.
from langchain import PromptTemplate, LLMChain, OpenAI
prompt = PromptTemplate(template="Translate '{text}' to French.", input_variables=["text"])
llm = OpenAI(temperature=0)
chain = LLMChain(llm=llm, prompt=prompt)
result = chain.run(text="Hello")
print(result)from langchain import PromptTemplate, LLMChain
prompt = PromptTemplate(template="Say hello to {name}.", input_variables=["name"])
chain = LLMChain(prompt=prompt)
result = chain.run(name="Alice")
print(result)