Skip to content

Commit e62f153

Browse files
committed
Cookbook updates
Signed-off-by: Prasad Mujumdar <prasad@okahu.ai>
1 parent ad7d210 commit e62f153

1 file changed

Lines changed: 82 additions & 17 deletions

File tree

documentation/Monocle_Cookbook.md

Lines changed: 82 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,99 @@ Import the package and add Monocle a single line of code to enable Monocle telem
2020
```
2121
Now when you run the application, it will generate the trace files `monocle_trace_*.json` in the directory where the application is ran.
2222

23-
- Typescript
24-
Get the Monocle package
25-
26-
```shell
27-
npm install --save monacle2ai
28-
```
29-
Instrument your app code
30-
```js
31-
const { setupMonocle } = require("monacle2ai")
32-
setup_monocle_telemetry(workflow_name="your-app-name")
23+
## Combining multiple APIs under single traceID
24+
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)
3333
```
34+
- Wrapp the code under `monocle_trace`
35+
```python
36+
with monocle_trace():
37+
embedding_api()
38+
inferece_api()
39+
3440

3541
## 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.
3743
```python
3844
...
3945
def conversation():
40-
while True:
4146
...
4247
message = input("How can I help you:")
43-
cleaned_message = gaurdrail_chai(message) ==> GenAI code
44-
result = rag_chat_chain.invoke(message) ==> GenAI code
48+
cleaned_message = response = openai.chat.completions.create(message) ==> GenAI code
49+
result = rag_chat_chain.invoke(cleaned_message) ==> GenAI code
4550
```
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.
52+
- Enableing scope programatically at method level
53+
```python
54+
with monocle_trace_scope("conversation"):
55+
message = input("How can I help you:")
56+
cleaned_message = response = openai.chat.completions.create(message) ==> GenAI code
57+
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+
def conversation():
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.
4875
```json
4976
"attributes": {
5077
"span.type": "inference",
5178
...
5279
"scope.conversation": "0xcb80e6f772968ed50ead80657b09cf52",
53-
```
80+
```
81+
82+
## 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
89+
90+
web_app = Flask(__name__)
91+
setup_monocle_telemetry(workflow_name = "my-chatbot-webapp")
92+
93+
def main():
94+
web_app.run(host="0.0.0.0", port=8096, debug=False)
95+
96+
@web_app.route('/chat',methods = ["POST"])
97+
def chat():
98+
try:
99+
coversation_id= request.headers["coversation-id"]
100+
question = request.args["question"]
101+
response = chat(question, coversation_id)
102+
return response
103+
104+
```
105+
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.
113+
```json
114+
"attributes": {
115+
"span.type": "inference",
116+
...
117+
"scope.conversation": "conversion-id: 0xcb80e6f772968ed50ead80657b09cf52",
118+
```

0 commit comments

Comments
 (0)