LLPhant
LLPhant copied to clipboard
Comsumption with OpenAI
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;
}
}
`
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!
Thank you. Do you insert this features in the next release ?
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 :)
Hey @samuelgjekic,
I would love to have a contribution from you on this!
@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.
It' done 🚀