You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行Azure Semantic Kernel示例时Pinecone索引创建报错求助

Semantic Kernel Pinecone存储异常处理

异常信息

调用kernel.Memory.SaveInformationAsync时抛出异常:

内存存储不支持索引创建,需手动创建或使用CreateIndexAsync方法,确保索引状态为就绪。

异常原因

Pinecone作为向量存储,要求提前创建对应名称的索引(对应Semantic Kernel中的Collection),而PineconeMemoryStore不会自动创建索引。同时需保证索引维度与使用的Embedding模型匹配(text-embedding-ada-002模型的维度为1536)。

解决方法

方法1:手动在Pinecone控制台创建索引

  1. 登录Pinecone控制台进入目标项目
  2. 创建新索引:
    • 索引名称与代码中的MemoryCollectionName完全一致
    • 维度设置为1536
    • 等待索引状态变为Ready后再运行代码

方法2:代码中自动创建索引

在初始化PineconeMemoryStore后,添加索引检查与创建逻辑:

PineconeMemoryStore memoryStore = new(pineconeEnvironment, apiKey);

// 检查索引是否存在,不存在则创建
var exists = await memoryStore.DoesCollectionExistAsync(MemoryCollectionName);
if (!exists)
{
    // 匹配text-embedding-ada-002的1536维度
    await memoryStore.CreateIndexAsync(MemoryCollectionName, 1536);
    // 等待索引就绪,可根据实际情况调整等待时长
    await Task.Delay(TimeSpan.FromSeconds(30));
}

// 后续Kernel构建逻辑保持不变
IKernel kernel = Kernel.Builder
    .WithOpenAITextCompletionService("text-davinci-003", openAiKey)
    .WithOpenAITextEmbeddingGenerationService("text-embedding-ada-002", openAiKey)
    .WithMemoryStorage(memoryStore)
    .Build();

修改后完整代码示例

public static async Task RunAsync()
{
    using (Log.VerboseCall())
    {
        string apiKey = "...xxxxxxxxxxxxxxxxxxx...";  // Pinecone API Key
        string pineconeEnvironment = "us-west1-gcp-free"; // Pinecone环境
        string openAiKey = "...xxxxxxxxxxxxxxxxxxxxx..."; // OpenAI API Key

        PineconeMemoryStore memoryStore = new(pineconeEnvironment, apiKey);

        // 检查并创建索引
        var exists = await memoryStore.DoesCollectionExistAsync(MemoryCollectionName);
        if (!exists)
        {
            await memoryStore.CreateIndexAsync(MemoryCollectionName, 1536);
            await Task.Delay(TimeSpan.FromSeconds(30));
        }

        IKernel kernel = Kernel.Builder
            .WithOpenAITextCompletionService("text-davinci-003", openAiKey)
            .WithOpenAITextEmbeddingGenerationService("text-embedding-ada-002", openAiKey)
            .WithMemoryStorage(memoryStore)
            .Build();


        Console.WriteLine("== Printing Collections in DB ==");

        IAsyncEnumerable<string> collections = memoryStore.GetCollectionsAsync();

        await foreach (string collection in collections)
        {
            Console.WriteLine(collection);
        }

        Console.WriteLine("== Adding Memories ==");

        Dictionary<string, object> metadata = new()
        {
            { "type", "text" },
            { "tags", new List<string>() { "memory", "cats" } }
        };

        string additionalMetadata = System.Text.Json.JsonSerializer.Serialize(metadata);

        try
        {
            string key1 = await kernel.Memory.SaveInformationAsync(MemoryCollectionName, "british short hair", "cat1", null, additionalMetadata);
            string key2 = await kernel.Memory.SaveInformationAsync(MemoryCollectionName, "orange tabby", "cat2", null, additionalMetadata);
            string key3 = await kernel.Memory.SaveInformationAsync(MemoryCollectionName, "norwegian forest cat", "cat3", null, additionalMetadata);

            Console.WriteLine("== Retrieving Memories Through the Kernel ==");
            MemoryQueryResult? lookup = await kernel.Memory.GetAsync(MemoryCollectionName, "cat1");
            Console.WriteLine(lookup != null ? lookup.Metadata.Text : "ERROR: memory not found");

            Console.WriteLine("== Retrieving Memories Directly From the Store ==");
            var memory1 = await memoryStore.GetAsync(MemoryCollectionName, key1);
            var memory2 = await memoryStore.GetAsync(MemoryCollectionName, key2);
            var memory3 = await memoryStore.GetAsync(MemoryCollectionName, key3);

            Console.WriteLine(memory1 != null ? memory1.Metadata.Text : "ERROR: memory not found");
            Console.WriteLine(memory2 != null ? memory2.Metadata.Text : "ERROR: memory not found");
            Console.WriteLine(memory3 != null ? memory3.Metadata.Text : "ERROR: memory not found");

            Console.WriteLine("== Similarity Searching Memories: My favorite color is orange ==");
            IAsyncEnumerable<MemoryQueryResult> searchResults = kernel.Memory.SearchAsync(MemoryCollectionName, "My favorite color is orange", 1, 0.8);

            await foreach (MemoryQueryResult item in searchResults)
            {
                Console.WriteLine(item.Metadata.Text + " : " + item.Relevance);
            }
        }
        catch (Exception ex)
        {
            Log.Verbose(ex);
        }
    }
}

内容的提问来源于stack exchange,提问作者Leon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:24:56