summaryrefslogtreecommitdiff
path: root/notebooks/disease_retrieval_accuracy.ipynb
blob: ccb207b4011a73aeca1c0ecf2f8d88b4aff25314 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "945c420e-bb44-4ffb-b899-e049caf0d918",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.chdir('..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f2bdefb3-3e59-409a-81b4-2e9ffbdfdb1a",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/anaconda3/envs/kg_rag_test_2/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from kg_rag.utility import *\n",
    "from tqdm import tqdm\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "19fc98b9-64a8-40c0-9e5a-92b4392e6969",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = pd.read_csv('data/dataset_for_entity_retrieval_accuracy_analysis.csv')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "2851be4c-2a76-4f6d-b5f4-118e8122b155",
   "metadata": {},
   "outputs": [],
   "source": [
    "VECTOR_DB_PATH = config_data[\"VECTOR_DB_PATH\"]\n",
    "SENTENCE_EMBEDDING_MODEL_FOR_NODE_RETRIEVAL = config_data[\"SENTENCE_EMBEDDING_MODEL_FOR_NODE_RETRIEVAL\"]\n",
    "\n",
    "vectorstore = load_chroma(VECTOR_DB_PATH, SENTENCE_EMBEDDING_MODEL_FOR_NODE_RETRIEVAL)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "7255fbab-d8b4-43a3-b870-9d67ad79d061",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "322it [00:05, 56.20it/s]"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 4.74 s, sys: 896 ms, total: 5.64 s\n",
      "Wall time: 5.73 s\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "correct_retrieval = 0\n",
    "\n",
    "for index, row in tqdm(data.iterrows()):\n",
    "    question = row['text']\n",
    "    entities = disease_entity_extractor_v2(question)  \n",
    "    for entity in entities:\n",
    "        node_search_result = vectorstore.similarity_search_with_score(entity, k=1)\n",
    "        if node_search_result[0][0].page_content == row['node_hits']:\n",
    "            correct_retrieval += 1 \n",
    "            break\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "2f997335-bff7-431c-bbd8-608513eddcc7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Retrieval accuracy is 99.7%\n"
     ]
    }
   ],
   "source": [
    "retrieval_accuracy = 100*correct_retrieval/data.shape[0]\n",
    "print(f'Retrieval accuracy is {round(retrieval_accuracy,1)}%')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "afe971ab-b8b9-4c88-9657-c588813b412f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.10.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}