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.
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 ratings0% 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.
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....
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
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.
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..
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.
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
Python Advanced Programming: The Guide to Learn Python Programming. Reference with Exercises and Samples About Dynamical Programming, Multithreading, Multiprocessing, Debugging, Testing and More