Sunday, 23 Jun 2024

Can’t Get a Token Count: Troubleshooting Python/Flask Langchain RetrievalQA Chatbot

how much is a token on cb

Have you ever encountered the frustrating situation where you can’t seem to get a token count from the API for your Python/Flask Langchain RetrievalQA chatbot? It’s a common issue that many developers face, but fear not! In this article, we will delve into troubleshooting this problem and find a solution that works for you.

The Challenge: Counting Tokens

The challenge at hand is that the API doesn’t provide a token count, preventing you from deducting credits from your user’s accounts. While your deduct_credits function works fine, you need a way to reliably obtain the token count for each API call. Let’s explore some possible solutions.

Testing Callback Code

One approach is to test the callback code just after initializing your agent. By running the code snippet below, you can obtain the RetrievalQA chains token count in your terminal:

agent = initialize_agent(agent="conversational-react-description", llm=llm, tools=tools, verbose=True, max_iterations=1, early_stopping_method='generate', handle_parsing_errors=True, memory=memory, persona=persona)

with get_openai_callback() as cb:
    response ="Who is Olivia Wilde’s boyfriend? What is his current age raised to the 0.23 power?")
    print(f"Total Tokens: {cb.total_tokens}")
    print(f"Prompt Tokens: {cb.prompt_tokens}")
    print(f"Completion Tokens: {cb.completion_tokens}")
    print(f"Total Cost (USD): ${cb.total_cost}")

This method provides a convenient way to track the token count, but it doesn’t fulfill the requirement of monitoring user interactions with the chatbot. To achieve that, we need to integrate the callback into the /chat endpoint.

Tham Khảo Thêm:  Can I Unlock Straight Talk Phones?

Integrating the Callback

To integrate the callback into the /chat endpoint, you can follow the code example below:

@app.route('/chat', methods=['POST'])
def chat():
        data = request.get_json()
        user_message = data.get('message')

        if not user_message:
            app.logger.error("No message provided")
            return jsonify({"error": "No message provided"}), 400

        # Embed the user message
        user_message_embedding = Embeddings.embed_query(user_message)

        if user_message_embedding is None:
  "Embedding for user message is None")

        # Generate response using the agent
        agent_response =
        original_response = agent_response['output']

        # Embed the original agent response
        original_response_embedding = Embeddings.embed_query(original_response)

        if original_response_embedding is None:
  "Embedding for original agent response is None")

        # Post-process the response to ensure a professional tone
        professional_response = post_process_with_professional_tone(original_response)

        # Embed the post-processed response
        professional_response_embedding = Embeddings.embed_query(professional_response)

        if professional_response_embedding is None:
  "Embedding for post-processed response is None")

        # Store the embeddings in the Chroma DB
            'role': 'user',
            'content': user_message

            'role': 'assistant',
            'content': professional_response

        vectorstore.add([user_message_embedding, original_response_embedding, professional_response_embedding])

        return jsonify({"user_message": user_message, "chatbot_response": professional_response})

    except Exception as e:
        return jsonify({"error": str(e)}), 500

By integrating the callback into the /chat endpoint, you can effectively track the user’s chatbot interactions while obtaining the token count for each API call. This ensures a seamless deduction of credits from your user’s account.


In this article, we have explored the challenge of obtaining a token count from the API for your Python/Flask Langchain RetrievalQA chatbot. Through testing the callback code and integrating it into the /chat endpoint, you can reliably track the token usage for each API call and deduct credits accordingly. Remember to modify the code provided to fit your specific chatbot implementation.

Tham Khảo Thêm:  How to Use Touch ID on Your iPhone or iPad

Happy coding, and may your chatbot endeavors be successful!