Skip to content

Commit

Permalink
fix tool bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RomneyDa committed Jan 11, 2025
1 parent b35f00f commit 361b857
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 20 deletions.
7 changes: 5 additions & 2 deletions core/llm/constructMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions core/llm/llms/Ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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}`);
}
Expand Down
2 changes: 1 addition & 1 deletion core/llm/openaiTypeConverters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions core/llm/toolSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
1 change: 1 addition & 0 deletions extensions/vscode/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions gui/src/redux/slices/sessionSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 1 addition & 5 deletions gui/src/redux/thunks/streamNormalInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion gui/src/redux/thunks/streamResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 361b857

Please sign in to comment.