A simple human-in-the-loop multi-state AI agent designed to answer medical research questions with research papers from PubMed. The project uses FastAPI, OpenAI API and DSPy to process Chain-of-Thought reasoning for prompting the LLM. The backend is interfaced using a frontend implemented in Next.js and Tailwind CSS.
The major benefit of using stateful AI Agents is the inherit deterministic nature of output that is able to be inferred. This is particularly useful in businesses and industries where specific and predictable output is required, such as pharmaceutical companies, financial services, etc. Deep Research agents are great at gathering information from a plethora of different sources - however they are not necessarily the sources that you want to use, particularly not in highly specialized fields. This project offers an alternative, particular for medical research, to the Deep Research agents that have surfaced from OpenAI, Google and Anthropic.
Implementing states for an AI Agent allows a much more controllable and deterministic output, while retaining the ability to augment an entire workflow. Furthermore, states are inherently modular, making it easier to manage and scale.
This project is based on the StateFlow research paper, using states with cascading function calling and passing of output between states.
- Tech Stack
- Overview
- Functions
- Roadmap
- Backlog
- Getting Started
- Development
- Deployment
- Contributing
- Contact
- Frontend: Next.js (React framework)
- Backend: Python with FastAPI (Python framework) and Uvicorn (ASGI server)
- Data Validation: Pydantic (Type checking)
- Language Model: OpenAI API (GPT models)
- Prompting Framework: DSPy (Chain-of-Thought reasoning)
- Styling: Tailwind CSS, Radix UI (UI components)
- Animation: Framer Motion
- Deployment: Render
This project implements a number of different Python frameworks and libraries to create a multi-state AI agent for knowledge extraction in medical research. The agent has been designed with 5 states in mind; Start, Clarify, Research, Analyze, and Conclusion. Each state has a number of functions that are used to extract knowledge from the research papers. Below is a list of the functions for each state.
solve_task: Initializes the research process and transitions to the Clarify state
generate_clarifying_questions: Generates relevant questions to better understand the user's research needsClarifyQuestions(DSPy Signature): Processes the task description to generate targeted clarifying questions
fetch_research_papers: Retrieves research papers from PubMed based on the queryprocess_research_papers: Processes and evaluates retrieved papersenhance_search_query: Optimizes the search query for better resultsenhance_query_with_dspy: Enhances the query using clarifying answerscheck_paper_accessibility: Checks if papers are openly accessiblePaperEvaluation(DSPy Signature): Evaluates papers for relevance and scientific meritrelevancy_score: Ranks each research paper based on its relevance to the user's querycitation_score: Ranks each research paper based on its methdology, study design, and other factors
analyze_papers: Performs comprehensive analysis of selected papers, altogetheranalyze_paper_content: Analyzes individual paper content using full text or abstractfetch_pdf_content: Retrieves and extracts text from PDF papers using URLfetch_pmc_paper_content: Fetches paper content from PubMed CentralPaperAnalysis(DSPy Signature): Extracts supporting and opposing evidence from papers
conclude_research: Generates final conclusions based on analyzed papersConclude(DSPy Signature): Processes all findings to create a comprehensive conclusion
- Implement the Conclusion state
- Implement option to select data sources (PubMed, Embase, etc.)
- Implement Web Search API: [Brave Search API] (https://brave.com/search/api/)
- Implement PICO Search Option for research paper literature review
- Implement paper access type (Open Access, Paywalled, etc.) tag
- Implement citation scoring of entire research paper pdf rather than abstract
- Implement fine-tuned embedding models on medical nomenclature
This project is work in progress. Below is a list of tasks and fixes that are pending.
- Add state transition to move between states (02/01/2025)
- Implement proper component structure and file path organization on the frontend (03/01/2025)
- Create classes.ts to consolidate Tailwind classes (05/01/2025)
- Break up app.py into separate Python scripts for abstraction and separation of concerns based on Uncle Bob's Clean Code principles (23/08/2025)
- Add more descriptive logging and error handling for debugging and troubleshooting
- Add proper DSPy instantiation of prompt optimization
- Break up main app.py (serverless function) into smaller callable functions
- Abstract app.py into separate components (data models, states, tools, DSPy Signatures, logging, etc.)
- Add logging for streaming of research paper abstract information & token usage for PaperEvaluation
- Node.js (v14 or later)
- Python (v3.7 or later)
- OpenAI API key
-
Clone the repository:
git clone https://github.com/kallemickelborg/agentic-ai.git cd agentic-ai -
Set up the frontend:
cd frontend npm install -
Set up the backend:
cd backend python -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate` pip install -r requirements.txt -
Create a
.envfile in the backend directory with your OpenAI API key:OPENAI_API_KEY=your_api_key_here
-
Start the backend server:
cd backend uvicorn app:app --reload -
In a new terminal, start the frontend development server:
cd frontend npm run dev -
Open your browser and navigate to
http://localhost:3000
This project is configured for deployment on Render. Follow these steps:
- Fork this repository to your GitHub account.
- Create a new Web Service on Render, connecting to your forked repository.
- Set up the environment variables in Render, including your OpenAI API key.
- Deploy the service on Render.
For detailed deployment instructions, refer to the Render documentation.
Contributions are welcome! Please feel free to submit a Pull Request. If you are a medical researcher or student and know about the process of conducting research, please feel free to write to me and help me understand the medical research process better.
If you have any questions or feedback, please feel free to contact me at kallemickelborg@gmail.com