-
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 pre-bound versions of `chatComplete` …
…and `output` APIs (#200568) (#201028) # Backport This will backport the following commits from `main` to `8.x`: - [[inference] add pre-bound versions of `chatComplete` and `output` APIs (#200568)](#200568) <!--- 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-20T19:09:11Z","message":"[inference] add pre-bound versions of `chatComplete` and `output` APIs (#200568)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/199084\r\n\r\nIntroduce pre-bound versions of the inference APIs.\r\n\r\nAccessing the bound versions can be done using the same `getClient` API,\r\nvia an additional `bindTo` parameter:\r\n\r\n**without bindings**\r\n```ts\r\nconst inferenceClient = myStartDeps.inference.getClient({ request });\r\n\r\nconst chatResponse = inferenceClient.chatComplete({\r\n connectorId: 'my-connector-id',\r\n functionCalling: 'simulated',\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n```\r\n\r\n**with bindings**\r\n```ts\r\nconst inferenceClient = myStartDeps.inference.getClient({\r\n request,\r\n bindTo: {\r\n connectorId: 'my-connector-id',\r\n functionCalling: 'simulated',\r\n }\r\n});\r\n\r\nconst chatResponse = inferenceClient.chatComplete({\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n```\r\n\r\n*Note: this is only done for the server-side, as there isn't much value\r\nin scoping APIs on the browser side in my opinion*\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"3c8f0777f4a4563824d0fb1f545524bf4346e3a2","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","v8.17.0"],"title":"[inference] add pre-bound versions of `chatComplete` and `output` APIs","number":200568,"url":"https://github.com/elastic/kibana/pull/200568","mergeCommit":{"message":"[inference] add pre-bound versions of `chatComplete` and `output` APIs (#200568)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/199084\r\n\r\nIntroduce pre-bound versions of the inference APIs.\r\n\r\nAccessing the bound versions can be done using the same `getClient` API,\r\nvia an additional `bindTo` parameter:\r\n\r\n**without bindings**\r\n```ts\r\nconst inferenceClient = myStartDeps.inference.getClient({ request });\r\n\r\nconst chatResponse = inferenceClient.chatComplete({\r\n connectorId: 'my-connector-id',\r\n functionCalling: 'simulated',\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n```\r\n\r\n**with bindings**\r\n```ts\r\nconst inferenceClient = myStartDeps.inference.getClient({\r\n request,\r\n bindTo: {\r\n connectorId: 'my-connector-id',\r\n functionCalling: 'simulated',\r\n }\r\n});\r\n\r\nconst chatResponse = inferenceClient.chatComplete({\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n```\r\n\r\n*Note: this is only done for the server-side, as there isn't much value\r\nin scoping APIs on the browser side in my opinion*\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"3c8f0777f4a4563824d0fb1f545524bf4346e3a2"}},"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/200568","number":200568,"mergeCommit":{"message":"[inference] add pre-bound versions of `chatComplete` and `output` APIs (#200568)\n\n## Summary\r\n\r\nFix https://github.com/elastic/kibana/issues/199084\r\n\r\nIntroduce pre-bound versions of the inference APIs.\r\n\r\nAccessing the bound versions can be done using the same `getClient` API,\r\nvia an additional `bindTo` parameter:\r\n\r\n**without bindings**\r\n```ts\r\nconst inferenceClient = myStartDeps.inference.getClient({ request });\r\n\r\nconst chatResponse = inferenceClient.chatComplete({\r\n connectorId: 'my-connector-id',\r\n functionCalling: 'simulated',\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n```\r\n\r\n**with bindings**\r\n```ts\r\nconst inferenceClient = myStartDeps.inference.getClient({\r\n request,\r\n bindTo: {\r\n connectorId: 'my-connector-id',\r\n functionCalling: 'simulated',\r\n }\r\n});\r\n\r\nconst chatResponse = inferenceClient.chatComplete({\r\n messages: [{ role: MessageRole.User, content: 'Do something' }],\r\n});\r\n```\r\n\r\n*Note: this is only done for the server-side, as there isn't much value\r\nin scoping APIs on the browser side in my opinion*\r\n\r\n---------\r\n\r\nCo-authored-by: Elastic Machine <[email protected]>","sha":"3c8f0777f4a4563824d0fb1f545524bf4346e3a2"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Pierre Gayvallet <[email protected]>
- Loading branch information
1 parent
63934e8
commit 29209cb
Showing
29 changed files
with
811 additions
and
61 deletions.
There are no files selected for viewing
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
35 changes: 35 additions & 0 deletions
35
x-pack/packages/ai-infra/inference-common/src/chat_complete/bound_api.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { ChatCompleteOptions, ChatCompleteCompositeResponse } from './api'; | ||
import type { ToolOptions } from './tools'; | ||
|
||
/** | ||
* Static options used to call the {@link BoundChatCompleteAPI} | ||
*/ | ||
export type BoundChatCompleteOptions< | ||
TToolOptions extends ToolOptions = ToolOptions, | ||
TStream extends boolean = false | ||
> = Pick<ChatCompleteOptions<TToolOptions, TStream>, 'connectorId' | 'functionCalling'>; | ||
|
||
/** | ||
* Options used to call the {@link BoundChatCompleteAPI} | ||
*/ | ||
export type UnboundChatCompleteOptions< | ||
TToolOptions extends ToolOptions = ToolOptions, | ||
TStream extends boolean = false | ||
> = Omit<ChatCompleteOptions<TToolOptions, TStream>, 'connectorId' | 'functionCalling'>; | ||
|
||
/** | ||
* Version of {@link ChatCompleteAPI} that got pre-bound to a set of static parameters | ||
*/ | ||
export type BoundChatCompleteAPI = < | ||
TToolOptions extends ToolOptions = ToolOptions, | ||
TStream extends boolean = false | ||
>( | ||
options: UnboundChatCompleteOptions<TToolOptions, TStream> | ||
) => ChatCompleteCompositeResponse<TToolOptions, TStream>; |
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
38 changes: 38 additions & 0 deletions
38
x-pack/packages/ai-infra/inference-common/src/output/bound_api.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { OutputOptions, OutputCompositeResponse } from './api'; | ||
import type { ToolSchema } from '../chat_complete/tool_schema'; | ||
|
||
/** | ||
* Static options used to call the {@link BoundOutputAPI} | ||
*/ | ||
export type BoundOutputOptions< | ||
TId extends string = string, | ||
TOutputSchema extends ToolSchema | undefined = ToolSchema | undefined, | ||
TStream extends boolean = false | ||
> = Pick<OutputOptions<TId, TOutputSchema, TStream>, 'connectorId' | 'functionCalling'>; | ||
|
||
/** | ||
* Options used to call the {@link BoundOutputAPI} | ||
*/ | ||
export type UnboundOutputOptions< | ||
TId extends string = string, | ||
TOutputSchema extends ToolSchema | undefined = ToolSchema | undefined, | ||
TStream extends boolean = false | ||
> = Omit<OutputOptions<TId, TOutputSchema, TStream>, 'connectorId' | 'functionCalling'>; | ||
|
||
/** | ||
* Version of {@link OutputAPI} that got pre-bound to a set of static parameters | ||
*/ | ||
export type BoundOutputAPI = < | ||
TId extends string = string, | ||
TOutputSchema extends ToolSchema | undefined = ToolSchema | undefined, | ||
TStream extends boolean = false | ||
>( | ||
options: UnboundOutputOptions<TId, TOutputSchema, TStream> | ||
) => OutputCompositeResponse<TId, TOutputSchema, TStream>; |
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
126 changes: 126 additions & 0 deletions
126
x-pack/plugins/inference/common/chat_complete/bind_chat_complete.test.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { | ||
BoundChatCompleteOptions, | ||
ChatCompleteAPI, | ||
MessageRole, | ||
UnboundChatCompleteOptions, | ||
} from '@kbn/inference-common'; | ||
import { bindChatComplete } from './bind_chat_complete'; | ||
|
||
describe('bindChatComplete', () => { | ||
let chatComplete: ChatCompleteAPI & jest.MockedFn<ChatCompleteAPI>; | ||
|
||
beforeEach(() => { | ||
chatComplete = jest.fn(); | ||
}); | ||
|
||
it('calls chatComplete with both bound and unbound params', async () => { | ||
const bound: BoundChatCompleteOptions = { | ||
connectorId: 'some-id', | ||
functionCalling: 'native', | ||
}; | ||
|
||
const unbound: UnboundChatCompleteOptions = { | ||
messages: [{ role: MessageRole.User, content: 'hello there' }], | ||
}; | ||
|
||
const boundApi = bindChatComplete(chatComplete, bound); | ||
|
||
await boundApi({ ...unbound }); | ||
|
||
expect(chatComplete).toHaveBeenCalledTimes(1); | ||
expect(chatComplete).toHaveBeenCalledWith({ | ||
...bound, | ||
...unbound, | ||
}); | ||
}); | ||
|
||
it('forwards the response from chatComplete', async () => { | ||
const expectedReturnValue = Symbol('something'); | ||
chatComplete.mockResolvedValue(expectedReturnValue as any); | ||
|
||
const boundApi = bindChatComplete(chatComplete, { connectorId: 'my-connector' }); | ||
|
||
const result = await boundApi({ | ||
messages: [{ role: MessageRole.User, content: 'hello there' }], | ||
}); | ||
|
||
expect(result).toEqual(expectedReturnValue); | ||
}); | ||
|
||
it('only passes the expected parameters from the bound param object', async () => { | ||
const bound = { | ||
connectorId: 'some-id', | ||
functionCalling: 'native', | ||
foo: 'bar', | ||
} as BoundChatCompleteOptions; | ||
|
||
const unbound: UnboundChatCompleteOptions = { | ||
messages: [{ role: MessageRole.User, content: 'hello there' }], | ||
}; | ||
|
||
const boundApi = bindChatComplete(chatComplete, bound); | ||
|
||
await boundApi({ ...unbound }); | ||
|
||
expect(chatComplete).toHaveBeenCalledTimes(1); | ||
expect(chatComplete).toHaveBeenCalledWith({ | ||
connectorId: 'some-id', | ||
functionCalling: 'native', | ||
messages: unbound.messages, | ||
}); | ||
}); | ||
|
||
it('ignores mutations of the bound parameters after binding', async () => { | ||
const bound: BoundChatCompleteOptions = { | ||
connectorId: 'some-id', | ||
functionCalling: 'native', | ||
}; | ||
|
||
const unbound: UnboundChatCompleteOptions = { | ||
messages: [{ role: MessageRole.User, content: 'hello there' }], | ||
}; | ||
|
||
const boundApi = bindChatComplete(chatComplete, bound); | ||
|
||
bound.connectorId = 'some-other-id'; | ||
|
||
await boundApi({ ...unbound }); | ||
|
||
expect(chatComplete).toHaveBeenCalledTimes(1); | ||
expect(chatComplete).toHaveBeenCalledWith({ | ||
connectorId: 'some-id', | ||
functionCalling: 'native', | ||
messages: unbound.messages, | ||
}); | ||
}); | ||
|
||
it('does not allow overriding bound parameters with the unbound object', async () => { | ||
const bound: BoundChatCompleteOptions = { | ||
connectorId: 'some-id', | ||
functionCalling: 'native', | ||
}; | ||
|
||
const unbound = { | ||
messages: [{ role: MessageRole.User, content: 'hello there' }], | ||
connectorId: 'overridden', | ||
} as UnboundChatCompleteOptions; | ||
|
||
const boundApi = bindChatComplete(chatComplete, bound); | ||
|
||
await boundApi({ ...unbound }); | ||
|
||
expect(chatComplete).toHaveBeenCalledTimes(1); | ||
expect(chatComplete).toHaveBeenCalledWith({ | ||
connectorId: 'some-id', | ||
functionCalling: 'native', | ||
messages: unbound.messages, | ||
}); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
x-pack/plugins/inference/common/chat_complete/bind_chat_complete.ts
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { | ||
ChatCompleteAPI, | ||
ChatCompleteOptions, | ||
BoundChatCompleteAPI, | ||
BoundChatCompleteOptions, | ||
UnboundChatCompleteOptions, | ||
ToolOptions, | ||
} from '@kbn/inference-common'; | ||
|
||
/** | ||
* Bind chatComplete to the provided parameters, | ||
* returning a bound version of the API. | ||
*/ | ||
export function bindChatComplete( | ||
chatComplete: ChatCompleteAPI, | ||
boundParams: BoundChatCompleteOptions | ||
): BoundChatCompleteAPI; | ||
export function bindChatComplete( | ||
chatComplete: ChatCompleteAPI, | ||
boundParams: BoundChatCompleteOptions | ||
) { | ||
const { connectorId, functionCalling } = boundParams; | ||
return (unboundParams: UnboundChatCompleteOptions<ToolOptions, boolean>) => { | ||
const params: ChatCompleteOptions<ToolOptions, boolean> = { | ||
...unboundParams, | ||
connectorId, | ||
functionCalling, | ||
}; | ||
return chatComplete(params); | ||
}; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { bindChatComplete } from './bind_chat_complete'; |
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.