Medical Chatbot with Verified Answers

Build an AI chatbot that answers drug safety questions with FDA-verified sources. Ground your LLM in verified knowledge to eliminate hallucinations completely.

The Problem

Traditional AI Systems Cannot Prove Their Sources

Probabilistic retrieval: Vector embeddings return "similar" documents based on cosine similarity, not exact matches. No proof the source actually contains what the AI claims.

Unverifiable citations: Traditional RAG systems cite documents, but can't prove which exact sentences support each claim. Users must trust the AI.

LLM hallucinations: Even with retrieved context, LLMs blend information from their training data with retrieved sources. Impossible to distinguish fact from fabrication.

No audit trail: Citations degrade over time as vector indices change. A citation that worked 6 months ago may return different results today.

The Solution

Use Lemma as Knowledge Layer for Your LLM

Instead of asking the LLM directly, query Lemma first to get verified FDA sources, then send those sources to your LLM as context. The LLM can only generate answers based on verified sources — no hallucinations possible.

Every answer grounded in verified FDA sources

Cryptographic fingerprints prove FDA origin

Natural language responses with verifiable citations

Safe for clinical use and regulatory compliance

How It Works

1

User Asks Question

"What are the side effects of aspirin?"

2

Query Lemma Search API

Retrieve relevant FDA label segments with fingerprints. Get exact text from official sources, not LLM memory.

3

Send Context to Your LLM

Pass verified sources to GPT-4/Claude with strict instructions: "Use ONLY these FDA sources to answer."

4

Return Verified Answer

LLM generates natural language response with citations. Every claim includes fingerprint for verification.

Complete Integration Flow

1

User Question

User: "What are the side effects of aspirin?"

2

Query Lemma Search API

POST /v1/search
{
  "question": "aspirin side effects adverse reactions",
  "top_k": 5
}
3

Lemma Returns Verified FDA Data

200 OK - Verified Sources
{
  "results": [
    {
      "text": "Allergy alert: Aspirin may cause a severe allergic reaction which may include: hives, facial swelling, shock, asthma (wheezing).",
      "fingerprint": "01010111100101011101000110011100...",
      "drug": {"name": "aspirin"},
      "section": "Warnings"
    }
  ]
}
4

Send to Your LLM

System Prompt:

"You are a medical AI assistant. Answer using ONLY the verified FDA sources below. Do NOT use your training data or general knowledge."

Context:

Verified FDA Source: "Allergy alert: Aspirin may cause a severe allergic reaction..."

[fp:01010111100101011101000110011100...]

User Question:

"What are the side effects of aspirin?"

5

User Receives Verified Answer

AI Assistant:

"Based on FDA drug labels, aspirin can cause severe allergic reactions including hives, facial swelling, shock, and asthma symptoms like wheezing."

[Citation: fp:01010111100101011101000110011100...]

✓ Click fingerprint to verify this claim came from official FDA source

Implementation Code

Python
import openai
import requests
import os

LEMMA_API_KEY = os.getenv("LEMMA_API_KEY")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")

def answer_with_verified_sources(user_question: str) -> dict:
    """
    Answer medical questions using verified FDA sources.
    Zero hallucinations - LLM can only use provided sources.
    """
    
    # Step 1: Get verified FDA sources from Lemma
    lemma_response = requests.post(
        "https://api.lemma.la/v1/search",
        headers={
            "x-api-key": LEMMA_API_KEY,
            "Content-Type": "application/json"
        },
        json={
            "question": user_question,
            "top_k": 5
        }
    )
    
    response_data = lemma_response.json()
    sources = response_data.get('data', {}).get('results', [])
    
    # Step 2: Build context from verified sources only
    context = "Verified FDA Sources:\n\n"
    for i, source in enumerate(sources, 1):
        context += f"{i}. {source['text']}\n"
        context += f"   [Citation: fp:{source['fingerprint'][:30]}...]\n\n"
    
    # Step 3: Send to LLM with strict instructions
    system_prompt = """You are a medical AI assistant. You must answer using ONLY 
the verified FDA sources provided. Do NOT use your training data or general knowledge. 
Include [Citation: fp:...] after each claim."""

    user_prompt = f"""User Question: "{user_question}"

{context}

Answer the question using ONLY the sources above. If the sources don't contain 
enough information, say so."""

    llm_response = openai.chat.completions.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": user_prompt}
        ],
        temperature=0.2  # Low temperature for factual accuracy
    )
    
    answer = llm_response.choices[0].message.content
    
    # Step 4: Return answer with verification data
    return {
        "answer": answer,
        "sources": sources,
        "verified": True
    }

# Example usage
result = answer_with_verified_sources("What are the side effects of aspirin?")
print(result['answer'])
print(f"\nBacked by {len(result['sources'])} verified FDA sources")

Why This Works

Impossible to Hallucinate

LLM has no choice but to use provided sources. It cannot invent information because it's not in the context window.

Cryptographic Verification

Every claim includes fingerprint. Users can verify any statement came from FDA source with mathematical certainty.

Natural Conversations

LLM still generates fluent, natural language. Users get conversational answers, not raw FDA label text.

Regulatory Compliant

All information traced to official FDA sources. Safe for clinical use, legal proceedings, and regulatory audits.

Ready to Build?

Get your API key and start building your medical chatbot with verified answers in minutes.