-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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 cancelation support for chatComplete and output (…
…#203108) (#204588) # Backport This will backport the following commits from `main` to `8.x`: - [[inference] Add cancelation support for chatComplete and output (#203108)](#203108) <!--- 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-12-17T15:13:17Z","message":"[inference] Add cancelation support for chatComplete and output (#203108)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/200757\r\n\r\nAdd cancelation support for `chatComplete` and `output`, based on an\r\nabort signal.\r\n\r\n\r\n### Examples\r\n\r\n#### response mode\r\n\r\n```ts\r\nimport { isInferenceRequestAbortedError } from '@kbn/inference-common';\r\n\r\ntry {\r\n const abortController = new AbortController();\r\n const chatResponse = await inferenceClient.chatComplete({\r\n connectorId: 'some-gen-ai-connector',\r\n abortSignal: abortController.signal,\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n });\r\n} catch(e) {\r\n if(isInferenceRequestAbortedError(e)) {\r\n // request was aborted, do something\r\n } else {\r\n // was another error, do something else\r\n }\r\n}\r\n\r\n// elsewhere\r\nabortController.abort()\r\n```\r\n\r\n#### stream mode\r\n\r\n```ts\r\nimport { isInferenceRequestAbortedError } from '@kbn/inference-common';\r\n\r\nconst abortController = new AbortController();\r\nconst events$ = inferenceClient.chatComplete({\r\n stream: true,\r\n connectorId: 'some-gen-ai-connector',\r\n abortSignal: abortController.signal,\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n\r\nevents$.subscribe({\r\n next: (event) => {\r\n // do something\r\n },\r\n error: (err) => {\r\n if(isInferenceRequestAbortedError(e)) {\r\n // request was aborted, do something\r\n } else {\r\n // was another error, do something else\r\n }\r\n }\r\n});\r\n\r\nabortController.abort();\r\n```","sha":"0b74f62a338fe1f6b281a019e71279f0d2ffb81f","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:version","Team:AI Infra","v8.18.0"],"title":"[inference] Add cancelation support for chatComplete and output","number":203108,"url":"https://github.com/elastic/kibana/pull/203108","mergeCommit":{"message":"[inference] Add cancelation support for chatComplete and output (#203108)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/200757\r\n\r\nAdd cancelation support for `chatComplete` and `output`, based on an\r\nabort signal.\r\n\r\n\r\n### Examples\r\n\r\n#### response mode\r\n\r\n```ts\r\nimport { isInferenceRequestAbortedError } from '@kbn/inference-common';\r\n\r\ntry {\r\n const abortController = new AbortController();\r\n const chatResponse = await inferenceClient.chatComplete({\r\n connectorId: 'some-gen-ai-connector',\r\n abortSignal: abortController.signal,\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n });\r\n} catch(e) {\r\n if(isInferenceRequestAbortedError(e)) {\r\n // request was aborted, do something\r\n } else {\r\n // was another error, do something else\r\n }\r\n}\r\n\r\n// elsewhere\r\nabortController.abort()\r\n```\r\n\r\n#### stream mode\r\n\r\n```ts\r\nimport { isInferenceRequestAbortedError } from '@kbn/inference-common';\r\n\r\nconst abortController = new AbortController();\r\nconst events$ = inferenceClient.chatComplete({\r\n stream: true,\r\n connectorId: 'some-gen-ai-connector',\r\n abortSignal: abortController.signal,\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n\r\nevents$.subscribe({\r\n next: (event) => {\r\n // do something\r\n },\r\n error: (err) => {\r\n if(isInferenceRequestAbortedError(e)) {\r\n // request was aborted, do something\r\n } else {\r\n // was another error, do something else\r\n }\r\n }\r\n});\r\n\r\nabortController.abort();\r\n```","sha":"0b74f62a338fe1f6b281a019e71279f0d2ffb81f"}},"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/203108","number":203108,"mergeCommit":{"message":"[inference] Add cancelation support for chatComplete and output (#203108)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/200757\r\n\r\nAdd cancelation support for `chatComplete` and `output`, based on an\r\nabort signal.\r\n\r\n\r\n### Examples\r\n\r\n#### response mode\r\n\r\n```ts\r\nimport { isInferenceRequestAbortedError } from '@kbn/inference-common';\r\n\r\ntry {\r\n const abortController = new AbortController();\r\n const chatResponse = await inferenceClient.chatComplete({\r\n connectorId: 'some-gen-ai-connector',\r\n abortSignal: abortController.signal,\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n });\r\n} catch(e) {\r\n if(isInferenceRequestAbortedError(e)) {\r\n // request was aborted, do something\r\n } else {\r\n // was another error, do something else\r\n }\r\n}\r\n\r\n// elsewhere\r\nabortController.abort()\r\n```\r\n\r\n#### stream mode\r\n\r\n```ts\r\nimport { isInferenceRequestAbortedError } from '@kbn/inference-common';\r\n\r\nconst abortController = new AbortController();\r\nconst events$ = inferenceClient.chatComplete({\r\n stream: true,\r\n connectorId: 'some-gen-ai-connector',\r\n abortSignal: abortController.signal,\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n\r\nevents$.subscribe({\r\n next: (event) => {\r\n // do something\r\n },\r\n error: (err) => {\r\n if(isInferenceRequestAbortedError(e)) {\r\n // request was aborted, do something\r\n } else {\r\n // was another error, do something else\r\n }\r\n }\r\n});\r\n\r\nabortController.abort();\r\n```","sha":"0b74f62a338fe1f6b281a019e71279f0d2ffb81f"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Pierre Gayvallet <[email protected]>
- Loading branch information
1 parent
046324b
commit abde299
Showing
27 changed files
with
688 additions
and
24 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.