Skip to content

Commit

Permalink
Add integration test, update example
Browse files Browse the repository at this point in the history
  • Loading branch information
vblagoje committed Apr 24, 2024
1 parent 17f3754 commit 1540227
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions integrations/langfuse/example/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "prompt_source": messages}}
)
print(response["llm"]["replies"][0])
print(response["tracer"]["trace_url"])
54 changes: 54 additions & 0 deletions integrations/langfuse/tests/test_tracing.py
Original file line number Diff line number Diff line change
@@ -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}"

0 comments on commit 1540227

Please sign in to comment.