Skip to content

Commit

Permalink
keep openai format on audio
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp committed Nov 14, 2024
1 parent a810062 commit a590e15
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions langfuse/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from collections import defaultdict
from dataclasses import dataclass
from inspect import isclass
from typing import List, Optional
from typing import Optional


import openai.resources
Expand Down Expand Up @@ -256,7 +256,7 @@ def _extract_chat_response(kwargs: any):
"role": kwargs.get("role", None),
}

audio_content = None
audio = None

if kwargs.get("function_call") is not None:
response.update({"function_call": kwargs["function_call"]})
Expand All @@ -267,30 +267,21 @@ def _extract_chat_response(kwargs: any):
if kwargs.get("audio") is not None:
audio = kwargs["audio"].__dict__

base64_data_uri = (
f"data:audio/{audio.get('format', 'wav')};base64,{audio.get('data', None)}"
)

audio_content = [
{"type": "text", "text": audio.get("transcript", None)},
{
"type": "output_audio",
"output_audio": {
"data": LangfuseMedia(
None,
base64_data_uri=base64_data_uri,
),
"format": audio.get("format", "wav"),
},
},
]
if "data" in audio and audio["data"] is not None:
# base64_data_uri is detected in ingestion_consumer and handled accordingly
audio["data"] = (
f"data:audio/{audio.get('format', 'wav')};base64,{audio.get('data', None)}"
)

response.update(
{
"content": kwargs.get("content", None) or audio_content,
"content": kwargs.get("content", None),
}
)

if audio is not None:
response.update({"audio": audio})

return response


Expand Down

0 comments on commit a590e15

Please sign in to comment.