Skip to content

Commit

Permalink
[8.x] OpenAI: Ignore chunks with an empty `choices` list (#…
Browse files Browse the repository at this point in the history
…192951) (#192960)

# Backport

This will backport the following commits from `main` to `8.x`:
- [OpenAI: Ignore chunks with an empty `choices` list
(#192951)](#192951)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Pierre
Gayvallet","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-09-15T18:28:44Z","message":"OpenAI:
Ignore chunks with an empty `choices` list (#192951)\n\n##
Summary\r\n\r\nOpenAI added usage stats to their APIs
recently:\r\nhttps://community.openai.com/t/usage-stats-now-available-when-using-streaming-with-the-chat-completions-api-or-completions-api/738156\r\n\r\nHowever,
it's somewhat non BWC:\r\n\r\n> Note that this usage-specific chunk will
have choices: [], so if you\r\nturn this feature on, you may need to
update any code that accesses\r\n\r\n(Héhéhé)\r\n\r\nLeveraging this
feature is for another day. This PR is just about\r\navoiding the whole
thing to
explode.","sha":"8bffd618059aacc30d6190a0d143d8b0c7217faf","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","Team:Obs
AI Assistant","ci:project-deploy-observability","v8.16.0","Team:AI
Infra"],"title":"OpenAI: Ignore chunks with an empty `choices`
list","number":192951,"url":"https://github.com/elastic/kibana/pull/192951","mergeCommit":{"message":"OpenAI:
Ignore chunks with an empty `choices` list (#192951)\n\n##
Summary\r\n\r\nOpenAI added usage stats to their APIs
recently:\r\nhttps://community.openai.com/t/usage-stats-now-available-when-using-streaming-with-the-chat-completions-api-or-completions-api/738156\r\n\r\nHowever,
it's somewhat non BWC:\r\n\r\n> Note that this usage-specific chunk will
have choices: [], so if you\r\nturn this feature on, you may need to
update any code that accesses\r\n\r\n(Héhéhé)\r\n\r\nLeveraging this
feature is for another day. This PR is just about\r\navoiding the whole
thing to
explode.","sha":"8bffd618059aacc30d6190a0d143d8b0c7217faf"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192951","number":192951,"mergeCommit":{"message":"OpenAI:
Ignore chunks with an empty `choices` list (#192951)\n\n##
Summary\r\n\r\nOpenAI added usage stats to their APIs
recently:\r\nhttps://community.openai.com/t/usage-stats-now-available-when-using-streaming-with-the-chat-completions-api-or-completions-api/738156\r\n\r\nHowever,
it's somewhat non BWC:\r\n\r\n> Note that this usage-specific chunk will
have choices: [], so if you\r\nturn this feature on, you may need to
update any code that accesses\r\n\r\n(Héhéhé)\r\n\r\nLeveraging this
feature is for another day. This PR is just about\r\navoiding the whole
thing to
explode.","sha":"8bffd618059aacc30d6190a0d143d8b0c7217faf"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Pierre Gayvallet <[email protected]>
  • Loading branch information
kibanamachine and pgayvallet authored Sep 15, 2024
1 parent 4773f52 commit 5f8e564
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ export async function getTokenCountFromOpenAIStream({
delta: { content?: string; function_call?: { name?: string; arguments: string } };
}>;
} => {
return 'object' in line && line.object === 'chat.completion.chunk';
return (
'object' in line && line.object === 'chat.completion.chunk' && line.choices.length > 0
);
}
)
.reduce(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const openAIAdapter: InferenceConnectorAdapter = {
}),
filter(
(line): line is OpenAI.ChatCompletionChunk =>
'object' in line && line.object === 'chat.completion.chunk'
'object' in line && line.object === 'chat.completion.chunk' && line.choices.length > 0
),
map((chunk): ChatCompletionChunkEvent => {
const delta = chunk.choices[0].delta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function processOpenAiStream({
}),
filter(
(line): line is CreateChatCompletionResponseChunk =>
'object' in line && line.object === 'chat.completion.chunk'
'object' in line && line.object === 'chat.completion.chunk' && line.choices.length > 0
),
map((chunk): ChatCompletionChunkEvent => {
const delta = chunk.choices[0].delta;
Expand Down

0 comments on commit 5f8e564

Please sign in to comment.