You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Program.cs 2.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Reflection;
  2. using Microsoft.Extensions.Logging;
  3. using Microsoft.SemanticKernel.Connectors.OpenAI;
  4. using Microsoft.SemanticKernel.Connectors.Qdrant;
  5. using Microsoft.SemanticKernel.Memory;
  6. using UglyToad.PdfPig;
  7. using UglyToad.PdfPig.DocumentLayoutAnalysis.TextExtractor;
  8. public sealed class Program
  9. {
  10. private const string WafFileName = "azure-well-architected.pdf";
  11. static async Task Main()
  12. {
  13. var kernelSettings = KernelSettings.LoadSettings();
  14. using ILoggerFactory loggerFactory = LoggerFactory.Create(builder =>
  15. {
  16. builder
  17. .SetMinimumLevel(kernelSettings.LogLevel ?? LogLevel.Warning)
  18. .AddConsole()
  19. .AddDebug();
  20. });
  21. var memoryBuilder = new MemoryBuilder();
  22. var memory = memoryBuilder.WithLoggerFactory(loggerFactory)
  23. .WithQdrantMemoryStore(kernelSettings.QdrantEndpoint, 1536)
  24. .WithAzureOpenAITextEmbeddingGeneration(kernelSettings.EmbeddingDeploymentOrModelId, kernelSettings.Endpoint, kernelSettings.ApiKey)
  25. .Build();
  26. await ImportDocumentAsync(memory, WafFileName).ConfigureAwait(false);
  27. }
  28. public static async Task ImportDocumentAsync(ISemanticTextMemory memory, string filename)
  29. {
  30. var asm = Assembly.GetExecutingAssembly();
  31. var currentDirectory = Path.GetDirectoryName(asm.Location);
  32. if (currentDirectory is null)
  33. {
  34. throw new DirectoryNotFoundException($"Could not find directory for assembly '{asm}'.");
  35. }
  36. var filePath = Path.Combine(currentDirectory, filename);
  37. using var pdfDocument = PdfDocument.Open(File.OpenRead(filePath));
  38. var pages = pdfDocument.GetPages();
  39. foreach (var page in pages)
  40. {
  41. try
  42. {
  43. var text = ContentOrderTextExtractor.GetText(page);
  44. var descr = text.Take(100);
  45. await memory.SaveInformationAsync(
  46. collection: "waf",
  47. text: text,
  48. id: $"{Guid.NewGuid()}",
  49. description: $"Document: {descr}").ConfigureAwait(false);
  50. }
  51. catch (Exception ex)
  52. {
  53. Console.WriteLine(ex.Message);
  54. }
  55. }
  56. }
  57. }