Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
29 changes: 13 additions & 16 deletions packages/ai-proxy/src/provider-dispatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export type DispatchBody = {
messages: ChatCompletionMessage[];
tools?: ChatCompletionTool[];
tool_choice?: ChatCompletionToolChoice;
parallel_tool_calls?: boolean;
};

export class ProviderDispatcher {
Expand All @@ -101,10 +102,20 @@ export class ProviderDispatcher {
throw new AINotConfiguredError();
}

const { tools, messages, tool_choice: toolChoice } = body;
const {
tools,
messages,
tool_choice: toolChoice,
parallel_tool_calls: parallelToolCalls,
} = body;

const enrichedTools = this.enrichToolDefinitions(tools);
const model = this.bindToolsIfNeeded(this.chatModel, enrichedTools, toolChoice);
const model = enrichedTools?.length
? this.chatModel.bindTools(enrichedTools, {
tool_choice: toolChoice,
parallel_tool_calls: parallelToolCalls,
Comment on lines +108 to +116
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tool_choice: toolChoice,
parallel_tool_calls: parallelToolCalls,
} = body;
const enrichedTools = this.enrichToolDefinitions(tools);
const model = this.bindToolsIfNeeded(this.chatModel, enrichedTools, toolChoice);
const model = enrichedTools?.length
? this.chatModel.bindTools(enrichedTools, {
tool_choice: toolChoice,
parallel_tool_calls: parallelToolCalls,
// eslint-disable-next-line @typescript-eslint/naming-convention
const { tools, messages, tool_choice, parallel_tool_calls } = body;
const enrichedTools = this.enrichToolDefinitions(tools);
const model = enrichedTools?.length
? this.chatModel.bindTools(enrichedTools, {
tool_choice,
parallel_tool_calls,

})
: this.chatModel;

try {
const response = await model.invoke(messages as BaseMessageLike[]);
Expand Down Expand Up @@ -136,20 +147,6 @@ export class ProviderDispatcher {
}
}

private bindToolsIfNeeded(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

simplify by adding a one liner

chatModel: ChatOpenAI,
tools: ChatCompletionTool[] | undefined,
toolChoice?: ChatCompletionToolChoice,
) {
if (!tools || tools.length === 0) {
return chatModel;
}

return chatModel.bindTools(tools, {
tool_choice: toolChoice as 'auto' | 'none' | 'required' | undefined,
});
}

private enrichToolDefinitions(tools?: ChatCompletionTool[]) {
if (!tools || !Array.isArray(tools)) return tools;

Expand Down
39 changes: 39 additions & 0 deletions packages/ai-proxy/test/provider-dispatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,45 @@ describe('ProviderDispatcher', () => {
});
});

describe('when parallel_tool_calls is provided', () => {
it('should pass parallel_tool_calls to bindTools', async () => {
const dispatcher = new ProviderDispatcher(
{ name: 'gpt4', provider: 'openai', apiKey: 'dev', model: 'gpt-4o' },
new RemoteTools(apiKeys),
);

await dispatcher.dispatch({
messages: [{ role: 'user', content: 'test' }],
tools: [{ type: 'function', function: { name: 'test', parameters: {} } }],
tool_choice: 'auto',
parallel_tool_calls: false,
} as unknown as DispatchBody);

expect(bindToolsMock).toHaveBeenCalledWith(
[{ type: 'function', function: { name: 'test', parameters: {} } }],
{ tool_choice: 'auto', parallel_tool_calls: false },
);
});

it('should pass parallel_tool_calls: true when explicitly set', async () => {
const dispatcher = new ProviderDispatcher(
{ name: 'gpt4', provider: 'openai', apiKey: 'dev', model: 'gpt-4o' },
new RemoteTools(apiKeys),
);

await dispatcher.dispatch({
messages: [{ role: 'user', content: 'test' }],
tools: [{ type: 'function', function: { name: 'test', parameters: {} } }],
parallel_tool_calls: true,
} as unknown as DispatchBody);

expect(bindToolsMock).toHaveBeenCalledWith(
expect.any(Array),
{ tool_choice: undefined, parallel_tool_calls: true },
);
});
});

describe('when there is not remote tool', () => {
it('should not enhance the remote tools definition', async () => {
const remoteTools = new RemoteTools(apiKeys);
Expand Down
Loading