Skip to content

Commit

Permalink
chore: remove tool and tool call data when disable tools to avoid som…
Browse files Browse the repository at this point in the history
…e models not supporting
  • Loading branch information
adolphnov committed Dec 5, 2024
1 parent 3085c46 commit 7b4998a
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/agent/model_middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function AIMiddleware({ config, activeTools, onStream, toolChoice, messag
}

function warpMessages(params: LanguageModelV1CallOptions, tools: Record<string, any>, activeTools: string[], rawSystemPrompt: string | undefined) {
const { prompt: messages, mode } = params;
let { prompt: messages, mode } = params;
const trimMessages = (messages: LanguageModelV1Prompt) => {
if (messages.at(-1)?.role === 'tool') {
const content = messages.at(-1)!.content;
Expand All @@ -133,11 +133,15 @@ function warpMessages(params: LanguageModelV1CallOptions, tools: Record<string,
newMessages.push(message);
}
activeTools.length = 0;
params.prompt = newMessages;
messages = newMessages;
} else if (activeTools.length === 0) {
messages = messages.filter(message => ['user', 'system'].includes(message.role)
|| (message.role === 'assistant' && message.content[0].type !== 'tool-call'));
}
}
return messages;
};
trimMessages(messages);
messages = trimMessages(messages);
if (activeTools.length > 0) {
if (messages[0].role === 'system') {
messages[0].content = `${rawSystemPrompt}\n\nYou can consider using the following tools:\n##TOOLS${activeTools.map(name =>
Expand All @@ -148,6 +152,7 @@ function warpMessages(params: LanguageModelV1CallOptions, tools: Record<string,
(mode as any).tools = undefined;
messages[0].role === 'system' && (messages[0].content = rawSystemPrompt ?? '');
}
params.prompt = messages;
}

async function warpModel(model: LanguageModelV1, config: AgentUserConfig, activeTools: string[], toolChoice: ToolChoice, chatModel: string) {
Expand Down

0 comments on commit 7b4998a

Please sign in to comment.