Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
using System.Text.Json;
using CrestApps.Core.AI.Documents.Models;
using CrestApps.Core.AI.Extensions;
using CrestApps.Core.AI.Models;
using CrestApps.Core.AI.Orchestration;
using CrestApps.Core.AI.Tooling;
using Cysharp.Text;

using Microsoft.Extensions.AI;
using Microsoft.Extensions.DataIngestion;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace CrestApps.Core.AI.Documents.Tools;

Expand Down Expand Up @@ -155,6 +158,9 @@ sessionObj is AIChatSession session &&
return $"Document '{document.FileName}' is not a recognized tabular format. Use 'read_document' instead.";
}

var extension = Path.GetExtension(document.FileName);
Comment thread
MikeAlhayek marked this conversation as resolved.
Outdated
var options = arguments.Services.GetRequiredService<IOptions<ChatDocumentsOptions>>().Value;

// Reconstruct full text from chunks.
var chunkStore = arguments.Services.GetService<IAIDocumentChunkStore>();

Expand All @@ -169,6 +175,69 @@ sessionObj is AIChatSession session &&

if (chunks.Count == 0)
{
if (!options.EmbeddableFileExtensions.Contains(extension))
Comment thread
MikeAlhayek marked this conversation as resolved.
Outdated
{
var fileStore = arguments.Services.GetService<IDocumentFileStore>();

if (fileStore is null || string.IsNullOrWhiteSpace(document.StoredFilePath))
{
logger.LogWarning("AI tool '{ToolName}' failed: file store is not available or file path is missing for non-embeddable tabular document '{FileName}'.", Name, document.FileName);

return $"Document '{document.FileName}' has no extractable text content.";
}

var reader = arguments.Services.GetKeyedService<IngestionDocumentReader>(extension);

if (reader is null)
{
logger.LogWarning("AI tool '{ToolName}' failed: no document reader registered for non-embeddable tabular document '{FileName}'.", Name, document.FileName);

return $"Document '{document.FileName}' has no extractable text content.";
}

await using var stream = await fileStore.GetFileAsync(document.StoredFilePath);

if (stream is null)
{
logger.LogWarning("AI tool '{ToolName}' failed: tabular document file not found at path '{Path}'.", Name, document.StoredFilePath);

return $"Document '{document.FileName}' has no extractable text content.";
}

try
{
var mediaType = MediaTypeHelper.InferMediaType(extension, document.ContentType);

var ingestionDoc = await reader.ReadAsync(stream, document.FileName, mediaType, cancellationToken);

var tabularText = string.Join(Environment.NewLine, ingestionDoc.EnumerateContent()
.Select(element => element.Text)
.Where(content => !string.IsNullOrWhiteSpace(content)));

if (string.IsNullOrWhiteSpace(tabularText))
{
logger.LogWarning("AI tool '{ToolName}' failed: extracted text is empty for non-embeddable tabular document '{FileName}'.", Name, document.FileName);

return $"Document '{document.FileName}' has no extractable text content.";
}

var tabularContent = LimitTabularRows(tabularText, maxRows);

if (logger.IsEnabled(LogLevel.Debug))
{
logger.LogDebug("AI tool '{ToolName}' completed.", Name);
}

return $"[Tabular data from: {document.FileName}]\n\n{tabularContent}";
}
catch (Exception ex)
{
logger.LogWarning(ex, "AI tool '{ToolName}' failed: unable to read non-embeddable tabular document '{FileName}' from stored file.", Name, document.FileName);

return $"Document '{document.FileName}' has no extractable text content.";
}
}

logger.LogWarning("AI tool '{ToolName}' failed: no chunks found for document '{FileName}'.", Name, document.FileName);

return $"Document '{document.FileName}' has no extractable text content.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Answer only from the uploaded documents.
If the uploaded documents do not contain the answer, say so instead of using general knowledge.
{% endif %}
{% endif %}
{% endif %}
Comment thread
MikeAlhayek marked this conversation as resolved.

{% if hasUserSuppliedDocuments %}
### Available documents:
Expand Down
Loading