Add logging and retry-without-tools for empty AI responses
When the model returns empty content with page context present, the 15 tool definitions likely overwhelm the model's attention. Now on empty response, the request is retried with tools stripped so the model focuses on answering from the page data. Also added diagnostic logging for page context receipt and empty response details. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -171,6 +171,13 @@ public class ChatService : IChatService
|
||||
if (!string.IsNullOrEmpty(request.PageContext))
|
||||
{
|
||||
userContent = $"{request.Message}\n\n[Current page data]\n{request.PageContext}";
|
||||
_logger.LogInformation("Page context attached — {Length} chars, preview: {Preview}",
|
||||
request.PageContext.Length,
|
||||
request.PageContext.Length > 200 ? request.PageContext[..200] + "..." : request.PageContext);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogInformation("No page context attached to message");
|
||||
}
|
||||
ollamaMessages.Add(new OllamaToolMessage { Role = "user", Content = userContent });
|
||||
|
||||
@@ -248,9 +255,31 @@ public class ChatService : IChatService
|
||||
}
|
||||
else
|
||||
{
|
||||
var rawContent = string.IsNullOrWhiteSpace(response?.Message?.Content)
|
||||
? "I'm sorry, I couldn't process that request."
|
||||
: response!.Message!.Content;
|
||||
var rawContent = response?.Message?.Content;
|
||||
|
||||
// If the model returned empty content and page context is present,
|
||||
// the tool definitions may be overwhelming the model. Retry without
|
||||
// tools so it can focus on answering from the page data.
|
||||
if (string.IsNullOrWhiteSpace(rawContent) && !string.IsNullOrEmpty(request.PageContext) && i == 0)
|
||||
{
|
||||
_logger.LogWarning("Model returned empty content with page context present — retrying without tools");
|
||||
var retryRequest = new OllamaToolChatRequest
|
||||
{
|
||||
Model = modelName,
|
||||
Messages = ollamaMessages,
|
||||
Stream = false,
|
||||
Tools = null // drop tools so the model focuses on the page data
|
||||
};
|
||||
var retryResponse = await CallOllamaAsync(ollamaUrl, retryRequest);
|
||||
rawContent = retryResponse?.Message?.Content;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(rawContent))
|
||||
{
|
||||
_logger.LogWarning("Model returned empty content (no tool calls). ToolCalls: {ToolCalls}, Done: {Done}",
|
||||
response?.Message?.ToolCalls?.Count ?? -1, response?.Done);
|
||||
rawContent = "I'm sorry, I couldn't process that request.";
|
||||
}
|
||||
|
||||
// If the model responded without calling any tools on the first attempt
|
||||
// AND there are no prior tool results AND no page context data, it
|
||||
|
||||
Reference in New Issue
Block a user