We ve seen that LLMs lack the ability to solve complex calculations, so we are
going to take another approach.
Instead of having the LLM generate the
answer directly, which is difficult, we ll instruct the LLM to generate the
Python code to calculate the answer and then run that code in a Python
interpreter to get the answer.
I am installing a package called
langchain -experimental which contains experimental components and features
related to Langchain, pip install -q -langchain -experimental.
I am running it.
It s important to note that this library
is intended for research and exploration and some parts are not yet stable or
secure for production use, this is just a demonstration of the Langchain capabilities.
Let s see how to use Python -Apple.
I am importing the Python -Apple class
from the Langchain -Experimental -Utilities module from Langchain
-Experimental .utilities import Python -Apple.
I am creating an instance of the Python
-Apple class which provides a way to execute Python code.
Python underline Apple equals and I am calling the constructor of the class.
Now, I can execute a Python expression within the Apple environment like this.
Python -Apple .run and the Python expression as a string.
For example, this line of Python code takes a list with all numbers divisible
by 13 between 1 and 99 using list comprehension and prints the list.
I am running it.
In this example, there is no LLM involved.
Let s go ahead and see how to create an agent.
Agents are what make Langchain so useful.
Agents combine Langchain tools and chains.
I am importing a function called
createPythonAgent from the Experimental -Agent toolkits.
This function is used to create a special type of Langchain agent that can interact
with Python code.
Next, I will import the Python -Apple
tool class.
This tool provides a Python -Apple
environment within Langchain allowing the agent to execute Python code directly.
Finally, I will import chatOpenAi to create the LLM.
I am creating the LLM.
LLM equals chatOpenAi.
I will use gpt for turbo because we need a powerful model.
Model equals gpt for turbo preview and the temperature equals 0.
I am instantiating a Python agent executor using the openAi LLM.
This allows us to have the language model execute Python code.
AgentExecutor equals createPythonAgent and the arguments.
LLM equals LLM, tool equals Python -Apple tool and the verbose equals to.
In the function createPythonAgent, there is an argument called tool.
Let s talk about this for a moment.
Tools are essentially functions that
agents can use to interact with the outside world.
These can range from general utilities such as a math calculator or a search
function to other chase or even other agents.
I will execute the Python agent.
The problem to solve is given in natural language format.
AgentExecutor .invoke and a task.
Calculate the square root of the
factorial of 12 and display it with 4 decimal points.
I am running the code.
You are now looking at the process of how
an LLM generates Python code.
The LLM first makes a plan, then an
observation and finally a thought.
It runs the code and we receive the
correct answer.
I am solving the same problem in Python
idle to check the result.
I ve got the same result.
I am assigning another task to it.
Response equals agentExecutor .invoke and
a task.
This time it s to calculate 5 .1 to the
power of 7 .3.
Both the base and the exponent are floats
and LLM alone might fail to provide the correct answer.
I am executing the code.
Notice how it constructs its chain of thought.
If the generated code doesn t work initially and it detects an error, it
will attempt to resolve the issue and generate another piece of code, hopefully functional.
I am running it in idle to 5 .1 to the power of 7 .3.
We see that the response was correct.
Note that response is a Python dictionary
with two key value pairs.
The keys are input, representing what the
user asked and output which contains the LLM response.
Great job.
Congratulations on reaching this far.
You ve made fantastic progress and learned a lot about langchain.
You ve learned about connecting langchain to LLMs, creating and using prompt
templates and building both simple and sequential chains using langchain agents
and more.