LlamaIndexTS: PineconeVectorStore always returns 'Empty Response'

What is the issue?

I am trying to use pinecone db along with llama index to create a RAG application. I was following your example closely but no matter what query I pass into my BaseQueryEngine I get the same response…

Response {
  metadata: {},
  response: 'Empty Response',
  sourceNodes: [
    Document {
      id_: '9dab9a39-c8fd-4c1c-a51a-33c5277c64fd',
      metadata: [Object],
      excludedEmbedMetadataKeys: [],
      excludedLlmMetadataKeys: [],
      relationships: {},
      hash: 'hasqQpeIQjqCSkn5snCmwKG2Ty2fM1hw4IAebcnoyO8=',
      text: '',
      embedding: [Array],
      textTemplate: '',
      metadataSeparator: '\n'
    },
    Document {
      id_: 'f76c78cd-84bb-4b81-826c-feb802171347',
      metadata: [Object],
      excludedEmbedMetadataKeys: [],
      excludedLlmMetadataKeys: [],
      relationships: {},
      hash: 'zzoar6NL5v1WZM0i05r/9iT7cR3REO+AWtGeNvxcwxk=',
      text: '',
      embedding: [Array],
      textTemplate: '',
      metadataSeparator: '\n'
    }
  ]
}

Here is the code for my next.js application.

import {
  PineconeVectorStore,
  serviceContextFromDefaults,
  SimpleNodeParser, SentenceSplitter, VectorStoreIndex,
  BaseQueryEngine,
} from "llamaindex";

async function initializeChatEngine(): Promise<BaseQueryEngine> {
  const pineconeVectorStore = new PineconeVectorStore();
  console.log("Pinecone Vector Store initialized successfully. ", await pineconeVectorStore.index());
  const serviceContext = serviceContextFromDefaults({
    nodeParser: new SimpleNodeParser({
      textSplitter: new SentenceSplitter(),
      includeMetadata: true,
      includePrevNextRel: true,
    }),
  });
  const index = await VectorStoreIndex.fromVectorStore(pineconeVectorStore, serviceContext);

  return index.asQueryEngine();
}


export async function getChatEngine(): Promise<BaseQueryEngine> {
  return initializeChatEngine();
}

this function then gets called here…

import { z } from "zod";
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
import { Role } from "~/types/message";
import { getChatEngine } from "~/server/ai/chat/chatEngine";

// Define the expected input for the chatbotResponse mutation
const chatbotInput = z.object({
  query: z.string(),
  history: z.array(
    z.object({
      role: z.string(),
      content: z.string(),
    }),
  ),
});

export const chatRouter = createTRPCRouter({
  chatbotResponse: protectedProcedure
    .input(chatbotInput)
    .mutation(async ({ input }) => {
      const question: string = input.query;
      const chatEngine = await getChatEngine();
      const res = await chatEngine.query({
        query: question,
      });
      console.log(res);
      return { role: Role.AI, content: res.response };
    }),
});

I have confirmed that the pineconeVectorStore is correct as the log output properly describes the index I am trying to use.

System information

  • “llamaindex”: “0.1.11”,
  • “next”: “^13.5.6”
  • @pinecone-database/pinecone”: “^1.1.2”
  • “typescript”: “^5.3.3”
  • node -v v21.2.0

Python side

All of the data inside the pinecone index is generated in a different codebase using Llama index python inside a jupyter lab. Whats weird is that when I run

query_engine = index.as_query_engine()
response = query_engine.query("...")

it works perfectly.

Here is my code for the python data processing side.

!pip install llama-index datasets pinecone-client openai transformers pypdf python-dotenv google

from dotenv import load_dotenv
import os
from pinecone import Pinecone
from pinecone import PodSpec
from llama_index.vector_stores import PineconeVectorStore
# Load environment variables from .env file
load_dotenv()

