iangithub / devllmapp

極速ChatGPT開發者兵器指南一書範例源始碼
MIT License
16 stars 7 forks source link

請問KernelMemory 要如何使用Ollama 的向量資料庫,並存到 Qdrant #3

Closed melandz closed 4 months ago

melandz commented 4 months ago

目前測試執行沒辦法直接使用Ollama 的向量資料庫,請問要如何調整,謝謝。

//程式碼 using Microsoft.KernelMemory;

class Program { static async Task Main(string[] args) { var ollamaEmbeddingConfig = new OpenAIConfig { Endpoint = "http://localhost:11434/api", EmbeddingModel = "mxbai-embed-large", APIKey = "0" };

    var ollamaTextConfig = new OpenAIConfig
    {
        Endpoint = "http://localhost:11434/v1",
        TextModel = "8b",
        APIKey = "0"
    };

    var kernelMemory = new KernelMemoryBuilder()
        .WithOpenAITextGeneration(ollamaTextConfig)
        .WithOpenAITextEmbeddingGeneration(ollamaEmbeddingConfig)
        .WithQdrantMemoryDb("http://localhost:6333")
        .Build<MemoryServerless>();

    await ImportKm(kernelMemory);
}

static async Task ImportKm(MemoryServerless memory)
{
    string documentPath = Path.Combine(Directory.GetCurrentDirectory(), "道路交通管理處罰條例.pdf");
    await memory.ImportDocumentAsync(documentPath, documentId: "taiwan_traffic_law");
}

}

錯誤訊息

fail: Microsoft.KernelMemory.Pipeline.BaseOrchestrator[0] Pipeline start failed System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetEmbeddingsAsync(IList1 data, Kernel kernel, Nullable1 dimensions, CancellationToken cancellationToken) at Microsoft.SemanticKernel.AI.Embeddings.TextEmbeddingGenerationExtensions.GenerateEmbeddingAsync(ITextEmbeddingGenerationService generator, String text, CancellationToken cancellationToken) at Microsoft.KernelMemory.Handlers.GenerateEmbeddingsHandler.InvokeAsync(DataPipeline pipeline, CancellationToken cancellationToken) at Microsoft.KernelMemory.Pipeline.InProcessPipelineOrchestrator.RunPipelineAsync(DataPipeline pipeline, CancellationToken cancellationToken) at Microsoft.KernelMemory.Pipeline.BaseOrchestrator.ImportDocumentAsync(String index, DocumentUploadRequest uploadRequest, CancellationToken cancellationToken) Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SemanticKernel.Connectors.OpenAI.ClientCore.GetEmbeddingsAsync(IList1 data, Kernel kernel, Nullable1 dimensions, CancellationToken cancellationToken) at Microsoft.SemanticKernel.AI.Embeddings.TextEmbeddingGenerationExtensions.GenerateEmbeddingAsync(ITextEmbeddingGenerationService generator, String text, CancellationToken cancellationToken) at Microsoft.KernelMemory.Handlers.GenerateEmbeddingsHandler.InvokeAsync(DataPipeline pipeline, CancellationToken cancellationToken) at Microsoft.KernelMemory.Pipeline.InProcessPipelineOrchestrator.RunPipelineAsync(DataPipeline pipeline, CancellationToken cancellationToken) at Microsoft.KernelMemory.Pipeline.BaseOrchestrator.ImportDocumentAsync(String index, DocumentUploadRequest uploadRequest, CancellationToken cancellationToken) at Program.ImportKm(MemoryServerless memory) in /Program.cs:line 36 at Program.Main(String[] args) in /Program.cs:line 30 at Program.

(String[] args)

另外目前書中也是以OpenAI為主,請問是否要額外寫CustomEmbeddingGenerator?

iangithub commented 4 months ago

目前 Ollama 僅支援 OpenAI compatible API 相容,Embedding API 尚未支援,因此必須使用 CustomEmbeddingGenerator,繼承ITextEmbeddingGenerator 介面實作GenerateEmbeddingAsync

iangithub commented 4 months ago

我做了一個範例,可以試試看 https://github.com/iangithub/devllmapp/tree/main/OtherSample/ollamaEmbeddingSample

melandz commented 4 months ago

感謝您,我再來測試看看。