ChatApi/Data/TextData.cs
2024-12-22 15:49:43 -03:00

74 lines
2.7 KiB
C#

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Embeddings;
using System.Text;
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
namespace ChatApi.Data
{
public class TextData
{
private readonly ITextEmbeddingGenerationService _textEmbeddingGenerationService;
private readonly SharepointDomvsService _sharepointDomvsService;
public TextData(ITextEmbeddingGenerationService textEmbeddingGenerationService, SharepointDomvsService sharepointDomvsService)
{
_textEmbeddingGenerationService = textEmbeddingGenerationService;
_sharepointDomvsService = sharepointDomvsService;
}
public async Task SalvarTextoComEmbeddingNoMongoDB(string textoCompleto)
{
var textoArray = new List<string>();
string[] textolinhas = textoCompleto.Split(
new string[] { "\n" },
StringSplitOptions.None
);
var title = textolinhas[0];
var builder = new StringBuilder();
foreach (string line in textolinhas)
{
if (line.StartsWith("**") || line.StartsWith("\r**"))
{
if (builder.Length > 0)
{
textoArray.Add(title.Replace("**", "").Replace("\r", "") + ": " + Environment.NewLine + builder.ToString());
builder = new StringBuilder();
title = line;
}
}
else
{
builder.AppendLine(line);
}
}
foreach(var item in textoArray)
{
await SalvarNoMongoDB(item);
}
}
private async Task SalvarNoMongoDB(string texto)
{
// Gerar embedding para o texto
var embedding = await _textEmbeddingGenerationService.GenerateEmbeddingAsync(texto);
// Converter embedding para um formato serializável (como um array de floats)
var embeddingArray = embedding.ToArray().Select(e => (double)e).ToArray();
// Criar documento MongoDB com conteúdo e embedding
var documento = new TextoComEmbedding
{
Conteudo = texto,
Embedding = embeddingArray
};
await _sharepointDomvsService.CreateAsync(documento);
}
}
}
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.