LangChain is an artificial intelligence framework designed for programmers to develop applications using large language models. Let's delve into How to use Langchain?
Step 1: Setting
Before diving into LangChain, make sure you have a well-configured development environment. Install the necessary dependencies, including Python or JavaScript, depending on your preferences. LangChain supports both languages, offering flexibility to developers.
pip install langchain
conda install langchain -c conda-forge
Step 2: LLM
To use LangChain effectively, you will often need to integrate it with various components, such as model providers, data stores, and APIs. We will integrate LangChain with OpenAi model APIs. You can also do it using Hugging Face.
!pip install openai
import os
os.environ["OPENAI_API_KEY") ="YOUR_OPENAI_TOKEN"
from langchain.llms import OpenAI
llm = OpenAI(temperature=0.9)
text = "What would be a good company name for a company that makes candy floss?"
print(llm(text))
Step 3: LangChain Message Templates
LangChain message templates make it easy to create good messages for language models. This helps developers use LangChain seamlessly in their applications, making things efficient and consistent.
llm("Can India be economically stronger in future?")
prompt = """Question: Can India be economically stronger in future?
Let's think step by step.
Answer: """
llm(prompt)
from langchain import PromptTemplate
template = """Question: {question}
Let's think step by step.
Answer: """
prompt =PromptTemplate(template=template,input_variables=["question"))
prompt.format(question="Can India be economically stronger in future?")
llm(prompt)
Stage 4: Chains
In LangChain, using a single language model (LLM) is fine for simple tasks, but we need to link or chain multiple LLMs for more complex applications.
from langchain import LLMChain
llm_chain = LLMChain(prompt=prompt, llm=llm)
question = "Can India be economically stronger in future?"
print(llm_chain.run(question))
Step 5: Agents and tools
Agents are entities empowered to make decisions and perform actions using a language model (LLM). They operate by executing specific Tools, which are functions with different purposes, such as Google Search, Database searching or even other Chains and Agents. Tools are the pillars for agents to interact with the outside world effectively.
from langchain.agents import load_tools
from langchain.agents import initialize_agent
!pip install wikipedia
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
tools = load_tools(["wikipedia", "llm-math"), llm=llm)
agent = initialize_agent(tools, llm, agent="zero-shot-react-description", verbose=True)
agent.run("In what year was the film Chocolate factory released? What is this year raised to the 0.43 power?")
Step 6: Memory
Memory is like a way for these programs to remember things from one step to the next. It allows them to store and retrieve information between different calls or actions. LangChain makes this easy with a standard way of handling memory, offering several memory options to choose from.
from langchain import OpenAI, ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm, verbose=True)
conversation.predict(input="Hi there!")
conversation.predict(input="Can we talk about ai?")
conversation.predict(input="I'm interested in Deep Learning.")
Step 7: Document uploader
We use document loaders to load data from a source such as documents. These uploaders can capture data from a simple text file, the text of any web page, or even the transcription of a YouTube video.
from langchain.document_loaders import TextLoader
loader = TextLoader("./index.md")
loader.load()
Step 8: Indices
Indexes help you organize documents in a way that makes it easier for language models (LLMs) to understand and work with them effectively. This module provides useful tools for managing documents, including:
1. Scale: It is a numerical representation of information such as text, images, audio, documents, etc.
2. Text dividers: If we have long chunks of text, text dividers help break them down into smaller, more manageable chunks, making them easier for LLMs to handle.
3. Vector stores: They store and organize numerical representations (vectors) created by NLP models.
import requests
url = "https://raw.fricklles/state_of_the_union.txt"
res = requests.get(url)
with open("state_of_the_union.txt", "w") as f:
f.write(res.text)
# Document Loader
from langchain.document_loaders import TextLoader
loader = TextLoader('./state_of_the_union.txt')
documents = loader.load()
# Text Splitter
from langchain.text_splitter import CharacterTextSplitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)
!pip install sentence_transformers
# Embeddings
from langchain.embeddings import HuggingFaceEmbeddings
embeddings = HuggingFaceEmbeddings()
#text = "This is a test document."
#query_result = embeddings.embed_query(text)
#doc_result = embeddings.embed_documents([text))
!pip install faiss-cpu
# Vectorstore: https://python.langchain.com/en/latest/modules/indexes/vectorstores.html
from langchain.vectorstores import FAISS
db = FAISS.from_documents(docs, embeddings)
query = "What did the president say about Ketanji Brown Jackson"
docs = db.similarity_search(query)
print(docs[0).page_content)
The post How to use Langchain? The step-by-step guide first appeared on MarkTechPost.