A simple human-in-the-loop multi-state AI agent designed to answer medical research questions with research papers from PubMed. This project is based on the StateFlow research paper, using states with cascading function calling in a research pipeline. The benefit of using states is that it allows for a more structured and modular approach to the research process, making it easier to manage and scale. Using states is a different but highly effective approach for building AI agents, allowing for more deterministic and predictable behavior. The function calling is implemented using 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.
- Frontend: Next.js (React framework)
- Backend: Python with FastAPI (Python framework)
- 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
This project is a work in progress, and so needs more work to be fully functional. Below is a list of tasks.
- Finish the Conclusion state placeholder
- Add functions for Conclusion state
- Refactor functions for each state for better readability and maintainability
- Add more descriptive logging and error handling for debugging and troubleshooting
- Improve the UI/UX for the frontend for better user experience and legibility
- Add proper DSPy instantiation of prompt optimization
- 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
.env
file 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.