Skip to content

Resolved issue related to convertPdfToText #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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,5 +1,6 @@
using BotSharp.Abstraction.Knowledges.Models;
using BotSharp.Abstraction.Knowledges.Settings;
using Microsoft.AspNetCore.Http;

namespace BotSharp.OpenAPI.Controllers;

Expand All @@ -16,13 +17,6 @@ public KnowledgeBaseController(IKnowledgeService knowledgeService, IServiceProvi
_services = services;
}

[HttpPost("/knowledge-base/embed")]
public async Task EmbedKnowledge()
{
var chunks = await _knowledgeService.CollectChunkedKnowledge();
await _knowledgeService.EmbedKnowledge(chunks);
}

[HttpGet("/knowledge/{agentId}")]
public async Task<List<RetrievedResult>> RetrieveKnowledge([FromRoute] string agentId, [FromQuery(Name = "q")] string question)
{
Expand Down Expand Up @@ -70,8 +64,13 @@ public async Task<IActionResult> FeedKnowledge([FromRoute] string agentId, List<
foreach (var formFile in files)
{
var filePath = Path.GetTempFileName();
using var stream = System.IO.File.Create(filePath);
await formFile.CopyToAsync(stream);


using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
await formFile.CopyToAsync(stream);
await stream.FlushAsync(); // Ensure all data is written to the file
}

var content = await textConverter.ConvertPdfToText(filePath, startPageNum, endPageNum);

Expand All @@ -84,6 +83,9 @@ await _knowledgeService.Feed(new KnowledgeFeedModel
AgentId = agentId,
Content = content
});

// Delete the temp file after processing to clean up
System.IO.File.Delete(filePath);
}

return Ok(new { count = files.Count, size });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,24 @@ namespace BotSharp.Plugin.KnowledgeBase.Services;

public class PigPdf2TextConverter : IPdf2TextConverter
{
public async Task<string> ConvertPdfToText(string filePath, int? startPageNum, int? endPageNum)
public Task<string> ConvertPdfToText(string filePath, int? startPageNum, int? endPageNum)
{
return await OpenPdfDocumentAsync(filePath, startPageNum, endPageNum);
// since PdfDocument.Open is not async, we dont need to make this method async
// if you need this method to be async, consider wrapping the call in Task.Run for CPU-bound work
return Task.FromResult(OpenPdfDocument(filePath, startPageNum, endPageNum));
}

private async Task<string> OpenPdfDocumentAsync(string filePath, int? startPageNum, int? endPageNum)
private string OpenPdfDocument(string filePath, int? startPageNum, int? endPageNum)
{
var document = PdfDocument.Open(filePath);
var content = "";
using var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
using var document = PdfDocument.Open(fileStream);
var content = new StringBuilder();
foreach (Page page in document.GetPages())
{
if (startPageNum.HasValue && page.Number < startPageNum.Value)
{
continue;
}

if (endPageNum.HasValue && page.Number > endPageNum.Value)
{
continue;
}
content += page.Text;
if (startPageNum.HasValue && page.Number < startPageNum.Value) continue;
if (endPageNum.HasValue && page.Number > endPageNum.Value) continue;
content.Append(page.Text);
}
return content;
return content.ToString();
}
}