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
By default monocle instrumetation will generate traces for every chain or API call that your application. If you want to combine some traces for multiple APIs under a single traceID,
25
+
- Use `start_trace()` and `stop_trace()` APIs
26
+
```python
27
+
token = start_trace()
28
+
try:
29
+
embedding_api()
30
+
inferece_api()
31
+
finally:
32
+
stop_trace(token)
33
33
```
34
+
- Wrapp the code under `monocle_trace`
35
+
```python
36
+
with monocle_trace():
37
+
embedding_api()
38
+
inferece_api()
39
+
34
40
35
41
## Track application business logic coded in a top level application method/API
36
-
Consider a chatbot application with a method called conversation() that implements a chat conversion thread with end user. When the
42
+
Consider a chatbot application with a method called conversation() that implements a chat conversion thread with end user. This method in turn calls other APIs like OpenAI and Langchain to use LLMs and generate responses.
result = rag_chat_chain.invoke(cleaned_message) ==> GenAI code
45
50
```
46
-
47
-
The above code will generate two traces (one per chain invocation). All the spans in these traces will have an attribute called `Conversaion` with a unique value.
51
+
By default monocle instrumetation will generate a unique trace ID for every chain or API call that your application. This is very useful to track how your app is using the GenAI services. However, that's often not sufficient. As an app developer or owner, you might want to look at bigger picture from the logic or business context. For example, you want to look at the prompts or latency etc at the conversion level than API level. Monocle has this notion of [scopes](Monocle_User_Guide.md#scopes) which allows to you tie multiple traces/spans under a unique id so you can group it.
result = rag_chat_chain.invoke(cleaned_message) ==> GenAI code
58
+
```
59
+
- By adding a decorator `monocle_trace_scope_method` to this `conversation()` method
60
+
```python
61
+
@monocle_trace_scope_method("conversation")
62
+
defconversation():
63
+
...
64
+
```
65
+
- Configuraging the method name in ```monocle_scope.json``` file that's placed in the working directory of the application
66
+
```json
67
+
{
68
+
"package": "myapp.bot",
69
+
"object": "chat",
70
+
"method": "conversation",
71
+
"scope_name": "conversation"
72
+
}
73
+
```
74
+
The above code will generate two traces (one per chain invocation). All the spans in these traces will have an attribute called `conversaion` with a unique value.
## Build on existing application logic to capture scope
83
+
Imagine you have a chatbot where the frontend app is running in browser and the backend gen AI code is running in a REST framework like Flask or hosted in serverless cloud service like Azure function or AWS Lambda. Let's say that the application has a notion of conversaions, a chat thread that goes between end user and chatbot. A conversation IDs is genearted in the frontend to track each conversions and for sent as a REST header to stateless backend to retrieve the right context. Monocle enables you to track this conversation ID as a scope so all the gen AI APIs called during a conversation are marked with this unique conversation ID.
84
+
- GenAI code running in the Flask
85
+
Monocle support Flask instrumentation out of the box. All you need to do is to add `setup_monocle_telemetry()` in your flask app and specify the http headers you want to track in the `monocle_scope.json` file
86
+
```python
87
+
from flask import Flask, request, jsonify
88
+
from monocle_apptrace.instrumentation.common.instrumentor import setup_monocle_telemetry
Save the `monocle_scope.json` in the folder where you run the Flask application
106
+
```json
107
+
{
108
+
"http_header": "client-id",
109
+
"scope_name": "conversation"
110
+
}
111
+
```
112
+
The above code will generate two traces (one per chain invocation). All the spans in these traces will have an attribute called `conversaion` with a unique value.
0 commit comments