Skip to content

Commit 76cb603

Browse files
authored
Merge branch 'main' into tmoreau89/android
2 parents 1bf33ef + c82bedd commit 76cb603

File tree

5 files changed

+949
-118
lines changed

5 files changed

+949
-118
lines changed
Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,70 @@
11
# LangChain <> Llama3 Cookbooks
22

3-
LLM agents use [planning, memory, and tools](https://lilianweng.github.io/posts/2023-06-23-agent/) to accomplish tasks.
3+
LLM agents use [planning, memory, and tools](https://lilianweng.github.io/posts/2023-06-23-agent/) to accomplish tasks. Agents can empower Llama 3 with important new capabilities. Here, we will show how to give Llama 3 the ability to perform web search, as well as multi-modality: image generation (text-to-image), image analysis (image-to-text), and voice (text-to-speech) tools!
44

5-
LangChain offers several different ways to implement agents.
5+
LangChain offers several different ways to implement agents with Llama 3:
66

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.
7+
(1) `ReAct agent` - Uses [AgentExecutor](https://python.langchain.com/docs/modules/agents/quick_start/) with [tool-calling](https://python.langchain.com/docs/integrations/chat/) versions of Llama 3.
88

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.
9+
(2) `LangGraph tool calling agent` - Uses [LangGraph](https://python.langchain.com/docs/langgraph) with [tool-calling](https://python.langchain.com/docs/integrations/chat/) versions of Llama 3.
10+
11+
(3) `LangGraph custom agent` - Uses [LangGraph](https://python.langchain.com/docs/langgraph) with **any** version of Llama 3 (so long as it supports structured output).
12+
13+
As we move from option (1) to (3) the degree of customization and flexibility increases:
14+
15+
(1) `ReAct agent` using AgentExecutor is a great for getting started quickly with minimal code, but requires a version of Llama 3 with reliable tool-calling, is the least customizable, and uses higher-level AgentExecutor abstraction.
16+
17+
(2) `LangGraph tool calling agent` is more customizable than (1) because the LLM assistant (planning) and tool call (action) nodes are defined by the user, but it still requires a version of Llama 3 with reliable tool-calling.
18+
19+
(3) `LangGraph custom agent` does not require a version of Llama 3 with reliable tool-calling and is the most customizable, but requires the most work to implement.
20+
21+
![langgraph_agent_architectures](https://github.com/rlancemartin/llama-recipes/assets/122662504/5ed2bef0-ae11-4efa-9e88-ab560a4d0022)
1022

1123
---
1224

13-
### AgentExecutor Agent
25+
### `ReAct agent`
1426

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.
27+
The AgentExecutor manages the loop of planning, executing tool calls, and processing outputs until an AgentFinish signal is generated, indicating task completion.
1628

1729
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.
1830

19-
This shows how to build an agent that uses web search, text2image, image2text, and text2speech tools.
20-
2131
---
2232

23-
### LangGraph Agent
33+
### `LangGraph tool calling agent`
2434

2535
[LangGraph](https://python.langchain.com/docs/langgraph) is a library from LangChain that can be used to build reliable agents.
2636

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
37+
Our second notebook, `langgraph-tool-calling-agent`, shows an alternative to AgentExecutor for building a Llama 3 powered agent.
38+
39+
---
3140

32-
Our second notebook, `langgraph-agent`, shows an alternative way to AgentExecutor to build a Llama 3 powered agent in LangGraph.
41+
### `LangGraph custom agent`
3342

34-
It discusses some of the trade-offs between AgentExecutor and LangGraph.
43+
Our third notebook, `langgraph-custom-agent`, shows how to build a Llama 3 powered agent without reliance on tool-calling.
3544

3645
---
3746

38-
### LangGraph RAG Agent
47+
### `LangGraph RAG Agent`
3948

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:
49+
Our fourth notebook, `langgraph-rag-agent`, shows how to apply LangGraph to build a custom Llama 3 powered RAG agent that use ideas from 3 papers:
4150

4251
* 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.
4352
* Self-RAG [paper](https://arxiv.org/abs/2310.11511) adds self-grading on generations for hallucinations and for ability to answer the question.
4453
* Adaptive RAG [paper](https://arxiv.org/abs/2403.14403) routes queries between different RAG approaches based on their complexity.
4554

4655
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)
56+
- **Planning:** The sequence of RAG steps (e.g., retrieval, grading, and generation) that we want the agent to take.
57+
- **Memory:** All the RAG-related information (input question, retrieved documents, etc) that we want to pass between steps.
58+
- **Tool use:** All the tools needed for RAG (e.g., decide web search or vectorstore retrieval based on the question).
5059

5160
We will build from CRAG (blue, below) to Self-RAG (green) and finally to Adaptive RAG (red):
5261

53-
![Screenshot 2024-05-03 at 10 50 02 AM](https://github.com/rlancemartin/llama-recipes/assets/122662504/ec4aa1cd-3c7e-4cd1-a1e7-7deddc4033a8)
62+
![langgraph_rag_agent_](https://github.com/rlancemartin/llama-recipes/assets/122662504/ec4aa1cd-3c7e-4cd1-a1e7-7deddc4033a8)
5463

5564
---
65+
66+
### `Local LangGraph RAG Agent`
5667

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.
68+
Our fifth notebook, `langgraph-rag-agent-local`, shows how to apply LangGraph to build advanced RAG agents using Llama 3 that run locally and reliably.
6069

61-
See this [video overview](https://www.youtube.com/watch?v=sgnrL7yo1TE) for more detail.
70+
See this [video overview](https://www.youtube.com/watch?v=sgnrL7yo1TE) for more detail on the design of this agent.

recipes/use_cases/agents/langchain/langgraph-agent.ipynb renamed to recipes/use_cases/agents/langchain/langgraph-custom-agent.ipynb

Lines changed: 54 additions & 43 deletions
Large diffs are not rendered by default.

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

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

recipes/use_cases/agents/langchain/tool-calling-agent.ipynb

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,6 @@
1818
"! pip install -U langchain_groq langchain tavily-python replicate"
1919
]
2020
},
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. In this notebook we'll use the following tools with Agent Executor:\n",
33-
"\n"
34-
]
35-
},
3621
{
3722
"attachments": {
3823
"6af2e76d-6f3d-44c9-a128-c19e70a400e9.png": {
@@ -43,6 +28,14 @@
4328
"id": "3fcd455a-6255-4c7c-9e8e-2aaf1b323c05",
4429
"metadata": {},
4530
"source": [
31+
"# Tool calling agent with Llama 3\n",
32+
"\n",
33+
"LLM-powered agents combine planning, memory, and tool-use (see [here](https://lilianweng.github.io/posts/2023-06-23-agent/), [here](https://www.deeplearning.ai/the-batch/how-agents-can-improve-llm-performance/)).\n",
34+
"\n",
35+
"LangChain's [agent executor](https://python.langchain.com/docs/modules/agents/agent_types/tool_calling/) offers a simple way to quickly get started with agents.\n",
36+
"\n",
37+
"Here, we will show how to augment a tool-calling version of Llama 3 with various multi-modal capabilities using an agent. \n",
38+
"\n",
4639
"![image.png](attachment:6af2e76d-6f3d-44c9-a128-c19e70a400e9.png)"
4740
]
4841
},
@@ -51,11 +44,11 @@
5144
"id": "8baac811-22b0-4bcb-a7d4-6eb87a1b61d3",
5245
"metadata": {},
5346
"source": [
54-
"## Tools\n",
47+
"## Introduce the tools that we want our agent to use\n",
5548
"\n",
5649
"### 1. Custom Function\n",
5750
"\n",
58-
"We'll define a few tools that our agent will use."
51+
"This is a custom function that we want our agent to utilize."
5952
]
6053
},
6154
{
@@ -85,8 +78,8 @@
8578
},
8679
{
8780
"cell_type": "code",
88-
"execution_count": 2,
89-
"id": "07dc6530-abb0-4bda-a071-1163239ab2c6",
81+
"execution_count": null,
82+
"id": "75c46af4-f03e-4448-a49e-8bfe0e644c2a",
9083
"metadata": {},
9184
"outputs": [],
9285
"source": [
@@ -105,23 +98,15 @@
10598
"source": [
10699
"### 2. Web Search\n",
107100
"\n",
108-
"Let's use [Tavily](https://tavily.com/#api) for web search."
101+
"We'll use [Tavily](https://tavily.com/#api) for web search."
109102
]
110103
},
111104
{
112105
"cell_type": "code",
113-
"execution_count": 3,
114-
"id": "16569e69-890c-4e0c-947b-35ed2e0cf0c7",
106+
"execution_count": null,
107+
"id": "4c6be142-757c-4ee4-983e-0b5a54fafff9",
115108
"metadata": {},
116-
"outputs": [
117-
{
118-
"name": "stdin",
119-
"output_type": "stream",
120-
"text": [
121-
" ········\n"
122-
]
123-
}
124-
],
109+
"outputs": [],
125110
"source": [
126111
"# Ensure API key is set\n",
127112
"import os\n",
@@ -148,25 +133,17 @@
148133
"source": [
149134
"### 3. Text-2-Image\n",
150135
"\n",
151-
"We'll use Replicate, which [hosts an open DALL-E model](https://replicate.com/lucataco/open-dalle-v1.1/versions/1c7d4c8dec39c7306df7794b28419078cb9d18b9213ab1c21fdc46a1deca0144).\n",
136+
"We'll use [Replicate](https://replicate.com/), which offers free to try API key and hosts an [open DALL-E model](https://replicate.com/lucataco/open-dalle-v1.1/versions/1c7d4c8dec39c7306df7794b28419078cb9d18b9213ab1c21fdc46a1deca0144).\n",
152137
"\n",
153-
"Test the code before converting it to a tool (this may take 1-2 minutes to run):"
138+
"Test the code (this may take 1-2 minutes to run):"
154139
]
155140
},
156141
{
157142
"cell_type": "code",
158-
"execution_count": 5,
159-
"id": "c130481d-dc6f-48e0-b795-7e3a4438fb6a",
143+
"execution_count": null,
144+
"id": "2fb8879e-24e2-4370-b89e-10644ce1ddfc",
160145
"metadata": {},
161-
"outputs": [
162-
{
163-
"name": "stdin",
164-
"output_type": "stream",
165-
"text": [
166-
" ········\n"
167-
]
168-
}
169-
],
146+
"outputs": [],
170147
"source": [
171148
"# Ensure API key is set\n",
172149
"REPLICATE_API_TOKEN = getpass()\n",
@@ -282,7 +259,7 @@
282259
"source": [
283260
"### 4. Image-2-Text\n",
284261
"\n",
285-
"We'll use Replicate, which [hosts llava-13b](https://replicate.com/yorickvp/llava-13b).\n",
262+
"We'll use Replicate, which hosts [llava-13b](https://replicate.com/yorickvp/llava-13b).\n",
286263
"\n",
287264
"Test the code before converting it to a tool:"
288265
]
@@ -363,7 +340,7 @@
363340
"source": [
364341
"### 5. Text-2-Speech\n",
365342
"\n",
366-
"We'll use Replicate, which [hosts text-2-speech](https://replicate.com/cjwbw/seamless_communication).\n",
343+
"We'll use Replicate, which hosts [text-2-speech](https://replicate.com/cjwbw/seamless_communication).\n",
367344
"\n",
368345
"Test the code before creating yet another custom tool (this may take a couple of minutes to run):"
369346
]
@@ -498,7 +475,7 @@
498475
"\n",
499476
"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",
500477
"\n",
501-
"We can review LLMs that support tool calling [here](https://python.langchain.com/docs/integrations/chat/) and Groq is included.\n",
478+
"We can review LangChain LLM integrations that support tool calling [here](https://python.langchain.com/docs/integrations/chat/) and Groq is included.\n",
502479
"\n",
503480
"[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."
504481
]
@@ -836,7 +813,7 @@
836813
"source": [
837814
"We can see that the agent correctly decides which tool to call for different query using AgentExecutor. \n",
838815
"\n",
839-
"In the next [notebook](langgraph-agent.ipynb), we will show an alternative way to implement this agent using LangGraph."
816+
"In the next [notebook](langgraph-tool-calling-agent.ipynb), we will show an alternative way to implement this agent using LangGraph."
840817
]
841818
}
842819
],
@@ -856,7 +833,7 @@
856833
"name": "python",
857834
"nbconvert_exporter": "python",
858835
"pygments_lexer": "ipython3",
859-
"version": "3.11.9"
836+
"version": "3.10.14"
860837
}
861838
},
862839
"nbformat": 4,

scripts/spellcheck_conf/wordlist.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1342,9 +1342,12 @@ sha
13421342
tmoreau
13431343
toolchain
13441344
wifi
1345+
AgentFinish
1346+
ReAct
1347+
customizable
13451348
Kaggle
13461349
SalesBot
13471350
Weaviate
13481351
MediaGen
13491352
SDXL
1350-
SVD
1353+
SVD

0 commit comments

Comments
 (0)