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

Groq models don't return the usage #1651

Open
ValenCassa opened this issue May 19, 2024 · 3 comments
Open

Groq models don't return the usage #1651

ValenCassa opened this issue May 19, 2024 · 3 comments

Comments

@ValenCassa
Copy link
Contributor

ValenCassa commented May 19, 2024

Description

Seems like Groq models do not follow the OpenAI spec for usage. They return the usage object under a x-groq key in the last chunk:

{
  id: "chatcmpl-8566e76f-7ded-47d7-a2ca-d212098af00c",
  object: "chat.completion.chunk",
  created: 1716131679,
  model: "llama3-8b-8192",
  system_fingerprint: "fp_179b0f92c9",
  choices: [{ index: 0, delta: {}, logprobs: null, finish_reason: "stop" }],
  x_groq: {
    id: "req_01hy8ppkajfqk987jv2vx88m9r",
    usage: {
      queue_time: 0.07535312,
      prompt_tokens: 23,
      prompt_time: 0.006,
      completion_tokens: 19,
      completion_time: 0.022,
      total_tokens: 42,
      total_time: 0.027999999999999997,
    },
  },
};

Code example

import { createOpenAI } from '@ai-sdk/openai';
import { streamText } from 'ai';
import dotenv from 'dotenv';

dotenv.config();

const groq = createOpenAI({
  apiKey: process.env.GROQ_API_KEY ?? '',
  baseURL: 'https://api.groq.com/openai/v1',
});

async function main() {
  const result = await streamText({
    model: groq.chat('llama3-70b-8192'),
    prompt: 'Invent a new holiday and describe its traditions.',
  });

  for await (const textPart of result.textStream) {
    process.stdout.write(textPart);
  }

  console.log();
  console.log('Token usage:', await result.usage); // This results in NaN
  console.log('Finish reason:', await result.finishReason);
}

main().catch(console.error);

Additional context

No response

@sheldonj
Copy link

I don't know if this is related but even the normal open models for streamObject return NaN for usage values.

@lgrammel
Copy link
Collaborator

@sheldonj for OpenAI it should work. In case you use createOpenAI, you need to set compatibility to strict: https://sdk.vercel.ai/providers/ai-sdk-providers/openai#provider-instance (this was necessary to prevent breaking changes with OpenAI-"compatible" providers).

@sheldonj
Copy link

@sheldonj for OpenAI it should work. In case you use createOpenAI, you need to set compatibility to strict: https://sdk.vercel.ai/providers/ai-sdk-providers/openai#provider-instance (this was necessary to prevent breaking changes with OpenAI-"compatible" providers).

Confirmed that using strict mode returns usage! Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants