0% found this document useful (0 votes)
30 views11 pages

LangChain_cheatsheet_1704475842

The document is a cheatsheet for LangChain, detailing its functionalities for working with large language models (LLMs) like OpenAI and HuggingFace. It covers prompt templates, chains, agents, memory management, document loaders, vector stores, and retrieval systems to enhance interactions with LLMs. Each section includes code snippets demonstrating how to implement these features effectively.

Uploaded by

danielsss294
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views11 pages

LangChain_cheatsheet_1704475842

The document is a cheatsheet for LangChain, detailing its functionalities for working with large language models (LLMs) like OpenAI and HuggingFace. It covers prompt templates, chains, agents, memory management, document loaders, vector stores, and retrieval systems to enhance interactions with LLMs. Each section includes code snippets demonstrating how to implement these features effectively.

Uploaded by

danielsss294
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

LANGCHAIN

CHEATSHEET

(c) Copyrights Reserved https://datasciencedojo.com


1 LARGE LANGUAGE
MODELS
An interface for OpenAI GPT-3.5-turbo LLM

from langchain.llms import OpenAI


llm = OpenAI(temperature=0.9)
text = "What do you know about Data Science Dojo ?"
llm(text)

>>> Data Science Dojo is one of the most popular


data science websites which focuses....

(c) Copyrights Reserved https://datasciencedojo.com


2 LARGE
LANGUAGE
MODELS
An interface for HugginFace LLM

from langchain import HuggingFaceHub


llm = HuggingFaceHub(repo_id="togethercomputer/LLaMA-
2-7B-32K", model_kwargs=
{"temperature":0,"max_length":64})
llm("How old is Data Science Dojo?")

>>> Data Science Dojo was founded in 2013, making


it 10 years old.

(c) Copyrights Reserved https://datasciencedojo.com


3 PROMPT
TEMPLATES
LangChain facilitates prompt management
and optimization by using prompt templates.

from langchain import PromptTemplate


template = """Question: {question}
Make the answer more engaging by incorporating puns.
Answer: """
prompt = PromptTemplate.from_template(template)
llm(prompt.format(question="Could you provide some
information on the impact of global warming?"))

>>> Global warming is no laughing matter, but that doesn’t....

(c) Copyrights Reserved https://datasciencedojo.com


4 CHAINS
Combining LLMs and prompt template can
enhance multi-step workflows.

from langchain import PromptTemplate


template = """Question: {question}
Make the answer more engaging by incorporating puns.
Answer: """
prompt = PromptTemplate.from_template(template)
llm(prompt.format(question="Could you provide some
information on the impact of global warming?"))

>>> Global warming is no laughing matter—but it sure


is..

(c) Copyrights Reserved https://datasciencedojo.com


5 AGENTS AND
TOOLS
Agent = The language model that drives decision
making
Tools = A capability of an agent

from langchain.agents import load_tools


from langchain.agents import initialize_agent
tools=load_tools(["wikipedia", "llm-math"], llm=llm)
agent=initialize_agent(tools,llm,agent="zero-shot-
react-description", verbose=True)
agent.run("Can you tell the distance between Earth and
the moon? And convert it into miles?")

>>> Action: Wikipedia


Action Input: Earth-moon distance
Action: Calculator
Action Input: 385400/1.609
Final Answer: The distance between Earth and the Moon is approximately
239,527.66 miles.

(c) Copyrights Reserved https://datasciencedojo.com


6 MEMORY
LangChain simplifies persistent state management
in chain or agent calls with a standard interface

from langchain.chains import ConversationChain


from langchain.memory importConversationBufferMemory
conversation = ConversationChain(
llm=llm, verbose=True,memory=ConversationBufferMemory()
conversation.predict(input="How can one overcomeanxiety?")
To overcome anxiety, it may be helpful to focus on the....
conversation.predict(input="Tell me more..")

>>> To overcome anxiety, it may be helpful to focus on the...


conversation.predict(input="Tell me more..")
>>> To be mindful of the present, it can be helpful to pra..

(c) Copyrights Reserved https://datasciencedojo.com


7 DOCUMENT
LOADERS
By combining language models with your own text
data, you can answer personalized queries. You can
load CSV,Markdown, PDF, and more.

from langchain.document_loaders import TextLoader


raw_document
=TextLoader("/work/data/Gregory.txt").load()

(c) Copyrights Reserved https://datasciencedojo.com


8 VECTOR STORES
One common method for storing and searching
unstructured data is to embed it as vectors, then embed
queries and retrieve the most similar vectors.

from langchain.embeddings.openai importOpenAIEmbeddings


from langchain.text_splitter import CharacterTextSplitter
from langchain.vectorstores import FAISS
# Text Splitter
text_splitter = CharacterTextSplitter(chunk_size=1000,chunk_overlap=0)
documents = text_splitter.split_documents(raw_document)
# Vector Store
db = FAISS.from_documents(documents,OpenAIEmbeddings())
# Similarity Search
query = "When was Gregory born?"
docs = db.similarity_search(query)
print(docs[0].page_content)

>>> Gregory I. Piatetsky-Shapiro (born 7 April 1958) is a data scientist


and the co-founder of the KDD conferences....

(c) Copyrights Reserved https://datasciencedojo.com


9 RETRIEVER
It is an easy way to combine documents with
language models

from langchain.chains import RetrievalQA


from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI(model_name="gpt-3.5-turbo",temperature=0)
qa_chain =RetrievalQA.from_chain_type(llm,retriever=db.as_retriever())
qa_chain({"query": "When was Gregory born?"})

>>> {'query': 'When was Gregory born?',


'result': 'Gregory Piatetsky-Shapiro was born on
April 7,1958.'}

(c) Copyrights Reserved https://datasciencedojo.com


Follow us to learn more about AI, Data Science,
and Large Language Models

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy