-
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.
Introduce the
InferenceChatModel
for langchain (#206429)
## Summary Part of #206710 This PR introduces the `InferenceChatModel` class, which is a langchain chatModel utilizing the inference APIs (`chatComplete`) under the hood. Creating instances of `InferenceChatModel` can either be done by manually importing the class from the new `@kbn/inference-langchain` package, or by using the new `createChatModel` API exposes from the inference plugin's start contract. The main upside of using this chatModel is that the unification and normalization layers are already being taken care of by the inference plugin, making sure that the underlying models are being used with the exact same capabilities. More details on the upsides and reasoning in the associated issue. ### Usage Usage is very straightforward ```ts const chatModel = await inferenceStart.getChatModel({ request, connectorId: myInferenceConnectorId, chatModelOptions: { temperature: 0.2, }, }); // just use it as another langchain chatModel, e.g. const response = await chatModel.stream('What is Kibana?'); for await (const chunk of response) { // do something with the chunk } ``` ### Important This PR is only adding the implementation, and not wiring it anywhere or using it in any existing code. This is meant to be done in a later stage. Merging that implementation first will allow to have distinct PRs for the integration with search (playground) and security (assistant + other workflows), with proper testing --------- Co-authored-by: kibanamachine <[email protected]> (cherry picked from commit 1c218f9) # Conflicts: # .github/CODEOWNERS
- Loading branch information
1 parent
94bd9eb
commit 445353b
Showing
57 changed files
with
2,460 additions
and
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -581,6 +581,7 @@ | |
"@kbn/inference-common": "link:x-pack/platform/packages/shared/ai-infra/inference-common", | ||
"@kbn/inference-endpoint-plugin": "link:x-pack/platform/plugins/shared/inference_endpoint", | ||
"@kbn/inference-endpoint-ui-common": "link:x-pack/platform/packages/shared/kbn-inference-endpoint-ui-common", | ||
"@kbn/inference-langchain": "link:x-pack/platform/packages/shared/ai-infra/inference-langchain", | ||
"@kbn/inference-plugin": "link:x-pack/platform/plugins/shared/inference", | ||
"@kbn/inference_integration_flyout": "link:x-pack/platform/packages/private/ml/inference_integration_flyout", | ||
"@kbn/infra-forge": "link:x-pack/platform/packages/private/kbn-infra-forge", | ||
|
@@ -1304,7 +1305,8 @@ | |
"yaml": "^2.5.1", | ||
"yauzl": "^2.10.0", | ||
"yazl": "^2.5.1", | ||
"zod": "^3.22.3" | ||
"zod": "^3.22.3", | ||
"zod-to-json-schema": "^3.23.0" | ||
}, | ||
"devDependencies": { | ||
"@apidevtools/swagger-parser": "^10.1.1", | ||
|
@@ -1877,8 +1879,7 @@ | |
"xml-crypto": "^6.0.0", | ||
"xmlbuilder": "13.0.2", | ||
"yargs": "^15.4.1", | ||
"yarn-deduplicate": "^6.0.2", | ||
"zod-to-json-schema": "^3.23.0" | ||
"yarn-deduplicate": "^6.0.2" | ||
}, | ||
"packageManager": "[email protected]" | ||
} |
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
44 changes: 44 additions & 0 deletions
44
x-pack/platform/packages/shared/ai-infra/inference-common/src/connectors/connector_config.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,44 @@ | ||
/* | ||
* 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 InferenceConnector, InferenceConnectorType } from './connectors'; | ||
|
||
/** | ||
* Returns the default model as defined in the connector's config, if available. | ||
* | ||
* Note: preconfigured connectors only expose their config if their `exposeConfig` flag | ||
* is set to true. | ||
*/ | ||
export const getConnectorDefaultModel = (connector: InferenceConnector): string | undefined => { | ||
switch (connector.type) { | ||
case InferenceConnectorType.OpenAI: | ||
case InferenceConnectorType.Gemini: | ||
case InferenceConnectorType.Bedrock: | ||
return connector.config?.defaultModel ?? undefined; | ||
case InferenceConnectorType.Inference: | ||
return connector.config?.providerConfig?.model_id ?? undefined; | ||
} | ||
}; | ||
|
||
/** | ||
* Returns the provider used for the given connector | ||
* | ||
* Inferred from the type for "legacy" connectors, | ||
* and from the provider config field for inference connectors. | ||
*/ | ||
export const getConnectorProvider = (connector: InferenceConnector): string => { | ||
switch (connector.type) { | ||
case InferenceConnectorType.OpenAI: | ||
return 'openai'; | ||
case InferenceConnectorType.Gemini: | ||
return 'gemini'; | ||
case InferenceConnectorType.Bedrock: | ||
return 'bedrock'; | ||
case InferenceConnectorType.Inference: | ||
return connector.config?.provider ?? 'unknown'; | ||
} | ||
}; |
31 changes: 31 additions & 0 deletions
31
...atform/packages/shared/ai-infra/inference-common/src/connectors/connector_to_inference.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,31 @@ | ||
/* | ||
* 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 { createInferenceRequestError } from '../errors'; | ||
import type { InferenceConnector, RawConnector } from './connectors'; | ||
import { isSupportedConnector } from './is_supported_connector'; | ||
|
||
/** | ||
* Converts an action connector to the internal inference connector format. | ||
* | ||
* The function will throw if the provided connector is not compatible | ||
*/ | ||
export const connectorToInference = (connector: RawConnector): InferenceConnector => { | ||
if (!isSupportedConnector(connector)) { | ||
throw createInferenceRequestError( | ||
`Connector '${connector.id}' of type '${connector.actionTypeId}' not recognized as a supported connector`, | ||
400 | ||
); | ||
} | ||
|
||
return { | ||
connectorId: connector.id, | ||
name: connector.name, | ||
type: connector.actionTypeId, | ||
config: connector.config ?? {}, | ||
}; | ||
}; |
54 changes: 54 additions & 0 deletions
54
x-pack/platform/packages/shared/ai-infra/inference-common/src/connectors/connectors.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,54 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** | ||
* The list of connector types that can be used with the inference APIs | ||
*/ | ||
export enum InferenceConnectorType { | ||
OpenAI = '.gen-ai', | ||
Bedrock = '.bedrock', | ||
Gemini = '.gemini', | ||
Inference = '.inference', | ||
} | ||
|
||
export const allSupportedConnectorTypes = Object.values(InferenceConnectorType); | ||
|
||
/** | ||
* Represents a stack connector that can be used for inference. | ||
*/ | ||
export interface InferenceConnector { | ||
/** the type of the connector, see {@link InferenceConnectorType} */ | ||
type: InferenceConnectorType; | ||
/** the name of the connector */ | ||
name: string; | ||
/** the id of the connector */ | ||
connectorId: string; | ||
/** | ||
* configuration (without secrets) of the connector. | ||
* the list of properties depends on the connector type (and subtype for inference) | ||
*/ | ||
config: Record<string, any>; | ||
} | ||
|
||
/** | ||
* Connector types are living in the actions plugin and we can't afford | ||
* having dependencies from this package to some mid-level plugin, | ||
* so we're just using our own connector mixin type. | ||
*/ | ||
export interface RawConnector { | ||
id: string; | ||
actionTypeId: string; | ||
name: string; | ||
config?: Record<string, any>; | ||
} | ||
|
||
export interface RawInferenceConnector { | ||
id: string; | ||
actionTypeId: InferenceConnectorType; | ||
name: string; | ||
config?: Record<string, any>; | ||
} |
11 changes: 11 additions & 0 deletions
11
x-pack/platform/packages/shared/ai-infra/inference-common/src/connectors/index.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,11 @@ | ||
/* | ||
* 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 { isSupportedConnectorType, isSupportedConnector } from './is_supported_connector'; | ||
export { connectorToInference } from './connector_to_inference'; | ||
export { getConnectorDefaultModel, getConnectorProvider } from './connector_config'; | ||
export { InferenceConnectorType, type InferenceConnector } from './connectors'; |
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
8 changes: 8 additions & 0 deletions
8
x-pack/platform/packages/shared/ai-infra/inference-common/src/utils/index.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,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 { generateFakeToolCallId } from './tool_calls'; |
12 changes: 12 additions & 0 deletions
12
x-pack/platform/packages/shared/ai-infra/inference-common/src/utils/tool_calls.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,12 @@ | ||
/* | ||
* 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 { v4 } from 'uuid'; | ||
|
||
export function generateFakeToolCallId() { | ||
return v4().substr(0, 6); | ||
} |
39 changes: 39 additions & 0 deletions
39
x-pack/platform/packages/shared/ai-infra/inference-langchain/README.md
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,39 @@ | ||
# @kbn/inference-langchain | ||
|
||
This package exposes utilities to use the inference APIs and plugin with langchain | ||
|
||
## InferenceChatModel | ||
|
||
The inference chat model is a langchain model leveraging the inference APIs under the hood. | ||
|
||
The main upside is that the unification and normalization layers are then fully handled | ||
by the inference plugin. The developer / consumer doesn't even need to know which provider | ||
is being used under the hood. | ||
|
||
The easiest way to create an `InferenceChatModel` is by using the inference APIs: | ||
|
||
```ts | ||
const chatModel = await inferenceStart.getChatModel({ | ||
request, | ||
connectorId: myInferenceConnectorId, | ||
chatModelOptions: { | ||
temperature: 0.2, | ||
}, | ||
}); | ||
|
||
// just use it as another langchain chatModel | ||
``` | ||
|
||
But the chatModel can also be instantiated directly if needed: | ||
|
||
```ts | ||
import { connectorToInference } from '@kbn/inference-common'; | ||
|
||
const chatModel = new InferenceChatModel({ | ||
chatComplete: inference.chatComplete, | ||
connector: connectorToInference(someInferenceConnector), | ||
logger: myPluginLogger, | ||
}); | ||
|
||
// just use it as another langchain chatModel | ||
``` |
12 changes: 12 additions & 0 deletions
12
x-pack/platform/packages/shared/ai-infra/inference-langchain/index.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,12 @@ | ||
/* | ||
* 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 { | ||
InferenceChatModel, | ||
type InferenceChatModelParams, | ||
type InferenceChatModelCallOptions, | ||
} from './src/chat_model'; |
Oops, something went wrong.