phidata icon indicating copy to clipboard operation
phidata copied to clipboard

Knowledge base without pgvector

Open sridharaiyer opened this issue 9 months ago • 2 comments

Hello team. Is there a way to use phidata as a RAG, where the vectors are stored in a vectordb such as chroma, dynamically created in the same directory, when I run the application using Streamlit?

Reason:

  1. Many a times, my consumers won't prefer spinning up a postgres docker container
  2. What if I want to deploy my streamlit app?
  3. If the postgres is not cleared of its old knowledge base, the LLM can get confused when I keep adding more documents.

sridharaiyer avatar May 16 '24 18:05 sridharaiyer

@sridharaiyer , we are close to adding chromadb (S/O @ysolanky). Within the week it should be out.

We have current integrations with Pinecone, Qdrant, Lancedb, Singlestore,

jacobweiss2305 avatar May 16 '24 19:05 jacobweiss2305

@sridharaiyer checkout lancedb its dynamic and can be deployed anywhere

Lancedb Assistant

1. Create a virtual environment

python3 -m venv ~/.venvs/aienv
source ~/.venvs/aienv/bin/activate

2. Install libraries

pip install -U lancedb pypdf pandas openai phidata

3. Run Assistant

python cookbook/integrations/lancedb/assistant.py

Assistant

cookbook/integrations/lancedb/assistant.py
import typer
from rich.prompt import Prompt
from typing import Optional

from phi.assistant import Assistant
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.lancedb.lancedb import LanceDb

# type: ignore
db_url = "/tmp/lancedb"

knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=LanceDb(table_name="recipes", uri=db_url),
)

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


def pdf_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
        use_tools=True,
        show_tool_calls=True,
        # Uncomment the following line to use traditional RAG
        # add_references_to_prompt=True,
    )
    if run_id is None:
        run_id = assistant.run_id
        print(f"Started Run: {run_id}\n")
    else:
        print(f"Continuing Run: {run_id}\n")

    while True:
        message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
        if message in ("exit", "bye"):
            break
        assistant.print_response(message)


if __name__ == "__main__":
    typer.run(pdf_assistant)

jacobweiss2305 avatar May 16 '24 20:05 jacobweiss2305

Thanks for the guidance. Closing this ticket.

sridharaiyer avatar May 17 '24 03:05 sridharaiyer