Skip to content

Collection of LLM prompts, tools, chains and agents for healthcare using LangChain & FHIR.

License

Notifications You must be signed in to change notification settings

chenzubing/medprompt

 
 

Repository files navigation

MEDPrompt

Prompts, tools, chains and agents for healthcare using LLMs & FHIR. ✍️

Libraries.io SourceRank PyPI download total GitHub tag (latest by date)

About

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!

Terminology

Architecture (Names correspond to the files in the repository)

Agent

Design principles (WIP)

  • 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.

Disclaimer:

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.

FHIR2Text -> Convert FHIR resources to plain text

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.

FHIR2Calculator -> Calculate clinical scores from a FHIR Bundle (Work in progress)

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.

Usage

  1. Observation
  2. FHIR Bundle More documentation and examples to follow..

Install

  • medprompt for the core package
  • embedding for using RAG with HuggingFace transformers
pip install medprompt
pip install medprompt[embedding]

Install Develop branch

pip install git+https://github.com/dermatologist/medprompt.git

OR
after cloning the repository
pip install -e .[embedding]

Using templates

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)

Using Tools and chains in an agent

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]

Using agents in LangServe

from medprompt.agents import FhirAgent
agent = FhirAgent()
add_routes(
    app,
    FhirAgent.get_agent(),
    path="/agent",
)

Give us a star ⭐️

If you find this project useful, give us a star. It helps others discover the project.

Contributing

Contributers

About

Collection of LLM prompts, tools, chains and agents for healthcare using LangChain & FHIR.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 91.3%
  • Jinja 7.5%
  • Dockerfile 1.1%
  • Shell 0.1%