The Ultimate AI & LangChain Cheatsheet
The Ultimate AI & LangChain Cheatsheet
Building AI-powered applications is one of the fastest-growing niches in software development. This cheatsheet covers the foundational concepts and code snippets you need to build Large Language Model (LLM) applications, particularly focusing on the LangChain ecosystem.
LangChain Basics
LangChain is a framework designed to simplify the creation of applications using LLMs.
from langchain.llms import OpenAI
from langchain.prompts import PromptTemplate
from langchain.chains import LLMChain
# 1. Initialize the LLM (Requires OPENAI_API_KEY environment variable)
llm = OpenAI(temperature=0.7)
# 2. Create a Prompt Template
prompt = PromptTemplate(
input_variables=["topic"],
template="Write a short, engaging tweet about {topic}."
)
# 3. Create a Chain connecting the LLM and the Prompt
chain = LLMChain(llm=llm, prompt=prompt)
# 4. Run the Chain
result = chain.run("Artificial Intelligence")
print(result)OpenAI API Guide
How to directly interact with OpenAI's API using the official Python client.
import openai
import os
openai.api_key = os.getenv("OPENAI_API_KEY")
# Chat Completion (Standard GPT-4/GPT-3.5 interaction)
response = openai.ChatCompletion.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Explain binary search in one sentence."}
]
)
print(response.choices[0].message['content'])Prompt Engineering
The art of communicating effectively with LLMs.
- Be Specific: Instead of "Write a blog post", use "Write a 500-word blog post about React Hooks aimed at beginners."
- Provide Context: "You are an expert Python developer. Given the following code snippet..."
- Few-Shot Prompting: Provide examples within the prompt.
Translate English to French: sea otter => loutre de mer peppermint => menthe poivrée cheese => - Chain of Thought: Encourage the model to reason. "Let's think step by step."
Embeddings Explained
Embeddings are numerical representations (vectors) of text. They capture semantic meaning, allowing computers to understand that "King" and "Queen" are related concepts.
from langchain.embeddings import OpenAIEmbeddings
embeddings = OpenAIEmbeddings()
# Convert text into a vector (a list of floats)
vector = embeddings.embed_query("What are embeddings in AI?")
# The resulting vector can be stored and compared against other vectors
print(len(vector)) # e.g., 1536 dimensions for OpenAI's standard modelVector Databases
Vector databases store and query embeddings efficiently, usually via similarity search (e.g., Cosine Similarity).
Popular Vector Databases:
- Pinecone (Managed SaaS)
- ChromaDB (Open-source, local)
- Qdrant (High-performance, Rust-based)
- pgvector (PostgreSQL extension)
Basic ChromaDB Example with LangChain:
from langchain.vectorstores import Chroma
from langchain.embeddings import OpenAIEmbeddings
from langchain.schema import Document
embeddings = OpenAIEmbeddings()
docs = [
Document(page_content="The sky is blue."),
Document(page_content="Grass is green.")
]
# Create a local vector store
db = Chroma.from_documents(docs, embeddings)
# Perform a similarity search
query = "What color is the sky?"
results = db.similarity_search(query, k=1)
print(results[0].page_content) # Output: "The sky is blue."RAG Architecture
Retrieval-Augmented Generation (RAG) is a pattern that grounds an LLM's response in external data, preventing hallucinations.
The Workflow:
- Load Data: Extract text from PDFs, websites, or databases.
- Chunking: Split the text into smaller, manageable chunks.
- Embed: Convert chunks into vector embeddings.
- Store: Save embeddings in a Vector Database.
- Retrieve: When a user asks a question, embed the query and search the Vector DB for the most similar chunks.
- Generate: Send the retrieved chunks + the user's question to the LLM to formulate a final answer.
# Simplified LangChain RAG Example
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
# Assuming 'db' is your populated Chroma instance from the previous section
retriever = db.as_retriever()
qa_chain = RetrievalQA.from_chain_type(llm=OpenAI(), retriever=retriever)
answer = qa_chain.run("What color is the sky based on the document?")AI Agent Workflow
Agents use LLMs as reasoning engines to determine which actions to take and in what order, using provided tools.
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
# Load tools the agent can use (e.g., a calculator and a Wikipedia searcher)
tools = load_tools(["wikipedia", "llm-math"], llm=llm)
# Initialize the agent
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True
)
# The agent decides which tool to use based on the prompt
agent.run("What is the square root of the birth year of Albert Einstein?")Read Next
The Ultimate AI & LangChain Cheatsheet
A comprehensive guide to AI development with LangChain and OpenAI. Master prompt engineering, RAG, agents, embeddings, and vector databases.
The Ultimate Deployment Cheatsheet
A comprehensive guide to deploying web apps. Master Vercel, VPS setup, Nginx, environment variables, domain configuration, and SSL certificates.