Package: mcodegpt
mCodeGPTIntroductionmCodeGPT is a Python package for the generation of cancer ontologies and Knowledge Bases using large language models (LLMs).mCodeGPT makes use of so-calledinstruction promptsin LLMs such as GPT-4.Two different strategies for knowledge extraction are currently implemented in mCodeGPT:SPIRES:Structured Prompt Interrogation and Recursive Extraction of SemanticsA Zero-shot learning (ZSL) approach to extracting nested semantic structures from textThis approach takes two inputs - 1) LinkML schema 2) free text, and outputs knowledge in a structure conformant with the supplied schema in JSON, YAML, RDF or OWL formatsUses GPT-3.5-turbo, GPT-4, or one of a variety of open LLMs on your local machineSPINDOCTOR:Structured Prompt Interpolation of Narrative Descriptions Or Controlled Terms for Ontological ReportingSummarize gene set descriptions (pseudo gene-set enrichment)Uses GPT-3.5-turbo or GPT-4mCodeGPT is a redistribution with function modification of the python libraryontogptunder BSD-3 license. Instructions below are mainly from theontogptbut with modifications for cancer research.Pre-requisitesPython 3.9+OpenAI API key: necessary for using OpenAI's GPT models. This is a paid API and you will be charged based on usage. If you do not have an OpenAI account,you may sign up here. You will need to set your API key using theOntology Access Kit:poetryrunrunoakset-apikey-eopenai<youropenaiapikey>You may also set additional API keys for optional resources:BioPortalaccount (for grounding). The BioPortal key is necessary for using ontologies fromBioPortal. You may get a key by signing up for an account on their web site.NCBI E-utilities. The NCBI email address and API key are used for retrieving text and metadata from PubMed. You may still access these resources without identifying yourself, but you may encounter rate limiting and errors.HuggingFace Hub. This API key is necessary to retrieve models from the HuggingFace Hub service.These optional keys may be set as follows:poetryrunrunoakset-apikey-ebioportal<yourbioportalapikey>
poetryrunrunoakset-apikey-encbi-email<youremailaddress>
poetryrunrunoakset-apikey-encbi-key<yourNCBIapikey>
poetryrunrunoakset-apikey-ehfhub-key<yourHuggingFaceHubapikey>SetupTo simply start using the package in your workspace:pipinstallmcodegptNote that some features require installing additional, optional dependencies.These may be installed as:poetryinstall--extrasextra_name# ORpipinstallmcodegpt[extra_name]whereextra_nameis one of the following:docs- dependencies for building documentationweb- dependencies for the web applicationrecipes- dependencies for recipe scraping and parsingtextract- the textract pluginhuggingface- dependencies for accessing LLMs from HuggingFace Hub, remotely or locallyFor feature development and contributing to the package:gitclonehttps://github.com/monarch-initiative/mcodegpt.gitcd~/path/to/mcodegpt
poetryinstallGetting StartedmCodeGPT is run from the command line. See the full list of commands with:mcodegpt--helpFor a simple example of text completion and testing to ensure mCodeGPT is set up correctly, create a text file containing the following, saving the file asexample.txt:Why did the squid cross the coral reef?Then try the following command:mcodegptcompleteexample.txtYou should get text output like the following:Perhaps the squid crossed the coral reef for a variety of reasons:

1. Food: Squids are known to feed on small fish and other marine organisms, and there could have been a rich food source on the other side of the reef.

