Repository: mem0ai/mem0
File: embedchain/vectordb/pineconedb.py (Python)

Code before:
    ):
        """add data in vector database

        :param documents: list of texts to add
        :type documents: List[str]
        :param metadatas: list of metadata associated with docs
        :type metadatas: List[object]
        :param ids: ids of docs
        :type ids: List[str]
        """

        docs = []
        if embeddings is None:
            embeddings = self.embedder.embedding_fn(documents)
        for id, text, metadata, embedding in zip(ids, documents, metadatas, embeddings):
            metadata["text"] = copy.copy(text)
            docs.append(
                {
                    "id": id,
                    "values": embedding,
                    "metadata": metadata,
                }
            )

        for i in range(0, len(docs), self.BATCH_SIZE):
            self.client.upsert(docs[i : i + self.BATCH_SIZE])

    def query(self, input_query: List[str], n_results: int, where: Dict[str, any], skip_embedding: bool) -> List[str]:
        """
        query contents from vector database based on vector similarity

        :param input_query: list of query string
        :type input_query: List[str]
        :param n_results: no of similar documents to fetch from database
        :type n_results: int
        :param where: Optional. to filter data
        :type where: Dict[str, any]
        :return: Database contents that are the result of the query
        :rtype: List[str]
        """
        if not skip_embedding:
            query_vector = self.embedder.embedding_fn([input_query])[0]
        else:
            query_vector = input_query
        contents = self.client.query(vector=query_vector, filter=where, top_k=n_results, include_metadata=True)
        embeddings = list(map(lambda content: content["metadata"]["text"], contents["matches"]))
        return embeddings

    def set_collection_name(self, name: str):
        """
        Set the name of the collection. A collection is an isolated space for vectors.

Review comment (suggestion):
I am not sure if the .add() is working as intended. I tried to do this but it doesn't work as expected:

```python
In [27]: pinecone_app.query("What is net worth of Elon Musk?")
Out[27]: "As of September 2021, Elon Musk's net worth is estimated to be around $300 billion, making him one of the richest individuals in the world. However, please note that net worth can fluctuate over time due to various factors such as stock market fluctuations and business ventures."

In [28]: pinecone_app.add("https://www.forbes.com/profile/elon-musk")
Out[28]: '8cf46026cabf9b05394a2658bd1fe890'

In [29]: pinecone_app.query("What is net worth of Elon Musk?")
Out[29]: "As of September 2021, Elon Musk's net worth is estimated to be around $300 billion, making him one of the richest individuals in the world. However, please note that net worth can fluctuate over time due to various factors such as stock market fluctuations and business ventures."
```

Since the forbes link contains the latest info, it should give a different answer after adding the web page. 


After looking at the pinecone UI, seems like the same doc gets indexed multiple times so likely an issue with the `.add()` function only. Please see the screenshot below.

<img width="1544" alt="Screenshot 2023-10-09 at 11 47 18 AM" src="https://github.com/embedchain/embedchain/assets/2945708/e944b733-ed96-481f-b38f-bf50fb3240e7">

Code after:
        skip_embedding: bool,
    ):
        """add data in vector database

        :param documents: list of texts to add
        :type documents: List[str]
        :param metadatas: list of metadata associated with docs
        :type metadatas: List[object]
        :param ids: ids of docs
        :type ids: List[str]
        """
        docs = []
        if embeddings is None:
            embeddings = self.embedder.embedding_fn(documents)
        for id, text, metadata, embedding in zip(ids, documents, metadatas, embeddings):
            metadata["text"] = text
            docs.append(
                {
                    "id": id,
                    "values": embedding,
                    "metadata": copy.deepcopy(metadata),
                }
            )

        for i in range(0, len(docs), self.BATCH_SIZE):
            self.client.upsert(docs[i : i + self.BATCH_SIZE])

    def query(self, input_query: List[str], n_results: int, where: Dict[str, any], skip_embedding: bool) -> List[str]:
        """
        query contents from vector database based on vector similarity

        :param input_query: list of query string
        :type input_query: List[str]
        :param n_results: no of similar documents to fetch from database
        :type n_results: int
        :param where: Optional. to filter data
        :type where: Dict[str, any]
        :return: Database contents that are the result of the query
        :rtype: List[str]
        """
        if not skip_embedding:
            query_vector = self.embedder.embedding_fn([input_query])[0]
        else:
            query_vector = input_query
        contents = self.client.query(vector=query_vector, filter=where, top_k=n_results, include_metadata=True)
        embeddings = list(map(lambda content: content["metadata"]["text"], contents["matches"]))
        return embeddings

    def set_collection_name(self, name: str):
        """
        Set the name of the collection. A collection is an isolated space for vectors.