Skip to main content
Give your chat experience document intelligence: ingest PDFs, run hybrid semantic + lexical retrieval, and stream grounded answers into CometChat.
  • Live (UI served from docs/ if deployed to GitHub Pages): static upload + ask demo
  • Source code: GitHub repository
  • Agent files: src/mastra/agents/*
  • Tools: src/mastra/tools/*
  • Workflow: src/mastra/workflows/pdf-workflow.ts
  • Server: src/server.ts

What you’ll build

  • Dual Mastra agents:
    • pdfAgent (multi‑PDF retrieval across all uploaded docs)
    • singlePdfAgent (restricted to latest uploaded PDF)
  • Deterministic retrieval + answer workflow (pdfWorkflow)
  • Express API with upload, ask (JSON), and streaming (SSE) endpoints
  • Persisted hybrid vector + BM25 store (.data/ namespace pdf)
  • Integration with CometChat AI Agents (endpoint wiring)


How it works

  • Users upload PDF(s) → server parses pages → chunks + embeddings generated → persisted in vector + manifest store.
  • Hybrid retrieval ranks candidate chunks: score = α * cosine + (1-α) * sigmoid(BM25) where α = hybridAlpha.
  • Multi‑query expansion (optional) generates paraphrased variants to boost recall.
  • Tools (retrieve-pdf-context, retrieve-single-pdf-context) return stitched context + source metadata.
  • Workflow (pdfWorkflow) orchestrates retrieval + answer; streaming endpoints emit meta then incremental token events.
  • Mastra agent(s) are exposed via REST endpoints you wire into CometChat AI Agents.

Repo layout (key files)

  • src/mastra/agents/pdf-agent.ts – multi‑PDF agent
  • src/mastra/agents/single-pdf-agent.ts – single latest PDF agent
  • src/mastra/tools/retrieve-pdf-context.ts / retrieve-single-pdf-context.ts – hybrid retrieval tools
  • src/mastra/workflows/pdf-workflow.ts – deterministic orchestration
  • src/lib/* – vector store, embeddings, manifest, PDF parsing
  • src/server.ts – Express API (upload, ask, streaming, manifest ops)
  • docs/index.html – optional static UI
  • .data/ – persisted vectors + manifest JSON

Prerequisites

  • Node.js 20+
  • OPENAI_API_KEY (embeddings + chat model)
  • A CometChat app (to register the agent)
  • (Optional) CORS_ORIGIN if restricting browser origins

Step 1 — Clone & install

Clone the example and install dependencies:
Create a .env with at least:

Step 2 — Define retrieval tools

File: src/mastra/tools/retrieve-pdf-context.ts (multi) & retrieve-single-pdf-context.ts (single)

Step 3 — Create agents

Files: src/mastra/agents/pdf-agent.ts, src/mastra/agents/single-pdf-agent.ts

Step 4 — Wire Mastra & workflow

File: src/mastra/index.ts registers agents + pdfWorkflow with storage (LibSQL or file‑backed).
Start the dev server:

Step 5 — Run locally


Step 6 — Upload and ask (API)

Tool input examples:
Fallback widens search (higher topK, more qVariants) if initial context is sparse.

JSON ask (Mastra dev agent route)

Mastra automatically exposes the API:
Expected response:

Step 7 — Deploy & connect to CometChat

  1. Deploy the project (e.g., Vercel, Railway, or AWS).
  2. Copy the deployed endpoint URL.
  3. In CometChat Dashboard → AI Agents, add a new agent:
    • Agent ID: knowledge
    • Endpoint: https://your-deployed-url/api/agents/knowledge/generate

Step 8 — Optimize & extend

  • Add more documents to the docs/ folder.
  • Use embeddings + vector DB (Pinecone, Weaviate) for large datasets.
  • Extend the agent with memory or multi-tool workflows.


Environment variables

.env example:

Endpoint summary

Curl Examples

Upload:
Ask (multi):
Stream (multi):
Ask (single):
Stream (single):

SSE Events


Tuning & retrieval knobs

Tip: For exploratory QA try topK=8, qVariants=5.

Troubleshooting & debugging

  • Enable internal logging (if available) to inspect scoring.
  • Inspect vectors: open .data/pdf-vectors.json.
  • Manifest corrupted? Delete .data/manifest.json and re‑upload.
  • Low lexical relevance? Lower hybridAlpha (e.g. 0.55).
  • Noise / irrelevant chunks? Reduce topK or lower qVariants.

Hardening & roadmap

  • SSE/WebSocket answer token streaming to clients (UI consumption)
  • Source highlighting + per‑chunk confidence
  • Semantic / layout‑aware advanced chunking
  • Vector deduplication & compression
  • Auth layer (API keys / JWT) & per‑user isolation
  • Background ingestion queue for large docs
  • Retrieval quality regression tests

Repository Links
  • Source: GitHub Repository
  • Multi agent: pdf-agent.ts
  • Single agent: single-pdf-agent.ts
  • Tools: retrieve-pdf-context.ts, retrieve-single-pdf-context.ts
  • Workflow: pdf-workflow.ts
  • Server: server.ts