Skip to content

Commit

Permalink
ai/core: add instruction to call specific tool to anthropic provider …
Browse files Browse the repository at this point in the history
…for generate object (#1283)
  • Loading branch information
lgrammel authored Apr 8, 2024
1 parent af465c9 commit 61e9e3a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
11 changes: 11 additions & 0 deletions packages/core/anthropic/anthropic-messages-language-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ describe('doGenerate', () => {
},
]);
expect(finishReason).toStrictEqual('tool-calls');

// check injection of tool use instruction:
expect((await server.getRequestBodyJson()).messages).toStrictEqual([
{
role: 'user',
content: [
{ type: 'text', text: 'Hello' },
{ type: 'text', text: `\n\nUse the 'json' tool.` },
],
},
]);
});

it('should extract usage', async () => {
Expand Down
16 changes: 9 additions & 7 deletions packages/core/anthropic/anthropic-messages-language-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,18 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV1 {
}

case 'object-tool': {
const { name, description, parameters } = mode.tool;

// add instruction to use tool:
baseArgs.messages[baseArgs.messages.length - 1].content.push({
type: 'text',
text: `\n\nUse the '${name}' tool.`,
});

return {
args: {
...baseArgs,
tools: [
{
name: mode.tool.name,
description: mode.tool.description,
input_schema: mode.tool.parameters,
},
],
tools: [{ name, description, input_schema: parameters }],
},
warnings,
};
Expand Down

0 comments on commit 61e9e3a

Please sign in to comment.