Skip to content

Commit

Permalink
Showing 2 changed files with 24 additions and 16 deletions.
17 changes: 7 additions & 10 deletions cookbook/integrations/pinecone/assistant.py
Original file line number Diff line number Diff line change
@@ -8,16 +8,13 @@
from phi.vectordb.pineconedb import PineconeDB

api_key = os.getenv("PINECONE_API_KEY")
index_name = "recipes"
index_name = "thai-recipe-index"

vector_db = PineconeDB(
name=index_name,
dimension=1536,
spec={
"region": "us-west-2",
"pod_type": "p1.x1",
},
metric="cosine",
spec={"serverless": {"cloud": "aws", "region": "us-west-2"}},
api_key=api_key,
)

@@ -27,20 +24,20 @@
)

# Comment out after first run
# knowledge_base.load(recreate=False)
knowledge_base.load(recreate=False, upsert=True)


def pdf_assistant(user: str = "user"):
def pinecone_assistant(user: str = "user"):
run_id: Optional[str] = None

assistant = Assistant(
run_id=run_id,
user_id=user,
knowledge_base=knowledge_base,
# tool_calls=True adds functions to
# search the knowledge base and chat history
tool_calls=True,
use_tools=True,
show_tool_calls=True,
debug_mode=True,
# Uncomment the following line to use traditional RAG
# add_references_to_prompt=True,
)
@@ -59,4 +56,4 @@ def pdf_assistant(user: str = "user"):


if __name__ == "__main__":
typer.run(pdf_assistant)
typer.run(pinecone_assistant)
23 changes: 17 additions & 6 deletions phi/vectordb/pineconedb/pineconedb.py
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
from phi.utils.log import logger
from pinecone.core.client.api.manage_indexes_api import ManageIndexesApi
from pinecone.models import ServerlessSpec, PodSpec

from pinecone.core.client.models import Vector

class PineconeDB(VectorDb):
"""A class representing a Pinecone database.
@@ -189,7 +189,7 @@ def upsert(
batch_size: Optional[int] = None,
show_progress: bool = False,
) -> None:
"""Upsert documents into the index.
"""insert documents into the index.
Args:
documents (List[Document]): The documents to upsert.
@@ -198,7 +198,18 @@ def upsert(
show_progress (bool, optional): Whether to show progress during upsert. Defaults to False.
"""
vectors = [{"id": doc.id, "values": doc.embedding, "metadata": doc.meta_data} for doc in documents]

vectors = []
for document in documents:
document.embed(embedder=self.embedder)
document.meta_data["text"] = document.content
vectors.append(
Vector(
id=document.id,
values=document.embedding,
metadata=document.meta_data,
)
)
self.index.upsert(
vectors=vectors,
namespace=namespace,
@@ -236,7 +247,6 @@ def search(
namespace: Optional[str] = None,
filter: Optional[Dict[str, Union[str, float, int, bool, List, dict]]] = None,
include_values: Optional[bool] = None,
include_metadata: Optional[bool] = None,
) -> List[Document]:
"""Search for similar documents in the index.
@@ -253,6 +263,7 @@ def search(
"""
query_embedding = self.embedder.get_embedding(query)

if query_embedding is None:
logger.error(f"Error getting embedding for Query: {query}")
return []
@@ -263,11 +274,11 @@ def search(
namespace=namespace,
filter=filter,
include_values=include_values,
include_metadata=include_metadata,
include_metadata=True,
)
return [
Document(
content=result.metadata.get("text", "") if result.metadata is not None else "",
content=(result.metadata.get("text", "") if result.metadata is not None else ""),
id=result.id,
embedding=result.values,
meta_data=result.metadata,

0 comments on commit 55646cc

Please sign in to comment.