Skip to content

Commit c113695

Browse files
authored
Add LangChain recipes (meta-llama#485)
2 parents 0e54f56 + 17e68a0 commit c113695

File tree

6 files changed

+2416
-0
lines changed

6 files changed

+2416
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# LangChain <> Llama3 Cookbooks
2+
3+
LLM agents use [planning, memory, and tools](https://lilianweng.github.io/posts/2023-06-23-agent/) to accomplish tasks.
4+
5+
LangChain offers several different ways to implement agents.
6+
7+
(1) Use [AgentExecutor](https://python.langchain.com/docs/modules/agents/quick_start/) with [tool-calling](https://python.langchain.com/docs/integrations/chat/) versions of Llama 3.
8+
9+
(2) Use [LangGraph](https://python.langchain.com/docs/langgraph), a library from LangChain that can be used to build reliable agents with Llama 3.
10+
11+
---
12+
13+
### AgentExecutor Agent
14+
15+
AgentExecutor is the runtime for an agent. AgentExecutor calls the agent, executes the actions it chooses, passes the action outputs back to the agent, and repeats.
16+
17+
Our first notebook, `tool-calling-agent`, shows how to build a [tool calling agent](https://python.langchain.com/docs/modules/agents/agent_types/tool_calling/) with AgentExecutor and Llama 3.
18+
19+
This shows how to build an agent that uses web search and retrieval tools.
20+
21+
---
22+
23+
### LangGraph Agent
24+
25+
[LangGraph](https://python.langchain.com/docs/langgraph) is a library from LangChain that can be used to build reliable agents.
26+
27+
LangGraph can be used to build agents with a few pieces:
28+
- **Planning:** Define a control flow of steps that you want the agent to take (a graph)
29+
- **Memory:** Persist information (graph state) across these steps
30+
- **Tool use:** Modify state at any step
31+
32+
Our second notebook, `langgraph-agent`, shows how to build a Llama 3 powered agent that uses web search and retrieval tool in LangGraph.
33+
34+
It discusses some of the trade-offs between AgentExecutor and LangGraph.
35+
36+
---
37+
38+
### LangGraph RAG Agent
39+
40+
Our third notebook, `langgraph-rag-agent`, shows how to apply LangGraph to build advanced Llama 3 powered RAG agents that use ideas from 3 papers:
41+
42+
* Corrective-RAG (CRAG) [paper](https://arxiv.org/pdf/2401.15884.pdf) uses self-grading on retrieved documents and web-search fallback if documents are not relevant.
43+
* Self-RAG [paper](https://arxiv.org/abs/2310.11511) adds self-grading on generations for hallucinations and for ability to answer the question.
44+
* Adaptive RAG [paper](https://arxiv.org/abs/2403.14403) routes queries between different RAG approaches based on their complexity.
45+
46+
We implement each approach as a control flow in LangGraph:
47+
- **Planning:** The sequence of RAG steps (e.g., retrieval, grading, and generation) that we want the agent to take
48+
- **Memory:** All the RAG-related information (input question, retrieved documents, etc) that we want to pass between steps
49+
- **Tool use:** All the tools needed for RAG (e.g., decide web search or vectorstore retrieval based on the question)
50+
51+
We will build from CRAG (blue, below) to Self-RAG (green) and finally to Adaptive RAG (red):
52+
53+
![Screenshot 2024-05-03 at 10 50 02 AM](https://github.com/rlancemartin/llama-recipes/assets/122662504/ec4aa1cd-3c7e-4cd1-a1e7-7deddc4033a8)
54+
55+
---
56+
57+
### Local LangGraph RAG Agent
58+
59+
Our fourth notebook, `langgraph-rag-agent-local`, shows how to apply LangGraph to build advanced RAG agents using Llama 3 that run locally and reliably.
60+
61+
See this [video overview](https://www.youtube.com/watch?v=sgnrL7yo1TE) for more detail.

recipes/use_cases/agents/langchain/langgraph-agent.ipynb

Lines changed: 698 additions & 0 deletions
Large diffs are not rendered by default.

recipes/use_cases/agents/langchain/langgraph-rag-agent-local.ipynb

Lines changed: 713 additions & 0 deletions
Large diffs are not rendered by default.

recipes/use_cases/agents/langchain/langgraph-rag-agent.ipynb

Lines changed: 643 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "9856250d-51bb-49d3-962d-483178cfafab",
6+
"metadata": {},
7+
"source": [
8+
"<a href=\"https://colab.research.google.com/github/meta-llama/llama-recipes/blob/main/recipes/use_cases/agents/langchain/tool-calling-agent.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": null,
14+
"id": "158a3f92-139a-4e92-9273-e018b027fc54",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"! pip install -U langchain_groq langchain langchain_community sentence-transformers tavily-python tiktoken langchainhub chromadb"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"id": "745f7d9f-15c4-41c8-94f8-09e1426581cc",
24+
"metadata": {},
25+
"source": [
26+
"# Tool calling agent with Llama 3\n",
27+
"\n",
28+
"[Tool calling](https://python.langchain.com/docs/modules/agents/agent_types/tool_calling/) allows an LLM to detect when one or more tools should be called.\n",
29+
"\n",
30+
"It will then respond with the inputs that should be passed to those tools. \n",
31+
"\n",
32+
"LangChain has a general agent that works with tool-calling LLMs.\n",
33+
"\n",
34+
"### Tools \n",
35+
"\n",
36+
"Let's define a few tools.\n",
37+
"\n",
38+
"`Retriever`"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": null,
44+
"id": "16569e69-890c-4e0c-947b-35ed2e0cf0c7",
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"import os\n",
49+
"\n",
50+
"os.environ['TAVILY_API_KEY'] = 'YOUR_TAVILY_API_KEY'\n",
51+
"os.environ['GROQ_API_KEY'] = 'YOUR_GROQ_API_KEY'"
52+
]
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": null,
57+
"id": "6debf81d-84d1-4645-aa25-07d24cdcbc2c",
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"from langchain_community.document_loaders import WebBaseLoader\n",
62+
"from langchain_community.vectorstores import Chroma\n",
63+
"from langchain_community.embeddings import HuggingFaceEmbeddings\n",
64+
"from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
65+
"\n",
66+
"loader = WebBaseLoader(\"https://docs.smith.langchain.com/overview\")\n",
67+
"docs = loader.load()\n",
68+
"documents = RecursiveCharacterTextSplitter(\n",
69+
" chunk_size=1000, chunk_overlap=200\n",
70+
").split_documents(docs)\n",
71+
"vector = Chroma.from_documents(documents, HuggingFaceEmbeddings())\n",
72+
"retriever = vector.as_retriever()\n",
73+
"\n",
74+
"from langchain.tools.retriever import create_retriever_tool\n",
75+
"retriever_tool = create_retriever_tool(\n",
76+
" retriever,\n",
77+
" \"langsmith_search\",\n",
78+
" \"Search for information about LangSmith. For any questions about LangSmith, you must use this tool!\",\n",
79+
")"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"id": "8454286f-f6d3-4ac0-a583-16315189d151",
85+
"metadata": {},
86+
"source": [
87+
"`Web search`"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"id": "8a4d9feb-80b7-4355-9cba-34e816400aa5",
94+
"metadata": {},
95+
"outputs": [],
96+
"source": [
97+
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
98+
"search = TavilySearchResults()"
99+
]
100+
},
101+
{
102+
"cell_type": "markdown",
103+
"id": "42215e7b-3170-4311-8438-5a7b385ebb64",
104+
"metadata": {},
105+
"source": [
106+
"`Custom`"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"id": "c130481d-dc6f-48e0-b795-7e3a4438fb6a",
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"from langchain.agents import tool\n",
117+
"\n",
118+
"@tool\n",
119+
"def magic_function(input: int) -> int:\n",
120+
" \"\"\"Applies a magic function to an input.\"\"\"\n",
121+
" return input + 2"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"id": "bfc0cfbe-d5ce-4c26-859f-5d29be121bef",
128+
"metadata": {},
129+
"outputs": [],
130+
"source": [
131+
"tools = [magic_function, search, retriever_tool] "
132+
]
133+
},
134+
{
135+
"cell_type": "markdown",
136+
"id": "e88c2e1d-1503-4659-be4d-98900a69253f",
137+
"metadata": {},
138+
"source": [
139+
"### LLM\n",
140+
"\n",
141+
"Here, we need a Llama 3 model that supports tool use.\n",
142+
"\n",
143+
"This can be accomplished via prompt engineering (e.g., see [here](https://replicate.com/hamelsmu/llama-3-70b-instruct-awq-with-tools)) or fine-tuning (e.g., see [here](https://huggingface.co/mzbac/llama-3-8B-Instruct-function-calling) and [here](https://huggingface.co/mzbac/llama-3-8B-Instruct-function-calling)).\n",
144+
"\n",
145+
"We can review LLMs that support tool calling [here](https://python.langchain.com/docs/integrations/chat/) and Groq is included.\n",
146+
"\n",
147+
"[Here](https://github.com/groq/groq-api-cookbook/blob/main/llama3-stock-market-function-calling/llama3-stock-market-function-calling.ipynb) is a notebook by Groq on function calling with Llama 3 and LangChain."
148+
]
149+
},
150+
{
151+
"cell_type": "code",
152+
"execution_count": null,
153+
"id": "99c919d2-198d-4c3b-85ba-0772bf7db383",
154+
"metadata": {},
155+
"outputs": [],
156+
"source": [
157+
"from langchain_groq import ChatGroq\n",
158+
"llm = ChatGroq(temperature=0, model=\"llama3-70b-8192\")"
159+
]
160+
},
161+
{
162+
"cell_type": "markdown",
163+
"id": "695ffb74-c278-4420-b10b-b18210d824eb",
164+
"metadata": {},
165+
"source": [
166+
"### Agent\n",
167+
"\n",
168+
"We use LangChain [tool calling agent](https://python.langchain.com/docs/modules/agents/agent_types/tool_calling/). "
169+
]
170+
},
171+
{
172+
"cell_type": "code",
173+
"execution_count": null,
174+
"id": "fae083a8-864c-4394-93e5-36d22aaa5fe3",
175+
"metadata": {},
176+
"outputs": [],
177+
"source": [
178+
"# Prompt \n",
179+
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
180+
"prompt = ChatPromptTemplate.from_messages(\n",
181+
" [\n",
182+
" (\"system\", \"You are a helpful assistant\"),\n",
183+
" (\"human\", \"{input}\"),\n",
184+
" MessagesPlaceholder(\"agent_scratchpad\"),\n",
185+
" ]\n",
186+
")\n",
187+
"prompt.pretty_print()"
188+
]
189+
},
190+
{
191+
"cell_type": "code",
192+
"execution_count": null,
193+
"id": "421f9565-bc1a-4141-aae7-c6bcae2c63fc",
194+
"metadata": {},
195+
"outputs": [],
196+
"source": [
197+
"### Run\n",
198+
"from langchain.agents import AgentExecutor, create_tool_calling_agent, tool\n",
199+
"agent = create_tool_calling_agent(llm, tools, prompt)\n",
200+
"agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)"
201+
]
202+
},
203+
{
204+
"cell_type": "code",
205+
"execution_count": null,
206+
"id": "229372f4-abb3-4418-9444-cadc548a8155",
207+
"metadata": {},
208+
"outputs": [],
209+
"source": [
210+
"agent_executor.invoke({\"input\": \"what is the value of magic_function(3)?\"})"
211+
]
212+
},
213+
{
214+
"cell_type": "markdown",
215+
"id": "016b447f-d374-4fc7-a1fe-0ce56856a763",
216+
"metadata": {},
217+
"source": [
218+
"Trace: \n",
219+
"\n",
220+
"https://smith.langchain.com/public/adf06494-94d6-4e93-98f3-60e65d2f2c19/r"
221+
]
222+
},
223+
{
224+
"cell_type": "code",
225+
"execution_count": null,
226+
"id": "6914b16c-be7a-4838-b080-b6af6b6e1417",
227+
"metadata": {},
228+
"outputs": [],
229+
"source": [
230+
"agent_executor.invoke({\"input\": \"whats the weather in sf?\"})"
231+
]
232+
},
233+
{
234+
"cell_type": "markdown",
235+
"id": "9e363535-29b8-45d8-85b6-10d2e21f93bc",
236+
"metadata": {},
237+
"source": [
238+
"Trace: https://smith.langchain.com/public/64a62781-7e3c-4acf-ae72-ce49ccb82960/r"
239+
]
240+
},
241+
{
242+
"cell_type": "code",
243+
"execution_count": null,
244+
"id": "8ce1b38c-a22a-4035-a9a1-2ea0da419ade",
245+
"metadata": {},
246+
"outputs": [],
247+
"source": [
248+
"agent_executor.invoke({\"input\": \"how can langsmith help with testing?\"})"
249+
]
250+
},
251+
{
252+
"cell_type": "markdown",
253+
"id": "e28cc79b-f6de-45fb-b7e5-84c119ba57da",
254+
"metadata": {},
255+
"source": [
256+
"This last question failed to run. \n",
257+
"\n",
258+
"Trace: https://smith.langchain.com/public/960a40e9-24f1-42a0-859d-2e0a30018d1c/r\n",
259+
"\n",
260+
"We can see that the agent correctly decides to query the vectorstore for a question about LangSmith. But it then inexplicably attempts web search. And it appears to get stuck in a loop of calling various tools before crashing.\n",
261+
"\n",
262+
"Of course, this is using a non-fine-tuned (only prompting) version of llama3 for tool-use. But, it illustates the reliability challenge with using Agent Executor. It is sensitive to the LLM's capacity for tool-use! \n",
263+
"\n",
264+
"In the next notebook, we will show an alternative way to implement this agent using LangGraph."
265+
]
266+
},
267+
{
268+
"cell_type": "code",
269+
"execution_count": null,
270+
"id": "55e7518c-e7d8-4ce7-9a4a-7909fb3a8b88",
271+
"metadata": {},
272+
"outputs": [],
273+
"source": []
274+
}
275+
],
276+
"metadata": {
277+
"kernelspec": {
278+
"display_name": "Python 3 (ipykernel)",
279+
"language": "python",
280+
"name": "python3"
281+
},
282+
"language_info": {
283+
"codemirror_mode": {
284+
"name": "ipython",
285+
"version": 3
286+
},
287+
"file_extension": ".py",
288+
"mimetype": "text/x-python",
289+
"name": "python",
290+
"nbconvert_exporter": "python",
291+
"pygments_lexer": "ipython3",
292+
"version": "3.11.9"
293+
}
294+
},
295+
"nbformat": 4,
296+
"nbformat_minor": 5
297+
}

scripts/spellcheck_conf/wordlist.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,3 +1310,7 @@ leaderboards
13101310
txn
13111311
ollama
13121312
tavily
1313+
AgentExecutor
1314+
LangGraph
1315+
langgraph
1316+
vectorstore

0 commit comments

Comments
 (0)