using Microsoft.KernelMemory; using System.Net.Http; var kmServiceEndpoint = "http://localhost:5000/"; var httpClient = new HttpClient(); httpClient.Timeout = TimeSpan.FromMinutes(10); var kmClient = new MemoryWebClient(kmServiceEndpoint, client:httpClient);
如果部署到正式環境,請記得調整 kmServiceEndpoint 的值。
3.呼叫`ImportDocumentAsync`來匯入文件
var docPath = @"C:\Projects\請假規則.docx"; var tenantId = "gss"; var documentId = "hr001"; await kmClient.ImportDocumentAsync(docPath, index: tenantId, documentId: documentId);
註: documentId 可以讓它自動產生,或是指定一個值給它(最好給英數字,不要給中文哦~)
4.透過`IsDocumentReadyAsync`來查看是否匯入完成
var isDocReady = await kmClient.IsDocumentReadyAsync(documentId, tenantId); if(isDocReady) Console.WriteLine("Document ingestion is complete."); else Console.WriteLine("Document is not ready yet.");
5.進行RAG提問
var question = "請假6天,需要簽到那位主管核准? 要事前幾天請呢?"; Console.WriteLine($"Question: {question}"); Console.Write("\nAnswer: "); var answerStream = kmClient.AskStreamingAsync(question, minRelevance: 0.7, options: new SearchOptions { Stream = true }, index: tenantId);
List sources = []; await foreach (var answer in answerStream) { Console.Write(answer.Result); sources.AddRange(answer.RelevantSources); await Task.Delay(5); }