Skip to content

Commit

Permalink
Add OpenAI Test Infrastructure (newrelic#926)
Browse files Browse the repository at this point in the history
* Add openai to tox

* Add OpenAI test files.

* Add test functions.

* [Mega-Linter] Apply linters fixes

---------

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: mergify[bot] <mergify[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 2, 2023
1 parent 17cd48d commit db63d45
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 0 deletions.
81 changes: 81 additions & 0 deletions tests/mlmodel_openai/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# 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)
48 changes: 48 additions & 0 deletions tests/mlmodel_openai/test_chat_completion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# 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
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ envlist =
python-framework_starlette-{py37,py38}-starlette{002001},
python-framework_starlette-{py37,py38,py39,py310,py311,pypy38}-starlettelatest,
python-framework_strawberry-{py37,py38,py39,py310,py311}-strawberrylatest,
python-mlmodel_openai-{py37,py38,py39,py310,py311,pypy38},
python-logger_logging-{py27,py37,py38,py39,py310,py311,pypy27,pypy38},
python-logger_loguru-{py37,py38,py39,py310,py311,pypy38}-logurulatest,
python-logger_loguru-py39-loguru{06,05},
Expand Down Expand Up @@ -341,6 +342,7 @@ deps =
framework_tornado: pycurl
framework_tornado-tornadolatest: tornado
framework_tornado-tornadomaster: https://github.com/tornadoweb/tornado/archive/master.zip
mlmodel_openai: openai
logger_loguru-logurulatest: loguru
logger_loguru-loguru06: loguru<0.7
logger_loguru-loguru05: loguru<0.6
Expand Down Expand Up @@ -460,6 +462,7 @@ changedir =
framework_starlette: tests/framework_starlette
framework_strawberry: tests/framework_strawberry
framework_tornado: tests/framework_tornado
mlmodel_openai: tests/mlmodel_openai
logger_logging: tests/logger_logging
logger_loguru: tests/logger_loguru
logger_structlog: tests/logger_structlog
Expand Down

0 comments on commit db63d45

Please sign in to comment.