langfuse-haystack 0.4.0__tar.gz → 0.5.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 (18) hide show
  1. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/CHANGELOG.md +5 -1
  2. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/PKG-INFO +1 -1
  3. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/example/basic_rag.py +1 -0
  4. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/example/chat.py +11 -1
  5. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/pyproject.toml +9 -8
  6. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/src/haystack_integrations/components/connectors/langfuse/langfuse_connector.py +14 -2
  7. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/tests/test_langfuse_span.py +2 -0
  8. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/tests/test_tracer.py +1 -1
  9. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/tests/test_tracing.py +19 -5
  10. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/.gitignore +0 -0
  11. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/LICENSE.txt +0 -0
  12. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/README.md +0 -0
  13. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/example/requirements.txt +0 -0
  14. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/pydoc/config.yml +0 -0
  15. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/src/haystack_integrations/components/connectors/langfuse/__init__.py +0 -0
  16. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/src/haystack_integrations/tracing/langfuse/__init__.py +0 -0
  17. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/src/haystack_integrations/tracing/langfuse/tracer.py +0 -0
  18. {langfuse_haystack-0.4.0 → langfuse_haystack-0.5.0}/tests/__init__.py +0 -0
@@ -1,6 +1,10 @@
1
1
  # Changelog
2
2
 
3
- ## [unreleased]
3
+ ## [integrations/langfuse-v0.4.0] - 2024-09-17
4
+
5
+ ### 🚀 Features
6
+
7
+ - Langfuse - support generation span for more LLMs (#1087)
4
8
 
5
9
  ### 🚜 Refactor
6
10
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: langfuse-haystack
3
- Version: 0.4.0
3
+ Version: 0.5.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
@@ -10,6 +10,7 @@ from haystack.components.embedders import SentenceTransformersDocumentEmbedder,
10
10
  from haystack.components.generators import OpenAIGenerator
11
11
  from haystack.components.retrievers import InMemoryEmbeddingRetriever
12
12
  from haystack.document_stores.in_memory import InMemoryDocumentStore
13
+
13
14
  from haystack_integrations.components.connectors.langfuse import LangfuseConnector
14
15
 
15
16
 
@@ -49,6 +49,16 @@ if __name__ == "__main__":
49
49
  ChatMessage.from_user("Tell me about {{location}}"),
50
50
  ]
51
51
 
52
- response = pipe.run(data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages}})
52
+ response = pipe.run(
53
+ data={
54
+ "prompt_builder": {
55
+ "template_variables": {"location": "Berlin"},
56
+ "template": messages,
57
+ },
58
+ "tracer": {
59
+ "invocation_context": {"some_key": "some_value"},
60
+ },
61
+ }
62
+ )
53
63
  print(response["llm"]["replies"][0])
54
64
  print(response["tracer"]["trace_url"])
@@ -69,8 +69,8 @@ dependencies = ["black>=23.1.0", "mypy>=1.0.0", "ruff>=0.0.243"]
69
69
 
70
70
  [tool.hatch.envs.lint.scripts]
71
71
  typing = "mypy --install-types --non-interactive --explicit-package-bases {args:src/ tests}"
72
- style = ["ruff check {args:. --exclude tests/}", "black --check --diff {args:.}"]
73
- fmt = ["black {args:.}", "ruff --fix {args:. --exclude tests/}", "style"]
72
+ style = ["ruff check {args:}", "black --check --diff {args:.}"]
73
+ fmt = ["black {args:.}", "ruff check --fix {args:}", "style"]
74
74
  all = ["style", "typing"]
75
75
 
76
76
  [tool.hatch.metadata]
@@ -84,7 +84,10 @@ skip-string-normalization = true
84
84
  [tool.ruff]
85
85
  target-version = "py38"
86
86
  line-length = 120
