transitive-bullshit / chatgpt-api

Node.js client for the official ChatGPT API. 🔥

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to grab total token usage from response

nchan1994 opened this issue · comments

Verify latest release

  • I verified that the issue exists in the latest chatgpt release

Verify webapp is working

  • I verify that the ChatGPT webapp is working properly for this account.

Environment details

Typescript, Visual Studio Code

Describe the Bug

Not a bug, but hoping to get some guidance.

Not very familiar with Typescript, but hoping to make a couple of changes to pre-existing code using this awesome ChatGPT API.

I want to grab and store the token usage for each prompt and response to and from openai. But idk how to grab the token response. The code snippet I have is below:

public async callOpenAI(prompt: string, messageId: string): Promise<any> {
    console.log('mid:' + messageId)

    if (messageId == '') {
      const res = await oraPromise(this.api.sendMessage(prompt), 
        {
        text: prompt,
      })
      return res
    } else {
      const res = await oraPromise(
        this.api.sendMessage(prompt, {
          parentMessageId: messageId
        }),
        {
          text: prompt,
        }
      )
      return res
    }
  }
}

Would be great if someone could tell me how I can edit/add to this so I can get the token usage. I read through some of the issues and someone recommended looking at 'details'? Where would I add this because I tried several ways doesn't seem to work.

Many thanks!