LLPhant icon indicating copy to clipboard operation
LLPhant copied to clipboard

[Feature] Return token usage

Open kieranx opened this issue 1 year ago • 5 comments

In the official OpenAI API, token usage is returned, e.g. "total_tokens": 1340

There seems to be no way to get this same info in LLPhant -- this is very basic stuff and I feel like it should be able to be accessed, given OpenAI do return that info? We can't figure out a way to do that in LLPhant currently however.

Edit: to be clear, this is for the questionanswering class, I know we can get it for the general chat class.

kieranx avatar Jul 31 '24 10:07 kieranx

What about https://github.com/theodo-group/LLPhant/blob/fea142b4e5b14095ae273166809abf77f0d68f9a/src/Chat/OpenAIChat.php#L74 ? Does it fit your needs?

f-lombardo avatar Jul 31 '24 12:07 f-lombardo

Is there a way to retrieve the total number of tokens used with Question Answering?

$qa = new QuestionAnswering( $memoryVectorStore, $embeddingGenerator, new OpenAIChat() );

$answer = $qa->answerQuestion('What is the secret of Alice?');

If not, is it possible to use OpenAIChat with vector-stored embeddings?

Vardan9898 avatar Jul 31 '24 19:07 Vardan9898

Maybe you can try this:

    $chat = new OpenAIChat(new OpenAIConfig());
    $qa = new QuestionAnswering(
        $filesVectorStore,
        $embeddingGenerator,
        $chat
    );

    $response = $qa->answerQuestion('What is the secret of Alice?');

    echo('Total tokens: ' .$chat->getTotalTokens());

f-lombardo avatar Aug 01 '24 07:08 f-lombardo

You want the total of token for the question embeddings + all generations?

MaximeThoonsen avatar Aug 01 '24 21:08 MaximeThoonsen

I started a small PR here. We need to add the count of embeddings to make it correct

MaximeThoonsen avatar Aug 01 '24 22:08 MaximeThoonsen