transitive-bullshit / agentic

AI agent stdlib that works with any LLM and TypeScript AI SDK.
MIT License
16.07k stars 2.11k forks source link

How to grab total token usage from response #597

Closed nchan1994 closed 1 month ago

nchan1994 commented 1 year ago

Verify latest release

Verify webapp is working

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!

transitive-bullshit commented 1 month ago

This project is undergoing a major revamp; closing out old issues as part of the prep process.

The chatgpt package is pretty outdated at this point. I recommend that you use the openai package or the openai-fetch package instead.