Skip to main content

AI agent for drug information queries using RAG to minimize hallucination

Project description

Pharma AI Agent

Python 3.10+ License: MIT CI

Ask about drugs. Get evidence-based answers.

An AI agent for drug information queries that uses RAG (Retrieval Augmented Generation) to ground every response in verified pharmacological data. No hallucinated drug interactions. No fabricated dosage recommendations. Just evidence.

Why?

LLMs hallucinate drug information. They invent interactions that don't exist, miss critical ones that do, and confidently state incorrect dosages. In healthcare, hallucination isn't just wrong -- it's dangerous.

Pharma AI Agent solves this by:

  1. Embedding verified data -- 200+ drug interactions and 30+ supplement safety profiles sourced from FDA, NIH, and WHO
  2. Retrieving before generating -- Every query first searches the knowledge base; the LLM only sees verified data
  3. Checking after generating -- Safety guardrails validate every response before it reaches the user
  4. Always citing sources -- No answer without a reference

Demo

from pharma_ai_agent import PharmaAgent

agent = PharmaAgent(provider="openai", api_key="sk-...")

response = agent.ask("Can I take ibuprofen with warfarin?")
print(response.answer)
# => [MAJOR INTERACTION] Warfarin + Ibuprofen
#    NSAIDs inhibit platelet aggregation and may cause GI bleeding;
#    combined with warfarin's anticoagulant effect, bleeding risk
#    increases significantly.
#    Management: Avoid concurrent use if possible...
#    Sources: FDA Drug Safety Communication (2015), NIH DailyMed

print(response.safety_score)  # 0.95
print(response.sources)       # ['FDA Drug Safety Communication (2015)', ...]

Batch Check

results = agent.check_medications(["warfarin", "ibuprofen", "vitamin D", "fish oil"])
for pair, result in results.items():
    if result["found"]:
        print(f"[{result['severity'].upper()}] {pair[0]} + {pair[1]}")

Quick Start

Install

pip install pharma-ai-agent

Configure

# Set your LLM provider API key
export OPENAI_API_KEY=sk-...
# Or for Anthropic:
# export ANTHROPIC_API_KEY=sk-ant-...
# export PHARMA_PROVIDER=anthropic

Use

from pharma_ai_agent import PharmaAgent

agent = PharmaAgent()
response = agent.ask("Is it safe to take vitamin D with atorvastatin?")
print(response.answer)

CLI

pharma-ai-agent
# Or run offline (knowledge base only, no LLM):
pharma-ai-agent --offline

Architecture

User Query
    |
    v
[Pre-Check Safety] -- block harmful queries
    |
    v
[Knowledge Retrieval] -- BM25 text search over embedded DB
    |                     200+ drug interactions
    |                     30+ supplement profiles
    v
[Context Injection] -- verified data injected into system prompt
    |
    v
[LLM Generation] -- OpenAI or Anthropic with tool use
    |                 interaction_checker | dosage_checker
    |                 drug_lookup | source_finder
    v
[Post-Check Safety] -- validate response, check for unsafe advice
    |
    v
[Disclaimer Injection] -- auto-append medical disclaimer
    |
    v
Structured Response
  .answer           -- natural language response
  .interactions     -- structured interaction data
  .sources          -- FDA/NIH/WHO citations
  .safety_score     -- 0.0-1.0 safety rating
  .disclaimer       -- legal disclaimer

Features

Knowledge Base (Zero Infrastructure)

  • No vector database required -- uses BM25-like text matching
  • No embedding model needed -- pure algorithmic retrieval
  • Embedded data -- ships with 200+ verified drug interactions
  • Extensible -- add custom interactions programmatically

Safety First

  • Pre-check: Blocks queries about self-harm, drug abuse, synthesis
  • Post-check: Flags responses with false assurances or missing citations
  • Auto-disclaimer: Every response includes a medical disclaimer
  • Source requirement: Refuses to answer without verified data
  • llm-medical-guard integration: Optional enhanced safety checks

