From 6891f2d884a8d5ddd7d0378eb5af73ac1c41344c Mon Sep 17 00:00:00 2001 From: Catherine Renelle <32781029+catrenelle@users.noreply.github.com> Date: Wed, 11 Feb 2026 19:26:50 -0500 Subject: [PATCH] Fix AI losing conversational context on terse follow-ups The tool-use nudge fired on every first response without tools, even when the conversation already had tool results the AI could reference. This forced the AI to make a new (broader) tool call instead of interpreting a short follow-up like "2026-02-11" in context of the prior exchange. Now the nudge only fires when there are no prior tool results in the conversation. Also added a system prompt rule telling the AI to interpret ambiguous follow-ups in context. Co-Authored-By: Claude Opus 4.6 --- src/Purrse.Api/Services/ChatService.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/Purrse.Api/Services/ChatService.cs b/src/Purrse.Api/Services/ChatService.cs index 1e4be23..759ead2 100644 --- a/src/Purrse.Api/Services/ChatService.cs +++ b/src/Purrse.Api/Services/ChatService.cs @@ -46,6 +46,7 @@ public class ChatService : IChatService - Respond in plain conversational English only. - Format financial amounts as currency (e.g. $1,234.56). - Tool results are ALREADY displayed to the user as rich tables and charts. Do NOT repeat or list the same data in your text response. Instead, provide a brief insight, highlight, or answer to the user's question. For example, if the user asks "what did I spend on groceries?" and the tool returns transactions, say something like "You spent $342.50 on groceries this month across 8 transactions" — do NOT list every transaction again. + - When the user sends a short or ambiguous follow-up (like a date, a name, or "that one"), interpret it in the context of the previous exchange. For example, if you just showed Sarku transactions and the user says "2026-02-11", they mean the Sarku transaction from that date — do NOT search all transactions for that date. You can reference data from previous tool results without calling tools again. Today's date is {0}. """; @@ -159,6 +160,9 @@ public class ChatService : IChatService ollamaMessages.Add(ollamaMsg); } + // Check if the conversation already has tool results from prior turns + var hasExistingToolResults = recentMessages.Any(m => m.Role == "tool"); + // Add current user message ollamaMessages.Add(new OllamaToolMessage { Role = "user", Content = request.Message }); @@ -238,9 +242,11 @@ public class ChatService : IChatService { var rawContent = response?.Message?.Content ?? "I'm sorry, I couldn't process that request."; - // If the model responded without calling any tools on the first attempt, - // it likely fabricated data or generated code. Nudge it to use tools. - if (toolResults.Count == 0 && i == 0) + // If the model responded without calling any tools on the first attempt + // AND there are no prior tool results in the conversation, it likely + // fabricated data. Nudge it to use tools. Skip the nudge when prior + // tool results exist — the model may be referencing earlier data. + if (toolResults.Count == 0 && i == 0 && !hasExistingToolResults) { _logger.LogWarning("Model responded without calling tools on first attempt, retrying with nudge"); ollamaMessages.Add(new OllamaToolMessage