use binary data

This commit is contained in:
Jicheng Lu 2024-09-11 20:03:54 -05:00
parent 057190a245
commit b8f013aad2
3 changed files with 6 additions and 9 deletions

View file

@ -63,7 +63,7 @@ public interface IFileStorageService
#endregion
#region Knowledge
bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, Stream stream);
bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, BinaryData fileData);
/// <summary>
/// Delete files in a knowledge collection. If fileId is null, remove all files in the collection.

View file

@ -5,7 +5,7 @@ namespace BotSharp.Core.Files.Services;
public partial class LocalFileStorageService
{
public bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, Stream stream)
public bool SaveKnowledgeBaseFile(string collectionName, string vectorStoreProvider, string fileId, string fileName, BinaryData fileData)
{
if (string.IsNullOrWhiteSpace(collectionName)
|| string.IsNullOrWhiteSpace(vectorStoreProvider)
@ -26,9 +26,9 @@ public partial class LocalFileStorageService
var filePath = Path.Combine(dir, fileName);
using var fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
stream.CopyTo(fs);
using var ds = fileData.ToStream();
ds.CopyTo(fs);
fs.Flush();
fs.Close();
return true;
}
catch (Exception ex)

View file

@ -208,11 +208,8 @@ public partial class KnowledgeService
private bool SaveDocument(string collectionName, string vectorStoreProvider, string fileId, string fileName, byte[] bytes)
{
var fileStoreage = _services.GetRequiredService<IFileStorageService>();
using var stream = new MemoryStream(bytes);
stream.Position = 0;
var saved = fileStoreage.SaveKnowledgeBaseFile(collectionName.CleanStr(), vectorStoreProvider.CleanStr(), fileId, fileName, stream);
stream.Close();
var data = BinaryData.FromBytes(bytes);
var saved = fileStoreage.SaveKnowledgeBaseFile(collectionName.CleanStr(), vectorStoreProvider.CleanStr(), fileId, fileName, data);
return saved;
}