# find API key in console at app.pinecone.io
pinecone_api_key = os.getenv("PINECONE_API_KEY")
pinecone_env = os.getenv("PINECONE_ENVIRONMENT")
# pinecone_index = os.getenv("PINECONE_INDEX")
pinecone_index_name = "party-policies"


# Instantiate class instance with your API key
pc = Pinecone(api_key=pinecone_api_key)

# Create your index (can skip this step if your index already exists)
if pinecone_index_name not in pc.list_indexes().names():
    pc.create_index(
        name=pinecone_index_name,
        dimension=1536,
        metric="euclidean",
        spec=PodSpec(
            environment='us-east-1-aws', 
            pod_type='p1.x1'
        ),
    )
    print(f"creating index: '{pinecone_index_name}'")
else:
    print(f"'{pinecone_index_name}' already exists. Here is the index description: '{pc.describe_index(pinecone_index_name)}'")

# Initialize your index 
pinecone_index = pc.Index(pinecone_index_name)

from llama_index import VectorStoreIndex, SimpleDirectoryReader, StorageContext
from llama_index.vector_stores.pinecone import PineconeVectorStore
from IPython.display import Markdown, display

# load documents
path = "./documents/"
documents = SimpleDirectoryReader(path).load_data()
print(f"Loaded {len(documents)} docs")

if "OPENAI_API_KEY" not in os.environ:
    raise EnvironmentError(f"Environment variable OPENAI_API_KEY is not set")

vector_store = PineconeVectorStore(pinecone_index=pinecone_index)
storage_context = StorageContext.from_defaults(vector_store=vector_store)
index = VectorStoreIndex.from_documents(
    documents, storage_context=storage_context
)

query_engine = index.as_query_engine()
response = query_engine.query("who has the most policies about the environment?")
display(Markdown(f"<b>{response}</b>"))

Output: The New Democratic Party of Canada has the most policies about the environment.

Any ideas as to how I can fix this?

P.S. I also tried implementing a ContextChatEngine and using pinecone as the retriever but it would give answers that indicated it did not use the pinecone index at all.

About this issue

  • Original URL
  • State: closed
  • Created 4 months ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

@owencraston I just released a new version 0.1.12. PineconeVectorStore was updated and is using the latest dependencies from Pinecone. You might wanna have a look into that.

of populating the pinecone vector db through the jupyter lab and then referencing the database in my web app In Juptyer lab, you’re using Python, I assume? Currently, we don’t guarantee the exchangeability of data created with Python and TS. Better try ingesting the data using TS too.

Hi @owencraston, I have tried using your code in nextjs app but I cannot reproduce your issue. The chatEngine work well from my side.

Response {
  metadata: {},
  response: '1. The letter must be rectangular, with four square corners and parallel opposite sides.\n' +
    '2. The letter must not be more than 11-1/2 inches long, or more than 6-1/8 inches high, or more than 1/4-inch thick.\n' +
    '3. The letter must not weigh more than 3.5 ounces.',
  sourceNodes: [
    Document {
      id_: '6e868990-8dd3-4d8b-9797-068aaf599adc',
      metadata: [Object],
      excludedEmbedMetadataKeys: [],
      excludedLlmMetadataKeys: [],
      relationships: {},
      hash: '9PQCMV/81JPkyI/LG4jp1i4BvUN965AeAtUzEz4/7ZQ=',
      text: '......'
    },
    Document {
      id_: 'ed769176-d2f3-4fc6-b092-a4b782aa5f2a',
      metadata: [Object],
      excludedEmbedMetadataKeys: [],
      excludedLlmMetadataKeys: [],
      relationships: {},
      hash: '/MVSwhojWt0eFu6FjPqM3s0rLnrx7Op2iKeb+emJSEQ=',
      text: '......'
    }
  ]
}

My system information:

You can have a look at my PR here to see how that chat engine work in typescript template

@thucpn can you have a look at this, when you’re working on https://github.com/run-llama/LlamaIndexTS/pull/297/files ?