How k-electron/cfr-copilot works
CFR Copilot is a local FDA regulatory-compliance assistant. Users ask regulatory questions through the CLI or Streamlit chat; the agent can choose regulation search or structured recall queries, while plain and structured RAG offer direct retrieval and citation-checked answers. Ingestion loads eCFR sections and openFDA recalls into separate search and relational stores. The README also documents retrieval/answer evaluation and optional tracing. Edges use README statements or SOURCE INDEX references; unsampled subsystems are represented only where those references support them.
Main entry points: cli.py CLI · agent.py Tool-using agent · index.py CFR indexer · retriever.py Composed retriever
Components 28
Question answering
- Tool-using agent
src/cfr_copilot/agent/agent.py - Retrieval context
src/cfr_copilot/agent/context.py - Agent tools
src/cfr_copilot/agent/tools.py - Agent middleware
src/cfr_copilot/agent/middleware.py - RAG chain
src/cfr_copilot/chains/rag.py - Verified answers
src/cfr_copilot/chains/structured.py - Answer schemas
src/cfr_copilot/chains/schemas.py - Composed retriever
src/cfr_copilot/retrieval/retriever.py - Reranking
src/cfr_copilot/retrieval/rerank.py - Local model client
src/cfr_copilot/llm.py
Data ingestion
- eCFR loader
src/cfr_copilot/ingest/ecfr.py - Legal chunking
src/cfr_copilot/ingest/chunking.py - CFR indexer
src/cfr_copilot/ingest/index.py - Recall loader
src/cfr_copilot/ingest/openfda.py
Search and storage
- Hybrid vector store
src/cfr_copilot/retrieval/vectorstore.py - Full CFR sections
src/cfr_copilot/sections.py - Postgres pool
src/cfr_copilot/db.py - Configuration
src/cfr_copilot/config.py
Evaluation and observability
- Evaluation dataset
src/cfr_copilot/evaluation/dataset.py - Retrieval evaluation
src/cfr_copilot/evaluation/retrieval.py - Answer evaluation
src/cfr_copilot/evaluation/answers.py - Tracing
src/cfr_copilot/tracing.py
Other components
- User
- eCFR service
- openFDA service
- Ollama
Connections 37
- User → CLI: asks via CLI
README.md - User → Chat UI: asks in chat
README.md - CLI → Tool-using agent: runs agent
src/cfr_copilot/cli.py - Chat UI → Tool-using agent: streams agent
README.md - Tool-using agent → Retrieval context: uses context
src/cfr_copilot/agent/agent.py - Tool-using agent → Agent tools: dispatches tools
src/cfr_copilot/agent/agent.py - Tool-using agent → Agent middleware: installs middleware
src/cfr_copilot/agent/agent.py - Agent tools → Composed retriever: searches regulations
src/cfr_copilot/agent/tools.py - Agent tools → Postgres pool: queries recalls
src/cfr_copilot/agent/tools.py - CLI → RAG chain: runs direct RAG
src/cfr_copilot/cli.py - CLI → Verified answers: requests verified answer
src/cfr_copilot/cli.py - Verified answers → RAG chain: builds on RAG
src/cfr_copilot/chains/structured.py - Verified answers → Answer schemas: returns typed answer
src/cfr_copilot/chains/structured.py - RAG chain → Hybrid vector store: retrieves documents
src/cfr_copilot/chains/rag.py - Composed retriever → Hybrid vector store: searches candidates
src/cfr_copilot/retrieval/retriever.py - Composed retriever → Reranking: reranks candidates
src/cfr_copilot/retrieval/retriever.py - Composed retriever → Full CFR sections: expands sections
src/cfr_copilot/retrieval/retriever.py - RAG chain → Local model client: generates answer
src/cfr_copilot/chains/rag.py - Tool-using agent → Local model client: creates agent model
src/cfr_copilot/agent/agent.py - eCFR service → eCFR loader: provides CFR XML
src/cfr_copilot/ingest/ecfr.py - eCFR loader → Legal chunking: supplies sections
src/cfr_copilot/ingest/chunking.py - CLI → CFR indexer: runs indexing
src/cfr_copilot/cli.py - CFR indexer → eCFR loader: loads CFR
src/cfr_copilot/ingest/index.py - CFR indexer → Legal chunking: chunks sections
src/cfr_copilot/ingest/index.py - CFR indexer → Hybrid vector store: writes vectors
src/cfr_copilot/ingest/index.py - CFR indexer → Full CFR sections: stores full sections
src/cfr_copilot/ingest/index.py - openFDA service → Recall loader: provides recall data
src/cfr_copilot/ingest/openfda.py - Recall loader → Postgres pool: stores recalls
src/cfr_copilot/ingest/openfda.py - Full CFR sections → Postgres pool: reads and writes
src/cfr_copilot/sections.py - Hybrid vector store → Local model client: embeds queries
src/cfr_copilot/retrieval/vectorstore.py - Local model client → Ollama: calls local models
src/cfr_copilot/llm.py - CLI → Evaluation dataset: loads evaluation set
src/cfr_copilot/cli.py - Retrieval evaluation → Evaluation dataset: uses questions
src/cfr_copilot/evaluation/retrieval.py - Retrieval evaluation → Composed retriever: measures retrieval
src/cfr_copilot/evaluation/retrieval.py - Answer evaluation → Evaluation dataset: uses questions
src/cfr_copilot/evaluation/answers.py - Answer evaluation → Verified answers: evaluates answers
src/cfr_copilot/evaluation/answers.py - Chat UI → Tracing: optional tracing