langfuse-haystack 0.3.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (20) hide show
  1. langfuse_haystack-0.4.0/CHANGELOG.md +39 -0
  2. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/PKG-INFO +1 -1
  3. langfuse_haystack-0.4.0/example/chat.py +54 -0
  4. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/pyproject.toml +2 -0
  5. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/src/haystack_integrations/tracing/langfuse/tracer.py +16 -2
  6. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/tests/test_tracing.py +31 -24
  7. langfuse_haystack-0.3.0/CHANGELOG.md +0 -19
  8. langfuse_haystack-0.3.0/example/chat.py +0 -27
  9. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/.gitignore +0 -0
  10. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/LICENSE.txt +0 -0
  11. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/README.md +0 -0
  12. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/example/basic_rag.py +0 -0
  13. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/example/requirements.txt +0 -0
  14. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/pydoc/config.yml +0 -0
  15. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/src/haystack_integrations/components/connectors/langfuse/__init__.py +0 -0
  16. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/src/haystack_integrations/components/connectors/langfuse/langfuse_connector.py +0 -0
  17. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/src/haystack_integrations/tracing/langfuse/__init__.py +0 -0
  18. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/tests/__init__.py +0 -0
  19. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/tests/test_langfuse_span.py +0 -0
  20. {langfuse_haystack-0.3.0 → langfuse_haystack-0.4.0}/tests/test_tracer.py +0 -0
@@ -0,0 +1,39 @@
1
+ # Changelog
2
+
3
+ ## [unreleased]
4
+
5
+ ### 🚜 Refactor
6
+
7
+ - Remove usage of deprecated `ChatMessage.to_openai_format` (#1001)
8
+
9
+ ### 📚 Documentation
10
+
11
+ - Add link to langfuse in LangfuseConnector (#981)
12
+
13
+ ### 🧪 Testing
14
+
15
+ - Do not retry tests in `hatch run test` command (#954)
16
+
17
+ ### ⚙️ Miscellaneous Tasks
18
+
19
+ - Retry tests to reduce flakyness (#836)
20
+ - `Langfuse` - replace DynamicChatPromptBuilder with ChatPromptBuilder (#925)
21
+ - Remove all `DynamicChatPromptBuilder` references in Langfuse integration (#931)
22
+
23
+ ## [integrations/langfuse-v0.2.0] - 2024-06-18
24
+
25
+ ## [integrations/langfuse-v0.1.0] - 2024-06-13
26
+
27
+ ### 🚀 Features
28
+
29
+ - Langfuse integration (#686)
30
+
31
+ ### 🐛 Bug Fixes
32
+
33
+ - Performance optimizations and value error when streaming in langfuse (#798)
34
+
35
+ ### ⚙️ Miscellaneous Tasks
36
+
37
+ - Use ChatMessage to_openai_format, update unit tests, pydocs (#725)
38
+
39
+ <!-- generated by git-cliff -->
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: langfuse-haystack
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Project-URL: Documentation, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/langfuse#readme
5
5
  Project-URL: Issues, https://github.com/deepset-ai/haystack-core-integrations/issues
6
6
  Project-URL: Source, https://github.com/deepset-ai/haystack-core-integrations/tree/main/integrations/langfuse
@@ -0,0 +1,54 @@
1
+ import os
2
+
3
+ # See README.md for more information on how to set up the environment variables
4
+ # before running this script
5
+
6
+ # In addition to setting the environment variables, you need to install the following packages:
7
+ # pip install cohere-haystack anthropic-haystack
8
+ os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
9
+
10
+ from haystack import Pipeline
11
+ from haystack.components.builders import ChatPromptBuilder
12
+ from haystack.components.generators.chat import HuggingFaceAPIChatGenerator, OpenAIChatGenerator
13
+ from haystack.dataclasses import ChatMessage
14
+ from haystack.utils.auth import Secret
15
+ from haystack.utils.hf import HFGenerationAPIType
16
+
17
+ from haystack_integrations.components.connectors.langfuse import LangfuseConnector
18
+ from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
19
+ from haystack_integrations.components.generators.cohere import CohereChatGenerator
20
+
21
+ os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
22
+
23
+ selected_chat_generator = "openai"
24
+
25
+ generators = {
26
+ "openai": OpenAIChatGenerator,
27
+ "anthropic": AnthropicChatGenerator,
28
+ "hf_api": lambda: HuggingFaceAPIChatGenerator(
29
+ api_type=HFGenerationAPIType.SERVERLESS_INFERENCE_API,
30
+ api_params={"model": "mistralai/Mixtral-8x7B-Instruct-v0.1"},
31
+ token=Secret.from_token(os.environ["HF_API_KEY"]),
32
+ ),
33
+ "cohere": CohereChatGenerator,
34
+ }
35
+
36
+ selected_chat_generator = generators[selected_chat_generator]()
37
+
38
+ if __name__ == "__main__":
39
+
40
+ pipe = Pipeline()
41
+ pipe.add_component("tracer", LangfuseConnector("Chat example"))
42
+ pipe.add_component("prompt_builder", ChatPromptBuilder())
43
+ pipe.add_component("llm", selected_chat_generator)
44
+
45
+ pipe.connect("prompt_builder.prompt", "llm.messages")
46
+
47
+ messages = [
48
+ ChatMessage.from_system("Always respond in German even if some input data is in other languages."),
49
+ ChatMessage.from_user("Tell me about {{location}}"),
50
+ ]
51
+
52
+ response = pipe.run(data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages}})
53
+ print(response["llm"]["replies"][0])
54
+ print(response["tracer"]["trace_url"])
@@ -47,6 +47,8 @@ dependencies = [
47
47
  "pytest",
48
48
  "pytest-rerunfailures",
49
49
  "haystack-pydoc-tools",
50
+ "anthropic-haystack",
51
+ "cohere-haystack"
50
52
  ]
51
53
  [tool.hatch.envs.default.scripts]
52
54
  test = "pytest {args:tests}"
@@ -10,8 +10,22 @@ from haystack.tracing import utils as tracing_utils
10
10
  import langfuse
11
11
 
12
12
  HAYSTACK_LANGFUSE_ENFORCE_FLUSH_ENV_VAR = "HAYSTACK_LANGFUSE_ENFORCE_FLUSH"
13
- _SUPPORTED_GENERATORS = ["AzureOpenAIGenerator", "OpenAIGenerator"]
14
- _SUPPORTED_CHAT_GENERATORS = ["AzureOpenAIChatGenerator", "OpenAIChatGenerator"]
13
+ _SUPPORTED_GENERATORS = [
14
+ "AzureOpenAIGenerator",
15
+ "OpenAIGenerator",
16
+ "AnthropicGenerator",
17
+ "HuggingFaceAPIGenerator",
18
+ "HuggingFaceLocalGenerator",
19
+ "CohereGenerator",
20
+ ]
21
+ _SUPPORTED_CHAT_GENERATORS = [
22
+ "AzureOpenAIChatGenerator",
23
+ "OpenAIChatGenerator",
24
+ "AnthropicChatGenerator",
25
+ "HuggingFaceAPIChatGenerator",
26
+ "HuggingFaceLocalChatGenerator",
27
+ "CohereChatGenerator",
28
+ ]
15
29
  _ALL_SUPPORTED_GENERATORS = _SUPPORTED_GENERATORS + _SUPPORTED_CHAT_GENERATORS
16
30
 
17
31
 
@@ -1,34 +1,38 @@
1
1
  import os
2
-
3
- # don't remove (or move) this env var setting from here, it's needed to turn tracing on
4
- os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
5
-
6
- from urllib.parse import urlparse
7
-
8
2
  import pytest
3
+ from urllib.parse import urlparse
9
4
  import requests
10
-
5
+ from requests.auth import HTTPBasicAuth
11
6
  from haystack import Pipeline
12
7
  from haystack.components.builders import ChatPromptBuilder
13
- from haystack.components.generators.chat import OpenAIChatGenerator
14
8
  from haystack.dataclasses import ChatMessage
15
- from requests.auth import HTTPBasicAuth
16
-
17
9
  from haystack_integrations.components.connectors.langfuse import LangfuseConnector
10
+ from haystack.components.generators.chat import OpenAIChatGenerator
11
+
12
+ from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
13
+ from haystack_integrations.components.generators.cohere import CohereChatGenerator
14
+
15
+ # don't remove (or move) this env var setting from here, it's needed to turn tracing on
16
+ os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
18
17
 
19
18
 
20
19
  @pytest.mark.integration
21
- @pytest.mark.skipif(
22
- not os.environ.get("LANGFUSE_SECRET_KEY", None) and not os.environ.get("LANGFUSE_PUBLIC_KEY", None),
23
- reason="Export an env var called LANGFUSE_SECRET_KEY and LANGFUSE_PUBLIC_KEY containing Langfuse credentials.",
20
+ @pytest.mark.parametrize(
21
+ "llm_class, env_var, expected_trace",
22
+ [
23
+ (OpenAIChatGenerator, "OPENAI_API_KEY", "OpenAI"),
24
+ (AnthropicChatGenerator, "ANTHROPIC_API_KEY", "Anthropic"),
25
+ (CohereChatGenerator, "COHERE_API_KEY", "Cohere"),
26
+ ],
24
27
  )
25
- def test_tracing_integration():
28
+ def test_tracing_integration(llm_class, env_var, expected_trace):
29
+ if not all([os.environ.get("LANGFUSE_SECRET_KEY"), os.environ.get("LANGFUSE_PUBLIC_KEY"), os.environ.get(env_var)]):
30
+ pytest.skip(f"Missing required environment variables: LANGFUSE_SECRET_KEY, LANGFUSE_PUBLIC_KEY, or {env_var}")
26
31
 
27
32
  pipe = Pipeline()
28
- pipe.add_component("tracer", LangfuseConnector(name="Chat example", public=True)) # public so anyone can verify run
33
+ pipe.add_component("tracer", LangfuseConnector(name=f"Chat example - {expected_trace}", public=True))
29
34
  pipe.add_component("prompt_builder", ChatPromptBuilder())
30
- pipe.add_component("llm", OpenAIChatGenerator(model="gpt-3.5-turbo"))
31
-
35
+ pipe.add_component("llm", llm_class())
32
36
  pipe.connect("prompt_builder.prompt", "llm.messages")
33
37
 
34
38
  messages = [
@@ -39,17 +43,20 @@ def test_tracing_integration():
39
43
  response = pipe.run(data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages}})
40
44
  assert "Berlin" in response["llm"]["replies"][0].content
41
45
  assert response["tracer"]["trace_url"]
46
+
42
47
  url = "https://cloud.langfuse.com/api/public/traces/"
43
48
  trace_url = response["tracer"]["trace_url"]
44
- parsed_url = urlparse(trace_url)
45
- # trace id is the last part of the path (after the last '/')
46
- uuid = os.path.basename(parsed_url.path)
49
+ uuid = os.path.basename(urlparse(trace_url).path)
50
+
47
51
  try:
48
- # GET request with Basic Authentication on the Langfuse API
49
52
  response = requests.get(
50
- url + uuid, auth=HTTPBasicAuth(os.environ.get("LANGFUSE_PUBLIC_KEY"), os.environ.get("LANGFUSE_SECRET_KEY"))
53
+ url + uuid, auth=HTTPBasicAuth(os.environ["LANGFUSE_PUBLIC_KEY"], os.environ["LANGFUSE_SECRET_KEY"])
51
54
  )
52
-
53
55
  assert response.status_code == 200, f"Failed to retrieve data from Langfuse API: {response.status_code}"
56
+
57
+ # check if the trace contains the expected LLM name
58
+ assert expected_trace in str(response.content)
59
+ # check if the trace contains the expected generation span
60
+ assert "GENERATION" in str(response.content)
54
61
  except requests.exceptions.RequestException as e:
55
- assert False, f"Failed to retrieve data from Langfuse API: {e}"
62
+ pytest.fail(f"Failed to retrieve data from Langfuse API: {e}")
@@ -1,19 +0,0 @@
1
- # Changelog
2
-
3
- ## [integrations/langfuse-v0.2.0] - 2024-06-18
4
-
5
- ## [integrations/langfuse-v0.1.0] - 2024-06-13
6
-
7
- ### 🚀 Features
8
-
9
- - Langfuse integration (#686)
10
-
11
- ### 🐛 Bug Fixes
12
-
13
- - Performance optimizations and value error when streaming in langfuse (#798)
14
-
15
- ### ⚙️ Miscellaneous Tasks
16
-
17
- - Use ChatMessage to_openai_format, update unit tests, pydocs (#725)
18
-
19
- <!-- generated by git-cliff -->
@@ -1,27 +0,0 @@
1
- import os
2
-
3
- os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
4
-
5
- from haystack import Pipeline
6
- from haystack.components.builders import ChatPromptBuilder
7
- from haystack.components.generators.chat import OpenAIChatGenerator
8
- from haystack.dataclasses import ChatMessage
9
- from haystack_integrations.components.connectors.langfuse import LangfuseConnector
10
-
11
- if __name__ == "__main__":
12
-
13
- pipe = Pipeline()
14
- pipe.add_component("tracer", LangfuseConnector("Chat example"))
15
- pipe.add_component("prompt_builder", ChatPromptBuilder())
16
- pipe.add_component("llm", OpenAIChatGenerator(model="gpt-3.5-turbo"))
17
-
18
- pipe.connect("prompt_builder.prompt", "llm.messages")
19
-
20
- messages = [
21
- ChatMessage.from_system("Always respond in German even if some input data is in other languages."),
22
- ChatMessage.from_user("Tell me about {{location}}"),
23
- ]
24
-
25
- response = pipe.run(data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages}})
26
- print(response["llm"]["replies"][0])
27
- print(response["tracer"]["trace_url"])