diff --git a/integrations/langfuse/example/chat.py b/integrations/langfuse/example/chat.py index a7408b118..9659a2b5e 100644 --- a/integrations/langfuse/example/chat.py +++ b/integrations/langfuse/example/chat.py @@ -31,3 +31,4 @@ data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "prompt_source": messages}} ) print(response["llm"]["replies"][0]) + print(response["tracer"]["trace_url"]) diff --git a/integrations/langfuse/tests/test_tracing.py b/integrations/langfuse/tests/test_tracing.py new file mode 100644 index 000000000..130bbec2d --- /dev/null +++ b/integrations/langfuse/tests/test_tracing.py @@ -0,0 +1,54 @@ +import os +from urllib.parse import urlparse + +import pytest +import requests + +from haystack import Pipeline +from haystack.components.builders import DynamicChatPromptBuilder +from haystack.components.generators.chat import OpenAIChatGenerator +from haystack.dataclasses import ChatMessage +from requests.auth import HTTPBasicAuth + +from haystack_integrations.components.others.langfuse import LangfuseComponent + + +@pytest.mark.integration +@pytest.mark.skipif( + not os.environ.get("LANGFUSE_SECRET_KEY", None) and not os.environ.get("LANGFUSE_PUBLIC_KEY", None), + reason="Export an env var called LANGFUSE_SECRET_KEY and LANGFUSE_PUBLIC_KEY containing Langfuse credentials.", + ) +def test_tracing_integration(): + + pipe = Pipeline() + pipe.add_component("tracer", LangfuseComponent("Chat example")) + pipe.add_component("prompt_builder", DynamicChatPromptBuilder()) + pipe.add_component("llm", OpenAIChatGenerator(model="gpt-3.5-turbo")) + + pipe.connect("prompt_builder.prompt", "llm.messages") + + messages = [ + ChatMessage.from_system("Always respond in German even if some input data is in other languages."), + ChatMessage.from_user("Tell me about {{location}}"), + ] + + response = pipe.run( + data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "prompt_source": messages}} + ) + assert "Berlin" in response["llm"]["replies"][0].content + assert response["tracer"]["trace_url"] + + url = "https://cloud.langfuse.com/api/public/traces/" + trace_url = response["tracer"]["trace_url"] + parsed_url = urlparse(trace_url) + # trace id is the last part of the path (after the last '/') + uuid = os.path.basename(parsed_url.path) + + try: + # GET request with Basic Authentication on the Langfuse API + response = requests.get(url+uuid, auth=HTTPBasicAuth(os.environ.get("LANGFUSE_PUBLIC_KEY"), + os.environ.get("LANGFUSE_SECRET_KEY"))) + + assert response.status_code == 200, f"Failed to retrieve data from Langfuse API: {response.status_code}" + except requests.exceptions.RequestException as e: + assert False, f"Failed to retrieve data from Langfuse API: {e}" \ No newline at end of file