Provider Agnostic

  • OpenAI: GPT-4o, GPT-4-turbo, GPT-3.5-turbo
  • Anthropic: Claude Sonnet, Claude Haiku, Claude Opus
  • Easy to add new providers

Tools

The agent uses structured tools rather than free-form generation:

Tool Purpose
interaction_checker Check drug pairs against verified database
dosage_checker Validate dosages against known safety limits
drug_lookup Get comprehensive drug/supplement information
source_finder Find FDA/NIH/WHO citations

Multilingual

  • English (en) and Korean (ko) supported
  • System prompts, disclaimers, and safety messages localized

Configuration

from pharma_ai_agent import PharmaAgent, AgentConfig

config = AgentConfig(
    provider="openai",           # "openai" or "anthropic"
    api_key="sk-...",            # or set via env var
    model="gpt-4o",             # model override
    locale="en",                 # "en" or "ko"
    strict_mode=True,            # enforce all safety checks
    temperature=0.1,             # low temp for factual responses
    max_tokens=2048,             # response length limit
    require_sources=True,        # refuse to answer without sources
)

agent = PharmaAgent(config=config)

Environment Variables

Variable Default Description
OPENAI_API_KEY -- OpenAI API key
ANTHROPIC_API_KEY -- Anthropic API key
PHARMA_PROVIDER openai LLM provider
PHARMA_MODEL (auto) Model name override
PHARMA_LOCALE en Response language
PHARMA_STRICT_MODE true Strict safety checks

Data Sources

All embedded data is sourced from public, authoritative databases:

  • FDA Drug Interaction Table -- Substrates, Inhibitors, and Inducers
  • NIH DailyMed -- Drug labeling and safety information
  • NIH Office of Dietary Supplements -- Supplement fact sheets
  • NIH NCCIH -- Herbal supplement safety data
  • WHO Essential Medicines -- Global drug safety standards
  • Published clinical trials -- Peer-reviewed interaction studies

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feat/my-feature)
  3. Add tests for new functionality
  4. Ensure all tests pass (pytest tests/ -v)
  5. Submit a pull request

Adding Drug Interactions

When adding new interactions to drug_db.py:

  • Include both generic and brand names
  • Cite at least one authoritative source (FDA, NIH, WHO)
  • Provide both clinical and patient-friendly descriptions
  • Include management recommendations
  • Set appropriate severity (major/moderate/minor)

Related Projects

Part of the Pillright open-source healthcare ecosystem:

Disclaimer

This software is for educational and informational purposes only. It does not constitute medical advice and should not be used as a substitute for professional medical consultation, diagnosis, or treatment. Always seek the advice of a qualified healthcare provider with any questions regarding medications, supplements, or medical conditions.

The drug interaction data embedded in this software is sourced from publicly available databases and may not be complete or up-to-date. Do not make medication decisions based solely on this software.

License

MIT License. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pharma_ai_agent-0.1.0.tar.gz (52.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pharma_ai_agent-0.1.0-py3-none-any.whl (55.3 kB view details)

Uploaded Python 3

File details

Details for the file pharma_ai_agent-0.1.0.tar.gz.

File metadata

  • Download URL: pharma_ai_agent-0.1.0.tar.gz
  • Upload date:
  • Size: 52.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for pharma_ai_agent-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6f849bcc7654a644f610977a80b18f0ab16926be155a6924ebbec07092866a71
MD5 6d4a2dc07130d141cd1f099e0d42a0ca
BLAKE2b-256 1f126a6cf1555cb2d630f39d44512f8e4c1260bd9d3056ab042dfeb5105329a1

See more details on using hashes here.

File details

Details for the file pharma_ai_agent-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pharma_ai_agent-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80bdc49dad4aeeba4bed4a9de71744e04343ae5930b7e296a92316c63e0b57b3
MD5 259a5016dd5dcfe90321b3f87c841fc1
BLAKE2b-256 8daf4a6f868c323e1569191f8ae96bed723891911abab3126cb9d7fd9eeca413

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page