-
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] Minor cleanup and restructure (#191069)
## Summary Fixing and improving a few things I noticed while discovering / ramping up on the existing code - address some nits - extract / reuse some low level functions - move things around - add unit tests
- Loading branch information
1 parent
7eb7e97
commit 56730e8
Showing
27 changed files
with
571 additions
and
211 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
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
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/inference/server/chat_complete/adapters/get_inference_adapter.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,24 @@ | ||
/* | ||
* 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 { InferenceConnectorType } from '../../../common/connectors'; | ||
import { getInferenceAdapter } from './get_inference_adapter'; | ||
import { openAIAdapter } from './openai'; | ||
|
||
describe('getInferenceAdapter', () => { | ||
it('returns the openAI adapter for OpenAI type', () => { | ||
expect(getInferenceAdapter(InferenceConnectorType.OpenAI)).toBe(openAIAdapter); | ||
}); | ||
|
||
it('returns undefined for Bedrock type', () => { | ||
expect(getInferenceAdapter(InferenceConnectorType.Bedrock)).toBe(undefined); | ||
}); | ||
|
||
it('returns undefined for Gemini type', () => { | ||
expect(getInferenceAdapter(InferenceConnectorType.Gemini)).toBe(undefined); | ||
}); | ||
}); |
29 changes: 29 additions & 0 deletions
29
x-pack/plugins/inference/server/chat_complete/adapters/get_inference_adapter.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,29 @@ | ||
/* | ||
* 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 { InferenceConnectorType } from '../../../common/connectors'; | ||
import type { InferenceConnectorAdapter } from '../types'; | ||
import { openAIAdapter } from './openai'; | ||
|
||
export const getInferenceAdapter = ( | ||
connectorType: InferenceConnectorType | ||
): InferenceConnectorAdapter | undefined => { | ||
switch (connectorType) { | ||
case InferenceConnectorType.OpenAI: | ||
return openAIAdapter; | ||
|
||
case InferenceConnectorType.Bedrock: | ||
// not implemented yet | ||
break; | ||
|
||
case InferenceConnectorType.Gemini: | ||
// not implemented yet | ||
break; | ||
} | ||
|
||
return undefined; | ||
}; |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/inference/server/chat_complete/adapters/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 { getInferenceAdapter } from './get_inference_adapter'; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 { KibanaRequest } from '@kbn/core-http-server'; | ||
import { defer, switchMap, throwError } from 'rxjs'; | ||
import type { ChatCompleteAPI, ChatCompletionResponse } from '../../common/chat_complete'; | ||
import { createInferenceRequestError } from '../../common/errors'; | ||
import type { InferenceStartDependencies } from '../types'; | ||
import { getConnectorById } from '../util/get_connector_by_id'; | ||
import { getInferenceAdapter } from './adapters'; | ||
import { createInferenceExecutor, chunksIntoMessage } from './utils'; | ||
|
||
export function createChatCompleteApi({ | ||
request, | ||
actions, | ||
}: { | ||
request: KibanaRequest; | ||
actions: InferenceStartDependencies['actions']; | ||
}) { | ||
const chatCompleteAPI: ChatCompleteAPI = ({ | ||
connectorId, | ||
messages, | ||
toolChoice, | ||
tools, | ||
system, | ||
}): ChatCompletionResponse => { | ||
return defer(async () => { | ||
const actionsClient = await actions.getActionsClientWithRequest(request); | ||
const connector = await getConnectorById({ connectorId, actionsClient }); | ||
const executor = createInferenceExecutor({ actionsClient, connector }); | ||
return { executor, connector }; | ||
}).pipe( | ||
switchMap(({ executor, connector }) => { | ||
const connectorType = connector.type; | ||
const inferenceAdapter = getInferenceAdapter(connectorType); | ||
|
||
if (!inferenceAdapter) { | ||
return throwError(() => | ||
createInferenceRequestError(`Adapter for type ${connectorType} not implemented`, 400) | ||
); | ||
} | ||
|
||
return inferenceAdapter.chatComplete({ | ||
system, | ||
executor, | ||
messages, | ||
toolChoice, | ||
tools, | ||
}); | ||
}), | ||
chunksIntoMessage({ | ||
toolChoice, | ||
tools, | ||
}) | ||
); | ||
}; | ||
|
||
return chatCompleteAPI; | ||
} |
Oops, something went wrong.