[inference] fix tool definition for rainbow-sprinkles (#208823)

## Summary

Port the tool definition hack that was done for bedrock to be used for
`rainbow-sprinkles` too.
This commit is contained in:
Pierre Gayvallet 2025-01-30 07:58:20 +01:00 committed by GitHub
parent 0c18b44621
commit 32175454c1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,7 +5,7 @@
* 2.0.
*/
import { InferenceEndpointProvider, ToolOptions } from '@kbn/inference-common';
import { InferenceEndpointProvider, MessageRole, ToolOptions } from '@kbn/inference-common';
import { fixSchemaArrayProperties } from '../../bedrock/convert_tools';
import type { CreateOpenAIRequestOptions } from '../types';
import { getProvider, getElasticModelProvider } from '../utils';
@ -40,5 +40,25 @@ const applyBedrockTransforms = (
}, {} as Required<ToolOptions>['tools']);
}
const hasToolUse = options.messages.some(
(message) =>
message.role === MessageRole.Tool ||
(message.role === MessageRole.Assistant && message.toolCalls?.length)
);
// bedrock does not accept tool calls in conversation history
// if no tools are present in the request.
if (hasToolUse && Object.keys(options.tools ?? {}).length === 0) {
options.tools = {
doNotCallThisTool: {
description: 'Do not call this tool, it is strictly forbidden',
schema: {
type: 'object',
properties: {},
},
},
};
}
return options;
};