-
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.
[inference] add pre-bound versions of
chatComplete
and output
APIs (
#200568) ## Summary Fix #199084 Introduce pre-bound versions of the inference APIs. Accessing the bound versions can be done using the same `getClient` API, via an additional `bindTo` parameter: **without bindings** ```ts const inferenceClient = myStartDeps.inference.getClient({ request }); const chatResponse = inferenceClient.chatComplete({ connectorId: 'my-connector-id', functionCalling: 'simulated', messages: [{ role: MessageRole.User, content: 'Do something' }], }); ``` **with bindings** ```ts const inferenceClient = myStartDeps.inference.getClient({ request, bindTo: { connectorId: 'my-connector-id', functionCalling: 'simulated', } }); const chatResponse = inferenceClient.chatComplete({ messages: [{ role: MessageRole.User, content: 'Do something' }], }); ``` *Note: this is only done for the server-side, as there isn't much value in scoping APIs on the browser side in my opinion* --------- Co-authored-by: Elastic Machine <[email protected]>
- Loading branch information
1 parent
c04d80b
commit 3c8f077
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.