-
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 simulated function calling (#192544)
## Summary Add simulated function calling to the inference plugin. For now, only the openAI adapter is supported. This is done by adding a new, optional `functionCalling` parameter to the chat and task APIs Implementation was adapted from the equivalent feature in the o11y assistant. --------- Co-authored-by: Dario Gieselaar <[email protected]>
- Loading branch information
1 parent
6221afa
commit 181d617
Showing
32 changed files
with
472 additions
and
33 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
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
9 changes: 9 additions & 0 deletions
9
x-pack/plugins/inference/server/chat_complete/simulated_function_calling/constants.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,9 @@ | ||
/* | ||
* 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 const TOOL_USE_START = '<|tool_use_start|>'; | ||
export const TOOL_USE_END = '<|tool_use_end|>'; |
84 changes: 84 additions & 0 deletions
84
...gins/inference/server/chat_complete/simulated_function_calling/get_system_instructions.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,84 @@ | ||
/* | ||
* 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 { TOOL_USE_END, TOOL_USE_START } from './constants'; | ||
import { ToolDefinition } from '../../../common/chat_complete/tools'; | ||
|
||
export function getSystemMessageInstructions({ | ||
tools, | ||
}: { | ||
tools?: Record<string, ToolDefinition>; | ||
}) { | ||
const formattedTools = Object.entries(tools ?? {}).map(([name, tool]) => { | ||
return { | ||
name, | ||
...tool, | ||
}; | ||
}); | ||
|
||
if (formattedTools.length) { | ||
return `In this environment, you have access to a set of tools you can use to answer the user's question. | ||
DO NOT call a tool when it is not listed. | ||
ONLY define input that is defined in the tool properties. | ||
If a tool does not have properties, leave them out. | ||
It is EXTREMELY important that you generate valid JSON between the \`\`\`json and \`\`\` delimiters. | ||
You may call them like this. | ||
Given the following tool: | ||
${JSON.stringify({ | ||
name: 'my_tool', | ||
description: 'A tool to call', | ||
schema: { | ||
type: 'object', | ||
properties: { | ||
myProperty: { | ||
type: 'string', | ||
}, | ||
}, | ||
}, | ||
})} | ||
Use it the following way: | ||
${TOOL_USE_START} | ||
\`\`\`json | ||
${JSON.stringify({ name: 'my_tool', input: { myProperty: 'myValue' } })} | ||
\`\`\`\ | ||
${TOOL_USE_END} | ||
Given the following tool: | ||
${JSON.stringify({ | ||
name: 'my_tool_without_parameters', | ||
description: 'A tool to call without parameters', | ||
})} | ||
Use it the following way: | ||
${TOOL_USE_START} | ||
\`\`\`json | ||
${JSON.stringify({ name: 'my_tool_without_parameters', input: {} })} | ||
\`\`\`\ | ||
${TOOL_USE_END} | ||
Here are the tools available: | ||
${JSON.stringify( | ||
formattedTools.map((tool) => ({ | ||
name: tool.name, | ||
description: tool.description, | ||
...(tool.schema ? { schema: tool.schema } : {}), | ||
})) | ||
)} | ||
`; | ||
} | ||
|
||
return `No tools are available anymore. DO NOT UNDER ANY CIRCUMSTANCES call any tool, regardless of whether it was previously called.`; | ||
} |
9 changes: 9 additions & 0 deletions
9
x-pack/plugins/inference/server/chat_complete/simulated_function_calling/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,9 @@ | ||
/* | ||
* 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 { wrapWithSimulatedFunctionCalling } from './wrap_with_simulated_function_calling'; | ||
export { parseInlineFunctionCalls } from './parse_inline_function_calls'; |
Oops, something went wrong.