You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Define a function to evaluate tool calls identified by the LLM. LLMs can hallucinate tool calls or make errors about the parameters that the tools need. Therefore, first validate the tool name and parameters by comparing them to the `toolRegistry` dictionary. Then, run the functions associated with the tools using the [`feval`](https://www.mathworks.com/help/matlab/ref/feval.html) function.
101
+
Define a function to evaluate tool calls identified by the LLM. LLMs can hallucinate tool calls or make errors about the parameters that the tools need. Therefore, first validate the tool name and parameters by comparing them to the `toolRegistry` dictionary. Then, run the functions associated with the tools. Return the result as an observation to the agent.
102
102
103
103
```matlab
104
-
function result = evaluateToolCall(toolCall,toolRegistry)
105
-
% Validate tool name
106
-
toolName = toolCall.function.name;
107
-
assert(isKey(toolRegistry,toolName),"Invalid tool name ''%s''.",toolName)
108
-
109
-
% Validate JSON syntax
110
-
try
111
-
args = jsondecode(toolCall.function.arguments);
112
-
catch
113
-
error("Model returned invalid JSON syntax for arguments of tool ''%s''.",toolName);
@@ -149,10 +145,10 @@ This architecture is an iterative workflow. For each iteration, the agent perfor
149
145
2. Action — The agent executes the next action.
150
146
3. Observation — The agent observes the tool output.
151
147
152
-
Define the function `runAgent` that answers a user query `userQuery` using the ReAct agent architecture and the tools provided in `toolRegistry`.
148
+
Define the function `runReActAgent` that answers a user query `userQuery` using the ReAct agent architecture and the tools provided in `toolRegistry`.
153
149
154
150
```matlab
155
-
function agentResponse = runAgent(userQuery,toolRegistry)
151
+
function agentResponse = runReActAgent(userQuery,toolRegistry)
156
152
```
157
153
158
154
To ensure the agent stops after it answers the user query, create a tool `finalAnswer` and add it to the tool list.
@@ -170,10 +166,10 @@ systemPrompt = ...
170
166
"Solve the problem. When done, call the tool finalAnswer else you will get stuck in a loop.";
171
167
```
172
168
173
-
Connect to the OpenAI Chat Completion API using the [`openAIChat`](../doc/functions/openAIChat.md) function. Use the OpenAI model `"gpt-4.1"`. Provide the LLM with tools using the `Tools` name\-value argument. Initialize the message history.
169
+
Connect to the OpenAI Chat Completion API using the [`openAIChat`](../doc/functions/openAIChat.md) function. Use the OpenAI model GPT\-4.1 mini. Provide the LLM with tools using the `Tools` name\-value argument. Initialize the message history.
Instruct the agent to plan the next step. Generate a response from the message history. To ensure the agent outputs text, set the `ToolChoice` name\-value argument to `"none"`.
201
197
202
198
```matlab
203
-
history = addUserMessage(history,"Plan your next step.");
1. Solve the quadratic equation x^2 + 2x - 3 = 0 to find both roots.
262
-
2. Compare the two roots and select the smallest one.
256
+
[Thought] To find the smallest root of the quadratic equation \(x^2 + 2x - 3 = 0\), I will first solve the quadratic equation to find both roots. Then, I will determine the smallest root.
257
+
258
+
I will start by solving the quadratic equation.
263
259
[Action] Calling tool 'solveQuadraticEquation' with args: "{\"a\":1,\"b\":2,\"c\":-3}"
264
-
[Observation] Result from tool 'solveQuadraticEquation': ["-3","1"]
265
-
[Thought] Now that I have both roots (-3 and 1), I will compare them to determine which is the smallest root. Then I will provide the smallest root as the final answer.
260
+
[Observation] Result from tool 'solveQuadraticEquation': [-3.0000000000000004,0.99999999999999978]
261
+
[Thought] I have found the roots of the equation to be approximately -3 and 1. Now, I will identify the smallest root between these two values.
266
262
[Action] Calling tool 'smallestRealNumber' with args: "{\"x1\":\"-3\",\"x2\":\"1\"}"
267
-
[Observation] Result from tool 'smallestRealNumber': "-3"
268
-
[Thought] I have identified -3 as the smallest root. My next step is to provide -3 as the final answer.
263
+
[Observation] Result from tool 'smallestRealNumber': -3
264
+
[Thought] The smallest root of the quadratic equation \(x^2 + 2x - 3 = 0\) is -3. I will now provide this as the final answer.
269
265
```
270
266
271
267
@@ -276,7 +272,7 @@ disp(agentResponse);
276
272
```
277
273
278
274
```matlabTextOutput
279
-
The smallest root of the equation x^2 + 2x - 3 = 0 is -3.
275
+
The smallest root of the equation \(x^2 + 2x - 3 = 0\) is -3.
0 commit comments