-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration test, update example
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}" |