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

[BUG] DSPy connector not showing the token count, although it is returned in the request #3119

Open
DamianB-BitFlipper opened this issue May 8, 2024 · 15 comments
Labels
c/ui enhancement New feature or request

Comments

@DamianB-BitFlipper
Copy link

Describe the bug
I am querying DSPy with Phoenix as the UI to visualize the queries. This tool has been a real game changer! I cannot help but notice that the token counter is stuck at 0 at the top of the UI.

See:

image

This is although the response dict from my model has the information:

See:

image

To Reproduce
Here is the code I use to setup Phoenix with DSPy after installing both arize-phoenix v3.25.0 and openinference-instrumentation-dspy v0.1.7.

# Phoenix Setup
import phoenix as px
from openinference.instrumentation.dspy import DSPyInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import SimpleSpanProcessor

PHOENIX_ENDPOINT = "http://127.0.0.1:6006/v1/traces"
TRACER_PROVIDER = trace_sdk.TracerProvider()
SPAN_OTLP_EXPORTER = OTLPSpanExporter(endpoint=PHOENIX_ENDPOINT)
TRACER_PROVIDER.add_span_processor(
    SimpleSpanProcessor(span_exporter=SPAN_OTLP_EXPORTER)
)

trace_api.set_tracer_provider(tracer_provider=TRACER_PROVIDER)
DSPyInstrumentor().instrument()

 # Start the Phoenix app
 px.launch_app()

Expected behavior
The token count would be pulled from telemetry and displayed in the UI.

Environment (please complete the following information):

  • OS: MacOS
  • Browser Brave

Thanks again for this amazing tool!

@DamianB-BitFlipper DamianB-BitFlipper added bug Something isn't working triage issues that need triage labels May 8, 2024
Copy link

dosubot bot commented May 8, 2024

It looks like the issue with the token count not displaying in the Phoenix UI might be related to how the token count data is being handled and passed to the UI components. The Phoenix UI expects tokenCountTotal, tokenCountPrompt, and tokenCountCompletion properties to be of type number and explicitly passed to the SpanItem component. If tokenCountTotal is not a number, the token count will not be displayed.

Ensure that the data from your model's response is correctly formatted and passed to the Phoenix backend. The backend should update the token count in the _Spans class using the SpanAttributes.LLM_TOKEN_COUNT_TOTAL attribute. This update happens in the _update_cached_statistics method, where it adds the token count from the span's attributes to the _token_count_total.

If the token count is correctly updated on the backend but still not displayed, check the data passed to the SpanItem component to ensure it's correctly formatted and not null or undefined. This verification will help identify if the issue lies in the data handling or the UI component rendering logic.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@dosubot dosubot bot added the c/ui label May 8, 2024
@axiomofjoy
Copy link
Contributor

@DamianB-BitFlipper Thanks for trying Phoenix. For DSPy, we do not grab token counts from the DSPy native spans themselves. Can you try instrumenting your application with our OpenAI instrumentation in addition to the DSPy instrumentation and see if that helps? https://github.com/Arize-ai/openinference/tree/main/python/instrumentation/openinference-instrumentation-openai#openinference-openai-instrumentation

@DamianB-BitFlipper
Copy link
Author

Thanks for the reply. What do you mean by "instrumenting your application". Do you mean simply calling: OpenAIInstrumentor().instrument()?

@DamianB-BitFlipper
Copy link
Author

Follow up: Adding OpenAIInstrumentor().instrument() does not affect anything.

Copy link

dosubot bot commented May 8, 2024

To address the issue with the token count not displaying in the Phoenix UI, ensure the following steps are correctly implemented:

  1. Model's Response Formatting: Verify that your model's response includes the token count as an integer value. This is crucial for Phoenix to process the data correctly.

  2. Passing Token Count to Phoenix Backend: When sending data to the Phoenix backend, include the token count in the span's attributes using the LLM_TOKEN_COUNT_TOTAL, LLM_TOKEN_COUNT_PROMPT, and LLM_TOKEN_COUNT_COMPLETION attributes. For example:

    span_attributes[SpanAttributes.LLM_TOKEN_COUNT_TOTAL] = model_response_token_count
  3. Updating the _Spans Class: Ensure that the token count is correctly updated in the _Spans class. When creating or updating a Span instance, include the token count from your model's response:

    span.token_count_total = model_response_token_count
  4. Phoenix UI Integration: The Phoenix UI is designed to display the token count if it's provided as a number. Make sure your backend correctly passes the tokenCountTotal, tokenCountPrompt, and tokenCountCompletion as part of the SpanItemProps to the SpanItem component. This ensures the UI component can render the token count correctly.

