Private
Public Access
1
0

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 <noreply@anthropic.com>
This commit is contained in:
Catherine Renelle
2026-02-11 19:26:50 -05:00
parent 3402a5210e
commit 6891f2d884
+9 -3
View File
@@ -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