fix tool bugs

This commit is contained in:
Dallin Romney 2025-01-10 23:13:33 -08:00
parent b35f00f3dc
commit 361b857be0
9 changed files with 24 additions and 20 deletions

View file

@ -93,6 +93,9 @@ export function constructMessages(
provider: string,
useTools: boolean,
): ChatMessage[] {
const filteredHistory = history.filter(
(item) => item.message.role !== "system",
);
const msgs: ChatMessage[] = [];
const systemMessage = constructSystemPrompt(model, provider, useTools);
@ -103,8 +106,8 @@ export function constructMessages(
});
}
for (let i = 0; i < history.length; i++) {
const historyItem = history[i];
for (let i = 0; i < filteredHistory.length; i++) {
const historyItem = filteredHistory[i];
if (historyItem.message.role === "user") {
// Gather context items for user messages

View file

@ -365,15 +365,16 @@ class Ollama extends BaseLLM {
signal: AbortSignal,
options: CompletionOptions,
): AsyncGenerator<ChatMessage> {
const ollamaMessages = messages.map(this._convertToOllamaMessage);
const chatOptions: OllamaChatOptions = {
model: this._getModel(),
messages: messages.map(this._convertToOllamaMessage),
messages: ollamaMessages,
options: this._getModelFileParams(options),
keep_alive: options.keepAlive ?? 60 * 30, // 30 minutes
stream: options.stream,
// format: options.format, // Not currently in base completion options
};
if (options.tools?.length) {
if (options.tools?.length && ollamaMessages.at(-1)?.role === "user") {
chatOptions.tools = options.tools.map((tool) => ({
type: "function",
function: {
@ -447,6 +448,7 @@ class Ollama extends BaseLLM {
try {
const j = JSON.parse(chunk) as OllamaChatResponse;
const chatMessage = convertChatMessage(j);
yield chatMessage;
} catch (e) {
throw new Error(`Error parsing Ollama response: ${e} ${chunk}`);
}

View file

@ -23,7 +23,7 @@ export function toChatMessage(
if (typeof message.content === "string") {
return {
role: message.role,
content: message.content,
content: message.content === "" ? " " : message.content, // LM Studio API doesn't accept empty strings
};
} else if (!message.content.some((item) => item.type !== "text")) {
// If no multi-media is in the message, just send as text

View file

@ -11,15 +11,15 @@ export const PROVIDER_TOOL_SUPPORT: Record<
return true;
}
},
openai: (model) => {
if (
["gpt-4", "o1", "chatgpt-4o-latest"].some((part) =>
model.toLowerCase().startsWith(part),
)
) {
return true;
}
},
// openai: (model) => {
// if (
// ["gpt-4", "o1", "chatgpt-4o-latest"].some((part) =>
// model.toLowerCase().startsWith(part),
// )
// ) {
// return true;
// }
// },
// https://ollama.com/search?c=tools
ollama: (model) => {
if (

View file

@ -175,6 +175,7 @@
"@types/jest": "^29.5.12",
"@types/jquery": "^3.5.29",
"@types/jsdom": "^21.1.6",
"@types/json-schema": "^7.0.15",
"@types/mozilla-readability": "^0.2.1",
"@types/mustache": "^4.2.5",
"@types/node-fetch": "^2.6.11",

1
gui/package-lock.json generated
View file

@ -178,6 +178,7 @@
"@types/jest": "^29.5.12",
"@types/jquery": "^3.5.29",
"@types/jsdom": "^21.1.6",
"@types/json-schema": "^7.0.15",
"@types/mozilla-readability": "^0.2.1",
"@types/mustache": "^4.2.5",
"@types/node-fetch": "^2.6.11",

View file

@ -134,6 +134,7 @@ export const sessionSlice = createSlice({
state.mainEditorContentTrigger =
state.history[state.history.length - 2].editorState;
state.history = state.history.slice(0, -2);
// TODO is this logic correct for tool use conversations?
}
},
// Trigger value picked up by editor with isMainInput to set its content

View file

@ -27,12 +27,8 @@ export const streamNormalInput = createAsyncThunk<
throw new Error("Default model not defined");
}
console.log("YOO", messages);
const includeTools =
useTools &&
modelSupportsTools(defaultModel.title, defaultModel.provider) &&
messages.length >= 2 &&
messages[messages.length - 2].role === "user";
useTools && modelSupportsTools(defaultModel.model, defaultModel.provider);
// Send request
const gen = extra.ideMessenger.llmStreamChat(

View file

@ -110,7 +110,7 @@ export const streamResponseThunk = createAsyncThunk<
// Construct messages from updated history
const updatedHistory = getState().session.history;
const messages = constructMessages(
updatedHistory,
[...updatedHistory],
defaultModel.model,
defaultModel.provider,
useTools,