-
Notifications
You must be signed in to change notification settings - Fork 241
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CM-8803] update open ai integration with open ai sdk version 1 (#93)
* Update result parser * Add deprecation logic * Update result parsing, add types * Update types * Add deprecation warning * Fix problem when patcher didn't catch deprecation exception * Remove deprecation warning, make some renamings in parsers, add unit tests for old openai version to a separate module * Fix lint errors
- Loading branch information
1 parent
3877fe0
commit 3cb5dd7
Showing
6 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- | ||
# ******************************************************* | ||
# ____ _ _ | ||
# / ___|___ _ __ ___ ___| |_ _ __ ___ | | | ||
# | | / _ \| '_ ` _ \ / _ \ __| | '_ ` _ \| | | ||
# | |__| (_) | | | | | | __/ |_ _| | | | | | | | ||
# \____\___/|_| |_| |_|\___|\__(_)_| |_| |_|_| | ||
# | ||
# Sign up for free at https://www.comet.com | ||
# Copyright (C) 2015-2023 Comet ML INC | ||
# This source code is licensed under the MIT license found in the | ||
# LICENSE file in the root directory of this package. | ||
# ******************************************************* | ||
|
||
import functools | ||
from typing import Optional | ||
|
||
|
||
@functools.lru_cache(maxsize=1) | ||
def openai_version() -> Optional[str]: | ||
try: | ||
import openai | ||
|
||
version: str = openai.__version__ | ||
return version | ||
except Exception: | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
tests/unit/autologgers/openai/test_chat_completion_parsers_openai_v0.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import box | ||
import pytest | ||
from testix import * | ||
|
||
from comet_llm.autologgers.openai import chat_completion_parsers | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def mock_imports(patch_module): | ||
patch_module(chat_completion_parsers, "metadata") | ||
|
||
def test_parse_create_result__input_is_openai_object__input_parsed_successfully(): | ||
create_result = Fake("create_result") | ||
with Scenario() as s: | ||
s.metadata.openai_version() >> "0.99.99" | ||
s.create_result.to_dict() >> { | ||
"choices": "the-choices", | ||
"some-key": "some-value", | ||
} | ||
|
||
outputs, metadata = chat_completion_parsers.parse_create_result(create_result) | ||
|
||
assert outputs == {"choices": "the-choices"} | ||
assert metadata == {"some-key": "some-value"} | ||
|
||
|
||
def test_parse_create_result__input_is_openai_object__input_parsed_successfully__model_key_renamed_to_output_model(): | ||
create_result = Fake("create_result") | ||
with Scenario() as s: | ||
s.metadata.openai_version() >> "0.99.99" | ||
s.create_result.to_dict() >> { | ||
"choices": "the-choices", | ||
"some-key": "some-value", | ||
"model": "the-model", | ||
} | ||
|
||
outputs, metadata = chat_completion_parsers.parse_create_result(create_result) | ||
|
||
assert outputs == {"choices": "the-choices"} | ||
assert metadata == {"some-key": "some-value", "output_model": "the-model"} | ||
|
||
|
||
def test_parse_create_result__input_is_generator_object__input_parsed_with_hardcoded_values_used(): | ||
create_result = (x for x in []) | ||
|
||
with Scenario() as s: | ||
s.metadata.openai_version() >> "0.99.99" | ||
outputs, metadata = chat_completion_parsers.parse_create_result(create_result) | ||
|
||
assert outputs == {"choices": "Generation is not logged when using stream mode"} | ||
assert metadata == {} |