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
Copy file name to clipboardExpand all lines: examples/ai_agent/AddObservabilityToAIAgent.md
+62-40Lines changed: 62 additions & 40 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,31 +1,33 @@
1
-
# Add OpenTelemetry Observability to an AI Agent
1
+
# Add OpenTelemetry Observability to AI Agent
2
2
3
-
This example shows how to add OpenTelemetry observability to an AI Agent. Observability enables monitoring of the requests sent to the agent, which can help with troubleshooting and optimization. In this example, [OpenTelemetry](https://opentelemetry.io/) is used to capture telemetry data including traces and metrics. OpenTelemetry is an open\-source observability framework that provides a standard way to generate, collect, and export telemetry data without vendor lock\-in.
3
+
This example shows how to add OpenTelemetry observability to an AI Agent.
4
4
5
-
The original example being instrumented is taken from the[ "Solve Simple Math Problem Using AI Agent"](https://github.com/matlab-deep-learning/llms-with-matlab/blob/main/examples/SolveSimpleMathProblemUsingAIAgent.md) example in the ["Large Language Models (LLMs) with MATLAB"](https://github.com/matlab-deep-learning/llms-with-matlab) package. It shows how to build an AI agent to find the smallest root of a quadratic equation.
5
+
*AI agents* are programs that autonomously plan and execute workflows. Typically, agents use large language models (LLMs) to process user queries and identify actions that need to be taken, also known as *tool calls*. The agent then executes the tool calls that the LLM has identified and returns the result to the LLM. Then, the LLM generates an answer or executes more tool calls.
6
6
7
-
AI agents are programs that autonomously plan and execute workflows. Typically, agents use large language models (LLMs) to process user queries and identify actions that need to be taken, also known as *tool calls*. The agent then executes the tool calls that the LLM has identified and returns the result to the LLM. Then, the LLM generates an answer or executes more tool calls.
7
+
This agent in this example is based on the [Solve Simple Math Problem Using AI Agent](https://github.com/matlab-deep-learning/llms-with-matlab/blob/main/examples/SolveSimpleMathProblemUsingAIAgent.md) example in the [Large Language Models (LLMs) with MATLAB](https://github.com/matlab-deep-learning/llms-with-matlab) add\-on. The agent finds the smallest root of a quadratic equation.
8
8
9
-
The following is a trace that shows the workflow in this example. For more information about tracing, see this[ introduction](https://opentelemetry.io/docs/concepts/signals/traces/) in the OpenTelemetry website.
9
+
*Observability* lets you monitor the requests sent to the agent, which can help with troubleshooting and optimization. This example uses [OpenTelemetry](https://opentelemetry.io/) to capture telemetry data including traces and metrics. OpenTelemetry is an open\-source observability framework that provides a standardized way to generate, collect, and export telemetry data.
This image shows a trace of the workflow in this example. For more information about tracing, see this[ introduction](https://opentelemetry.io/docs/concepts/signals/traces/) in the OpenTelemetry website.
- A tracing backend. For example, Jaeger ([https://www.jaegertracing.io/](https://www.jaegertracing.io/))
26
-
- A metrics backend. For example, Prometheus [(https://prometheus.io/](http://(https//prometheus.io/))
26
+
In addition, the example uses these external code packages:
27
27
28
-
The OpenTelemetry Collector is a component that uses configurable pipelines to ingest telemetry data in different formats, transform it, and send it to one or more backends. It decouples the instrumented applications from the backends and therefore enables changes to how the telemetry data is processed and where it is sent to without modifying application code. It also handles the complexities of data transmission across networks and retry logic. For details about how to configure the OpenTelemetry Collector, see the documentation at [https://opentelemetry.io/docs/collector/configuration/](https://opentelemetry.io/docs/collector/configuration/).
28
+
-[OpenTelemetry Collector](https://opentelemetry.io/docs/collector/). OpenTelemetry Collector is a component that uses configurable pipelines to ingest telemetry data in different formats, transform it, and send it to one or more backends. It decouples the instrumented applications from the backends and therefore enables changes to how the telemetry data is processed and where it is sent to without modifying application code. It also handles the complexities of data transmission across networks and retry logic. For details about how to configure the OpenTelemetry Collector, see the documentation at [https://opentelemetry.io/docs/collector/configuration/](https://opentelemetry.io/docs/collector/configuration/).
29
+
- A tracing backend. For example, Jaeger ([https://www.jaegertracing.io/](https://www.jaegertracing.io/)).
30
+
- A metrics backend. For example, Prometheus [(https://prometheus.io/](http://(https//prometheus.io/)).
29
31
30
32
Before proceeding, check both required add\-on packages are installed.
31
33
@@ -35,7 +37,9 @@ checkRequiredPackages
35
37
36
38
# Initialize OpenTelemetry
37
39
38
-
OpenTelemetry needs to be initialized and configured before it can be used. This needs to be done only once in a MATLAB session. The following initialization code sets the service name as an attribute. Otherwise, it uses default configurations.
40
+
First, initialize and configure OpenTelemetry. You only need to do this once during your MATLAB® session.
41
+
42
+
The following initialization code sets the service name as an attribute. Otherwise, it uses default configurations.
39
43
40
44
```matlab
41
45
function initializeOTel
@@ -56,9 +60,9 @@ runOnce(@initializeOTel);
56
60
57
61
This example uses the OpenAI® API, which requires an OpenAI API key. For information on how to obtain an OpenAI API key, as well as pricing, terms and conditions of use, and information about available models, see the OpenAI documentation at [https://platform.openai.com/docs/overview](https://platform.openai.com/docs/overview).
58
62
59
-
To connect to the OpenAI API from MATLAB® using LLMs with MATLAB, specify the OpenAI API key as an environment variable and save it to a file called ".env".
63
+
To connect to the OpenAI API from MATLAB using LLMs with MATLAB, specify the OpenAI API key as an environment variable and save it to a file called ".env".
To connect to OpenAI, the ".env" file must be on the search path. Load the environment file using the [`loadenv`](https://www.mathworks.com/help/matlab/ref/loadenv.html) function.
64
68
@@ -68,7 +72,7 @@ loadenv(".env")
68
72
69
73
# Capture Details of LLM Interactions
70
74
71
-
To build observability into AI Agent workflow, capture inputs, outputs, properties, and metadata of interactions with the OpenAI LLM. Start an OpenTelemetry span for each interaction, and then record LLM inputs, outputs and other data as attributes of the span. OpenTelemetry metrics can be used to aggregate quantities across multiple calls to the LLM, such as the total number of LLM calls and the total number of tokens.
75
+
To build observability into an AI Agent workflow, capture inputs, outputs, properties, and metadata of interactions with the LLM. Start an OpenTelemetry span for each interaction, and then record LLM inputs, outputs and other data as attributes of the span. OpenTelemetry metrics can be used to aggregate quantities across multiple calls to the LLM, such as the total number of LLM calls and the total number of tokens.
72
76
73
77
## Query Pricing
74
78
@@ -98,7 +102,7 @@ end
98
102
99
103
## Capture Details of Agent Creation
100
104
101
-
At agent creation, it is useful to record agent name and description, the underlying LLM model and any system prompt sent to the LLM.
105
+
When you create the agent, record the agent name and description, the underlying LLM model, as well as any system prompt sent to the LLM.
102
106
103
107
```matlab
104
108
function llm = createAgent(modelName, systemPrompt, tools)
@@ -108,7 +112,7 @@ function llm = createAgent(modelName, systemPrompt, tools)
% Capture OpenTelemetry span attributes related to the chat
@@ -128,17 +132,17 @@ end
128
132
129
133
## Capture Details of Agent Requests
130
134
131
-
For each LLM request, record the request input and output, the number of tokens, the associated costs, and the tools defined. Other properties including response ID and status, hyperparameters such as temperature, Top P, and stop sequences are also useful. For aggregated quantities such as total requests, tokens and costs, define metric instruments for tracking them.
135
+
For each LLM request, record the request input and output, the number of tokens, the associated costs, and the defined tools. You can also record other properties including response ID and status, hyperparameters such as temperature, Top P, and stop sequences. To track aggregated quantities such as total requests, tokens and costs, define metric instruments.
132
136
133
137
```matlab
134
-
function [thought,completeOutput] = invokeAgent(llm, history,toolChoice, tools, inputRate, outputRate)
138
+
function [thought,completeOutput] = invokeAgent(llm, history,toolChoice, tools, inputRate, outputRate)
Create a tool registry, which is passed to the agent to define what tools it can use.
265
+
Create a tool registry using the `registerToolRegistry` function, which is defined at the bottom of this example. In this example, the tools include a function to solve a quadratic equation, as well as a function to determine the smallest real number of a list of two arbitrary numbers.
262
266
263
267
```matlab
264
268
toolRegistry = registerToolRegistry;
265
269
```
266
270
267
-
Define the query to solve a simple math problem. Answer the query using the agent.
271
+
Define the query to solve a simple math problem.
268
272
269
273
```matlab
270
274
userQuery = "What is the smallest root of x^2+2x-3=0?";
275
+
```
276
+
277
+
Answer the query using the agent. Pass the tool registry to the agent to define what tools it can use.
[Thought] I will solve the quadratic equation x^2 + 2x - 3 = 0 to find its roots. Then I will determine the smallest root.
285
+
[Thought] I will find the roots of the quadratic equation x^2 + 2x - 3 = 0.
277
286
[Action] Calling tool 'solveQuadraticEquation' with args: "{\"a\":1,\"b\":2,\"c\":-3}"
278
287
[Observation] Result from tool 'solveQuadraticEquation': ["-3","1"]
279
-
[Thought] I will determine the smallest real number from the roots -3 and 1.
288
+
[Thought] I will determine the smallest root from the roots -3 and 1.
280
289
[Action] Calling tool 'smallestRealNumber' with args: "{\"x1\":\"-3\",\"x2\":\"1\"}"
281
290
[Observation] Result from tool 'smallestRealNumber': -3
282
-
[Thought] I will provide the final answer, which is the smallest root -3.
291
+
[Thought] I will return the smallest root, which is -3, as the final answer.
283
292
```
284
293
285
294
End the top\-level OpenTelemetry span and clear its scope. In functions, spans end implicitly at the end of the functions when the span variables run out of scope. In scripts, spans have to be ended explicitly.
@@ -301,17 +310,22 @@ The smallest root of the equation x^2 + 2x - 3 = 0 is -3.
301
310
302
311
# Visualize Trace
303
312
304
-
Visualize the generated trace in your tracing backend. For example, the following shows the trace in Jaeger. It shows every request sent to the LLM, every tool call, the order they happen, and the time they take. For every LLM request, it also shows the input and output, the hyperparameters used, the number of input and output tokens, and the costs incurred.
313
+
Visualize the generated trace in your tracing backend. For example, the following images show the trace in Jaeger.
The images show every request sent to the LLM, every tool call, the order they happen, and the time they take. For every LLM request, they also show the input and output, the hyperparameters used, the number of input and output tokens, and the costs incurred.
*Screenshots taken from Jaeger. Used with permission from the Jaeger project. This way to*[*jaegertracing.io*](https://www.jaegertracing.io)*.*
311
322
312
-
# Helper Functions
323
+
# Supporting Functions
313
324
314
325
## Check Required Packages
326
+
327
+
The `checkRequiredPackages` function checks both required add\-on packages are installed.
328
+
315
329
```matlab
316
330
function checkRequiredPackages
317
331
installed = matlab.addons.installedAddons;
@@ -411,7 +425,7 @@ end
411
425
412
426
## Validate Tool Calls
413
427
414
-
The validateToolCall function validates tool calls identified by the LLM. LLMs can hallucinate tool calls or make errors about the parameters that the tools need. Therefore, validate the tool name and parameters by comparing them to the `toolRegistry` dictionary.
428
+
The `validateToolCall` function validates tool calls identified by the LLM. LLMs can hallucinate tool calls or make errors about the parameters that the tools need. Therefore, validate the tool name and parameters by comparing them to the `toolRegistry` dictionary.
415
429
416
430
```matlab
417
431
function argValues = validateToolCall(toolCall,toolRegistry)
## Tool for Computing Roots of Second\-Order Polynomial
463
+
## Tool 1: Solve Quadratic Equation
464
+
465
+
The `solveQuadraticEquation` function computes the roots of a second\-order polynomial.
466
+
450
467
```matlab
451
468
function strR = solveQuadraticEquation(a,b,c)
452
469
% Start OpenTelemetry span
@@ -460,7 +477,10 @@ strR = string(r);
460
477
end
461
478
```
462
479
463
-
## Tool for Finding Smallest Real Number
480
+
## Tool 2: Determine Smallest Real Number
481
+
482
+
The `smallestRealNumber` function computes the smallest real number from a list of two arbitrary numbers.
483
+
464
484
```matlab
465
485
function xMin = smallestRealNumber(strX1,strX2)
466
486
% Start OpenTelemetry span
@@ -516,7 +536,7 @@ end
516
536
517
537
## Run Once
518
538
519
-
Helper to ensure the input function is only run once.
539
+
The `runOnce` function ensures the input function is only run once. This allows you to rerun the script multiple times without re\-initializing OpenTelemetry.
520
540
521
541
```matlab
522
542
function runOnce(fh)
@@ -529,5 +549,7 @@ end
529
549
```
530
550
531
551
# References
532
-
<aid="M_4aeb"></a>
533
-
\[1\] Shunyu Yao, Jeffrey Zhao, Dian Yu, Nan Du, Izhak Shafran, Karthik Narasimhan, and Yuan Cao. "ReAct: Synergizing Reasoning and Acting in Language Models". ArXiv, 10 March 2023. [https://doi.org/10.48550/arXiv.2210.03629](https://doi.org/10.48550/arXiv.2210.03629).
552
+
553
+
\[1\] Shunyu Yao, Jeffrey Zhao, Dian Yu, Nan Du, Izhak Shafran, Karthik Narasimhan, and Yuan Cao. "ReAct: Synergizing Reasoning and Acting in Language Models". ArXiv, 10 March 2023. [https://doi.org/10.48550/arXiv.2210.03629.](https://doi.org/10.48550/arXiv.2210.03629.)
0 commit comments