diff --git a/core/llm/constructMessages.ts b/core/llm/constructMessages.ts index 5b0d1b60b..02ad8364d 100644 --- a/core/llm/constructMessages.ts +++ b/core/llm/constructMessages.ts @@ -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 diff --git a/core/llm/llms/Ollama.ts b/core/llm/llms/Ollama.ts index 16f71cf15..1a08e0ad7 100644 --- a/core/llm/llms/Ollama.ts +++ b/core/llm/llms/Ollama.ts @@ -365,15 +365,16 @@ class Ollama extends BaseLLM { signal: AbortSignal, options: CompletionOptions, ): AsyncGenerator { + 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}`); } diff --git a/core/llm/openaiTypeConverters.ts b/core/llm/openaiTypeConverters.ts index d52bc73a7..0a500bc12 100644 --- a/core/llm/openaiTypeConverters.ts +++ b/core/llm/openaiTypeConverters.ts @@ -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 diff --git a/core/llm/toolSupport.ts b/core/llm/toolSupport.ts index 93d72751a..017ef7a88 100644 --- a/core/llm/toolSupport.ts +++ b/core/llm/toolSupport.ts @@ -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 ( diff --git a/extensions/vscode/package-lock.json b/extensions/vscode/package-lock.json index 389aeae95..b4712d8e2 100644 --- a/extensions/vscode/package-lock.json +++ b/extensions/vscode/package-lock.json @@ -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", diff --git a/gui/package-lock.json b/gui/package-lock.json index 4c735e3ef..ac7f6ce81 100644 --- a/gui/package-lock.json +++ b/gui/package-lock.json @@ -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", diff --git a/gui/src/redux/slices/sessionSlice.ts b/gui/src/redux/slices/sessionSlice.ts index a0c05caf5..ff3f90a9b 100644 --- a/gui/src/redux/slices/sessionSlice.ts +++ b/gui/src/redux/slices/sessionSlice.ts @@ -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 diff --git a/gui/src/redux/thunks/streamNormalInput.ts b/gui/src/redux/thunks/streamNormalInput.ts index d32e692c2..229140c8e 100644 --- a/gui/src/redux/thunks/streamNormalInput.ts +++ b/gui/src/redux/thunks/streamNormalInput.ts @@ -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( diff --git a/gui/src/redux/thunks/streamResponse.ts b/gui/src/redux/thunks/streamResponse.ts index 98a0d0195..fdea6d8fb 100644 --- a/gui/src/redux/thunks/streamResponse.ts +++ b/gui/src/redux/thunks/streamResponse.ts @@ -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,