Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[inference] improve simulated function calling instructions #193414

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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