Files
agno/cookbook/agent_concepts/knowledge/gcs_pdf_kb.py
T
Guy Rosin 0e139025c5 [refactor] Migrate from psycopg2 to psycopg (v3) (#3984)
## Summary

This PR migrates the codebase from psycopg2 to psycopg (v3). 

Most of the codebase was already using psycopg - what remained was
mainly `PostgresTools`, as well as some comments and dependencies.

Breaking change: I've changed the related dependency group name from
`agno[psycopg2]` to `agno[psycopg]`.

## Type of change

- [ ] Bug fix
- [ ] New feature
- [x] Breaking change
- [x] Improvement
- [ ] Model update
- [ ] Other:

---

## Checklist

- [x] Code complies with style guidelines
- [x] Ran format/validation scripts (`./scripts/format.sh` and
`./scripts/validate.sh`)
- [x] Self-review completed
- [x] Documentation updated (comments, docstrings)
- [x] Examples and guides: Relevant cookbook examples have been included
or updated (if applicable)
- [x] Tested in clean environment
- [x] Tests added/updated (if applicable)
2025-07-31 17:41:56 +02:00

30 lines
1.3 KiB
Python

"""
This agent answers questions using knowledge from a PDF stored in a Google Cloud Storage (GCS) bucket.
Setup Steps:
1. Install required libraries: agno, google-cloud-storage, psycopg-binary (for PostgreSQL vector DB).
2. Set up your GCS bucket and upload your PDF file.
3. For public GCS buckets: No authentication needed, just set the bucket and PDF path.
4. For private GCS buckets:
- Grant the service account Storage Object Viewer access to the bucket via Google Cloud Console
- Export GOOGLE_APPLICATION_CREDENTIALS with the path to your service account JSON before running the script
5. Update 'bucket_name' and 'blob_name' in the script to your PDF's location.
6. Run the script to load the knowledge base and ask questions.
"""
from agno.agent import Agent
from agno.knowledge.gcs.pdf import GCSPDFKnowledgeBase
from agno.vectordb.pgvector import PgVector
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
knowledge_base = GCSPDFKnowledgeBase(
bucket_name="your-gcs-bucket",
blob_name="path/to/your.pdf",
vector_db=PgVector(table_name="recipes", db_url=db_url),
)
knowledge_base.load(recreate=False) # Comment out after first run
agent = Agent(knowledge=knowledge_base, search_knowledge=True)
agent.print_response("How to make Thai curry?", markdown=True)