Skip to content

Commit

Permalink
Langfuse: add invocation_context to identify traces
Browse files Browse the repository at this point in the history
  • Loading branch information
vblagoje committed Oct 1, 2024
1 parent 97094f7 commit 51d3a83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion integrations/langfuse/example/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
ChatMessage.from_user("Tell me about {{location}}"),
]

response = pipe.run(data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages}})
response = pipe.run(
data={
"prompt_builder": {
"template_variables": {"location": "Berlin"},
"template": messages,
},
"tracer": {
"invocation_context": {"some_key": "some_value"},
},
}
)
print(response["llm"]["replies"][0])
print(response["tracer"]["trace_url"])
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, Dict, Optional

from haystack import component, tracing

from haystack_integrations.tracing.langfuse import LangfuseTracer
Expand Down Expand Up @@ -105,12 +107,16 @@ def __init__(self, name: str, public: bool = False):
tracing.enable_tracing(self.tracer)

@component.output_types(name=str, trace_url=str)
def run(self):
def run(self, invocation_context: Optional[Dict[str, Any]] = None):
"""
Runs the LangfuseConnector component.
:param invocation_context: A dictionary with additional context for the invocation. This parameter is useful when
users want to mark this particular invocation with additional information, e.g. a run id from their own
execution framework, user id, etc. These key-value pairs are then visible in the Langfuse traces.
:returns: A dictionary with the following keys:
- `name`: The name of the tracing component.
- `trace_url`: The URL to the tracing data.
"""
# invocation_context is not used here but it is registered as input to the component in Langfuse.
return {"name": self.name, "trace_url": self.tracer.get_trace_url()}

0 comments on commit 51d3a83

Please sign in to comment.