Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for Azure generators #815

Merged
merged 1 commit into from
Jun 18, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
import langfuse

HAYSTACK_LANGFUSE_ENFORCE_FLUSH_ENV_VAR = "HAYSTACK_LANGFUSE_ENFORCE_FLUSH"
_SUPPORTED_GENERATORS = ["AzureOpenAIGenerator", "OpenAIGenerator"]
_SUPPORTED_CHAT_GENERATORS = ["AzureOpenAIChatGenerator", "OpenAIChatGenerator"]
_ALL_SUPPORTED_GENERATORS = _SUPPORTED_GENERATORS + _SUPPORTED_CHAT_GENERATORS


class LangfuseSpan(Span):
Expand Down Expand Up @@ -109,7 +112,7 @@ def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> I
tags = tags or {}
span_name = tags.get("haystack.component.name", operation_name)

if tags.get("haystack.component.type") in ["OpenAIGenerator", "OpenAIChatGenerator"]:
if tags.get("haystack.component.type") in _ALL_SUPPORTED_GENERATORS:
span = LangfuseSpan(self.current_span().raw_span().generation(name=span_name))
else:
span = LangfuseSpan(self.current_span().raw_span().span(name=span_name))
Expand All @@ -119,14 +122,14 @@ def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> I

yield span

if tags.get("haystack.component.type") == "OpenAIGenerator":
if tags.get("haystack.component.type") in _SUPPORTED_GENERATORS:
meta = span._data.get("haystack.component.output", {}).get("meta")
if meta:
# Haystack returns one meta dict for each message, but the 'usage' value
# is always the same, let's just pick the first item
m = meta[0]
span._span.update(usage=m.get("usage") or None, model=m.get("model"))
elif tags.get("haystack.component.type") == "OpenAIChatGenerator":
elif tags.get("haystack.component.type") in _SUPPORTED_CHAT_GENERATORS:
replies = span._data.get("haystack.component.output", {}).get("replies")
if replies:
meta = replies[0].meta
Expand Down