Skip to content

YehoudaSaroussi/Ai_Pharmacy_Agent_Chat

Repository files navigation

Pharmacy Agent - Real-time Conversational AI Assistant

An AI pharmacy assistant that provides factual medication information (stock availability, ingredients, prescription requirements) while refusing medical advice and redirecting users to healthcare professionals. The system is stateless with real-time streaming responses, supports both English and Hebrew, and uses tool-based database queries for transparent, grounded information retrieval.

How It Actually Works

When you send a message, here's what happens:

Step 1: Understanding Your Question First, the agent figures out what kind of question you're asking. Is it a simple information request like "do you have ibuprofen?" or are you asking for medical advice like "what should I take for my pain?" This classification step is crucial for safety.

Step 2: Looking Up Information If you're asking for information (not medical advice), the agent checks which medication you're talking about and what you need to know. It might need to call multiple functions - first to find the medication, then to check the stock, or to see if it requires a prescription. (Each of these function calls is shown in real-time on the screen - like a tools log).

Step 3: Giving You an Answer The agent takes all the information it found and writes a natural response in the same language you used. The response streams to you word-by-word, just like ChatGPT, so you don't have to wait for the complete answer.

Safety First If at any point you ask for medical advice, the agent stops and redirects you to talk to a real healthcare professional. It won't guess, it won't give opinions, and it won't make treatment suggestions. It only shares factual data from the database.

Architecture Overview

Here's how I structured the project:

Frontend (static/index.html) A simple single-page application with a chat interface and a tool activity panel (for the tolls logs). vanilla JavaScript that connects to the backend and handles streaming responses. The UI updates in real-time as the agent work.

Backend (app/)

  • main.py - The FastAPI server that handles requests
  • agent.py - The brain of the operation - classifies intent, calls tools, generates responses
  • tools.py - Four functions: classify intent, get medication info, check inventory, check prescription requirements
  • db.py - Loads and searches the medication database
  • prompts.py - Instructions for the AI model on how to behave
  • constants.py - Configuration settings
  • schemas.py - Data models for requests and responses

Database (data/db.json) A simple JSON file with 5 medications and 10 sample users. Everything is in lowercase English.I added Hebrew aliases in the code so the agent can understand Hebrew medication names.

The Flow Client → FastAPI → Agent → OpenAI (for understanding) → Tools (for data) → OpenAI (for response) → Stream back to client

Tech Stack

Backend: Python 3.11+, FastAPI, OpenAI SDK, Pydantic Frontend: Vanilla JavaScript, HTML5, CSS3 Database: JSON file (5 medications, 10 users) AI Models: GPT-4o-mini for classification, GPT-5 for responses

How to Run with Docker

Clone the repository to your local machine:

git clone https://github.com/YehoudaSaroussi/Ai_pharmacy_agent_chat.git
cd Ai_pharmacy_agent_chat

Build and run the Docker container with your OpenAI API key:

docker build -t pharmacy-agent .
docker run -p 8000:8000 -e OPENAI_API_KEY=your_actual_api_key_here pharmacy-agent

Open your browser at http://localhost:8000 to access the chat interface.

Multi-Step Flow Demonstrations

Flow 1: Checking Availability and Prescription Requirements

What happens: User asks about a medication, then follows up asking if they need a prescription.

User: "Do you have lisinopril in stock?"

Agent Process:
1. Classifies intent → INFO
2. Extracts medication name → "lisinopril"
3. Calls get_medication_data_by_name("lisinopril")
4. Calls check_inventory(med_id)
5. Generates response

Agent: "I couldn't find lisinopril in stock. We currently have 0 units available."

User: "Does it require a prescription?"

Agent Process:
1. Classifies intent → INFO (detects follow-up about same medication)
2. Resolves "it" to "lisinopril" from conversation history
3. Calls get_prescription_info(med_id)
4. Generates response

Agent: "Yes, it requires a prescription."

Key Points:

  • The agent remembers context from previous messages
  • Each step is visible in the tools logs panel
  • It handles pronouns like "it" by looking at conversation history

Flow 2: Asking About Multiple Medications

What happens: User compares two medications in one question.

User: "What are the active ingredients in ibuprofen and cetirizine?"

Agent Process:
1. Classifies intent → INFO
2. Extracts multiple medications → ["ibuprofen", "cetirizine"]
3. Calls get_medication_data_by_name("ibuprofen")
4. Calls get_medication_data_by_name("cetirizine")
5. Generates comprehensive response

Agent: "For ibuprofen: The active ingredient is ibuprofen.
For cetirizine: The active ingredient is cetirizine hydrochloride."

Key Points:

  • Handles multiple medications in a single request
  • Makes parallel tool calls for efficiency
  • Structures the response clearly for each medication

Flow 3: Refusing Medical Advice

What happens: User tries to get treatment recommendations.

User: "I have a headache. Should I take ibuprofen or something else?"

Agent Process:
1. Classifies intent → MEDICAL_ADVICE (detects treatment recommendation request)
2. Detects language → English
3. Generates refusal message (no tool calls needed)

Agent: "I'd like to help, but I'm not able to provide medical advice or treatment
recommendations. These questions are best answered by a healthcare professional who
knows your medical history.

For medical guidance, please reach out to:
- Your doctor or pharmacist
- Your healthcare provider
- A medical professional

I can share factual information about medications in our inventory - like ingredients,
prescription requirements, and what's in stock. How else can I help?"

Key Points:

  • Safety mechanism kicks in immediately
  • No database lookups performed for medical advice requests
  • Redirects to appropriate healthcare resources
  • Offers to help with factual information instead

Evaluation Plan

Information Requests (English)

Testing basic factual queries about medications - stock availability, ingredients, prescription requirements, and general information.

Information Requests (Hebrew)

Verifying bilingual support by testing the same queries in Hebrew with proper language detection and response.

Multi-Medication Queries

Testing the agent's ability to handle questions about multiple medications simultaneously with parallel tool calls.

Follow-up Questions with Context

Verifying conversation memory by testing pronoun resolution ("it", "this", etc.) across multiple turns.

Medical Advice Refusal (Critical Safety Test)

Ensuring the agent correctly identifies and refuses treatment recommendations, dosing advice, and diagnosis requests.

Off-Topic Questions

Testing boundary handling for questions unrelated to pharmacy or medications.

Edge Cases

Testing error handling for medication not found, typos, out-of-stock items, and invalid inputs.

About

Stateless AI pharmacy assistant with real-time streaming, tool-based agent workflows, strict safety rules, and Hebrew/English support. Built with FastAPI, OpenAI API, and Docker.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors