langfuse-haystack 0.0.1__tar.gz → 0.0.2__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: langfuse-haystack
3
- Version: 0.0.1
3
+ Version: 0.0.2
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
@@ -23,20 +23,21 @@ Description-Content-Type: text/markdown
23
23
 
24
24
  # langfuse
25
25
 
26
- [![PyPI - Version](https://img.shields.io/pypi/v/langfuse.svg)](https://pypi.org/project/langfuse)
27
- [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/langfuse.svg)](https://pypi.org/project/langfuse)
26
+ [![PyPI - Version](https://img.shields.io/pypi/v/langfuse-haystack.svg)](https://pypi.org/project/langfuse-haystack)
27
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/langfuse-haystack.svg)](https://pypi.org/project/langfuse-haystack)
28
28
 
29
29
  -----
30
30
 
31
31
  **Table of Contents**
32
32
 
33
- - [Installation](#installation)
34
- - [License](#license)
33
+ - [langfuse](#langfuse)
34
+ - [Installation](#installation)
35
+ - [License](#license)
35
36
 
36
37
  ## Installation
37
38
 
38
39
  ```console
39
- pip install langfuse
40
+ pip install langfuse-haystack
40
41
  ```
41
42
 
42
43
  ## License
@@ -0,0 +1,22 @@
1
+ # langfuse
2
+
3
+ [![PyPI - Version](https://img.shields.io/pypi/v/langfuse-haystack.svg)](https://pypi.org/project/langfuse-haystack)
4
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/langfuse-haystack.svg)](https://pypi.org/project/langfuse-haystack)
5
+
6
+ -----
7
+
8
+ **Table of Contents**
9
+
10
+ - [langfuse](#langfuse)
11
+ - [Installation](#installation)
12
+ - [License](#license)
13
+
14
+ ## Installation
15
+
16
+ ```console
17
+ pip install langfuse-haystack
18
+ ```
19
+
20
+ ## License
21
+
22
+ `langfuse` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.
@@ -41,7 +41,7 @@ def get_pipeline(document_store: InMemoryDocumentStore):
41
41
  )
42
42
  basic_rag_pipeline.add_component("retriever", retriever)
43
43
  basic_rag_pipeline.add_component("prompt_builder", prompt_builder)
44
- basic_rag_pipeline.add_component("llm", OpenAIGenerator(model="gpt-3.5-turbo"))
44
+ basic_rag_pipeline.add_component("llm", OpenAIGenerator(model="gpt-3.5-turbo", generation_kwargs={"n": 2}))
45
45
 
46
46
  # Now, connect the components to each other
47
47
  # NOTE: the tracer component doesn't need to be connected to anything in order to work
@@ -0,0 +1,3 @@
1
+ langfuse-haystack
2
+ datasets
3
+ sentence-transformers
@@ -3,7 +3,8 @@ from haystack import tracing, component
3
3
  from haystack_integrations.tracing.langfuse import LangfuseTracer
4
4
 
5
5
  from langfuse import Langfuse
6
- from langfuse.openai import openai # noqa
6
+
7
+ # from langfuse.openai import openai # noqa
7
8
 
8
9
 
9
10
  @component
@@ -1,7 +1,6 @@
1
1
  import contextlib
2
2
  from typing import Any, Dict, Iterator, Optional, Union
3
3
 
4
- from haystack.lazy_imports import LazyImport
5
4
  from haystack.tracing import Span, Tracer, tracer
6
5
  from haystack.tracing import utils as tracing_utils
7
6
 
@@ -11,10 +10,13 @@ import langfuse
11
10
  class LangfuseSpan(Span):
12
11
  def __init__(self, span: "Union[langfuse.client.StatefulSpanClient, langfuse.client.StatefulTraceClient]") -> None:
13
12
  self._span = span
13
+ # locally cache tags
14
+ self._data = {}
14
15
 
15
16
  def set_tag(self, key: str, value: Any) -> None:
16
17
  coerced_value = tracing_utils.coerce_tag_value(value)
17
18
  self._span.update(metadata={key: coerced_value})
19
+ self._data[key] = value
18
20
 
19
21
  def set_content_tag(self, key: str, value: Any) -> None:
20
22
  if not tracer.is_content_tracing_enabled:
@@ -25,6 +27,8 @@ class LangfuseSpan(Span):
25
27
  elif key.endswith(".output"):
26
28
  self._span.update(output=value)
27
29
 
30
+ self._data[key] = value
31
+
28
32
  def raw_span(self) -> Any:
29
33
  return self._span
30
34
 
@@ -42,9 +46,11 @@ class LangfuseTracer(Tracer):
42
46
  def trace(self, operation_name: str, tags: Optional[Dict[str, Any]] = None) -> Iterator[Span]:
43
47
  tags = tags or {}
44
48
  span_name = tags.get("haystack.component.name", operation_name)
49
+ is_generation = False
45
50
 
46
51
  if tags.get("haystack.component.type") in ["OpenAIGenerator"]:
47
52
  span = LangfuseSpan(self.current_span().raw_span().generation(name=span_name))
53
+ is_generation = True
48
54
  else:
49
55
  span = LangfuseSpan(self.current_span().raw_span().span(name=span_name))
50
56
 
@@ -53,6 +59,14 @@ class LangfuseTracer(Tracer):
53
59
 
54
60
  yield span
55
61
 
62
+ if is_generation:
63
+ meta = span._data.get("haystack.component.output", {}).get("meta")
64
+ if meta:
65
+ # Haystack returns one meta dict for each message, but the 'usage' value
66
+ # is always the same, let's just pick the first item
67
+ m = meta[0]
68
+ span._span.update(usage=m.get("usage"), model=m.get("model"))
69
+
56
70
  span.raw_span().end()
57
71
  self._context.pop()
58
72
  self._tracer.flush()
@@ -1,21 +0,0 @@
1
- # langfuse
2
-
3
- [![PyPI - Version](https://img.shields.io/pypi/v/langfuse.svg)](https://pypi.org/project/langfuse)
4
- [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/langfuse.svg)](https://pypi.org/project/langfuse)
5
-
6
- -----
7
-
8
- **Table of Contents**
9
-
10
- - [Installation](#installation)
11
- - [License](#license)
12
-
13
- ## Installation
14
-
15
- ```console
16
- pip install langfuse
17
- ```
18
-
19
- ## License
20
-
21
- `langfuse` is distributed under the terms of the [Apache-2.0](https://spdx.org/licenses/Apache-2.0.html) license.
@@ -1,2 +0,0 @@
1
- datasets
2
- sentence-transformers