Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when quota info is missing from completion response #189

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

julien-nc
Copy link
Member

Do not attempt to update local quota if chat/completion response does not have usage.total_tokens set

… does not have usage.total_tokens set

Signed-off-by: Julien Veyssier <julien-nc@posteo.net>
@julien-nc julien-nc added bug Something isn't working 3. to review labels Feb 10, 2025
@@ -324,7 +324,7 @@ public function createCompletion(
throw new Exception($this->l10n->t('Unknown text generation error'), Http::STATUS_INTERNAL_SERVER_ERROR);
}

if (isset($response['usage'])) {
if (isset($response['usage'], $response['usage']['total_tokens'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (isset($response['usage'], $response['usage']['total_tokens'])) {
if (isset($response['usage']['total_tokens'])) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be enough, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't trust the behaviour of isset enough across all Php versions so I prefer to explicitly check each level separately.
But sure, I tried this in a Php console and it worked as expected:

$a = [];
echo $a['one']; // it crashed
echo isset($a['one']) ? 1 : 0; // prints 0
echo isset($a['one']['two']) ? 1 : 0; // does not crash and prints 0

@@ -461,9 +461,8 @@ public function createChatCompletion(
throw new Exception($this->l10n->t('Unknown text generation error'), Http::STATUS_INTERNAL_SERVER_ERROR);
}

if (isset($response['usage'])) {
if (isset($response['usage'], $response['usage']['total_tokens'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (isset($response['usage'], $response['usage']['total_tokens'])) {
if (isset($response['usage']['total_tokens'])) {

@julien-nc julien-nc merged commit 907f149 into main Feb 10, 2025
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3. to review bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants