Skip to content

Commit

Permalink
Add test functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
umaannamalai committed Sep 22, 2023
1 parent 093a1ca commit d9d6259
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
92 changes: 92 additions & 0 deletions tests/mlmodel_openai/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
from openai.util import convert_to_openai_object

from testing_support.fixtures import ( # noqa: F401, pylint: disable=W0611
collector_agent_registration_fixture,
collector_available_fixture,
)

_default_settings = {
"transaction_tracer.explain_threshold": 0.0,
"transaction_tracer.transaction_threshold": 0.0,
"transaction_tracer.stack_trace_threshold": 0.0,
"debug.log_data_collector_payloads": True,
"debug.record_transaction_failure": True,
"ml_insights_event.enabled": True,
}
collector_agent_registration = collector_agent_registration_fixture(
app_name="Python Agent Test (mlmodel_openai)",
default_settings=_default_settings,
linked_applications=["Python Agent Test (mlmodel_openai)"],
)


@pytest.fixture(autouse=True)
def openai_chat_completion_dict():
return {
"choices": [
{
"finish_reason": "stop",
"index": 0,
"message": {
"content": "212 degrees Fahrenheit is 100 degrees Celsius.",
"role": "assistant"
}
}
],
"created": 1676917710,
"id": "some-test-id-123456789",
"model": "gpt-3.5-turbo-0613",
"object": "chat.completion",
"usage": {
"completion_tokens": 7,
"prompt_tokens": 3,
"total_tokens": 10
},
}


@pytest.fixture(autouse=True)
def openai_embedding_dict():
return {
"data": [
{
"embedding": [
-0.006929283495992422,
-0.005336422007530928,
],
"index": 0,
"object": "embedding"
}
],
"model": "text-embedding-ada-002",
"object": "list",
"usage": {
"prompt_tokens": 5,
"total_tokens": 5
}
}


@pytest.fixture(autouse=True)
def openai_chat_completion_object(openai_chat_completion_dict):
return convert_to_openai_object(openai_chat_completion_dict)


@pytest.fixture(autouse=True)
def openai_embedding_object(openai_embedding_dict):
return convert_to_openai_object(openai_embedding_dict)
45 changes: 45 additions & 0 deletions tests/mlmodel_openai/test_chat_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import openai
import pytest


@pytest.fixture
def run_openai_chat_completion_sync():
openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a scientist."},
{"role": "user", "content": "What is the boiling point of water?"},
{"role": "assistant", "content": "The boiling point of water is 212 degrees Fahrenheit."},
{"role": "user", "content": "What is 212 degrees Fahrenheit converted to Celsius?"},
]
)


@pytest.fixture
def run_openai_chat_completion_async():
openai.ChatCompletion.acreate(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a scientist."},
{"role": "user", "content": "What is the boiling point of water?"},
{"role": "assistant", "content": "The boiling point of water is 212 degrees Fahrenheit or 100 degrees Celsius."},
]
)


def test_no_harm():
pass
30 changes: 30 additions & 0 deletions tests/mlmodel_openai/test_embeddings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2010 New Relic, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import openai
import pytest


@pytest.fixture
def run_openai_embedding_sync():
embedding = openai.Embedding.create(input="This is a test.", model="text-embedding-ada-002")


@pytest.fixture
def run_openai_embedding_async():
embedding = openai.Embedding.acreate(input="This is a test.", model="text-embedding-ada-002")


def test_no_harm():
pass

0 comments on commit d9d6259

Please sign in to comment.