1 00:00:00,830 --> 00:00:07,380 In this video, we ll discuss React, which is short from reasoning and acting. 2 00:00:08,650 --> 00:00:11,680 We will also create and use a React agent. 3 00:00:12,410 --> 00:00:15,300 React is a new approach that combines 4 00:00:15,310 --> 00:00:19,260 reasoning and acting capabilities of LLMs. 5 00:00:19,770 --> 00:00:24,260 It does this by allowing LLMs to generate 6 00:00:24,270 --> 00:00:29,740 reasoning traces and task -specific actions in an interleaved manner. 7 00:00:30,590 --> 00:00:36,820 This means that the model can reason about a task and then take actions in the 8 00:00:36,830 --> 00:00:40,640 real world to gather more information or complete the task. 9 00:00:42,490 --> 00:00:49,140 This combination of reasoning and acting allows React to overcome some of the 10 00:00:49,150 --> 00:00:54,860 limitations of existing LLMs, such as hallucinations and error propagations. 11 00:00:56,010 --> 00:01:01,560 Lang chain agents are one of the key components that make Lang chain so useful. 12 00:01:02,030 --> 00:01:07,940 They take Lang chain tools, which we ve looked at in some of the other videos, 13 00:01:08,490 --> 00:01:11,800 and chains and combine them into one agent. 14 00:01:13,170 --> 00:01:15,880 We will create and use a React agent. 15 00:01:17,490 --> 00:01:23,980 The big problem when using only tools or chains is that you probably won t want to 16 00:01:23,990 --> 00:01:27,080 use the same tool with every query you send. 17 00:01:27,630 --> 00:01:30,500 In our previous examples, we used as 18 00:01:30,510 --> 00:01:35,340 tools Wikipedia and DuckDuckGo, but not both. 19 00:01:36,250 --> 00:01:39,540 Using a React agent, it will check the 20 00:01:39,550 --> 00:01:45,920 input and then decide what is the best tool to use based on the user s query. 21 00:01:46,410 --> 00:01:47,320 Let s get started. 22 00:01:48,270 --> 00:01:50,900 I will install the LangChainHub package. 23 00:01:51,990 --> 00:01:55,660 pip install LangChainHub -q. 24 00:01:56,290 --> 00:02:00,040 The LangChainHub is a hub for sharing and 25 00:02:00,050 --> 00:02:03,200 discovering resources related to Lang chain. 26 00:02:03,790 --> 00:02:07,840 It stores and allows access to pre 27 00:02:07,850 --> 00:02:12,980 -trained prompts, chains, and agents that can be used in Lang chain projects. 28 00:02:13,890 --> 00:02:18,540 I am loading the OpenAI API key from .env. 29 00:02:21,170 --> 00:02:24,740 Next, let s import the modules for 30 00:02:24,750 --> 00:02:28,640 prompts, agents, tools, and external services. 31 00:02:29,310 --> 00:02:31,260 I am pasting the imports. 32 00:02:33,620 --> 00:02:35,530 I am setting up the LLM. 33 00:02:36,300 --> 00:02:39,230 LLM equals chat -open -ai -of. 34 00:02:40,120 --> 00:02:46,310 And I ll choose gpt4 -turbo, a powerful language model for processing text and 35 00:02:46,320 --> 00:02:47,370 generating responses. 36 00:02:48,340 --> 00:02:50,930 If you don t have access to gpt4 -turbo, 37 00:02:51,360 --> 00:02:53,630 use gpt3 .5 -turbo instead. 38 00:02:56,010 --> 00:02:59,020 Model name equals gpt4 -turbo -preview. 39 00:03:02,710 --> 00:03:06,450 And the temperature equals 0. 40 00:03:07,940 --> 00:03:11,510 Once again, if you are using a free plan 41 00:03:11,520 --> 00:03:18,090 and don t have access to gpt4 -turbo, use gpt3 .5 -turbo instead. 42 00:03:21,090 --> 00:03:26,460 In the next step, I will prepare the prompt template for asking questions. 43 00:03:27,670 --> 00:03:33,220 This template will structure the way the questions are presented to the agent. 44 00:03:34,690 --> 00:03:44,070 I will define a multi -line string, answer the following questions as best 45 00:03:44,080 --> 00:03:54,740 you can, and the questions, colon, and the Q in curly braces. 46 00:03:57,910 --> 00:04:08,120 I am converting the template into a prompt template equals the prompt 47 00:04:08,130 --> 00:04:14,480 -template constructor from template of template. 48 00:04:17,410 --> 00:04:20,420 I am retrieving the structure of the 49 00:04:20,430 --> 00:04:23,620 react prompt from the online -hub. 50 00:04:23,810 --> 00:04:40,260 Prompt equals I am running the code. 51 00:04:46,160 --> 00:04:49,370 Let's see the input variables of the prompt. 52 00:04:50,600 --> 00:04:53,790 Print prompt .inputVariables. 53 00:05:04,870 --> 00:05:10,600 If you want to see the template, use prompt .template. 54 00:05:11,830 --> 00:05:13,980 Print prompt .template. 55 00:05:17,060 --> 00:05:19,650 You can see the chain of docs. 56 00:05:23,450 --> 00:05:24,180 Take a look here. 57 00:05:26,420 --> 00:05:27,570 Ok, let's move on. 58 00:05:30,850 --> 00:05:37,460 In this example, I will use three tools, the Python -Apple tool for executing 59 00:05:37,470 --> 00:05:47,720 Python code, the Wikipedia tool for searching Wikipedia, and the DuckDuckGo 60 00:05:47,730 --> 00:05:50,060 search tool for general web searches. 61 00:05:51,650 --> 00:05:56,100 The agent will choose the best tool based 62 00:05:56,110 --> 00:05:57,800 on the user's input query. 63 00:05:59,070 --> 00:06:01,060 I am creating the first tool. 64 00:06:02,030 --> 00:06:05,940 I am creating an object of type Python -Apple tool. 65 00:06:06,890 --> 00:06:11,380 Python -Apple equals and I am calling the constructor of the class. 66 00:06:13,920 --> 00:06:15,150 And now the tool. 67 00:06:16,560 --> 00:06:23,830 Python -Apple tool equals tool of and the 68 00:06:23,840 --> 00:06:27,890 arguments are name equals Python -Apple. 69 00:06:29,570 --> 00:06:32,160 Give the tool a descriptive name. 70 00:06:32,890 --> 00:06:35,920 The second argument will be func. 71 00:06:36,620 --> 00:06:39,560 This is the function to run when using 72 00:06:39,570 --> 00:06:40,300 this tool. 73 00:06:41,610 --> 00:06:43,860 Python -Apple .run. 74 00:06:45,030 --> 00:06:47,500 And finally, a description. 75 00:06:51,290 --> 00:06:54,180 Useful when you need to run Python to 76 00:06:54,190 --> 00:06:55,120 answer a question. 77 00:06:56,550 --> 00:06:58,480 You should input Python code. 78 00:06:59,570 --> 00:07:02,200 Let's do the same for the Wikipedia tool. 79 00:07:04,000 --> 00:07:06,470 I am creating an API wrapper. 80 00:07:08,360 --> 00:07:14,740 I am calling the constructor and an Wikipedia query run object. 81 00:07:15,350 --> 00:07:19,080 Wikipedia equals Wikipedia query run. 82 00:07:20,540 --> 00:07:24,740 And the API wrapper equals API wrapper, 83 00:07:25,030 --> 00:07:27,300 the object I have just defined. 84 00:07:30,900 --> 00:07:33,650 Next, I will create the Wikipedia tool. 85 00:07:34,160 --> 00:07:45,200 Wikipedia tool equals tool of and the arguments name equals Wikipedia, func 86 00:07:45,210 --> 00:07:52,010 equals Wikipedia run and the description. 87 00:07:56,920 --> 00:07:57,330 Very well. 88 00:07:58,100 --> 00:08:01,470 Let's create the DuckDuckGo search tool. 89 00:08:02,240 --> 00:08:09,670 Search equals DuckDuckGo search run and 90 00:08:09,680 --> 00:08:09,990 the tool. 91 00:08:11,680 --> 00:08:12,810 DuckDuckGo tool. 92 00:08:16,910 --> 00:08:23,040 I am calling the constructor of the tool class and the arguments are name equals 93 00:08:23,050 --> 00:08:35,460 DuckDuckGo search, func equals search .run and the description. 94 00:08:42,470 --> 00:08:48,040 Pay attention to single quotes if you define the string using single quotes. 95 00:08:48,770 --> 00:08:51,220 Use a backslash before it. 96 00:08:53,910 --> 00:08:56,500 We have created the tools, the Python or 97 00:08:56,510 --> 00:09:01,320 Apple tool, the Wikipedia tool and the DuckDuckGo search tool. 98 00:09:05,850 --> 00:09:09,420 Next, I will collect all the tools into a list. 99 00:09:10,670 --> 00:09:12,900 Tools equals and the list. 100 00:09:13,810 --> 00:09:21,770 Python or Apple tool, Wikipedia tool and the DuckDuckGo tool. 101 00:09:23,380 --> 00:09:28,150 To create the agent, there are basically two main things you have to do. 102 00:09:28,680 --> 00:09:35,750 Load the tools the agent is going to use and initialize the agent using the agent 103 00:09:35,760 --> 00:09:37,690 executor and those tools. 104 00:09:38,260 --> 00:09:45,870 First, agent equals createReactAgent of 105 00:09:45,880 --> 00:09:48,750 LLM, tools and prompt. 106 00:09:51,940 --> 00:09:55,950 The createReact function was imported 107 00:09:55,960 --> 00:09:58,730 from langchain .agents. 108 00:10:01,110 --> 00:10:05,480 This line of code combines the LLM, the 109 00:10:05,490 --> 00:10:08,400 tools and the prompt to create a React agent. 110 00:10:11,270 --> 00:10:15,080 Next, I will set up an agent executor to 111 00:10:15,090 --> 00:10:16,000 run the agent. 112 00:10:18,940 --> 00:10:22,810 The executor examines the input and 113 00:10:22,820 --> 00:10:26,550 determines the best tool to use based on the user's query. 114 00:10:27,220 --> 00:10:33,130 I am creating the executor to manage the agent's actions and output. 115 00:10:34,740 --> 00:10:40,710 AgentExecutor equals and I am calling the constructor of the AgentExecutor class 116 00:10:42,550 --> 00:10:52,160 and the arguments, agent equals agent, this is the object returned by the 117 00:10:52,170 --> 00:11:04,470 createReactAgent function, tools equals tools, my list of tools and the verbose 118 00:11:04,480 --> 00:11:05,110 equals true. 119 00:11:10,250 --> 00:11:13,080 I will add two more important arguments, 120 00:11:14,010 --> 00:11:25,240 handle parsingErrors equals true and maxIterations equals 10. 121 00:11:28,980 --> 00:11:35,110 HandleParsingErrors controls how the agent executor handles potential parsing 122 00:11:35,120 --> 00:11:40,730 errors that might occur during the execution of tools or the agent itself. 123 00:11:40,960 --> 00:11:48,270 If it is set to true, the executor will attempt to gracefully handle parsing 124 00:11:48,280 --> 00:11:54,070 errors by logging the error message for debugging purposes, continuing with the 125 00:11:54,080 --> 00:12:00,630 execution process, potentially skipping the affected tool or part of the agent's response. 126 00:12:01,340 --> 00:12:05,570 It will also try to recover and proceed with the remaining steps. 127 00:12:06,320 --> 00:12:12,170 If it is set to false, which is the default, parsing errors will cause the 128 00:12:12,180 --> 00:12:15,290 execution to stop immediately, raising an exception. 129 00:12:16,120 --> 00:12:19,390 Using this argument can make agents more 130 00:12:19,400 --> 00:12:24,390 robust and less prone to unexpected failures due to parsing issues, 131 00:12:24,600 --> 00:12:29,410 especially when working with external tools or unpredictable input. 132 00:12:30,440 --> 00:12:37,710 MaxIterations sets a maximum limit on the number of iterations or steps the agent 133 00:12:37,720 --> 00:12:40,330 can take during a single execution. 134 00:12:41,180 --> 00:12:44,310 Using this argument prevents agents from 135 00:12:44,320 --> 00:12:50,330 running indefinitely, which could lead to performance issues, resource exhaustion 136 00:12:50,340 --> 00:12:51,990 or getting stuck in loops. 137 00:12:52,820 --> 00:12:56,230 Once the specific number of iterations is 138 00:12:56,240 --> 00:13:02,670 reached, the execution will stop, even if the agent hasn't fully completed its task. 139 00:13:03,220 --> 00:13:08,270 Note that the optimal value for MaxIterations depends on the complexity 140 00:13:08,280 --> 00:13:14,630 of the task and the expected execution time, you might need to adjust it based 141 00:13:14,640 --> 00:13:16,030 on the specific use case. 142 00:13:16,980 --> 00:13:20,210 Ok, we've successfully built the React 143 00:13:20,220 --> 00:13:24,570 agent, let's take a break and in the next video we'll test it. 144 00:13:25,180 --> 00:13:28,110 I'm really excited to see what it can do, see you