-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[OPIK-64] SDK missing openai metadata in opik openai integration (#667)
* Update openai integration and tests for it * Fix lint errors * Re-implement chat completions chunks aggregator * Remove unused piece of code * Make _generators_handler method abstract * Add engineering docstrings * Extract backend emulator setup into separate fixture, refactor first batch of library integration tests accordingly * Update type hint to old style for old python versions * Improve usage logging * Add protection from errors raised during openai stream chunks aggregation
- Loading branch information
1 parent
b11a2a9
commit 4a3970d
Showing
11 changed files
with
1,026 additions
and
1,149 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
59 changes: 59 additions & 0 deletions
59
sdks/python/src/opik/integrations/openai/chat_completion_chunks_aggregator.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,59 @@ | ||
import logging | ||
from typing import List, Optional | ||
from openai.types.chat import chat_completion_chunk, chat_completion | ||
|
||
from opik import logging_messages | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def aggregate( | ||
items: List[chat_completion_chunk.ChatCompletionChunk], | ||
) -> Optional[chat_completion.ChatCompletion]: | ||
# TODO: check if there are scenarios when stream contains more than one choice | ||
try: | ||
first_chunk = items[0] | ||
|
||
aggregated_response = { | ||
"choices": [{"index": 0, "message": {"role": "", "content": ""}}], | ||
"id": first_chunk.id, | ||
"created": first_chunk.created, | ||
"model": first_chunk.model, | ||
"object": "chat.completion", | ||
"system_fingerprint": first_chunk.system_fingerprint, | ||
} | ||
|
||
text_chunks: List[str] = [] | ||
|
||
for chunk in items: | ||
if chunk.choices and chunk.choices[0].delta: | ||
delta = chunk.choices[0].delta | ||
|
||
if ( | ||
delta.role | ||
and not aggregated_response["choices"][0]["message"]["role"] | ||
): | ||
aggregated_response["choices"][0]["message"]["role"] = delta.role | ||
|
||
if delta.content: | ||
text_chunks.append(delta.content) | ||
|
||
if chunk.choices and chunk.choices[0].finish_reason: | ||
aggregated_response["choices"][0]["finish_reason"] = chunk.choices[ | ||
0 | ||
].finish_reason | ||
|
||
if chunk.usage: | ||
aggregated_response["usage"] = chunk.usage.model_dump() | ||
|
||
aggregated_response["choices"][0]["message"]["content"] = "".join(text_chunks) | ||
result = chat_completion.ChatCompletion(**aggregated_response) | ||
|
||
return result | ||
except Exception as exception: | ||
LOGGER.error( | ||
logging_messages.FAILED_TO_PARSE_OPENAI_STREAM_CONTENT, | ||
str(exception), | ||
exc_info=True, | ||
) | ||
return None |
64 changes: 0 additions & 64 deletions
64
sdks/python/src/opik/integrations/openai/chunks_aggregator.py
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.