Skip to content

Promptify_NER using Cohere Model #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
198 changes: 198 additions & 0 deletions notebooks/Promptify_NER_Using_Cohere.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"id": "cKxp2N0L7Vsl"
},
"outputs": [],
"source": [
"import json\n",
"\n",
"from promptify import HubModel, Prompter, Pipeline,CohereModel"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"id": "XAdaUXRg7z9z"
},
"outputs": [],
"source": [
"model = CohereModel(model='command',api_key=\"api key\")\n",
"\n",
"prompter = Prompter('ner.jinja') # select a template or provide custom template\n",
"pipe = Pipeline(prompter , model)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "0WpJ3BSp9U2e",
"outputId": "8225f9e9-f745-4b7c-830c-1c0923dfce2f"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"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"
]
}
],
"source": [
"sent= \"\"\"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",
"print(sent)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "j2qq1w0t74ZK",
"outputId": "a2278abe-d784-4849-8289-d780ff885bfb"
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/1 [00:00<?, ?it/s]unknown field: parameter model is not a valid field\n",
"100%|██████████| 1/1 [00:02<00:00, 2.71s/it]\n"
]
}
],
"source": [
"# Named Entity Recognition with No labels, no description, no oneshot, no examples\n",
"# Simple prompt with instructions\n",
"# domain name gives more info to model for better result generation, the parameter is optional\n",
"# Output will be python object -> [ {'E' : Entity Name, 'T': Type of Entity } ]\n",
"\n",
"result = pipe.fit(sent,\n",
" domain=\"medical\",labels=None)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pVHFUewt9Qu6",
"outputId": "e325338b-a9fd-406c-e514-37c1d88f5350"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[{'text': '[{\"T\": \"age\", \"E\": \"93-year-old\"}, {\"T\": \"gender\", \"E\": \"female\"}, {\"T\": \"medical history\", \"E\": \"chronic right hip pain, osteoporosis, hypertension, depression, chronic atrial fibrillation\"}, {\"T\": \"admission_reason\", \"E\": \"evaluation and management of severe nausea and vomiting and urinary tract infection\"}]', 'parsed': {'status': 'completed', 'object_type': <class 'list'>, 'data': {'completion': [{'T': 'age', 'E': '93-year-old'}, {'T': 'gender', 'E': 'female'}, {'T': 'medical history', 'E': 'chronic right hip pain, osteoporosis, hypertension, depression, chronic atrial fibrillation'}, {'T': 'admission_reason', 'E': 'evaluation and management of severe nausea and vomiting and urinary tract infection'}], 'suggestions': []}}}]\n"
]
}
],
"source": [
"print(result)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "NsrBpXJzI391"
},
"source": [
"# **output**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "hvRIi-6fCqS-"
},
"outputs": [],
"source": [
"[\n",
" {\n",
" 'text': [\n",
" {\n",
" \"T\": \"age\",\n",
" \"E\": \"93-year-old\"\n",
" },\n",
" {\n",
" \"T\": \"gender\",\n",
" \"E\": \"female\"\n",
" },\n",
" {\n",
" \"T\": \"medical history\",\n",
" \"E\": \"chronic right hip pain, osteoporosis, hypertension, depression, chronic atrial fibrillation\"\n",
" },\n",
" {\n",
" \"T\": \"admission_reason\",\n",
" \"E\": \"evaluation and management of severe nausea and vomiting and urinary tract infection\"\n",
" }\n",
" ],\n",
" 'parsed': {\n",
" 'status': 'completed',\n",
" 'object_type': 'list',\n",
" 'data': {\n",
" 'completion': [\n",
" {\n",
" 'T': 'age',\n",
" 'E': '93-year-old'\n",
" },\n",
" {\n",
" 'T': 'gender',\n",
" 'E': 'female'\n",
" },\n",
" {\n",
" 'T': 'medical history',\n",
" 'E': 'chronic right hip pain, osteoporosis, hypertension, depression, chronic atrial fibrillation'\n",
" },\n",
" {\n",
" 'T': 'admission_reason',\n",
" 'E': 'evaluation and management of severe nausea and vomiting and urinary tract infection'\n",
" }\n",
" ],\n",
" 'suggestions': []\n",
" }\n",
" }\n",
" }\n",
"]\n"
]
}
],
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}