1 00:00:00,700 --> 00:00:06,590 We ve seen that LLMs lack the ability to solve complex calculations, so we are 2 00:00:06,600 --> 00:00:08,090 going to take another approach. 3 00:00:08,960 --> 00:00:11,570 Instead of having the LLM generate the 4 00:00:11,580 --> 00:00:16,710 answer directly, which is difficult, we ll instruct the LLM to generate the 5 00:00:16,720 --> 00:00:22,030 Python code to calculate the answer and then run that code in a Python 6 00:00:22,040 --> 00:00:24,310 interpreter to get the answer. 7 00:00:24,320 --> 00:00:27,950 I am installing a package called 8 00:00:27,960 --> 00:00:33,790 langchain -experimental which contains experimental components and features 9 00:00:33,800 --> 00:00:41,490 related to Langchain, pip install -q -langchain -experimental. 10 00:00:45,000 --> 00:00:45,710 I am running it. 11 00:00:46,040 --> 00:00:49,870 It s important to note that this library 12 00:00:49,880 --> 00:00:56,830 is intended for research and exploration and some parts are not yet stable or 13 00:00:56,840 --> 00:01:02,770 secure for production use, this is just a demonstration of the Langchain capabilities. 14 00:01:03,840 --> 00:01:06,450 Let s see how to use Python -Apple. 15 00:01:06,780 --> 00:01:09,710 I am importing the Python -Apple class 16 00:01:09,720 --> 00:01:16,540 from the Langchain -Experimental -Utilities module from Langchain 17 00:01:16,550 --> 00:01:23,640 -Experimental .utilities import Python -Apple. 18 00:01:26,050 --> 00:01:29,340 I am creating an instance of the Python 19 00:01:29,350 --> 00:01:33,400 -Apple class which provides a way to execute Python code. 20 00:01:34,410 --> 00:01:40,540 Python underline Apple equals and I am calling the constructor of the class. 21 00:01:41,270 --> 00:01:48,060 Now, I can execute a Python expression within the Apple environment like this. 22 00:01:48,930 --> 00:01:54,920 Python -Apple .run and the Python expression as a string. 23 00:01:55,970 --> 00:02:02,900 For example, this line of Python code takes a list with all numbers divisible 24 00:02:02,910 --> 00:02:10,100 by 13 between 1 and 99 using list comprehension and prints the list. 25 00:02:10,870 --> 00:02:11,520 I am running it. 26 00:02:12,530 --> 00:02:15,820 In this example, there is no LLM involved. 27 00:02:16,450 --> 00:02:18,780 Let s go ahead and see how to create an agent. 28 00:02:19,450 --> 00:02:22,800 Agents are what make Langchain so useful. 29 00:02:23,470 --> 00:02:26,660 Agents combine Langchain tools and chains. 30 00:02:27,910 --> 00:02:30,260 I am importing a function called 31 00:02:30,270 --> 00:02:35,280 createPythonAgent from the Experimental -Agent toolkits. 32 00:02:37,980 --> 00:02:45,530 This function is used to create a special type of Langchain agent that can interact 33 00:02:45,540 --> 00:02:46,530 with Python code. 34 00:02:47,340 --> 00:02:50,330 Next, I will import the Python -Apple 35 00:02:50,340 --> 00:02:51,030 tool class. 36 00:02:52,180 --> 00:02:54,890 This tool provides a Python -Apple 37 00:02:54,900 --> 00:03:00,850 environment within Langchain allowing the agent to execute Python code directly. 38 00:03:01,960 --> 00:03:06,570 Finally, I will import chatOpenAi to create the LLM. 39 00:03:09,120 --> 00:03:10,790 I am creating the LLM. 40 00:03:11,380 --> 00:03:13,390 LLM equals chatOpenAi. 41 00:03:13,820 --> 00:03:18,170 I will use gpt for turbo because we need a powerful model. 42 00:03:19,240 --> 00:03:30,040 Model equals gpt for turbo preview and the temperature equals 0. 43 00:03:33,650 --> 00:03:40,000 I am instantiating a Python agent executor using the openAi LLM. 44 00:03:40,630 --> 00:03:44,860 This allows us to have the language model execute Python code. 45 00:03:46,030 --> 00:03:55,230 AgentExecutor equals createPythonAgent and the arguments. 46 00:03:57,040 --> 00:04:07,880 LLM equals LLM, tool equals Python -Apple tool and the verbose equals to. 47 00:04:10,920 --> 00:04:16,430 In the function createPythonAgent, there is an argument called tool. 48 00:04:17,120 --> 00:04:19,030 Let s talk about this for a moment. 49 00:04:19,880 --> 00:04:23,050 Tools are essentially functions that 50 00:04:23,060 --> 00:04:26,830 agents can use to interact with the outside world. 51 00:04:27,540 --> 00:04:32,950 These can range from general utilities such as a math calculator or a search 52 00:04:32,960 --> 00:04:36,350 function to other chase or even other agents. 53 00:04:38,610 --> 00:04:40,800 I will execute the Python agent. 54 00:04:41,350 --> 00:04:45,540 The problem to solve is given in natural language format. 55 00:04:46,950 --> 00:04:51,240 AgentExecutor .invoke and a task. 56 00:04:53,360 --> 00:04:55,510 Calculate the square root of the 57 00:04:55,520 --> 00:04:59,690 factorial of 12 and display it with 4 decimal points. 58 00:05:00,860 --> 00:05:01,730 I am running the code. 59 00:05:04,680 --> 00:05:08,130 You are now looking at the process of how 60 00:05:08,140 --> 00:05:10,550 an LLM generates Python code. 61 00:05:16,810 --> 00:05:21,680 The LLM first makes a plan, then an 62 00:05:21,690 --> 00:05:25,280 observation and finally a thought. 63 00:05:26,190 --> 00:05:29,240 It runs the code and we receive the 64 00:05:29,250 --> 00:05:29,840 correct answer. 65 00:05:33,950 --> 00:05:36,660 I am solving the same problem in Python 66 00:05:36,670 --> 00:05:38,260 idle to check the result. 67 00:05:38,790 --> 00:05:40,320 I ve got the same result. 68 00:05:42,100 --> 00:05:44,570 I am assigning another task to it. 69 00:05:45,520 --> 00:05:51,590 Response equals agentExecutor .invoke and 70 00:05:51,600 --> 00:05:51,990 a task. 71 00:05:52,680 --> 00:05:57,650 This time it s to calculate 5 .1 to the 72 00:05:57,660 --> 00:05:59,290 power of 7 .3. 73 00:05:59,880 --> 00:06:03,290 Both the base and the exponent are floats 74 00:06:03,300 --> 00:06:07,810 and LLM alone might fail to provide the correct answer. 75 00:06:09,020 --> 00:06:10,550 I am executing the code. 76 00:06:14,450 --> 00:06:17,840 Notice how it constructs its chain of thought. 77 00:06:20,040 --> 00:06:26,810 If the generated code doesn t work initially and it detects an error, it 78 00:06:26,820 --> 00:06:32,650 will attempt to resolve the issue and generate another piece of code, hopefully functional. 79 00:06:34,060 --> 00:06:41,970 I am running it in idle to 5 .1 to the power of 7 .3. 80 00:06:45,170 --> 00:06:47,500 We see that the response was correct. 81 00:06:52,670 --> 00:06:55,780 Note that response is a Python dictionary 82 00:06:55,790 --> 00:06:57,680 with two key value pairs. 83 00:06:58,650 --> 00:07:03,280 The keys are input, representing what the 84 00:07:03,290 --> 00:07:09,240 user asked and output which contains the LLM response. 85 00:07:18,010 --> 00:07:18,840 Great job. 86 00:07:19,350 --> 00:07:21,520 Congratulations on reaching this far. 87 00:07:22,110 --> 00:07:26,140 You ve made fantastic progress and learned a lot about langchain. 88 00:07:26,330 --> 00:07:32,440 You ve learned about connecting langchain to LLMs, creating and using prompt 89 00:07:32,450 --> 00:07:38,520 templates and building both simple and sequential chains using langchain agents 90 00:07:38,530 --> 00:07:38,980 and more.