Skip to content

Commit 4619c90

Browse files
committed
Add tests
1 parent 1123c05 commit 4619c90

File tree

2 files changed

+160
-0
lines changed

2 files changed

+160
-0
lines changed

tests/mlmodel_openai/_mock_external_openai_server.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,39 @@
8888
"usage": {"completion_tokens": 11, "prompt_tokens": 53, "total_tokens": 64},
8989
},
9090
),
91+
"You are a mathematician.": (
92+
{
93+
"Content-Type": "application/json",
94+
"openai-model": "gpt-3.5-turbo-0613",
95+
"openai-organization": "new-relic-nkmd8b",
96+
"openai-processing-ms": "1469",
97+
"openai-version": "2020-10-01",
98+
"x-ratelimit-limit-requests": "200",
99+
"x-ratelimit-limit-tokens": "40000",
100+
"x-ratelimit-remaining-requests": "199",
101+
"x-ratelimit-remaining-tokens": "39940",
102+
"x-ratelimit-reset-requests": "7m12s",
103+
"x-ratelimit-reset-tokens": "90ms",
104+
"x-request-id": "49dbbffbd3c3f4612aa48def69059aad",
105+
},
106+
{
107+
"choices": [
108+
{
109+
"finish_reason": "stop",
110+
"index": 0,
111+
"message": {
112+
"content": "1 plus 2 is 3.",
113+
"role": "assistant",
114+
},
115+
}
116+
],
117+
"created": 1696888865,
118+
"id": "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTeat",
119+
"model": "gpt-3.5-turbo-0613",
120+
"object": "chat.completion",
121+
"usage": {"completion_tokens": 11, "prompt_tokens": 53, "total_tokens": 64},
122+
},
123+
),
91124
}
92125

93126

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Copyright 2010 New Relic, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import openai
16+
from testing_support.fixtures import reset_core_stats_engine
17+
18+
from newrelic.api.background_task import background_task
19+
from newrelic.api.ml_model import get_ai_message_ids
20+
from newrelic.api.transaction import add_custom_attribute, current_transaction
21+
22+
_test_openai_chat_completion_messages_1 = (
23+
{"role": "system", "content": "You are a scientist."},
24+
{"role": "user", "content": "What is 212 degrees Fahrenheit converted to Celsius?"},
25+
)
26+
_test_openai_chat_completion_messages_2 = (
27+
{"role": "system", "content": "You are a mathematician."},
28+
{"role": "user", "content": "What is 1 plus 2?"},
29+
)
30+
expected_message_ids_1 = [
31+
{
32+
"conversation_id": "my-awesome-id",
33+
"request_id": "49dbbffbd3c3f4612aa48def69059ccd",
34+
"message_id": "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTemv-0",
35+
},
36+
{
37+
"conversation_id": "my-awesome-id",
38+
"request_id": "49dbbffbd3c3f4612aa48def69059ccd",
39+
"message_id": "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTemv-1",
40+
},
41+
{
42+
"conversation_id": "my-awesome-id",
43+
"request_id": "49dbbffbd3c3f4612aa48def69059ccd",
44+
"message_id": "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTemv-2",
45+
},
46+
]
47+
expected_message_ids_2 = [
48+
{
49+
"conversation_id": "my-awesome-id",
50+
"request_id": "49dbbffbd3c3f4612aa48def69059aad",
51+
"message_id": "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTeat-0",
52+
},
53+
{
54+
"conversation_id": "my-awesome-id",
55+
"request_id": "49dbbffbd3c3f4612aa48def69059aad",
56+
"message_id": "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTeat-1",
57+
},
58+
{
59+
"conversation_id": "my-awesome-id",
60+
"request_id": "49dbbffbd3c3f4612aa48def69059aad",
61+
"message_id": "chatcmpl-87sb95K4EF2nuJRcTs43Tm9ntTeat-2",
62+
},
63+
]
64+
65+
66+
@reset_core_stats_engine()
67+
@background_task()
68+
def test_get_ai_message_ids_when_nr_message_ids_not_set():
69+
message_ids = get_ai_message_ids("request-id-1")
70+
assert message_ids == []
71+
72+
73+
@reset_core_stats_engine()
74+
def test_get_ai_message_ids_outside_transaction():
75+
message_ids = get_ai_message_ids("request-id-1")
76+
assert message_ids == []
77+
78+
79+
@reset_core_stats_engine()
80+
@background_task()
81+
def test_get_ai_message_ids_mulitple_async(loop, set_trace_info):
82+
set_trace_info()
83+
add_custom_attribute("conversation_id", "my-awesome-id")
84+
85+
async def _run():
86+
res1 = await openai.ChatCompletion.acreate(
87+
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages_1, temperature=0.7, max_tokens=100
88+
)
89+
res2 = await openai.ChatCompletion.acreate(
90+
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages_2, temperature=0.7, max_tokens=100
91+
)
92+
return [res1, res2]
93+
94+
results = loop.run_until_complete(_run())
95+
96+
message_ids = [m for m in get_ai_message_ids(results[0].id)]
97+
assert message_ids == expected_message_ids_1
98+
99+
message_ids = [m for m in get_ai_message_ids(results[1].id)]
100+
assert message_ids == expected_message_ids_2
101+
102+
# Make sure we aren't causing a memory leak.
103+
transaction = current_transaction()
104+
assert not transaction._nr_message_ids
105+
106+
107+
@reset_core_stats_engine()
108+
@background_task()
109+
def test_get_ai_message_ids_mulitple_sync(set_trace_info):
110+
set_trace_info()
111+
add_custom_attribute("conversation_id", "my-awesome-id")
112+
113+
results = openai.ChatCompletion.create(
114+
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages_1, temperature=0.7, max_tokens=100
115+
)
116+
message_ids = [m for m in get_ai_message_ids(results.id)]
117+
assert message_ids == expected_message_ids_1
118+
119+
results = openai.ChatCompletion.create(
120+
model="gpt-3.5-turbo", messages=_test_openai_chat_completion_messages_2, temperature=0.7, max_tokens=100
121+
)
122+
message_ids = [m for m in get_ai_message_ids(results.id)]
123+
assert message_ids == expected_message_ids_2
124+
125+
# Make sure we aren't causing a memory leak.
126+
transaction = current_transaction()
127+
assert not transaction._nr_message_ids

0 commit comments

Comments
 (0)