forked from promptslab/Promptify
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
378 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,377 @@ | ||
{ | ||
"nbformat": 4, | ||
"nbformat_minor": 0, | ||
"metadata": { | ||
"colab": { | ||
"provenance": [] | ||
}, | ||
"kernelspec": { | ||
"name": "python3", | ||
"display_name": "Python 3" | ||
}, | ||
"language_info": { | ||
"name": "python" | ||
}, | ||
"gpuClass": "standard" | ||
}, | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"metadata": { | ||
"id": "4HhNZ9PRTgxM" | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"%%capture\n", | ||
"!git clone https://github.com/promptslab/Promptify.git\n", | ||
"!pip3 install openai\n", | ||
"!pip3 install huggingface_hub" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"<h2>Features 🚀</h2>\n", | ||
"<ul>\n", | ||
" <li>🧙♀️ NLP in 2 lines of code with no training data required</li>\n", | ||
" <li>🔨 Easily add one shot, two shot, or few shot examples to the prompt</li>\n", | ||
" <li>✌ Output always provided as a Python object (e.g. list, dictionary) for easy parsing and filtering</li>\n", | ||
" <li>💥 Custom examples and samples can be easily added to the prompt</li>\n", | ||
" <li>💰 Optimized prompts to reduce OpenAI token costs (coming soon)</li>\n", | ||
"</ul>\n", | ||
"\n", | ||
"\n" | ||
], | ||
"metadata": { | ||
"id": "k5vxlro8lAWu" | ||
} | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### Define any LLM model (such as GPT-3) ✅" | ||
], | ||
"metadata": { | ||
"id": "oGti-CqqaoUw" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"%cd /content/Promptify\n", | ||
"\n", | ||
"\n", | ||
"import json\n", | ||
"from promptify import OpenAI\n", | ||
"from promptify import Prompter\n", | ||
"from pprint import pprint\n", | ||
"from IPython.display import Markdown, display\n", | ||
"from IPython.core.display import display, HTML\n", | ||
"\n", | ||
"\n", | ||
"# Define the API key for the OpenAI model\n", | ||
"api_key = \"\"\n", | ||
"\n", | ||
"\n", | ||
"# Create an instance of the OpenAI model, Currently supporting Openai's all model, In future adding more generative models from Hugginface and other platforms\n", | ||
"model = OpenAI(api_key)\n", | ||
"nlp_prompter = Prompter(model)\n", | ||
"\n", | ||
"\n", | ||
"# Example sentence for demonstration\n", | ||
"sentence = \"\"\"Posttransplant lymphoproliferative disorder, chronic renal insufficiency, \n", | ||
" squamous cell carcinoma of the skin, anemia secondary to chronic renal \n", | ||
" insufficiency and chemotherapy, and hypertension. The patient is here \n", | ||
" for followup visit and chemotherapy.\"\"\"\n", | ||
"\n", | ||
"print(sentence)" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"id": "Q1VTSg8XUATO", | ||
"outputId": "1e50b831-fec4-4630-8b2d-40f32708aa8e" | ||
}, | ||
"execution_count": 4, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"/content/Promptify\n", | ||
"Posttransplant lymphoproliferative disorder, chronic renal insufficiency, \n", | ||
" squamous cell carcinoma of the skin, anemia secondary to chronic renal \n", | ||
" insufficiency and chemotherapy, and hypertension. The patient is here \n", | ||
" for followup visit and chemotherapy.\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### 1: Relation extraction (zero-shot) 🚀\n", | ||
"\n" | ||
], | ||
"metadata": { | ||
"id": "NXwzgg9PbV0Z" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"\n", | ||
"result = nlp_prompter.fit('relation_extraction.jinja', \n", | ||
" domain = 'clinical',\n", | ||
" text_input = sentence)\n", | ||
"\n", | ||
"print(result['text'])\n", | ||
"\n", | ||
"# Output\n", | ||
"\n", | ||
"display(HTML('<h4>Sentence</h4>'))\n", | ||
"print('\\n')\n", | ||
"print(sentence)\n", | ||
"print('\\n')\n", | ||
"display(HTML('<h4>Output</h4>'))\n", | ||
"print(\"\\n\")\n", | ||
"pprint(eval(result['text']))" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 561 | ||
}, | ||
"id": "h9UW8wnZVhXe", | ||
"outputId": "760540d5-5423-4d24-dc25-293e3d44b103" | ||
}, | ||
"execution_count": 5, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
" [['Posttransplant lymphoproliferative disorder', 'has_diagnosis', 'patient'], \n", | ||
" ['chronic renal insufficiency', 'has_diagnosis', 'patient'], \n", | ||
" ['squamous cell carcinoma of the skin', 'has_diagnosis', 'patient'], \n", | ||
" ['anemia secondary to chronic renal insufficiency and chemotherapy', 'has_diagnosis', 'patient'], \n", | ||
" ['hypertension', 'has_diagnosis', 'patient'], \n", | ||
" ['patient', 'is_here_for', 'followup visit'], \n", | ||
" ['patient', 'is_here_for', 'chemotherapy']]\n" | ||
] | ||
}, | ||
{ | ||
"output_type": "display_data", | ||
"data": { | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
], | ||
"text/html": [ | ||
"<h4>Sentence</h4>" | ||
] | ||
}, | ||
"metadata": {} | ||
}, | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"\n", | ||
"\n", | ||
"Posttransplant lymphoproliferative disorder, chronic renal insufficiency, \n", | ||
" squamous cell carcinoma of the skin, anemia secondary to chronic renal \n", | ||
" insufficiency and chemotherapy, and hypertension. The patient is here \n", | ||
" for followup visit and chemotherapy.\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"output_type": "display_data", | ||
"data": { | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
], | ||
"text/html": [ | ||
"<h4>Output</h4>" | ||
] | ||
}, | ||
"metadata": {} | ||
}, | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"\n", | ||
"\n", | ||
"[['Posttransplant lymphoproliferative disorder', 'has_diagnosis', 'patient'],\n", | ||
" ['chronic renal insufficiency', 'has_diagnosis', 'patient'],\n", | ||
" ['squamous cell carcinoma of the skin', 'has_diagnosis', 'patient'],\n", | ||
" ['anemia secondary to chronic renal insufficiency and chemotherapy',\n", | ||
" 'has_diagnosis',\n", | ||
" 'patient'],\n", | ||
" ['hypertension', 'has_diagnosis', 'patient'],\n", | ||
" ['patient', 'is_here_for', 'followup visit'],\n", | ||
" ['patient', 'is_here_for', 'chemotherapy']]\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"## Load examples\n" | ||
], | ||
"metadata": { | ||
"id": "O50DUQbFaZhY" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"import json\n", | ||
"\n", | ||
"relation_examples = json.load(open(\"examples/data/relation_extraction.json\",'r'))\n", | ||
"print(\"Got\", len(relation_examples), \"examples.\")\n", | ||
"\n", | ||
"prompt_examples = []\n", | ||
"for sample in relation_examples:\n", | ||
" prompt_examples.append((sample['text'], sample['labels']))\n", | ||
"print()" | ||
], | ||
"metadata": { | ||
"id": "1dJ1zN1DxXn-", | ||
"colab": { | ||
"base_uri": "https://localhost:8080/" | ||
}, | ||
"outputId": "ab1f8ca6-b169-4bc8-f85a-c8377bbd4f0b" | ||
}, | ||
"execution_count": 7, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"Got 2 examples.\n", | ||
"\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"source": [ | ||
"### 2: Relation extraction with custom examples (one-shot, few-shot etc) 🚀\n", | ||
"\n" | ||
], | ||
"metadata": { | ||
"id": "DQiklr0ugcLJ" | ||
} | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [ | ||
"\n", | ||
"result = nlp_prompter.fit('relation_extraction.jinja', \n", | ||
" domain = 'clinical',\n", | ||
" examples = [prompt_examples[0]],\n", | ||
" text_input = sentence)\n", | ||
"# print(result)\n", | ||
"print(result['text'])\n", | ||
"\n", | ||
"# Output\n", | ||
"\n", | ||
"display(HTML('<h4>Sentence</h4>'))\n", | ||
"print('\\n')\n", | ||
"print(sentence)\n", | ||
"print('\\n')\n", | ||
"display(HTML('<h4>Output</h4>'))\n", | ||
"print(\"\\n\")\n", | ||
"pprint(eval(result['text']))" | ||
], | ||
"metadata": { | ||
"colab": { | ||
"base_uri": "https://localhost:8080/", | ||
"height": 456 | ||
}, | ||
"id": "EObz_MTSebBv", | ||
"outputId": "4fec323f-4736-48f0-d972-b228d8e572e0" | ||
}, | ||
"execution_count": 16, | ||
"outputs": [ | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
" [['Posttransplant lymphoproliferative disorder', 'is a', 'disease'], ['chronic renal insufficiency', 'is a', 'disease'], ['squamous cell carcinoma of the skin', 'is a', 'disease'], ['anemia secondary to chronic renal insufficiency and chemotherapy', 'is a', 'disease'], ['hypertension', 'is a', 'disease'], ['the patient', 'is here for', 'followup visit and chemotherapy']]\n" | ||
] | ||
}, | ||
{ | ||
"output_type": "display_data", | ||
"data": { | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
], | ||
"text/html": [ | ||
"<h4>Sentence</h4>" | ||
] | ||
}, | ||
"metadata": {} | ||
}, | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"\n", | ||
"\n", | ||
"Posttransplant lymphoproliferative disorder, chronic renal insufficiency, \n", | ||
" squamous cell carcinoma of the skin, anemia secondary to chronic renal \n", | ||
" insufficiency and chemotherapy, and hypertension. The patient is here \n", | ||
" for followup visit and chemotherapy.\n", | ||
"\n", | ||
"\n" | ||
] | ||
}, | ||
{ | ||
"output_type": "display_data", | ||
"data": { | ||
"text/plain": [ | ||
"<IPython.core.display.HTML object>" | ||
], | ||
"text/html": [ | ||
"<h4>Output</h4>" | ||
] | ||
}, | ||
"metadata": {} | ||
}, | ||
{ | ||
"output_type": "stream", | ||
"name": "stdout", | ||
"text": [ | ||
"\n", | ||
"\n", | ||
"[['Posttransplant lymphoproliferative disorder', 'is a', 'disease'],\n", | ||
" ['chronic renal insufficiency', 'is a', 'disease'],\n", | ||
" ['squamous cell carcinoma of the skin', 'is a', 'disease'],\n", | ||
" ['anemia secondary to chronic renal insufficiency and chemotherapy',\n", | ||
" 'is a',\n", | ||
" 'disease'],\n", | ||
" ['hypertension', 'is a', 'disease'],\n", | ||
" ['the patient', 'is here for', 'followup visit and chemotherapy']]\n" | ||
] | ||
} | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"source": [], | ||
"metadata": { | ||
"id": "6Hb-zzo6IWYy" | ||
}, | ||
"execution_count": null, | ||
"outputs": [] | ||
} | ||
] | ||
} |