Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding knowledge base cookbooks #1434

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions cookbook/knowledge/combined_kb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
from pathlib import Path

from phi.agent import Agent
from phi.knowledge.csv import CSVKnowledgeBase
from phi.knowledge.pdf import PDFKnowledgeBase, PDFUrlKnowledgeBase
from phi.knowledge.website import WebsiteKnowledgeBase
from phi.knowledge.combined import CombinedKnowledgeBase
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

# Create CSV knowledge base
csv_kb = CSVKnowledgeBase(
path=Path("data/csvs"),
vector_db=PgVector(
table_name="csv_documents",
db_url=db_url,
),
)

# Create PDF URL knowledge base
pdf_url_kb = PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=PgVector(
table_name="pdf_documents",
db_url=db_url,
),
)

# Create Website knowledge base
website_kb = WebsiteKnowledgeBase(
urls=["https://docs.phidata.com/introduction"],
max_links=10,
vector_db=PgVector(
table_name="website_documents",
db_url=db_url,
),
)

# Create Local PDF knowledge base
local_pdf_kb = PDFKnowledgeBase(
path="data/pdfs",
vector_db=PgVector(
table_name="pdf_documents",
db_url=db_url,
),
)

# Combine knowledge bases
knowledge_base = CombinedKnowledgeBase(
sources=[
csv_kb,
pdf_url_kb,
website_kb,
local_pdf_kb,
],
vector_db=PgVector(
table_name="combined_documents",
db_url=db_url,
),
)

# Initialize the Agent with the combined knowledge base
agent = Agent(
knowledge_base=knowledge_base,
add_references_to_prompt=True,
)

knowledge_base.load(recreate=False)

# Use the agent
agent.print_response("Ask me about something from the knowledge base", markdown=True)
28 changes: 28 additions & 0 deletions cookbook/knowledge/csv_kb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from pathlib import Path

from phi.agent import Agent
from phi.knowledge.csv import CSVKnowledgeBase
from phi.vectordb.pgvector import PgVector

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"


knowledge_base = CSVKnowledgeBase(
path=Path("data/csvs"),
vector_db=PgVector(
table_name="csv_documents",
db_url=db_url,
),
num_documents=5, # Number of documents to return on search
)
# Load the knowledge base
knowledge_base.load(recreate=False)

# Initialize the Agent with the knowledge_base
agent = Agent(
knowledge_base=knowledge_base,
add_references_to_prompt=True,
)

# Use the agent
agent.print_response("Ask me about something from the knowledge base", markdown=True)
27 changes: 27 additions & 0 deletions cookbook/knowledge/docx_kb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from pathlib import Path

from phi.agent import Agent
from phi.vectordb.pgvector import PgVector
from phi.knowledge.docx import DocxKnowledgeBase

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

# Create a knowledge base with the DOCX files from the data/docs directory
knowledge_base = DocxKnowledgeBase(
path=Path("data/docs"),
vector_db=PgVector(
table_name="docx_documents",
db_url=db_url,
),
)
# Load the knowledge base
knowledge_base.load(recreate=False)

# Create an agent with the knowledge base
agent = Agent(
knowledge_base=knowledge_base,
add_references_to_prompt=True,
)

# Ask the agent about the knowledge base
agent.print_response("Ask me about something from the knowledge base", markdown=True)
4 changes: 2 additions & 2 deletions cookbook/knowledge/json_kb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# Initialize the JSONKnowledgeBase
knowledge_base = JSONKnowledgeBase(
path=Path("data/docs"), # Table name: ai.json_documents
path=Path("data/json"), # Table name: ai.json_documents
vector_db=PgVector(
table_name="json_documents",
db_url=db_url,
Expand All @@ -18,7 +18,7 @@
# Load the knowledge base
knowledge_base.load(recreate=False)

# Initialize the Assistant with the knowledge_base
# Initialize the Agent with the knowledge_base
agent = Agent(
knowledge_base=knowledge_base,
add_references_to_prompt=True,
Expand Down
Loading