Skip to content
This repository has been archived by the owner on Jan 12, 2025. It is now read-only.

feat: Openai update #70

Merged
merged 5 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions examples/openai/audio.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import lunary
from openai import OpenAI
import os

client = OpenAI(
api_key=os.environ.get("OPENAI_API_KEY"),
)

lunary.monitor(client)

completion = client.chat.completions.create(
model="gpt-4o-audio-preview",
modalities=["audio", "text"],
audio={"voice": "alloy", "format": "wav"},
messages=[{"role": "user", "content": "Tell me a short story"}],
)

print(completion.choices[0])
17 changes: 0 additions & 17 deletions lunary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,23 +773,6 @@ def decorator(fn):

spans: Dict[str, Any] = {}

PARAMS_TO_CAPTURE = [
"temperature",
"top_p",
"top_k",
"stop",
"presence_penalty",
"frequency_penalty",
"seed",
"function_call",
"functions",
"tools",
"tool_choice",
"response_format",
"max_tokens",
"logit_bias",
]

class UserContextManager:
"""Context manager for Lunary user context."""

Expand Down
8 changes: 6 additions & 2 deletions lunary/openai_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"parallel_tool_calls"
]


class OpenAIUtils:
@staticmethod
def parse_role(role):
Expand All @@ -55,11 +54,15 @@ def parse_message(message):
]
tool_calls = tool_calls_serialized

audio = OpenAIUtils.get_property(message, "audio")
if audio is not None:
audio = json.loads(audio.model_dump_json(indent=2, exclude_unset=True))

parsed_message = {
"role": OpenAIUtils.get_property(message, "role"),
"content": OpenAIUtils.get_property(message, "content"),
"refusal": OpenAIUtils.get_property(message, "refusal"),
"audio": OpenAIUtils.get_property(message, "audio"),
"audio": audio,
"function_call": OpenAIUtils.get_property(message, "function_call"),
"tool_calls": tool_calls,
}
Expand All @@ -76,6 +79,7 @@ def parse_input(*args, **kwargs):
or kwargs.get("deployment_id", None)
)
extra = {key: kwargs[key] for key in MONITORED_KEYS if key in kwargs}

return {"name": name, "input": messages, "extra": extra}

@staticmethod
Expand Down
17 changes: 13 additions & 4 deletions lunary/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,33 @@ def flatten(self, obj, data):

PARAMS_TO_CAPTURE = [
"frequency_penalty",
"function_call",
"function_call",
"functions",
"logit_bias",
"logprobs",
"max_tokens",
"max_completion_tokens",
"n",
"presence_penalty",
"presence_penalty",
"response_format",
"seed",
"stop",
"stream",
"audio",
"modalities",
"temperature",
"tool_choice",
"tools",
"tool_calls",
"top_p",
"top_k",
"top_logprobs",
"top_p",
"prediction",
"service_tier",
"parallel_tool_calls",
# Additional params
"extra_headers",
"extra_query",
"extra_query",
"extra_body",
"timeout"
]
Expand Down
Loading