-
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.
Browse files
Browse the repository at this point in the history
# Backport This will backport the following commits from `main` to `8.x`: - [[inference] Add simulated function calling (#192544)](#192544) <!--- 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-09-18T10:42:28Z","message":"[inference] Add simulated function calling (#192544)\n\n## Summary\r\n\r\nAdd simulated function calling to the inference plugin. For now, only\r\nthe openAI adapter is supported. This is done by adding a new, optional\r\n`functionCalling` parameter to the chat and task APIs\r\n\r\nImplementation was adapted from the equivalent feature in the o11y\r\nassistant.\r\n\r\n---------\r\n\r\nCo-authored-by: Dario Gieselaar <[email protected]>","sha":"181d61723136084ec57801fab1cc99457c047977","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","Team:Obs AI Assistant","ci:project-deploy-observability","v8.16.0","Team:AI Infra"],"title":"[inference] Add simulated function calling ","number":192544,"url":"https://github.com/elastic/kibana/pull/192544","mergeCommit":{"message":"[inference] Add simulated function calling (#192544)\n\n## Summary\r\n\r\nAdd simulated function calling to the inference plugin. For now, only\r\nthe openAI adapter is supported. This is done by adding a new, optional\r\n`functionCalling` parameter to the chat and task APIs\r\n\r\nImplementation was adapted from the equivalent feature in the o11y\r\nassistant.\r\n\r\n---------\r\n\r\nCo-authored-by: Dario Gieselaar <[email protected]>","sha":"181d61723136084ec57801fab1cc99457c047977"}},"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/192544","number":192544,"mergeCommit":{"message":"[inference] Add simulated function calling (#192544)\n\n## Summary\r\n\r\nAdd simulated function calling to the inference plugin. For now, only\r\nthe openAI adapter is supported. This is done by adding a new, optional\r\n`functionCalling` parameter to the chat and task APIs\r\n\r\nImplementation was adapted from the equivalent feature in the o11y\r\nassistant.\r\n\r\n---------\r\n\r\nCo-authored-by: Dario Gieselaar <[email protected]>","sha":"181d61723136084ec57801fab1cc99457c047977"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}] BACKPORT--> Co-authored-by: Pierre Gayvallet <[email protected]>
- Loading branch information
1 parent
44abd1a
commit c8043c9
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.