MEDPrompt is a collection of prompts, tools, chains and agents for medical applications using LangChain and space using Agency. MEDPrompt also includes a collection of templates for using FHIR in LLM prompts (see below). The aim of MEDPrompt is to provide a conceptual framework and a set of tools for building healthcare applications using LLMs. Please read my Blog post. User contributions are highly appreciated!
- Prompts are inputs or queries to LLMs that users can provide to elicit specific responses from a Large Language Model (LLM). Example: You are an AI assistant. Summarize this clinical document in 250 words
- Tools are functions used by agents for getting things done. Example: To find patient ID from name.
- Chains are tools that use LLM calls to get things done. Example: Answer a clinical question based on patient health record using RAG
- Agents uses an LLM to orchestrate Chains and Tools to acheive the overarching goal. Example: Answer a doctors question related to a patient. Find patient, get health record, generate embedding and generate answer
- Space is an Agency based abstraction for an environment for agents to communicate according to the actor model. Example: FHIR space
- Decoupled - Each component is independent of the other with dependencies injected.
- LLM agnostic - Each component can use any LLM. LLMS are injected into chains and agents.
- No Permanent vector storage - No permanent storage of vectors. Vectors are generated on the fly.
- Fail silently - Each component should fail silently and log errors.
- Returns - Each component should return a LLM friendly message.
- Modular - Each component is a separate module that can be used independently.
- Extensible - New tools, chains and agents can be added easily.
- Reusable - Tools, chains and agents can be reused in different contexts.
- Testable - Each component can be tested independently.
- Documented - Each component is documented with examples.
- Open - Open source and open to contributions.
This repository is not associated with the Medprompt method of prompting. In this generic repository, I will be trying to implement the method using langchain abstractions. Get in touch to share your thoughts via GitHub discussions. Please submit a PR with a link to the official implementation if any.
This repository includes templates for converting FHIR resources into a text representation that can be injected into an LLM prompt. Only relevant information is extracted from the resource with simple transformations using helper functions. 🚒See this example usage.
Clinical calculators are tools that help healthcare professionals make medical decisions by providing them with quick and easy access to various medical formulas, scores, and algorithms. Calculations performed by LLMs are not reliable. FHIR2Calculator performs calculations on data fields extracted from a FHIR bundle and outputs the results as plain text that can be injected into LLM prompts.
Documentation is in progress. Any help will be highly appreciated.
See Examples folder
- Observation
- FHIR Bundle More documentation and examples to follow..
- medprompt for the core package
- embedding for using RAG with HuggingFace transformers
pip install medprompt
pip install medprompt[embedding]
pip install git+https://github.com/dermatologist/medprompt.git
OR
after cloning the repository
pip install -e .[embedding]
from medprompt import MedPrompter
prompt = MedPrompter()
prompt.set_template(
template_name="fhir_search_oai_chat_v1.json")
print(prompt.get_template_variables())
messages = prompt.generate_prompt(
{"question": "Find Conditions for patient with first name John?"})
print(messages)
from medprompt.tools import FhirPatientSearchTool, CreateEmbeddingFromFhirBundle, ConvertFhirToTextTool, GetMedicalRecordTool
from medprompt.chains import get_rag_tool
from medprompt.utils import HapiFhirServer
from os import getenv
# Dependency injection
from kink import di
di["fhir_server"] = HapiFhirServer()
di["patient_id"] = getenv("PATIENT_ID", "592911")
di["get_medical_record_tool"] = GetMedicalRecordTool()
tools = [FhirPatientSearchTool(), CreateEmbeddingFromFhirBundle(), ConvertFhirToTextTool(), get_rag_tool]
from medprompt.agents import FhirAgent
agent = FhirAgent()
add_routes(
app,
FhirAgent.get_agent(),
path="/agent",
)
If you find this project useful, give us a star. It helps others discover the project.
- PR welcome
- Add templates in this folder as jinja2 or JSON.
- Follow the naming conventions in the folder.
- Add tools, chains and agents in the appropriately named folders.
- Add documentations here as a markdown file with the same name.
- Add a link in the index.md file.
- Please see CONTRIBUTING.md