Skip to content

Commit

Permalink
[inference] improve simulated function calling instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Sep 19, 2024
1 parent 28aa274 commit b5ea763
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
2 changes: 2 additions & 0 deletions x-pack/plugins/inference/scripts/util/kibana_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,15 @@ export class KibanaClient {
system,
toolChoice,
tools,
functionCalling,
}) => {
const body: ChatCompleteRequestBody = {
connectorId: chatCompleteConnectorId,
system,
messages,
toolChoice,
tools,
functionCalling,
};

return stream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,18 @@ export function getSystemMessageInstructions({
It is EXTREMELY important that you generate valid JSON between the \`\`\`json and \`\`\` delimiters.
You may call them like this.
IMPORTANT: make sure you start and end a tool call with the ${TOOL_USE_START} and ${TOOL_USE_END} markers, it MUST
be included in the tool call.
Given the following tool:
You may call tools like this:
${TOOL_USE_START}
\`\`\`json
${JSON.stringify({ name: '[name of the tool]', input: { myProperty: 'myValue' } })}
\`\`\`\
${TOOL_USE_END}
For example, given the following tool:
${JSON.stringify({
name: 'my_tool',
Expand All @@ -54,13 +63,15 @@ export function getSystemMessageInstructions({
\`\`\`\
${TOOL_USE_END}
Given the following tool:
Another example: 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: {} })}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function parseInlineFunctionCalls({ logger }: { logger: Logger }) {
logger.debug('Parsing function call:\n' + buffer);

const match = buffer.match(
/<\|tool_use_start\|>\s*```json\n?(.*?)(\n```\s*).*<\|tool_use_end\|>/s
/<\|tool_use_start\|>\s*```json\n?(.*?)(\n?```\s*).*<\|tool_use_end\|>/s
);

const functionCallBody = match?.[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,19 @@ export function getSystemMessageInstructions({
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:
IMPORTANT: make sure you start and end a tool call with the ${TOOL_USE_START} and ${TOOL_USE_END} markers, it MUST
be included in the tool call.
You may call tools like this:
${TOOL_USE_START}
\`\`\`json
${JSON.stringify({ name: '[name of the tool]', input: { myProperty: 'myValue' } })}
\`\`\`\
${TOOL_USE_END}
For example, given the following tool:
${JSON.stringify({
name: 'my_tool',
Expand All @@ -55,13 +64,15 @@ export function getSystemMessageInstructions({
\`\`\`\
${TOOL_USE_END}
Given the following tool:
Another example: given the following tool:
${JSON.stringify({
name: 'my_tool_without_parameters',
description: 'A tool to call without parameters',
})}
Use it the following way:
Use it the following way:
${TOOL_USE_START}
\`\`\`json
${JSON.stringify({ name: 'my_tool_without_parameters', input: {} })}
Expand All @@ -77,7 +88,7 @@ export function getSystemMessageInstructions({
...(fn.parameters ? { parameters: fn.parameters } : {}),
}))
)}
`;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function parseInlineFunctionCalls({ logger }: { logger: Logger }) {
logger.debug('Parsing function call:\n' + buffer);

const match = buffer.match(
/<\|tool_use_start\|>\s*```json\n?(.*?)(\n```\s*).*<\|tool_use_end\|>/s
/<\|tool_use_start\|>\s*```json\n?(.*?)(\n?```\s*).*<\|tool_use_end\|>/s
);

const functionCallBody = match?.[1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export class KibanaClient {
const url = format({
...parsed,
pathname: `/${[
baseUrl,
...(baseUrl ? [baseUrl] : []),
...(props.ignoreSpaceId || !this.spaceId ? [] : ['s', this.spaceId]),
props.pathname.startsWith('/') ? props.pathname.substring(1) : props.pathname,
].join('/')}`,
Expand Down

0 comments on commit b5ea763

Please sign in to comment.