LLPhant icon indicating copy to clipboard operation
LLPhant copied to clipboard

Comsumption with OpenAI

Open ClicShopping opened this issue 10 months ago • 5 comments

Hello, Do you include this point inside LLphant to know to consumption in response for OpenAI ?. I do not find it ?

example `

  /**
   * @param $client
   * @param string $question
   * @return mixed
   * @throws \Exception
   */
  private static function saveOpenAiUsage($client, string $question): mixed
  {
    try {
      $result = $client['choices'][0]['message']['content'];

      $array_usage = [
        'promptTokens' => $client->usage->promptTokens,
        'completionTokens' => $client->usage->completionTokens,
        'totalTokens' => $client->usage->totalTokens,
      ];

      static::saveData($question, $result, $array_usage);

      return $result;
    } catch (RuntimeException $e) {
      throw new \Exception('Error appears, please look the console error');

      return false;
    }
  }

`

ClicShopping avatar Apr 27 '24 16:04 ClicShopping

Hey! Here is a example on how to get the usage values in in LLPhant:

    public function generateText(string $prompt): string
    {
        $answer = $this->generate($prompt);
        $this->handleTools($answer);

        $usageArray = $answer->usage->toArray(); // Get the usage values and store in array
    
        // Access the values 
        $promptToken = $usageArray['prompt_tokens']; 
        $totalTokens = $usageArray['total_tokens'];
        $completionTokens = $usageArray['completion_tokens'];

        /* Do whatever with the values */ 


        return $answer->choices[0]->message->content ?? '';
    }

This is the generateText function in OpenAiChat.php

You can access the usage values from the response.

Hope this helped!

samuelgjekic avatar Apr 27 '24 19:04 samuelgjekic

Thank you. Do you insert this features in the next release ?

ClicShopping avatar Apr 27 '24 20:04 ClicShopping

Thank you. Do you insert this features in the next release ?

No problem hope it helped! Well i would love to contribute to the library. If i get permission from owner to do it, i will add it :)

samuelgjekic avatar Apr 27 '24 20:04 samuelgjekic

Hey @samuelgjekic,

I would love to have a contribution from you on this!

MaximeThoonsen avatar Apr 28 '24 10:04 MaximeThoonsen

@ClicShopping, @samuelgjekic, @MaximeThoonsen I provided the PR #116 to store the last response from OpenAI. In this way we can have the token usage including also the other response objects.

ezimuel avatar May 07 '24 17:05 ezimuel

It' done 🚀

MaximeThoonsen avatar May 12 '24 09:05 MaximeThoonsen