hotfix: only use tools system prompt for models that support tools

This commit is contained in:
Nate 2024-12-16 22:05:54 -08:00
parent 0acf200aa6
commit 3a3bf73cb7
4 changed files with 25 additions and 9 deletions

View file

@ -85,8 +85,9 @@ const MODEL_SUPPORTS_IMAGES: string[] = [
"llama3.2",
];
function modelSupportsTools(modelName: string) {
function modelSupportsTools(modelName: string, provider: string) {
return (
provider === "anthropic" &&
modelName.includes("claude") &&
(modelName.includes("3-5") || modelName.includes("3.5"))
);

View file

@ -66,11 +66,15 @@ Always follow these guidelines when generating code responses.`;
const TOOL_USE_RULES = `When using tools, follow the following guidelines:
- Avoid calling tools unless they are absolutely necessary. For example, if you are asked a simple programming question you do not need web search. As another example, if the user asks you to explain something about code, do not create a new file.`;
function constructSystemPrompt(model: string): string | null {
function constructSystemPrompt(
model: string,
provider: string,
useTools: boolean,
): string | null {
if (CUSTOM_SYS_MSG_MODEL_FAMILIES.some((family) => model.includes(family))) {
return SYSTEM_MESSAGE + "\n\n" + TOOL_USE_RULES;
}
if (modelSupportsTools(model)) {
if (useTools && modelSupportsTools(model, provider)) {
return TOOL_USE_RULES;
}
@ -83,10 +87,12 @@ const CANCELED_TOOL_CALL_MESSAGE =
export function constructMessages(
history: ChatHistoryItem[],
model: string,
provider: string,
useTools: boolean,
): ChatMessage[] {
const msgs: ChatMessage[] = [];
const systemMessage = constructSystemPrompt(model);
const systemMessage = constructSystemPrompt(model, provider, useTools);
if (systemMessage) {
msgs.push({
role: "system" as const,

View file

@ -4,18 +4,18 @@ import { InputModifiers, MessageContent, SlashCommandDescription } from "core";
import { constructMessages } from "core/llm/constructMessages";
import { renderChatMessage } from "core/util/messageContent";
import posthog from "posthog-js";
import { v4 as uuidv4 } from "uuid";
import { selectDefaultModel } from "../slices/configSlice";
import {
submitEditorAndInitAtIndex,
updateHistoryItemAtIndex,
} from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
import { gatherContext } from "./gatherContext";
import { streamThunkWrapper } from "./streamThunkWrapper";
import { resetStateForNewMessage } from "./resetStateForNewMessage";
import { streamNormalInput } from "./streamNormalInput";
import { streamSlashCommand } from "./streamSlashCommand";
import { selectDefaultModel } from "../slices/configSlice";
import { v4 as uuidv4 } from "uuid";
import { streamThunkWrapper } from "./streamThunkWrapper";
const getSlashCommandForInput = (
input: MessageContent,
@ -61,6 +61,7 @@ export const streamResponseThunk = createAsyncThunk<
await dispatch(
streamThunkWrapper(async () => {
const state = getState();
const useTools = state.ui.useTools;
const defaultModel = selectDefaultModel(state);
const slashCommands = state.config.config.slashCommands || [];
const inputIndex = index ?? state.session.history.length;
@ -94,7 +95,12 @@ export const streamResponseThunk = createAsyncThunk<
// Construct messages from updated history
const updatedHistory = getState().session.history;
const messages = constructMessages(updatedHistory, defaultModel.model);
const messages = constructMessages(
updatedHistory,
defaultModel.model,
defaultModel.provider,
useTools,
);
posthog.capture("step run", {
step_name: "User Input",

View file

@ -9,9 +9,9 @@ import {
streamUpdate,
} from "../slices/sessionSlice";
import { ThunkApiType } from "../store";
import { streamThunkWrapper } from "./streamThunkWrapper";
import { resetStateForNewMessage } from "./resetStateForNewMessage";
import { streamNormalInput } from "./streamNormalInput";
import { streamThunkWrapper } from "./streamThunkWrapper";
export const streamResponseAfterToolCall = createAsyncThunk<
void,
@ -26,6 +26,7 @@ export const streamResponseAfterToolCall = createAsyncThunk<
await dispatch(
streamThunkWrapper(async () => {
const state = getState();
const useTools = state.ui.useTools;
const initialHistory = state.session.history;
const defaultModel = selectDefaultModel(state);
@ -59,6 +60,8 @@ export const streamResponseAfterToolCall = createAsyncThunk<
const messages = constructMessages(
[...updatedHistory],
defaultModel.model,
defaultModel.provider,
useTools,
);
unwrapResult(await dispatch(streamNormalInput(messages)));
}),