Home Artificial Intelligence What’s LangChain ? Prompt Templates : Chains: Agents & Tools : Memory: Document Loader : Indexes:

What’s LangChain ? Prompt Templates : Chains: Agents & Tools : Memory: Document Loader : Indexes:

1
What’s LangChain ?
Prompt Templates :
Chains:
Agents & Tools :
Memory:
Document Loader :
Indexes:

A framework for developing applications powered by language models.

The right way to install and arrange an environment for LangChain?

Install Langchain.

!pip install langchain

Consider LangChain as a bridge that makes LLMs accessible for developers. So LangChain will normally require integrations with a number of model providers, data stores, API’s, etc.
Establishing an environment for the provider is obligatory, and there is a wide selection of providers to pick from.

Screenshot captured by writer

So all of those require an environment to be set before using their model. So let’s arrange one, pip install any LLM from available in LangChain. Then go ahead and create an environment.

pip install openai
import os
os.environ["OPENAI_API_KEY"] = "hshhhtotogjgjkhk"

Language models normally take text as inputs , these text are known as prompts . The prompt templates in Langchain are built to make constructing prompts with dynamic inputs easier.

from langchain import PromptTemplate
template = """ hdhfkfgdvjkdkdld
sdhjdllgllhllkhkhk #pass prompt of your chosing here .
llsldpppeppptoyoouououp {uio}"""
prompt = PromptTemplate(input_variables=["uio"],template=template)

That is one in every of the important thing feature of LangChain — That enables to chain multiple LLMs and Prompts together .

This ability to chain multiple components right into a single entity could turn out to be useful when we will create a series that takes user input, formats it with a PromptTemplate, after which passes the formatted response to an LLM.
So there’s different chains available: Easy LLM Chains, Index-related Chains, and may make Custom Chains.

from langchain import LLMChain
llmchain=LLMChain(prompt=prompt,llm=llm)
query ="-----------" #pass a matter of your chosing here .

print(llmchain.run(query))

There are different chains that will be used for specific scenarios inside chains :

Captured by writer .

Agents use an LLM to find out which actions to take and in what order. An motion can either be using a tool and observing its output, or returning to the user.

In-order to attain this consequence, we now have to distinguish between the tools and LLMs which are getting used for the duty to the agents. So, is a function that performs a particular duty. This will be things like: Google Search, Database lookup, Python REPL, Wikipedia, ChatGPTPlugin, and other chains. is the language model powering the agent.

Agents have 4 differing types as of now :
zero-shot-react-description , react-docstore ,self-ask-with-search , conversational-react-description .

from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI

llm = OpenAI(temperature=0)
tools = load_tools(["wikipedia", "llm-math"], llm=llm) #!pip install wikipedia for this
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)

agent.run("--------------") #put the query in here .

By default chains and agents are stateless i.e., they treat every incoming query / input prompt independently. For instance like chatbots, which don’t have the aptitude of remember anything.

It is very essential to recollect previous interactions, each at a brief term but additionally at a protracted term level. The concept of “Memory” exists to do exactly that.

Here every time whenever you give an input on the the model will reply with relevant info. And when a unique input is given to the model will are likely to remember the previous input and make answers considering the relation with the previous input as well.

from langchain.llms import OpenAI
from langchain.chains import ConversationChain
llm = OpenAI(temperature=0)
conversation = ConversationChain(llm=llm,verbose=True)
conversation.predict(input="-----------")

Combining language models along with your own text data is a robust strategy to differentiate them. Step one in doing that is to load the info into “Documents” — a elaborate way of say some pieces of text. The document loader is geared toward making this easy.

Principally you’ll be able to upload your text documents and make the model work on it as whether it is an given input.

These are widely used but additionally they support cloud services Notions, Obsidian, GCP, AWS, Git, and Figma as Proprietary dataset.

from langchain.document_loaders.csv_loader import CSVLoader
loader = CSVLoader(file_path='---------.csv')

data = loader.load()

For the Document Loaders to work efficiently they need indexes as Indexes structure the files higher for the models to be interactive.

Indexes consult with ways to structure documents in order that LLMs can best interact with them. This module incorporates utility functions for working with documents.

There are 3 things to know to make this process :
— Is a numerical representation of the file/document/audio used .
— When coping with large files, splitting them up into smaller chuks is obligatory.
— These store and index vector embeddings from the NLP model used.

Get the document loader to load a input file, then split the loaded file using text splitter.

from langchain.text_splitter import CharacterTextSplitter
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
texts = text_splitter.split_documents(documents)

Then setup the embeddings, sometimes they may require to put in third party packages so do those. Here installed sentence_transformer for this task .

from langchain.embeddings import HuggingFaceEmbeddings 
embeddings = HuggingFaceEmbeddings()

Then finally setup a vector store, there are quite a bit to pick from the lot available services in long chain.
Ask any query related to the uploaded document / File and let the model search info for you .

from langchain.vectorstores import FAISS
db = FAISS.from_documents(texts, embeddings)

query = "--------------"
qa.run(query)
qa.similarity_seacrh(query) # finds similiar content because the query from the doc .

That is LangChain and its essential features.

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here