Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

langfuse: value error for usage when streaming is enabled #797

Closed
Redna opened this issue Jun 10, 2024 · 0 comments · Fixed by #798
Closed

langfuse: value error for usage when streaming is enabled #797

Redna opened this issue Jun 10, 2024 · 0 comments · Fixed by #798
Assignees
Labels
bug Something isn't working

Comments

@Redna
Copy link
Contributor

Redna commented Jun 10, 2024

Describe the bug
When using streaming in the OpenAIGenerator or OpenAIChatgenerator, langfuse will throw an exception that it cannot convert an empty dict into usage input.

site-packages/langfuse/utils/__init__.py", line 101, in _convert_usage_input
    raise ValueError(
ValueError: Usage object must have either {input, output, total, unit} or {promptTokens, completionTokens, totalTokens}
Paris{'tracer': {'name': 'test_langfuse', 'trace_url': 'https://cloud.langfuse.com/trace/4f3dd665-52f1-4938-9882-680dfda94ee8'}, 'llm': {'replies': ['According to the given context, the capital of France is:\n\nParis'], 'meta': [{'model': 'gpt-3.5-turbo', 'index': 0, 'finish_reason': 'stop', 'usage': {}}]}}

To Reproduce

import os

os.environ["TOKENIZERS_PARALLELISM"] = "false"
os.environ["HAYSTACK_CONTENT_TRACING_ENABLED"]="true"
os.environ["LANGFUSE_SECRET_KEY"]="<secret_key>"
os.environ["LANGFUSE_PUBLIC_KEY"]="<public_key>"
os.environ["LANGFUSE_HOST"]="https://cloud.langfuse.com"


from haystack import Pipeline
from haystack.components.retrievers.in_memory import InMemoryBM25Retriever
from haystack.components.builders.prompt_builder import PromptBuilder
from haystack.components.generators import OpenAIGenerator
from haystack.document_stores.in_memory import InMemoryDocumentStore
from haystack import Document

from haystack.utils import Secret

from haystack_integrations.components.connectors.langfuse import LangfuseConnector


def print_callback(data):
    print(data.content, end="")

docstore = InMemoryDocumentStore()
docstore.write_documents([Document(content="Rome is the capital of Italy"), Document(content="Paris is the capital of France")])

query = "What is the capital of France?"

template = """
Given the following information, answer the question.

Context: 
{% for document in documents %}
    {{ document.content }}
{% endfor %}

Question: {{ query }}?
"""
pipe = Pipeline()

pipe.add_component("tracer", LangfuseConnector(name="test_langfuse"))

pipe.add_component("retriever", InMemoryBM25Retriever(document_store=docstore))
pipe.add_component("prompt_builder", PromptBuilder(template=template))
pipe.add_component("llm", OpenAIGenerator(api_key=Secret.from_token("<your-api-key>"),
                                          api_base_url="http://localhost:30091/v1",
                                          streaming_callback=print_callback))

pipe.connect("retriever", "prompt_builder.documents")
pipe.connect("prompt_builder", "llm")

res=pipe.run({
    "prompt_builder": {
        "query": query
    },
    "retriever": {
        "query": query
    }
})

print(res)

Describe your environment (please complete the following information):

  • OS: MacOS
  • Haystack version: 2.2.1
  • Integration version:
    langfuse-haystack 0.0.4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants