Skip to main content
Glama
kartikeya788

pharma-rag-mcp

by kartikeya788

pharma-rag-mcp

一个完全本地化的端到端**检索增强生成(RAG)**系统,用于医药销售情报——基于 LangChain、ChromaDB、Ollama 和模型上下文协议(MCP)构建。

该系统将药品标签、临床试验文档和销售通话记录导入本地向量数据库,将其暴露为 MCP 工具,并通过由本地运行的 LLM 驱动的 LangGraph ReAct 智能体来回答自然语言问题。


架构

data/sources/          ← raw .txt files (drug labels, trials, call notes)
      │
      ▼
data/ingest.py         ← loads, splits into chunks, embeds with all-MiniLM-L6-v2
      │
      ▼
chroma_db/             ← persisted ChromaDB collections (384-dim vectors)
  ├── drug_info/
  ├── competitor_intel/
  └── pitch_content/
      │
      ▼
mcp_server/server.py   ← MCP server over stdio — exposes 4 retrieval tools
      │   (MCP JSON-RPC)
      ▼
agent/agent.py         ← LangGraph ReAct agent (ChatOllama + MCP tools)
      │
      ▼
ui/app.py              ← Gradio chat interface (browser)

支撑模块

模块

用途

rag/embeddings.py

HuggingFace 嵌入模型封装(all-MiniLM-L6-v2

rag/vectorstore.py

ChromaDB 集合构建器 / 加载器

eval/evaluate.py

检索质量评估(命中率、MRR、上下文精确率)


Related MCP server: DocAgent-MCP

知识库

11 种药物 × 3 种文档类型 = 33 个源文件

集合

源文件夹

内容

drug_info

data/sources/drug_labels/

FDA 风格的药品标签摘要

competitor_intel

data/sources/clinical_trials/

临床试验结果

pitch_content

data/sources/call_notes/

销售代表通话记录

药物: Dupixent、Eliquis、Entresto、Farxiga、Fasenra、Jardiance、Rinvoq、Skyrizi、Trelegy Ellipta、Trulicity、Xarelto


前提条件

  • Python 3.13+

  • Ollama 在本地运行,并已拉取一个模型:

    ollama pull llama3.2
  • 一个安装了依赖的 Python 虚拟环境(参见设置)。


设置

# 1. Clone and enter the project
git clone <repo-url>
cd pharma-rag-mcp

# 2. Create and activate a virtual environment
python -m venv .venv
# Windows:
.venv\Scripts\activate
# macOS / Linux:
source .venv/bin/activate

# 3. Install dependencies
pip install -r requirements.txt

# 4. Configure environment (optional — defaults work out of the box)
cp .env.example .env
# Edit .env to set OLLAMA_MODEL and OLLAMA_BASE_URL if needed

# 5. Build the vector database (only needed once)
python -m data.run_ingest

运行系统

每一层都可以独立使用。使用智能体或 UI 之前,请先启动 Ollama。

智能体(命令行)

python -m agent.agent

交互式命令行聊天,具有两层记忆:

  • 会话记忆 —— 每一轮的查询、调用的工具、来源模式和答案都保存在当前运行的 RAM 中。

  • 长期记忆 —— 退出时,会话(带时间戳)会追加到磁盘上的 memory/long_term.json 文件中。

输入 byecloseendexitgoodbyequit 中的任意一个即可退出——智能体将打印会话摘要并在退出前刷新记忆。

答案来源模式

智能体会检测并标记每个答案的产生方式:

徽章

含义

[Source: Knowledge Base]

答案完全基于检索到的文本块

[Source: Knowledge Base + General Knowledge]

检索到的事实与一般专业知识相结合;知识库之外的内容用 [GK] 内联标记

[Source: General Knowledge only]

工具未返回相关内容;基于一般的医药/销售知识回答

多轮上下文

智能体在回答每个问题时,会将最近 N 轮 的对话历史传递给 LLM,因此像 “他无视了我,我该如何重新接触?” 这样的后续问题会在上下文中得到回答。N 由 config.yaml 控制:

agent:
  history_window: 3   # number of prior turns to include

Gradio UI(浏览器)

python -m ui.app

http://localhost:7860 打开聊天界面。

仅 MCP 服务器(stdio 传输)

python -m mcp_server.server

加载三个 ChromaDB 集合,并等待 stdin 上的 MCP JSON-RPC 消息。

已注册的工具:

工具

描述

search_drug_info

搜索药品标签文档

search_competitor_intel

搜索临床试验数据

search_pitch_content

搜索销售通话记录

search_all

搜索所有三个集合,合并结果

检索评估

# Evaluate all three collections
python -m eval.evaluate

# Evaluate one collection with k=5
python -m eval.evaluate --collection drug_info --k 5

按集合和总体打印命中率、MRR 和上下文精确率。


配置

文件

用途

.env

Ollama 模型和基础 URL(从 .env.example 复制)

config.yaml

智能体行为(对话历史窗口)

.env

变量

默认值

描述

OLLAMA_MODEL

llama3.2

Ollama 模型名称(必须先拉取)

OLLAMA_BASE_URL

http://localhost:11434

Ollama HTTP 守护进程 URL

config.yaml

agent:
  history_window: 3   # prior turns passed to LLM for multi-turn context

项目结构

pharma-rag-mcp/
├── config.yaml             # Agent configuration
├── data/
│   ├── ingest.py           # IngestionPipeline class
│   ├── run_ingest.py       # CLI: build + spot-check all collections
│   └── sources/
│       ├── drug_labels/    # 11 × drug label .txt files
│       ├── clinical_trials/# 11 × clinical trial .txt files
│       └── call_notes/     # 11 × sales call note .txt files
├── rag/
│   ├── embeddings.py       # EmbeddingModel (all-MiniLM-L6-v2)
│   └── vectorstore.py      # VectorStoreManager (ChromaDB)
├── mcp_server/
│   ├── server.py           # MCP server entrypoint (stdio)
│   └── tools.py            # 4 retrieval tool definitions
├── agent/
│   └── agent.py            # PharmaAgent + CLI loop with memory
├── ui/
│   └── app.py              # Gradio chat UI
├── eval/
│   └── evaluate.py         # Hit Rate / MRR / Context Precision
├── memory/
│   └── long_term.json      # Persisted session history (auto-created)
├── chroma_db/              # Persisted vector collections (git-ignored)
├── .env.example
├── pyproject.toml
└── requirements.txt

关键设计决策

本地优先 —— 无云 API,无 API 密钥。嵌入使用 HuggingFace,向量存储使用 ChromaDB,生成使用 Ollama。

MCP 作为检索层 —— MCP 服务器将检索与生成清晰分离。任何兼容 MCP 的客户端都可以调用搜索工具。

自适应答案模式 —— 智能体自动检测问题是否可以仅从知识库回答、是否需要与一般专业知识结合,或者完全超出知识库范围。每个答案都清晰标记,以便用户始终知道来源。

滑动历史窗口 —— 只有最近 N 轮对话会发送给 LLM,在保持上下文窗口使用受限的同时,仍支持自然的的多轮对话。

两层记忆 —— 会话记忆(RAM 中)在退出时刷新到持久化的 JSON 日志中,为所有运行中的每次查询、使用的工具、来源模式和答案提供完整的审计轨迹。

F
license - not found
Not graded
quality - not tested
B
maintenance

Maintenance

Maintainers
Response time
Release cycle
Releases (12mo)
Commit activity

Resources

Unclaimed servers have limited discoverability.

Looking for Admin?

If you are the server author, to access and configure the admin panel.

Related MCP Servers

  • F
    license
    Not graded
    quality
    C
    maintenance
    Enables natural language queries on technical specifications and automated code compliance checks using local RAG with vector search, integrated via MCP.
  • F
    license
    Not graded
    quality
    C
    maintenance
    Enables local document question-answering and retrieval via MCP, supporting multi-turn conversation, intent recognition, and tools for document search, Q&A, and summarization.
    5
  • A
    license
    Not graded
    quality
    C
    maintenance
    A privacy-preserving local RAG system integrated with MCP, enabling natural language queries over ingested documents and a SQLite database through vector search and local database tools.
    MIT

View all related MCP servers

Related MCP Connectors

  • Multi-engine search for AI agents. Trust scoring, local corpus, MCP-native. Self-hostable, BYOK.

  • Certified SEC EDGAR fact memory for AI agents with zero hallucination and filing provenance.

  • Hosted MCP server exposing US hospital procedure cost data to AI assistants

View all MCP Connectors

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/kartikeya788/pharma-rag-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server