DEV Community

Cover image for How I Built a Clinical Trial Memory AI Agent with Qwen Cloud in 5 Weeks
sirmos
sirmos

Posted on Edited on

How I Built a Clinical Trial Memory AI Agent with Qwen Cloud in 5 Weeks

The Problem I Set Out to Solve

Clinical trials run for years. Patient data gets scattered across session notes, emails, and spreadsheets. When a doctor asks "what happened to this patient three months ago?", nobody knows fast enough. Adverse events get missed. Documentation takes hours.

I built CTMC (Clinical Trial Memory Coordinator) to fix that.

What CTMC Does

CTMC is a persistent AI memory system for clinical trial teams:

  • Every session note is chunked and embedded using Qwen's text embedding-v3 model
  • Stored as searchable semantic memory scoped to each patient and trial
  • Natural language queries are answered by qwen-max using RAG, grounded in real session data with source citations
  • An anomaly detector automatically fires safety alerts on new ingestions
  • One-click FDA adverse event reports generated from memory in seconds
  • Full compliance audit trail of every memory read and write

The Tech Stack

  • Qwen Cloud qwen-max for synthesis, text-embedding-v3 for semantic embeddings
  • Node.js / Express backend with custom memory engine
  • React + Vite frontend
  • Alibaba Cloud ECS (Singapore) for deployment
  • PM2 for process management

The Memory Pipeline

The core insight: don't store session notes as raw text. Instead:

  1. Chunk notes into semantic units (adverse events, observations, protocol notes)
  2. Embed each chunk via text-embedding-v3
  3. On query, embed the question, rank chunks by cosine similarity
  4. Feed top-k chunks to qwen-max with strict grounding instructions
  5. Return answer with exact session date citations

This gives you a memory that never hallucinates every answer is traceable to a real session.

The Biggest Challenge

Getting the anomaly detector to work reliably. The first version silently returned hasAnomaly: false on every new session. The fix was tightening the JSON output format in the system prompt, Qwen was wrapping responses in markdown code blocks that broke JSON parsing.

The Killer Demo Moment

After ingesting 6 months of patient sessions showing ALT liver enzyme progression (22 → 24 → 31 → 52 → 89 → 112 U/L), asking:

"What adverse events has P042 had and are they getting worse?"

The agent recalled the full progression across all sessions, cited each date, and flagged the escalating hepatotoxicity, in under 3 seconds.

What I Learned

Qwen's text-embedding-v3 model produces high-quality embeddings for clinical text. The cosine similarity ranking consistently surfaced the most relevant chunks even across months of session history.

The qwen-max model with strict grounding prompts is remarkably reliable at staying within the retrieved context, crucial for clinical applications where hallucination is dangerous.

Try It

Built in 5 weeks for the Qwen Cloud Global AI Hackathon. The memory grows smarter with every session and never forgets.

qwencloud #alibabacloud #ai #hackathon #clinicaltrials #nodejs #react #h0hackathon

Top comments (0)