Lambdua / openai4j

Java client library for OpenAI API.Full support for all OpenAI API models including Completions, Chat, Edits, Embeddings, Audio, Files, Assistants-v2, Images, Moderations, Batch, and Fine-tuning.
MIT License
331 stars 31 forks source link

StreamOption.includeUsage = true causes OpenAiService.mapStreamToAccumulator to throw java.lang.IndexOutOfBoundsException #60

Closed MishaKopaniov closed 2 months ago

MishaKopaniov commented 2 months ago

When StreamOption.includeUsage = true, the last usage chunk contains no choices, resulting in an IndexOutOfBoundsException.

Example of the Last Usage Chunk: { "id": "chatcmpl-A4yfRYHaCuVyGtk5r0DnV9donhXja", "object": "chat.completion.chunk", "created": 1725749881, "model": "gpt-4o-2024-05-13", "choices": [], "usage": { "prompt_tokens": 611, "completion_tokens": 13, "total_tokens": 624 }, "system_fingerprint": "fp_992d1ea92d" }

The OpenAiService.mapStreamToAccumulator method attempts to access the first choice in the choices array with the following code: ChatCompletionChoice firstChoice = chunk.getChoices().get(0);

However, this code assumes that there is always at least one choice available. When choices is empty, this leads to a java.lang.IndexOutOfBoundsException.

Although my understanding of the model is limited, it seems reasonable to either:

  1. Ignore the usage chunk during mapStreamToAccumulator.
  2. Add usage data to AssistantMessage model
Lambdua commented 2 months ago

get it , Will be fixed in the next version