87
- lint.select = [
87
+ exclude = ["example", "tests"]
88
+
89
+ [tool.ruff.lint]
90
+ select = [
88
91
  "A",
89
92
  "ARG",
90
93
  "B",
@@ -110,8 +113,7 @@ lint.select = [
110
113
  "W",
111
114
  "YTT",
112
115
  ]
113
-
114
- lint.ignore = [
116
+ ignore = [
115
117
  # Allow non-abstract empty methods in abstract base classes
116
118
  "B027",
117
119
  # Ignore checks for possible passwords
@@ -127,14 +129,13 @@ lint.ignore = [
127
129
  # Asserts
128
130
  "S101",
129
131
  ]
130
- lint.unfixable = [
132
+ unfixable = [
131
133
  # Don't touch unused imports
132
134
  "F401",
133
135
  ]
134
- extend-exclude = ["tests", "example"]
135
136
 
136
137
  [tool.ruff.lint.isort]
137
- known-first-party = ["src"]
138
+ known-first-party = ["haystack_integrations"]
138
139
 
139
140
  [tool.ruff.lint.flake8-tidy-imports]
140
141
  ban-relative-imports = "parents"
@@ -1,8 +1,12 @@
1
- from haystack import component, tracing
1
+ from typing import Any, Dict, Optional
2
+
3
+ from haystack import component, logging, tracing
2
4
 
3
5
  from haystack_integrations.tracing.langfuse import LangfuseTracer
4
6
  from langfuse import Langfuse
5
7
 
8
+ logger = logging.getLogger(__name__)
9
+
6
10
 
7
11
  @component
8
12
  class LangfuseConnector:
@@ -105,12 +109,20 @@ class LangfuseConnector:
105
109
  tracing.enable_tracing(self.tracer)
106
110
 
107
111
  @component.output_types(name=str, trace_url=str)
108
- def run(self):
112
+ def run(self, invocation_context: Optional[Dict[str, Any]] = None):
109
113
  """
110
114
  Runs the LangfuseConnector component.
111
115
 
116
+ :param invocation_context: A dictionary with additional context for the invocation. This parameter
117
+ is useful when users want to mark this particular invocation with additional information, e.g.
118
+ a run id from their own execution framework, user id, etc. These key-value pairs are then visible
119
+ in the Langfuse traces.
112
120
  :returns: A dictionary with the following keys:
113
121
  - `name`: The name of the tracing component.
114
122
  - `trace_url`: The URL to the tracing data.
115
123
  """
124
+ logger.debug(
125
+ "Langfuse tracer invoked with the following context: '{invocation_context}'",
126
+ invocation_context=invocation_context,
127
+ )
116
128
  return {"name": self.name, "trace_url": self.tracer.get_trace_url()}
@@ -3,7 +3,9 @@ import os
3
3
  os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"] = "true"
4
4
 
5
5
  from unittest.mock import Mock
6
+
6
7
  from haystack.dataclasses import ChatMessage
8
+
7
9
  from haystack_integrations.tracing.langfuse.tracer import LangfuseSpan
8
10
 
9
11
 
@@ -1,5 +1,5 @@
1
1
  import os
2
- from unittest.mock import Mock, MagicMock, patch
2
+ from unittest.mock import MagicMock, Mock, patch
3
3
 
4
4
  from haystack_integrations.tracing.langfuse.tracer import LangfuseTracer
5
5
 
@@ -1,14 +1,17 @@
1
1
  import os
2
- import pytest
2
+ import random
3
+ import time
3
4
  from urllib.parse import urlparse
5
+
6
+ import pytest
4
7
  import requests
5
- from requests.auth import HTTPBasicAuth
6
8
  from haystack import Pipeline
7
9
  from haystack.components.builders import ChatPromptBuilder
8
- from haystack.dataclasses import ChatMessage
9
- from haystack_integrations.components.connectors.langfuse import LangfuseConnector
10
10
  from haystack.components.generators.chat import OpenAIChatGenerator
11
+ from haystack.dataclasses import ChatMessage
12
+ from requests.auth import HTTPBasicAuth
11
13
 
14
+ from haystack_integrations.components.connectors.langfuse import LangfuseConnector
12
15
  from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
13
16
  from haystack_integrations.components.generators.cohere import CohereChatGenerator
14
17
 
@@ -40,10 +43,19 @@ def test_tracing_integration(llm_class, env_var, expected_trace):
40
43
  ChatMessage.from_user("Tell me about {{location}}"),
41
44
  ]
42
45
 
43
- response = pipe.run(data={"prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages}})
46
+ response = pipe.run(
47
+ data={
48
+ "prompt_builder": {"template_variables": {"location": "Berlin"}, "template": messages},
49
+ "tracer": {"invocation_context": {"user_id": "user_42"}},
50
+ }
51
+ )
44
52
  assert "Berlin" in response["llm"]["replies"][0].content
45
53
  assert response["tracer"]["trace_url"]
46
54
 
55
+ # add a random delay between 1 and 3 seconds to make sure the trace is flushed
56
+ # and that the trace is available in Langfuse when we fetch it below
57
+ time.sleep(random.uniform(1, 3))
58
+
47
59
  url = "https://cloud.langfuse.com/api/public/traces/"
48
60
  trace_url = response["tracer"]["trace_url"]
49
61
  uuid = os.path.basename(urlparse(trace_url).path)
@@ -58,5 +70,7 @@ def test_tracing_integration(llm_class, env_var, expected_trace):
58
70
  assert expected_trace in str(response.content)
59
71
  # check if the trace contains the expected generation span
60
72
  assert "GENERATION" in str(response.content)
73
+ # check if the trace contains the expected user_id
74
+ assert "user_42" in str(response.content)
61
75
  except requests.exceptions.RequestException as e:
62
76
  pytest.fail(f"Failed to retrieve data from Langfuse API: {e}")