Skip to content

Commit 182c7a8

Browse files
committed
Add request/ response IDs.
1 parent f6d13f8 commit 182c7a8

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

newrelic/hooks/mlmodel_openai.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def wrap_chat_completion_create(wrapped, instance, args, kwargs):
3737
return
3838

3939
custom_attrs_dict = transaction._custom_params
40-
conversation_id = custom_attrs_dict["conversation_id"] if "conversation_id" in custom_attrs_dict.keys() else str(uuid.uuid4())
40+
conversation_id = custom_attrs_dict.get("conversation_id", str(uuid.uuid4()))
4141

4242
chat_completion_id = str(uuid.uuid4())
4343
available_metadata = get_trace_linking_metadata()
@@ -46,6 +46,8 @@ def wrap_chat_completion_create(wrapped, instance, args, kwargs):
4646

4747
response_headers = getattr(response, "_nr_response_headers", None)
4848
response_model = response.model
49+
response_id = response.get("id", "")
50+
request_id = response_headers.get("x-request-id", "")
4951
settings = transaction.settings if transaction.settings is not None else global_settings()
5052

5153
chat_completion_summary_dict = {
@@ -55,8 +57,9 @@ def wrap_chat_completion_create(wrapped, instance, args, kwargs):
5557
"span_id": span_id,
5658
"trace_id": trace_id,
5759
"transaction_id": transaction._transaction_id,
60+
"request_id": request_id,
5861
"api_key_last_four_digits": f"sk-{response.api_key[-4:]}",
59-
"response_time": ft.duration,
62+
"duration": ft.duration,
6063
"request.model": kwargs.get("model") or kwargs.get("engine"),
6164
"response.model": response_model,
6265
"response.organization": response.organization,
@@ -77,44 +80,46 @@ def wrap_chat_completion_create(wrapped, instance, args, kwargs):
7780
"vendor": "openAI",
7881
"ingest_source": "Python",
7982
"number_of_messages": len(kwargs.get("messages", [])) + len(response.choices),
80-
"api_version": response_headers.get("openai-version", "")
8183
}
8284

8385
transaction.record_ml_event("LlmChatCompletionSummary", chat_completion_summary_dict)
8486
message_list = list(kwargs.get("messages", [])) + [response.choices[0].message]
8587

86-
create_chat_completion_message_event(transaction, message_list, chat_completion_id, span_id, trace_id, response_model)
88+
create_chat_completion_message_event(transaction, settings.app_name, message_list, chat_completion_id, span_id, trace_id, response_model, response_id, request_id)
8789

8890
return response
8991

9092

9193
def check_rate_limit_header(response_headers, header_name, is_int):
9294
if not response_headers:
93-
return None
95+
return ""
96+
9497
if header_name in response_headers:
9598
header_value = response_headers.get(header_name)
9699
if is_int:
97100
header_value = int(header_value)
98101
return header_value
99102
else:
100-
return None
103+
return ""
101104

102105

103-
def create_chat_completion_message_event(transaction, message_list, chat_completion_id, span_id, trace_id, response_model):
106+
def create_chat_completion_message_event(transaction, app_name, message_list, chat_completion_id, span_id, trace_id, response_model, response_id, request_id):
104107
if not transaction:
105108
return
106109

107110
for index, message in enumerate(message_list):
108111
chat_completion_message_dict = {
109-
"id": str(uuid.uuid4()),
112+
"id": "%s-%s" % (response_id, index),
113+
"appName": app_name,
114+
"request_id": request_id,
110115
"span_id": span_id,
111116
"trace_id": trace_id,
112117
"transaction_id": transaction._transaction_id,
113118
"content": message.get("content", "")[:MAX_LOG_MESSAGE_LENGTH],
114119
"role": message.get("role"),
115120
"completion_id": chat_completion_id,
116121
"sequence": index,
117-
"model": response_model,
122+
"response.model": response_model,
118123
"vendor": "openAI",
119124
"ingest_source": "Python",
120125
}

tests/mlmodel_openai/test_chat_completion.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ def set_trace_info():
6464
'transaction_id': None,
6565
'span_id': "span-id",
6666
'trace_id': "trace-id",
67+
'request_id': "49dbbffbd3c3f4612aa48def69059ccd",
6768
'api_key_last_four_digits': 'sk-CRET',
68-
'response_time': None, # Response time varies each test run
69+
'duration': None, # Response time varies each test run
6970
'request.model': 'gpt-3.5-turbo',
7071
'response.model': 'gpt-3.5-turbo-0613',
7172
'response.organization': 'new-relic-nkmd8b',
@@ -86,53 +87,58 @@ def set_trace_info():
8687
'vendor': 'openAI',
8788
'ingest_source': 'Python',
8889
'number_of_messages': 3,
89-
'api_version': '2020-10-01'
9090
},
9191
),
9292
(
9393
{'type': 'LlmChatCompletionMessage'},
9494
{
95-
'id': None,
95+
'id': "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTemv-0",
96+
'appName': 'Python Agent Test (mlmodel_openai)',
97+
'request_id': "49dbbffbd3c3f4612aa48def69059ccd",
9698
'span_id': "span-id",
9799
'trace_id': "trace-id",
98100
'transaction_id': None,
99101
'content': 'You are a scientist.',
100102
'role': 'system',
101103
'completion_id': None,
102104
'sequence': 0,
103-
'model': 'gpt-3.5-turbo-0613',
105+
'response.model': 'gpt-3.5-turbo-0613',
104106
'vendor': 'openAI',
105107
'ingest_source': 'Python'
106108
},
107109
),
108110
(
109111
{'type': 'LlmChatCompletionMessage'},
110112
{
111-
'id': None,
113+
'id': "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTemv-1",
114+
'appName': 'Python Agent Test (mlmodel_openai)',
115+
'request_id': "49dbbffbd3c3f4612aa48def69059ccd",
112116
'span_id': "span-id",
113117
'trace_id': "trace-id",
114118
'transaction_id': None,
115119
'content': 'What is 212 degrees Fahrenheit converted to Celsius?',
116120
'role': 'user',
117121
'completion_id': None,
118122
'sequence': 1,
119-
'model': 'gpt-3.5-turbo-0613',
123+
'response.model': 'gpt-3.5-turbo-0613',
120124
'vendor': 'openAI',
121125
'ingest_source': 'Python'
122126
},
123127
),
124128
(
125129
{'type': 'LlmChatCompletionMessage'},
126130
{
127-
'id': None,
131+
'id': "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTemv-2",
132+
'appName': 'Python Agent Test (mlmodel_openai)',
133+
'request_id': "49dbbffbd3c3f4612aa48def69059ccd",
128134
'span_id': "span-id",
129135
'trace_id': "trace-id",
130136
'transaction_id': None,
131137
'content': '212 degrees Fahrenheit is equal to 100 degrees Celsius.',
132138
'role': 'assistant',
133139
'completion_id': None,
134140
'sequence': 2,
135-
'model': 'gpt-3.5-turbo-0613',
141+
'response.model': 'gpt-3.5-turbo-0613',
136142
'vendor': 'openAI',
137143
'ingest_source': 'Python'
138144
}

0 commit comments

Comments
 (0)