By following these steps, you should be able to resolve the issue with the token count not displaying in the Phoenix UI. Ensure that each step is correctly implemented and that the token count values are passed as numbers from the backend to the UI components.

Details

Sources


About Dosu
This response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research.

Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

@axiomofjoy
Copy link
Contributor

Follow up: Adding OpenAIInstrumentor().instrument() does not affect anything.

@DamianB-BitFlipper Here is a code snippet.

import os

import dspy
from openinference.instrumentation.dspy import DSPyInstrumentor
from openinference.instrumentation.openai import OpenAIInstrumentor
from opentelemetry import trace as trace_api
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor

# Logs to the Phoenix Collector if running locally
endpoint = "http://localhost:6006/v1/traces"
if os.environ.get("PHOENIX_COLLECTOR_ENDPOINT"):
    endpoint = os.environ["PHOENIX_COLLECTOR_ENDPOINT"] + "/v1/traces"

resource = Resource(attributes={})
tracer_provider = trace_sdk.TracerProvider(resource=resource)
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))

trace_api.set_tracer_provider(tracer_provider=tracer_provider)


DSPyInstrumentor().instrument()
OpenAIInstrumentor().instrument()


class BasicQA(dspy.Signature):
    """Answer questions with short factoid answers."""

    question = dspy.InputField()
    answer = dspy.OutputField(desc="often between 1 and 5 words")


if __name__ == "__main__":
    turbo = dspy.OpenAI(model="gpt-3.5-turbo")

    dspy.settings.configure(lm=turbo)

    # Define the predictor.
    generate_answer = dspy.Predict(BasicQA)

    # Call the predictor on a particular input.
    pred = generate_answer(
        question="What is the capital of the united states?"  # noqa: E501
    )  # noqa: E501
    print(f"Predicted Answer: {pred.answer}")

@axiomofjoy
Copy link
Contributor

axiomofjoy commented May 8, 2024

Requirements:

openai
openinference-instrumentation-dspy
openinference-instrumentation-openai
opentelemetry-exporter-otlp-proto-http

@DamianB-BitFlipper
Copy link
Author

Thanks for the quick reply. I tried exactly your code. I had to add phoenix.launch_app() however to get the application up and running. It still does not work.

See:
image

I fear that there may not be a ready solution, and that this may be an issue that needs to be addressed in the DSPy instrumentation. What do you think.

I am running the latest versions of everything:

arize-phoenix==3.25.0
openai==1.26.0
openinference-instrumentation-openai==0.1.4
dspy-ai==2.4.9
openinference-instrumentation-dspy==0.1.7
opentelemetry-exporter-otlp-proto-http==1.24.0

@axiomofjoy
Copy link
Contributor

When you expand the trace, do you see a new ChatCompletion LLM span?

Screenshot 2024-05-08 at 3 27 02 PM

@DamianB-BitFlipper
Copy link
Author

I believe yes:

image

And it started showing the total tokens, for some reason. Thanks for the assistance! Maybe this warrants an update to the docs of DSPy integration? If you look closely, even the video you show on the docs shows 0 tokens.

Thanks once again!

@DamianB-BitFlipper
Copy link
Author

For posterity's record, the changes I made were adding:

tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
OpenAIInstrumentor().instrument()

@axiomofjoy axiomofjoy added enhancement New feature or request and removed bug Something isn't working labels May 9, 2024
@axiomofjoy
Copy link
Contributor

Thanks @DamianB-BitFlipper, glad you're unblocked. I've changed the label from bug to enhancement. We probably want those token counts showing up without the OpenAI instrumentation, as you pointed out.

@DamianB-BitFlipper
Copy link
Author

Thank you!

@axiomofjoy axiomofjoy removed the triage issues that need triage label May 9, 2024
@duxtinto
Copy link

duxtinto commented May 19, 2024

Hi!

I'm using dspy-ai[chromadb]==2.4.9 in Ubuntu, and even thought at the beginning I could not see any ChatCompletion traces after enabling the openai instrumentation, then suddenly, it started to work.

After some research, I think that dspy has a cache for the requests to the openai gpt api, and no trace is recorded whenever the cache is hit.

image

@DamianB-BitFlipper
Copy link
Author

DamianB-BitFlipper commented May 19, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c/ui enhancement New feature or request
Projects
Status: 📘 Todo
Development

No branches or pull requests

3 participants