Litellm openai audio streaming (#6325)

* refactor(main.py): streaming_chunk_builder

use <100 lines of code

refactor each component into a separate function - easier to maintain + test

* fix(utils.py): handle choices being None

openai pydantic schema updated

* fix(main.py): fix linting error

* feat(streaming_chunk_builder_utils.py): update stream chunk builder to support rebuilding audio chunks from openai

* test(test_custom_callback_input.py): test message redaction works for audio output

* fix(streaming_chunk_builder_utils.py): return anthropic token usage info directly

* fix(stream_chunk_builder_utils.py): run validation check before entering chunk processor

* fix(main.py): fix import
This commit is contained in:
Krish Dholakia 2024-10-19 16:16:51 -07:00 committed by GitHub
parent 979e8ea526
commit c58d542282
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 638 additions and 282 deletions

View file

@ -4,12 +4,13 @@ Common utility functions used for translating messages across providers
import json
from copy import deepcopy
from typing import Dict, List, Literal, Optional
from typing import Dict, List, Literal, Optional, Union
import litellm
from litellm.types.llms.openai import (
AllMessageValues,
ChatCompletionAssistantMessage,
ChatCompletionResponseMessage,
ChatCompletionUserMessage,
)
from litellm.types.utils import Choices, ModelResponse, StreamingChoices
@ -67,12 +68,18 @@ def convert_openai_message_to_only_content_messages(
return converted_messages
def get_content_from_model_response(response: ModelResponse) -> str:
def get_content_from_model_response(response: Union[ModelResponse, dict]) -> str:
"""
Gets content from model response
"""
if isinstance(response, dict):
new_response = ModelResponse(**response)
else:
new_response = response
content = ""
for choice in response.choices:
for choice in new_response.choices:
if isinstance(choice, Choices):
content += choice.message.content if choice.message.content else ""
if choice.message.function_call: