{
    "cells": [
        {
            "language": "markdown",
            "source": [
                "# Prompt - Name entity recognition (NER) - Medical"
            ],
            "outputs": []
        },
        {
            "language": "markdown",
            "source": [
                "### Add Prompt module and get API key from .env file"
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "const Prompt = require('../index.js')\nconst {Prompter, OpenAI} = Prompt;\n\nconst DotEnv = require('dotenv');\nDotEnv.config({path: '../.env'});\nconst apiKey = process.env.OPENAI_API_KEY;"
            ],
            "outputs": []
        },
        {
            "language": "markdown",
            "source": [
                "### Load the model for OpenAI"
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "let model = new OpenAI(apiKey);\nlet prompt = new Prompter(model);"
            ],
            "outputs": []
        },
        {
            "language": "markdown",
            "source": [
                "### Call the prompt module \n* The `domain` property helps fucus the language model.\n* The `textInput` property is the text to be parsed."
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "let result = await prompt.fit('ner', {\n    domain: 'medical',\n    textInput: `The patient is a 93-year-old female \n    with a medical history of chronic right hip pain, \n    osteoporosis, hypertension, depression, and \n    chronic atrial fibrillation admitted for evaluation \n    and management of severe nausea and vomiting and \n    urinary tract infection`\n  });\nconsole.log(result);"
            ],
            "outputs": [
                {
                    "items": [
                        {
                            "mime": "application/vnd.code.notebook.stdout",
                            "value": [
                                "5.97585708296299 seconds",
                                "{",
                                "  promptTokens: 198,",
                                "  completionTokens: 164,",
                                "  totalTokens: 362,",
                                "  data: [",
                                "    { label: 'Age', text: '93-year-old' },",
                                "    { label: 'Medical Condition', text: 'chronic right hip pain' },",
                                "    { label: 'Medical Condition', text: 'osteoporosis' },",
                                "    { label: 'Medical Condition', text: 'hypertension' },",
                                "    { label: 'Medical Condition', text: 'depression' },",
                                "    { label: 'Medical Condition', text: 'chronic atrial fibrillation' },",
                                "    { label: 'Symptom', text: 'severe nausea and vomiting' },",
                                "    { label: 'Medical Condition', text: 'urinary tract infection' },",
                                "    { branch: 'Medical', group: 'History' }",
                                "  ]",
                                "}",
                                ""
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "language": "markdown",
            "source": [
                "### With labels\n* The `domain` property helps fucus the language model.\n* The `textInput` property is the text to be parsed.\n* The `labels` array is a list entities that the model should extract. "
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "let result = await prompt.fit('ner', {\n    domain: 'medical',\n    labels: [\"symptom\", \"disease\"],\n    textInput: 'The patient is a 93-year-old female with a medical history of chronic right hip pain, osteoporosis, hypertension, depression, and chronic atrial fibrillation admitted for evaluation and management of severe nausea and vomiting and urinary tract infection'\n  });\nconsole.log(result);"
            ],
            "outputs": [
                {
                    "items": [
                        {
                            "mime": "application/vnd.code.notebook.stdout",
                            "value": [
                                "2.5018897919654846 seconds",
                                "{",
                                "  promptTokens: 190,",
                                "  completionTokens: 56,",
                                "  totalTokens: 246,",
                                "  data: [",
                                "    { label: 'symptom', text: 'nausea and vomiting' },",
                                "    { label: 'disease', text: 'urinary tract infection' },",
                                "    { branch: 'Medical', group: 'History' }",
                                "  ]",
                                "}",
                                ""
                            ]
                        }
                    ]
                }
            ]
        },
        {
            "language": "markdown",
            "source": [
                "### With inline fine tuning (One shot)\n* The `domain` property helps fucus the language model.\n* The `textInput` property is the text to be parsed.\n* The `labels` array is a list entities that the model should extract. \n* The `examples` array take an array of arrays. Each of which has two properties. The example input text and the labels data you would expect to be returned. You nay add muiltple examples, but remember there is a limit to how big the prompt can be, which is set by the model you are using."
            ],
            "outputs": []
        },
        {
            "language": "javascript",
            "source": [
                "let text = \"Leptomeningeal metastases (LM) occur in patients with breast cancer (BC) and lung cancer (LC). The cerebrospinal fluid (CSF) tumour microenvironment (TME) of LM patients is not well defined at a single-cell level. We did an analysis based on single-cell RNA sequencing (scRNA-seq) data and four patient-derived CSF samples of idiopathic intracranial hypertension (IIH)\"\nlet labels = [\n  {'label': 'disease', 'text': 'Leptomeningeal metastases'}, \n  {'label': 'disease', 'text': 'breast cancer'}, \n  {'label': 'disease', 'text': 'lung cancer'}, \n  {'label': 'biomarker', 'text': 'cerebrospinal fluid'}, \n  {'label': 'disease', 'text': 'tumour microenvironment'}, \n  {'label': 'test', 'text': 'single-cell RNA sequencing'}, \n  {'label': 'disease', 'text': 'idiopathic intracranial hypertension'}\n]\nlet fineTune = [\n  [text, labels]\n]\n\nlet result = await prompt.fit('ner', {\n    domain: 'medical',\n    labels: [\"symptom\", \"disease\" ],\n    examples: fineTune,\n    textInput: 'The patient is a 93-year-old female with a medical history of chronic right hip pain, osteoporosis, hypertension, depression, and chronic atrial fibrillation admitted for evaluation and management of severe nausea and vomiting and urinary tract infection'\n  });\nconsole.log(result);"
            ],
            "outputs": [
                {
                    "items": [
                        {
                            "mime": "application/vnd.code.notebook.stdout",
                            "value": [
                                "7.170070542097092 seconds",
                                "{",
                                "  promptTokens: 499,",
                                "  completionTokens: 194,",
                                "  totalTokens: 693,",
                                "  data: [",
                                "    { label: 'symptom', text: 'chronic right hip pain' },",
                                "    { label: 'disease', text: 'osteoporosis' },",
                                "    { label: 'disease', text: 'hypertension' },",
                                "    { label: 'disease', text: 'depression' },",
                                "    { label: 'disease', text: 'chronic atrial fibrillation' },",
                                "    { label: 'symptom', text: 'severe nausea and vomiting' },",
                                "    { label: 'disease', text: 'urinary tract infection' }",
                                "  ]",
                                "}",
                                ""
                            ]
                        }
                    ]
                }
            ]
        }
    ]
}