Skip to content

Commit 0bb34db

Browse files
committed
Prefix conversation id with llm (#1012)
* Change conversation_id->llm.conversation_id * Fixup formatting
1 parent b8c3a38 commit 0bb34db

9 files changed

+32
-29
lines changed

newrelic/hooks/external_botocore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,7 +511,7 @@ def handle_chat_completion_event(
511511
span_id,
512512
):
513513
custom_attrs_dict = transaction._custom_params
514-
conversation_id = custom_attrs_dict.get("conversation_id", "")
514+
conversation_id = custom_attrs_dict.get("llm.conversation_id", "")
515515

516516
chat_completion_id = str(uuid.uuid4())
517517

newrelic/hooks/mlmodel_openai.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def wrap_chat_completion_sync(wrapped, instance, args, kwargs):
193193

194194
# Get conversation ID off of the transaction
195195
custom_attrs_dict = transaction._custom_params
196-
conversation_id = custom_attrs_dict.get("conversation_id", "")
196+
conversation_id = custom_attrs_dict.get("llm.conversation_id", "")
197197

198198
settings = transaction.settings if transaction.settings is not None else global_settings()
199199
app_name = settings.app_name
@@ -654,7 +654,7 @@ async def wrap_chat_completion_async(wrapped, instance, args, kwargs):
654654

655655
# Get conversation ID off of the transaction
656656
custom_attrs_dict = transaction._custom_params
657-
conversation_id = custom_attrs_dict.get("conversation_id", "")
657+
conversation_id = custom_attrs_dict.get("llm.conversation_id", "")
658658

659659
settings = transaction.settings if transaction.settings is not None else global_settings()
660660
app_name = settings.app_name

tests/external_botocore/test_bedrock_chat_completion.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
chat_completion_expected_events,
2424
chat_completion_invalid_access_key_error_events,
2525
chat_completion_payload_templates,
26-
chat_completion_invalid_access_key_error_events,
2726
)
2827
from conftest import BOTOCORE_VERSION
2928
from testing_support.fixtures import (
@@ -128,7 +127,7 @@ def test_bedrock_chat_completion_in_txn_with_convo_id(set_trace_info, exercise_m
128127
@background_task(name="test_bedrock_chat_completion_in_txn_with_convo_id")
129128
def _test():
130129
set_trace_info()
131-
add_custom_attribute("conversation_id", "my-awesome-id")
130+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
132131
exercise_model(prompt=_test_bedrock_chat_completion_prompt, temperature=0.7, max_tokens=100)
133132

134133
_test()
@@ -160,7 +159,7 @@ def _test():
160159
@reset_core_stats_engine()
161160
@validate_custom_event_count(count=0)
162161
def test_bedrock_chat_completion_outside_txn(set_trace_info, exercise_model):
163-
add_custom_attribute("conversation_id", "my-awesome-id")
162+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
164163
exercise_model(prompt=_test_bedrock_chat_completion_prompt, temperature=0.7, max_tokens=100)
165164

166165

@@ -237,7 +236,7 @@ def test_bedrock_chat_completion_error_invalid_model(bedrock_server, set_trace_i
237236
@background_task(name="test_bedrock_chat_completion_error_invalid_model")
238237
def _test():
239238
set_trace_info()
240-
add_custom_attribute("conversation_id", "my-awesome-id")
239+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
241240
with pytest.raises(_client_error):
242241
bedrock_server.invoke_model(
243242
body=b"{}",
@@ -283,7 +282,7 @@ def _test():
283282

284283
with pytest.raises(_client_error): # not sure where this exception actually comes from
285284
set_trace_info()
286-
add_custom_attribute("conversation_id", "my-awesome-id")
285+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
287286
exercise_model(prompt="Invalid Token", temperature=0.7, max_tokens=100)
288287

289288
_test()

tests/mlmodel_openai/test_chat_completion.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
@background_task()
147147
def test_openai_chat_completion_sync_in_txn_with_convo_id(set_trace_info):
148148
set_trace_info()
149-
add_custom_attribute("conversation_id", "my-awesome-id")
149+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
150150
openai.ChatCompletion.create(
151151
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages, temperature=0.7, max_tokens=100
152152
)
@@ -272,7 +272,7 @@ def test_openai_chat_completion_sync_in_txn_no_convo_id(set_trace_info):
272272
@reset_core_stats_engine()
273273
@validate_custom_event_count(count=0)
274274
def test_openai_chat_completion_sync_outside_txn():
275-
add_custom_attribute("conversation_id", "my-awesome-id")
275+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
276276
openai.ChatCompletion.create(
277277
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages, temperature=0.7, max_tokens=100
278278
)
@@ -335,7 +335,7 @@ def test_openai_chat_completion_async_conversation_id_unset(loop, set_trace_info
335335
@background_task()
336336
def test_openai_chat_completion_async_conversation_id_set(loop, set_trace_info):
337337
set_trace_info()
338-
add_custom_attribute("conversation_id", "my-awesome-id")
338+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
339339

340340
loop.run_until_complete(
341341
openai.ChatCompletion.acreate(

tests/mlmodel_openai/test_chat_completion_error.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@
131131
def test_chat_completion_invalid_request_error_no_model(set_trace_info):
132132
with pytest.raises(openai.InvalidRequestError):
133133
set_trace_info()
134-
add_custom_attribute("conversation_id", "my-awesome-id")
134+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
135135
openai.ChatCompletion.create(
136136
# no model provided,
137137
messages=_test_openai_chat_completion_messages,
@@ -215,7 +215,7 @@ def test_chat_completion_invalid_request_error_no_model(set_trace_info):
215215
def test_chat_completion_invalid_request_error_invalid_model(set_trace_info):
216216
with pytest.raises(openai.InvalidRequestError):
217217
set_trace_info()
218-
add_custom_attribute("conversation_id", "my-awesome-id")
218+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
219219
openai.ChatCompletion.create(
220220
model="does-not-exist",
221221
messages=({"role": "user", "content": "Model does not exist."},),
@@ -315,7 +315,7 @@ def test_chat_completion_invalid_request_error_invalid_model(set_trace_info):
315315
def test_chat_completion_authentication_error(monkeypatch, set_trace_info):
316316
with pytest.raises(openai.error.AuthenticationError):
317317
set_trace_info()
318-
add_custom_attribute("conversation_id", "my-awesome-id")
318+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
319319
monkeypatch.setattr(openai, "api_key", None) # openai.api_key = None
320320
openai.ChatCompletion.create(
321321
model="gpt-3.5-turbo",
@@ -439,7 +439,7 @@ def test_chat_completion_wrong_api_key_error(monkeypatch, set_trace_info):
439439
def test_chat_completion_invalid_request_error_no_model_async(loop, set_trace_info):
440440
with pytest.raises(openai.InvalidRequestError):
441441
set_trace_info()
442-
add_custom_attribute("conversation_id", "my-awesome-id")
442+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
443443
loop.run_until_complete(
444444
openai.ChatCompletion.acreate(
445445
# no model provided,
@@ -481,7 +481,7 @@ def test_chat_completion_invalid_request_error_no_model_async(loop, set_trace_in
481481
def test_chat_completion_invalid_request_error_invalid_model_async(loop, set_trace_info):
482482
with pytest.raises(openai.InvalidRequestError):
483483
set_trace_info()
484-
add_custom_attribute("conversation_id", "my-awesome-id")
484+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
485485
loop.run_until_complete(
486486
openai.ChatCompletion.acreate(
487487
model="does-not-exist",
@@ -520,7 +520,7 @@ def test_chat_completion_invalid_request_error_invalid_model_async(loop, set_tra
520520
def test_chat_completion_authentication_error_async(loop, monkeypatch, set_trace_info):
521521
with pytest.raises(openai.error.AuthenticationError):
522522
set_trace_info()
523-
add_custom_attribute("conversation_id", "my-awesome-id")
523+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
524524
monkeypatch.setattr(openai, "api_key", None) # openai.api_key = None
525525
loop.run_until_complete(
526526
openai.ChatCompletion.acreate(

tests/mlmodel_openai/test_chat_completion_error_v1.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
def test_chat_completion_invalid_request_error_no_model(set_trace_info, sync_openai_client):
128128
with pytest.raises(TypeError):
129129
set_trace_info()
130-
add_custom_attribute("conversation_id", "my-awesome-id")
130+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
131131
sync_openai_client.chat.completions.create(
132132
messages=_test_openai_chat_completion_messages, temperature=0.7, max_tokens=100
133133
)
@@ -160,7 +160,7 @@ def test_chat_completion_invalid_request_error_no_model(set_trace_info, sync_ope
160160
def test_chat_completion_invalid_request_error_no_model_async(loop, set_trace_info, async_openai_client):
161161
with pytest.raises(TypeError):
162162
set_trace_info()
163-
add_custom_attribute("conversation_id", "my-awesome-id")
163+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
164164
loop.run_until_complete(
165165
async_openai_client.chat.completions.create(
166166
messages=_test_openai_chat_completion_messages, temperature=0.7, max_tokens=100
@@ -242,7 +242,7 @@ def test_chat_completion_invalid_request_error_no_model_async(loop, set_trace_in
242242
def test_chat_completion_invalid_request_error_invalid_model(set_trace_info, sync_openai_client):
243243
with pytest.raises(openai.NotFoundError):
244244
set_trace_info()
245-
add_custom_attribute("conversation_id", "my-awesome-id")
245+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
246246
sync_openai_client.chat.completions.create(
247247
model="does-not-exist",
248248
messages=({"role": "user", "content": "Model does not exist."},),
@@ -281,7 +281,7 @@ def test_chat_completion_invalid_request_error_invalid_model(set_trace_info, syn
281281
def test_chat_completion_invalid_request_error_invalid_model_async(loop, set_trace_info, async_openai_client):
282282
with pytest.raises(openai.NotFoundError):
283283
set_trace_info()
284-
add_custom_attribute("conversation_id", "my-awesome-id")
284+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
285285
loop.run_until_complete(
286286
async_openai_client.chat.completions.create(
287287
model="does-not-exist",

tests/mlmodel_openai/test_chat_completion_v1.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@
146146
@background_task()
147147
def test_openai_chat_completion_sync_in_txn_with_convo_id(set_trace_info, sync_openai_client):
148148
set_trace_info()
149-
add_custom_attribute("conversation_id", "my-awesome-id")
149+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
150150
sync_openai_client.chat.completions.create(
151151
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages, temperature=0.7, max_tokens=100
152152
)
@@ -272,7 +272,7 @@ def test_openai_chat_completion_sync_in_txn_no_convo_id(set_trace_info, sync_ope
272272
@reset_core_stats_engine()
273273
@validate_custom_event_count(count=0)
274274
def test_openai_chat_completion_sync_outside_txn(sync_openai_client):
275-
add_custom_attribute("conversation_id", "my-awesome-id")
275+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
276276
sync_openai_client.chat.completions.create(
277277
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages, temperature=0.7, max_tokens=100
278278
)
@@ -335,7 +335,7 @@ def test_openai_chat_completion_async_conversation_id_unset(loop, set_trace_info
335335
@background_task()
336336
def test_openai_chat_completion_async_conversation_id_set(loop, set_trace_info, async_openai_client):
337337
set_trace_info()
338-
add_custom_attribute("conversation_id", "my-awesome-id")
338+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
339339

340340
loop.run_until_complete(
341341
async_openai_client.chat.completions.create(

tests/mlmodel_openai/test_get_llm_message_ids.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@
1313
# limitations under the License.
1414

1515
import openai
16+
from testing_support.fixtures import (
17+
reset_core_stats_engine,
18+
validate_custom_event_count,
19+
)
20+
1621
from newrelic.api.background_task import background_task
1722
from newrelic.api.ml_model import get_llm_message_ids, record_llm_feedback_event
1823
from newrelic.api.transaction import add_custom_attribute, current_transaction
19-
from testing_support.fixtures import reset_core_stats_engine, validate_custom_event_count
2024

2125
_test_openai_chat_completion_messages_1 = (
2226
{"role": "system", "content": "You are a scientist."},
@@ -114,7 +118,7 @@ def test_get_llm_message_ids_outside_transaction():
114118
@background_task()
115119
def test_get_llm_message_ids_mulitple_async(loop, set_trace_info):
116120
set_trace_info()
117-
add_custom_attribute("conversation_id", "my-awesome-id")
121+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
118122

119123
async def _run():
120124
res1 = await openai.ChatCompletion.acreate(
@@ -172,7 +176,7 @@ async def _run():
172176
@background_task()
173177
def test_get_llm_message_ids_mulitple_sync(set_trace_info):
174178
set_trace_info()
175-
add_custom_attribute("conversation_id", "my-awesome-id")
179+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
176180

177181
results = openai.ChatCompletion.create(
178182
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages_1, temperature=0.7, max_tokens=100

tests/mlmodel_openai/test_get_llm_message_ids_v1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_get_llm_message_ids_outside_transaction():
116116
@background_task()
117117
def test_get_llm_message_ids_mulitple_async(loop, set_trace_info, async_openai_client):
118118
set_trace_info()
119-
add_custom_attribute("conversation_id", "my-awesome-id")
119+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
120120

121121
async def _run():
122122
res1 = await async_openai_client.chat.completions.create(
@@ -174,7 +174,7 @@ async def _run():
174174
@background_task()
175175
def test_get_llm_message_ids_mulitple_sync(set_trace_info, sync_openai_client):
176176
set_trace_info()
177-
add_custom_attribute("conversation_id", "my-awesome-id")
177+
add_custom_attribute("llm.conversation_id", "my-awesome-id")
178178

179179
results = sync_openai_client.chat.completions.create(
180180
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages_1, temperature=0.7, max_tokens=100

0 commit comments

Comments
 (0)