...mCodeGPT is intended to be used for information extraction. The following examples show how to accomplish this.Strategy 1: Knowledge extraction using SPIRESWorking MechanismYou provide an arbitrary data model, describing the structure you want to extract text into. This can be nested (but see limitations below). The predefinedtemplatesmay be used.Provide your preferred annotations for groundingNamedEntityfieldsmCodeGPT will:Generate a promptFeed the prompt to a language modelParse the results into a dictionary structureGround the results using a preferred annotator (e.g., an ontology)InputConsider some text from one of the input files being used in the mCodeGPT test suite. You can find the text filehere. You can download the raw file from the GitHub link to that input text file, or copy its contents over into another file, say,abstract.txt. An excerpt:Patient John Smith (HSI-5421) underwent a tissue biopsy at the bladder
collection site. The histopathology report confirmed the presence of cancer
in the...
[cancer tumor size]
...lives in zip code 90210. He is still alive, with no reported death date at this time.
For any further information, please contact John Smith atjsmith@email.comor (555) 123-4567.
...
...We can extract knowledge from the above text this into theFHIR mCode Cancer Ontology Standardby running the following command:Commandmcodegptextract-tcancer.CancerAnnotations-i~/path/to/abstract.txtThe mCode standard is fromHL7 FHIR.Note: The value accepted by the-t/--templateargument is the base name of one of the LinkML schema / data model which can be found in thetemplatesfolder.OutputThe output returned from the above command can be optionally redirected into an output file using the-o/--output.The following is a small part of what the larger schema-compliant output looks like:raw_completion_output:|-human_specimen_identifier: HSI-5421human_specimen_collection_site: bladder collection sitehuman_specimen_specimen_type: tissue biopsyname: John Smithcontact_info: jsmith@email.com or (555) 123-4567birth_date: April 15, 1975gender: malezip_code: 90210us_core_race: Caucasianus_core_birth_sex: maleus_core_ethnicity: non-Hispanicdeath_date: N/Adisease_status_evidence_type: histopathology reporttumor_identifier: T-BC5421tumor_body_location: bladdertumor_size_longest_dimension: 4 cmcancer_stage_stage_type: T2cancer_asserted_date: January 10, 2022cancer_body_site: bladdertumor_marker_test_result_value: elevated levels of urinary bladder tumor antigen (UBTA)people_human_specimen_identifier_interaction: John Smithpeople_human_specimen_collection_site_interaction: bladder collection sitepeople_human_specimen_specimen_type_interaction: tissue biopsypeople_name_interaction: John Smithpeople_contact_info_interaction: jsmith@email.com or (555) 123-4567people_birth_date_interaction: April 15, 1975people_gender_interaction: malepeople_zip_code_interaction: 90210people_us_core_race_interaction: Caucasianpeople_us_core_birth_sex_interaction: malepeople_us_core_ethnicity_interaction: non-Hispanicpeople_death_date_interaction: N/Apeople_disease_status_evidence_type_interaction: histopathology reportpeople_tumor_identifier_interaction: T-BC5421people_tumor_body_location_interaction: bladderpeople_tumor_size_longest_dimension_interaction: 4 cmpeople_cancer_stage_stage_type_interaction: T2people_cancer_asserted_date_interaction: January 10, 2022people_cancer_body_site_interaction: bladderpeople_tumor_marker_test_result_value_interaction: elevated levels of urinary bladder tumor antigen (UBTA)Local ModelsTo use a local model, specify it with the-mor--modeloption.Example:mcodegptextract-tdrug-i~/path/to/abstract.txt-mnous-hermes-13bSee the list of all available models with this command:mcodegptlist-modelsWhen specifying a local model for the first time, it will be downloaded to your local system.Text length limitLLMs have context sizes limiting the combined length of their inputs and outputs. The text-davinci-003 model, for example, whas a total 4,000 token limit (prompt + completion), while the gpt-3.5-turbo-16k model has a larger context of 16 thousand tokens.Schema tipsIt helps to have an understanding of theLinkMLschema language, but it should be possible to define your own schemas using the examples insrc/mcodegpt/templatesas a guide.mCodeGPT-specific extensions are specified asannotations.You can specify a set of annotators for a field using theannotatorsannotation.Ex.:Gene:is_a:NamedThingid_prefixes:-HGNCannotations:annotators:gilda:, bioportal:hgnc-nr, obo:prThe annotators are applied in order.Additionally, when performing grounding, the following measures can be taken to improve accuracy:Specify the valid set of ID prefixes usingid_prefixesSome vocabularies have structural IDs that are amenable to regexes, you can specify these usingpatternYou can make use ofvalues_fromslot to specify aDynamic Value SetFor example, you can constrain the set of valid locations for a gene product to be subclasses ofcellular_componentin GO orcellin CLEx.:classes:...GeneLocation:is_a:NamedEntityid_prefixes:-GO-CLannotations:annotators:"sqlite:obo:go,sqlite:obo:cl"slot_usage:id:values_from:-GOCellComponentType-CellTypeenums:GOCellComponentType:reachable_from:source_ontology:obo:gosource_nodes:-GO:0005575## cellular_componentCellType:reachable_from:source_ontology:obo:clsource_nodes:-CL:0000000## cellOWL ExportsTheextractcommand will let you export the results as OWL axioms, utilizinglinkml-owlmappings in the schema.Ex.:mcodegptextract-trecipe-irecipe-spaghetti.txt-orecipe-spaghetti.owl-Oowlsrc/mcodegpt/templates/recipe.yamlis an example schema that uses linkml-owl mappings.See theMakefilefor a full pipeline that involves using robot to extract a subset of FOODON
and merge in the extracted results. This usesrecipe-scrapers.OWL output:recipe-all-merged.owlClassification:Web Application SetupThere is a bare bones web application for running mCodeGPT and viewing results.Install the required dependencies by running the following command:poetryinstall-EwebThen run this command to start the web application:poetryrunweb-mcodegptNote: The agent running uvicorn must have the API key set, so for obvious reasons don't host this publicly without authentication, unless you want your credits drained.SPINDOCTOR web appTo start:poetryrunstreamlitrunsrc/mcodegpt/streamlit/spindoctor.pyHuggingFace HubNote: support for HuggingFace-provided models is currently a work in progress.A select number of LLMs may be accessed through HuggingFace Hub. See the full list usingmcodegpt list-modelsSpecify a model name with the-moption.Example:mcodegptextract-tmendelian_disease.MendelianDisease-itests/input/cases/mendelian-disease-sly.txt-mFLAN_T5_BASECitationSPIRES is described further in: Caufield JH, Hegde H, Emonet V, Harris NL, Joachimiak MP, Matentzoglu N, et al. Structured prompt interrogation and recursive extraction of semantics (SPIRES): A method for populating knowledge bases using zero-shot learning. arXiv publication:http://arxiv.org/abs/2304.02711SPINDOCTOR is described further in: Joachimiak MP, Caufield JH, Harris NL, Kim H, Mungall CJ. Gene Set Summarization using Large Language Models. arXiv publication:http://arxiv.org/abs/2305.13338ContributingContributions on recipes to test welcome from anyone.