So let me now go and accept all of the changes.
And let me run everything in debug here.
So I want to debug this to show you exactly what is going to happen with the intermediate results.
So we can see first things first, the call to the LM.
The first call is what is the length of the word dog.
And I put a breakpoint here.
So once the AI will return a message we can go and check it.
Cool.
So we got a response.
Let me show you all the local variables and the variable AI message.
Let's take a look at all the fields.
And we're looking for the attribute in the field of tool calls.
We can see it right over here in the middle.
Let me go and open it.
And we can see this is a list containing one element.
And in that element we have all of the information about the tool call.
So the function we want to invoke is get text length.
The arguments is going to be dictionary containing the keyword text and the input dog.
We can also see the tool called id here.
Cool.
So let's continue.
Now the execution step by step.
So the number of tool calls is greater than one.
So we are going to execute this if statement.
And first thing is we want to append to the messages list the AI message with all of the information
about the tool call.
So this is going to be our agent trajectory.
And now we want to iterate over the tool calls which we have only one.
And we want to extract from that tool.
Call the name the arguments and the ID.
So let me show you again a the structure of the tool call.
So we're going to do that.
And once we have that we'll find all the variables we need in order to invoke the tool.
So now we want to get the actual Python function to invoke.
So we simply use the helper function we wrote earlier.
Find tool by name.
And the tool to use variable is going to contain the structured invokable in class.
So we can simply use the invoke method on it.
And then we simply go and invoke that with the input and we get back an observable.
So let me go and put a breakpoint in in the tool that we wrote.
So let me go up here and this is here.
Let me put a breakpoint here and let me continue the run and let me continue the run.
All right.
So we can see we got here.
Let's go and run everything.
And the result we get is the length of this text here.
So let's go and continue.
And the result should be three.
And we can see here in the observation.
Let's go and check out this observation here.
This observation is three.
Cool.
So now we want to append the observation the result of the tool execution.
We want to append it to the message list.
And now we want to go and to continue the iteration on the tool calls which we don't have anymore.
So we're going to finish the iteration in this for loop over here.
And we're going to hit this continue and continue statement.
So let's go and click continue.
Right.
So now we've finished.
Now the iteration over the tool calls.
Now we're left to do another iteration in the while loop with the message history containing the agent
trajectory and all the intermediate results.
So let me show you the messages here so we can see we have a list of three elements.
The first one is the human message.
The second one is the AI message with all the tool calling information.
And the third message is a tool message containing the result of the tool execution.
All right.
So let's invoke the LM call.
Now we're waiting for a response.
And now the response should have zero two calls when we get it back.
So let's take a look at the AI message.
We get back here.
And now let's take a look at the tool calls field.
And we can see it's actually an empty list.
And that's why we are going to print the answer and break and finish the run here.
And we can see that the final answer of the LM is the length of the word dog is three characters.
So we have our react agent working with function calling.