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

get_openai_callback For Streaming Requests Returns 0's for Token Counts #21813

Open
5 tasks done
Thimows opened this issue May 17, 2024 · 1 comment
Open
5 tasks done
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: openai Primarily related to OpenAI integrations

Comments

@Thimows
Copy link

Thimows commented May 17, 2024

Checked other resources

  • I added a very descriptive title to this issue.
  • I searched the LangChain documentation with the integrated search.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

with get_openai_callback() as cb:
    async for event in agent_executor.astream_events(
        {
            "input": input,
            "chat_history": history
        },
        version="v1",
    ):

       # Do stuff....

        print(f"Total Tokens: {cb.total_tokens}")

Output:

Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0
Total Tokens: 0

Error Message and Stack Trace (if applicable)

No response

Description

OpenAI supports returning the token count while streaming: https://community.openai.com/t/usage-stats-now-available-when-using-streaming-with-the-chat-completions-api-or-completions-api/738156

However, this does not seem to work when using Langchain. It only seems to return 0's for agent_executor.astream_events.

This makes it very difficult to get the exact token counts for my agent. Are there any solutions?

System Info

langchain==0.1.16
langchain-community==0.0.38
langchain-core==0.1.52
langchain-experimental==0.0.55
langchain-openai==0.1.7
langchain-text-splitters==0.0.1
langchainhub==0.1.15

Platform: mac
Python version: 3.9.6

@dosubot dosubot bot added Ɑ: agent Related to agents module 🔌: openai Primarily related to OpenAI integrations 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature labels May 17, 2024
@eyurtsev
Copy link
Collaborator

This PR will resolve the issue: #21548

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ɑ: agent Related to agents module 🤖:bug Related to a bug, vulnerability, unexpected error with an existing feature 🔌: openai Primarily related to OpenAI integrations
Projects
None yet
Development

No branches or pull requests

2 participants