Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] [inference] add support for openAI native stream token count (#…
…200745) (#201007) # Backport This will backport the following commits from `main` to `8.x`: - [[inference] add support for openAI native stream token count (#200745)](#200745) <!--- 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-11-20T16:53:44Z","message":"[inference] add support for openAI native stream token count (#200745)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/192962\r\n\r\nAdd support for native openAI token count for streaming APIs.\r\n\r\nThis is done by adding the `stream_options: {\"include_usage\": true}`\r\nparameter when `stream: true` is being used\r\n([doc](https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream_options)),\r\nand then using the `usage` entry for the last emitted chunk.\r\n\r\n**Note**: this was done only for the `OpenAI` and `AzureAI`\r\n[providers](https://github.com/elastic/kibana/blob/83a701e837a7a84a86dcc8d359154f900f69676a/x-pack/plugins/stack_connectors/common/openai/constants.ts#L27-L31),\r\nand **not** for the `Other` provider. The reasoning is that not all\r\nopenAI \"\"\"compatible\"\"\" providers fully support all options, so I didn't\r\nwant to risk adding a parameter that could cause some models using an\r\nopenAI adapter to reject the requests. This is also the reason why I did\r\nnot change the way\r\n[getTokenCountFromOpenAIStream](https://github.com/elastic/kibana/blob/8bffd618059aacc30d6190a0d143d8b0c7217faf/x-pack/plugins/actions/server/lib/get_token_count_from_openai_stream.ts#L15)\r\nfunction, as we want that to work for all providers.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"67171e15c2bd9063059701c4974f76f480ccd538","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","Team:AI Infra"],"title":"[inference] add support for openAI native stream token count","number":200745,"url":"https://github.com/elastic/kibana/pull/200745","mergeCommit":{"message":"[inference] add support for openAI native stream token count (#200745)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/192962\r\n\r\nAdd support for native openAI token count for streaming APIs.\r\n\r\nThis is done by adding the `stream_options: {\"include_usage\": true}`\r\nparameter when `stream: true` is being used\r\n([doc](https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream_options)),\r\nand then using the `usage` entry for the last emitted chunk.\r\n\r\n**Note**: this was done only for the `OpenAI` and `AzureAI`\r\n[providers](https://github.com/elastic/kibana/blob/83a701e837a7a84a86dcc8d359154f900f69676a/x-pack/plugins/stack_connectors/common/openai/constants.ts#L27-L31),\r\nand **not** for the `Other` provider. The reasoning is that not all\r\nopenAI \"\"\"compatible\"\"\" providers fully support all options, so I didn't\r\nwant to risk adding a parameter that could cause some models using an\r\nopenAI adapter to reject the requests. This is also the reason why I did\r\nnot change the way\r\n[getTokenCountFromOpenAIStream](https://github.com/elastic/kibana/blob/8bffd618059aacc30d6190a0d143d8b0c7217faf/x-pack/plugins/actions/server/lib/get_token_count_from_openai_stream.ts#L15)\r\nfunction, as we want that to work for all providers.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"67171e15c2bd9063059701c4974f76f480ccd538"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/200745","number":200745,"mergeCommit":{"message":"[inference] add support for openAI native stream token count (#200745)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/192962\r\n\r\nAdd support for native openAI token count for streaming APIs.\r\n\r\nThis is done by adding the `stream_options: {\"include_usage\": true}`\r\nparameter when `stream: true` is being used\r\n([doc](https://platform.openai.com/docs/api-reference/chat/create#chat-create-stream_options)),\r\nand then using the `usage` entry for the last emitted chunk.\r\n\r\n**Note**: this was done only for the `OpenAI` and `AzureAI`\r\n[providers](https://github.com/elastic/kibana/blob/83a701e837a7a84a86dcc8d359154f900f69676a/x-pack/plugins/stack_connectors/common/openai/constants.ts#L27-L31),\r\nand **not** for the `Other` provider. The reasoning is that not all\r\nopenAI \"\"\"compatible\"\"\" providers fully support all options, so I didn't\r\nwant to risk adding a parameter that could cause some models using an\r\nopenAI adapter to reject the requests. This is also the reason why I did\r\nnot change the way\r\n[getTokenCountFromOpenAIStream](https://github.com/elastic/kibana/blob/8bffd618059aacc30d6190a0d143d8b0c7217faf/x-pack/plugins/actions/server/lib/get_token_count_from_openai_stream.ts#L15)\r\nfunction, as we want that to work for all providers.\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"67171e15c2bd9063059701c4974f76f480ccd538"}}]}] BACKPORT--> Co-authored-by: Pierre Gayvallet <[email protected]>
- Loading branch information