from phi.agent import Agent
from phi.storage.agent.postgres import PgAgentStorage
from phi.knowledge.pdf import PDFUrlKnowledgeBase
from phi.vectordb.pgvector import PgVector2

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

agent = Agent(
storage=PgAgentStorage(table_name="recipe_agent", db_url=db_url),
knowledge_base=PDFUrlKnowledgeBase(
urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=PgVector2(collection="recipe_documents", db_url=db_url),
), # Show tool calls in the response
show_tool_calls=True, # Enable the agent to search the knowledge base
search_knowledge=True, # Enable the agent to read the chat history
read_chat_history=True,
)

# Comment out after first run

agent.knowledge_base.load(recreate=False) # type: ignore

agent.print_response("How do I make pad thai?", markdown=True)

PgVectorDb Params

ParameterTypeDefaultDescription
table_namestr-The name of the table to use.
schemastr-The schema to use.
db_urlstr-The database URL to connect to.
db_engineEngine-The database engine to use.
embedderEmbedder-The embedder to use.
search_typeSearchTypevectorThe search type to use.
vector_indexUnion[Ivfflat, HNSW]-The vector index to use.
distanceDistancecosineThe distance to use.
prefix_matchbool-Whether to use prefix matching.
vector_score_weightfloat0.5Weight for vector similarity in hybrid search. Must be between 0 and 1.
content_languagestr-The content language to use.
schema_versionint-The schema version to use.
auto_upgrade_schemabool-Whether to auto upgrade the schema.