├── LICENSE ├── README.md ├── healthcare_kg_dump ├── Disease_e7067a8cdc396e4c168db56633c6ba01.data.json.gz ├── Disease_e7067a8cdc396e4c168db56633c6ba01.structure.json ├── ENCRYPTION ├── MedicalSpecialty_391287bf242fd62f2123949c25879b39.data.json.gz ├── MedicalSpecialty_391287bf242fd62f2123949c25879b39.structure.json ├── Symptom_9a717b6435e24f212d740a3a9a281b5d.data.json.gz ├── Symptom_9a717b6435e24f212d740a3a9a281b5d.structure.json ├── Treatment_4c4ac06fd21d631f7c21330fa8027002.data.json.gz ├── Treatment_4c4ac06fd21d631f7c21330fa8027002.structure.json ├── dump.json ├── hasSpecialty_206934413066179b4039b4a644b7ad85.data.json.gz ├── hasSpecialty_206934413066179b4039b4a644b7ad85.structure.json ├── hasSymptom_8e2d5cb0e33cc0a56efaf7b4745bf065.data.json.gz ├── hasSymptom_8e2d5cb0e33cc0a56efaf7b4745bf065.structure.json ├── isTreatedBy_d8ae529adeed7ddcf4b523b0c03f183f.data.json.gz └── isTreatedBy_d8ae529adeed7ddcf4b523b0c03f183f.structure.json └── notebook └── Healthcare_Chatbot_with_KG_ChatGPT_ArangoDB.ipynb /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Sachin Sharma 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to Build a Knowledge Graph Enhanced Chatbot with ChatGPT and ArangoDB 2 | 3 | In this repository, we will build a HealthCare Chatbot powered by 3 key technologies; Knowledge Graphs, ChatGPT, and ArangoDB (next-generation graph database). A detailed blog post for [Building Knowledge Graph-Driven Chatbot with ChatGPT and ArangoDB](https://sachinsharma9780.medium.com/how-to-build-a-knowledge-graph-enhanced-chatbot-with-chatgpt-and-arangodb-f609be6073d5#1cba-414b284d7d48) is also available. The benefits which come with building this type of chatbot are: 4 | 5 | Intelligent Information Retrieval: The chatbot can structurally organize and represent healthcare data, such as medical conditions, treatments, prescriptions, and procedures, by leveraging a **Knowledge Graph**. When consumers ask inquiries or seek advice about healthcare topics, the chatbot may respond with accurate and pertinent information. 6 | 7 | Processing Textual Information (or Natural Language): **ChatGPT** allows the chatbot to comprehend user inquiries and provide responses that sound human. Users can conversationally communicate with the chatbot by asking questions or requesting help in simple language (rather than writing complex database queries), thanks to its ability to interpret and process natural language input. 8 | 9 | Seamless Integration of Data: **ArangoDB** (a multi-model Graph Database) can be used to store and manage healthcare data. The healthcare data can be stored as triplets in the knowledge graph. This will enable effective information querying, indexing, and retrieval, enabling the chatbot to respond quickly and accurately. 10 | 11 | https://github.com/sachinsharma9780/chatbot_with_ChatGPT_KnowledgeGraph_ArangodB/assets/40523048/9e3e0f29-c6fc-4df0-a388-30ae69a5a1b6 12 | 13 | ### Restoring Healthcare Knowledge Graph into ArangoDB 14 | 15 | Here is an example showing how to restore Healthcare Knowledge Graph dump into the ArangoDB (create Healthcare_KG database in ArangoDB): 16 | ``` 17 | arangorestore --server.endpoint "tcp://127.0.0.1:8529" --server.username "root" --server.database "Healthcare_KG" --server.password "" --input-directory “./healthcare_kg_dump” 18 | ``` 19 | -------------------------------------------------------------------------------- /healthcare_kg_dump/Disease_e7067a8cdc396e4c168db56633c6ba01.data.json.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sachinsharma9780/chatbot_with_ChatGPT_KnowledgeGraph_ArangodB/a54c68e3a063e6cd822c75f9c0eed115677f8766/healthcare_kg_dump/Disease_e7067a8cdc396e4c168db56633c6ba01.data.json.gz -------------------------------------------------------------------------------- /healthcare_kg_dump/Disease_e7067a8cdc396e4c168db56633c6ba01.structure.json: -------------------------------------------------------------------------------- 1 | {"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"277234710","deleted":false,"globallyUniqueId":"c150104874/","id":"277234710","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":0},"minReplicationFactor":1,"name":"Disease","numberOfShards":1,"planId":"150104874","replicationFactor":3,"schema":null,"shardKeys":["_key"],"shards":{"s150104875":["PRMR-c1ny24bx","PRMR-mrbszauq","PRMR-5mkqlx0n"]},"status":3,"tempObjectId":"0","type":2,"version":9,"waitForSync":false,"writeConcern":1}} -------------------------------------------------------------------------------- /healthcare_kg_dump/ENCRYPTION: -------------------------------------------------------------------------------- 1 | none -------------------------------------------------------------------------------- /healthcare_kg_dump/MedicalSpecialty_391287bf242fd62f2123949c25879b39.data.json.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sachinsharma9780/chatbot_with_ChatGPT_KnowledgeGraph_ArangodB/a54c68e3a063e6cd822c75f9c0eed115677f8766/healthcare_kg_dump/MedicalSpecialty_391287bf242fd62f2123949c25879b39.data.json.gz -------------------------------------------------------------------------------- /healthcare_kg_dump/MedicalSpecialty_391287bf242fd62f2123949c25879b39.structure.json: -------------------------------------------------------------------------------- 1 | {"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"277234713","deleted":false,"globallyUniqueId":"c150104880/","id":"277234713","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":0},"minReplicationFactor":1,"name":"MedicalSpecialty","numberOfShards":1,"planId":"150104880","replicationFactor":3,"schema":null,"shardKeys":["_key"],"shards":{"s150104881":["PRMR-c1ny24bx","PRMR-mrbszauq","PRMR-5mkqlx0n"]},"status":3,"tempObjectId":"0","type":2,"version":9,"waitForSync":false,"writeConcern":1}} -------------------------------------------------------------------------------- /healthcare_kg_dump/Symptom_9a717b6435e24f212d740a3a9a281b5d.data.json.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sachinsharma9780/chatbot_with_ChatGPT_KnowledgeGraph_ArangodB/a54c68e3a063e6cd822c75f9c0eed115677f8766/healthcare_kg_dump/Symptom_9a717b6435e24f212d740a3a9a281b5d.data.json.gz -------------------------------------------------------------------------------- /healthcare_kg_dump/Symptom_9a717b6435e24f212d740a3a9a281b5d.structure.json: -------------------------------------------------------------------------------- 1 | {"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"277234716","deleted":false,"globallyUniqueId":"c150104877/","id":"277234716","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":0},"minReplicationFactor":1,"name":"Symptom","numberOfShards":1,"planId":"150104877","replicationFactor":3,"schema":null,"shardKeys":["_key"],"shards":{"s150104878":["PRMR-c1ny24bx","PRMR-mrbszauq","PRMR-5mkqlx0n"]},"status":3,"tempObjectId":"0","type":2,"version":9,"waitForSync":false,"writeConcern":1}} -------------------------------------------------------------------------------- /healthcare_kg_dump/Treatment_4c4ac06fd21d631f7c21330fa8027002.data.json.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sachinsharma9780/chatbot_with_ChatGPT_KnowledgeGraph_ArangodB/a54c68e3a063e6cd822c75f9c0eed115677f8766/healthcare_kg_dump/Treatment_4c4ac06fd21d631f7c21330fa8027002.data.json.gz -------------------------------------------------------------------------------- /healthcare_kg_dump/Treatment_4c4ac06fd21d631f7c21330fa8027002.structure.json: -------------------------------------------------------------------------------- 1 | {"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"277234719","deleted":false,"globallyUniqueId":"c150104883/","id":"277234719","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":0},"minReplicationFactor":1,"name":"Treatment","numberOfShards":1,"planId":"150104883","replicationFactor":3,"schema":null,"shardKeys":["_key"],"shards":{"s150104884":["PRMR-c1ny24bx","PRMR-mrbszauq","PRMR-5mkqlx0n"]},"status":3,"tempObjectId":"0","type":2,"version":9,"waitForSync":false,"writeConcern":1}} -------------------------------------------------------------------------------- /healthcare_kg_dump/dump.json: -------------------------------------------------------------------------------- 1 | {"database":"Healthcare_KG","lastTickAtDumpStart":"278347966","useEnvelope":false,"properties":{"id":"276820502","name":"Healthcare_KG","isSystem":false}} -------------------------------------------------------------------------------- /healthcare_kg_dump/hasSpecialty_206934413066179b4039b4a644b7ad85.data.json.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sachinsharma9780/chatbot_with_ChatGPT_KnowledgeGraph_ArangodB/a54c68e3a063e6cd822c75f9c0eed115677f8766/healthcare_kg_dump/hasSpecialty_206934413066179b4039b4a644b7ad85.data.json.gz -------------------------------------------------------------------------------- /healthcare_kg_dump/hasSpecialty_206934413066179b4039b4a644b7ad85.structure.json: -------------------------------------------------------------------------------- 1 | {"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"277234722","deleted":false,"globallyUniqueId":"c150104889/","id":"277234722","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":0},"minReplicationFactor":1,"name":"hasSpecialty","numberOfShards":1,"planId":"150104889","replicationFactor":3,"schema":null,"shardKeys":["_key"],"shards":{"s150104890":["PRMR-c1ny24bx","PRMR-mrbszauq","PRMR-5mkqlx0n"]},"status":3,"tempObjectId":"0","type":3,"version":9,"waitForSync":false,"writeConcern":1}} -------------------------------------------------------------------------------- /healthcare_kg_dump/hasSymptom_8e2d5cb0e33cc0a56efaf7b4745bf065.data.json.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sachinsharma9780/chatbot_with_ChatGPT_KnowledgeGraph_ArangodB/a54c68e3a063e6cd822c75f9c0eed115677f8766/healthcare_kg_dump/hasSymptom_8e2d5cb0e33cc0a56efaf7b4745bf065.data.json.gz -------------------------------------------------------------------------------- /healthcare_kg_dump/hasSymptom_8e2d5cb0e33cc0a56efaf7b4745bf065.structure.json: -------------------------------------------------------------------------------- 1 | {"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"277234727","deleted":false,"globallyUniqueId":"c150104886/","id":"277234727","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":0},"minReplicationFactor":1,"name":"hasSymptom","numberOfShards":1,"planId":"150104886","replicationFactor":3,"schema":null,"shardKeys":["_key"],"shards":{"s150104887":["PRMR-c1ny24bx","PRMR-mrbszauq","PRMR-5mkqlx0n"]},"status":3,"tempObjectId":"0","type":3,"version":9,"waitForSync":false,"writeConcern":1}} -------------------------------------------------------------------------------- /healthcare_kg_dump/isTreatedBy_d8ae529adeed7ddcf4b523b0c03f183f.data.json.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sachinsharma9780/chatbot_with_ChatGPT_KnowledgeGraph_ArangodB/a54c68e3a063e6cd822c75f9c0eed115677f8766/healthcare_kg_dump/isTreatedBy_d8ae529adeed7ddcf4b523b0c03f183f.data.json.gz -------------------------------------------------------------------------------- /healthcare_kg_dump/isTreatedBy_d8ae529adeed7ddcf4b523b0c03f183f.structure.json: -------------------------------------------------------------------------------- 1 | {"indexes":[],"parameters":{"allowUserKeys":true,"cacheEnabled":false,"cid":"277234732","deleted":false,"globallyUniqueId":"c150104892/","id":"277234732","isDisjoint":false,"isSmart":false,"isSmartChild":false,"isSystem":false,"keyOptions":{"allowUserKeys":true,"type":"traditional","lastValue":0},"minReplicationFactor":1,"name":"isTreatedBy","numberOfShards":1,"planId":"150104892","replicationFactor":3,"schema":null,"shardKeys":["_key"],"shards":{"s150104893":["PRMR-c1ny24bx","PRMR-mrbszauq","PRMR-5mkqlx0n"]},"status":3,"tempObjectId":"0","type":3,"version":9,"waitForSync":false,"writeConcern":1}} -------------------------------------------------------------------------------- /notebook/Healthcare_Chatbot_with_KG_ChatGPT_ArangoDB.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": null, 6 | "metadata": {}, 7 | "outputs": [], 8 | "source": [ 9 | "import openai\n", 10 | "import gradio \n", 11 | "import os\n", 12 | "from arango import ArangoClient" 13 | ] 14 | }, 15 | { 16 | "cell_type": "code", 17 | "execution_count": null, 18 | "metadata": {}, 19 | "outputs": [], 20 | "source": [ 21 | "# Initialize the ArangoDB client.\n", 22 | "client = ArangoClient(\"http://127.0.0.1:8529\")\n", 23 | "db = client.db('Healthcare_KG', username='root', password='')" 24 | ] 25 | }, 26 | { 27 | "cell_type": "code", 28 | "execution_count": null, 29 | "metadata": {}, 30 | "outputs": [], 31 | "source": [ 32 | "openai.api_key = \"Your_OpenAI_Key\"" 33 | ] 34 | }, 35 | { 36 | "cell_type": "code", 37 | "execution_count": null, 38 | "metadata": {}, 39 | "outputs": [], 40 | "source": [ 41 | "examples = \"\"\"\n", 42 | "# What are the symptoms of the disease Asthma?\n", 43 | "WITH Symptom\n", 44 | "FOR disease IN Disease\n", 45 | " FILTER disease.label == 'Asthma'\n", 46 | " FOR v, e, s IN 1..1 OUTBOUND disease hasSymptom\n", 47 | " RETURN v.label\n", 48 | "# What are the symptoms of the disease Diabetes?\n", 49 | "WITH Symptom\n", 50 | "FOR disease IN Disease\n", 51 | " FILTER disease.label == 'Diabetes'\n", 52 | " FOR v, e, s IN 1..1 OUTBOUND disease hasSymptom\n", 53 | " RETURN v.label\n", 54 | "# How to treat Asthma or what medications to use when suffering from Asthma or what are the treatments for Asthma?\n", 55 | "WITH Treatment\n", 56 | "FOR disease IN Disease\n", 57 | " FILTER disease.label == 'Asthma'\n", 58 | " FOR v, e, s IN 1..1 OUTBOUND disease isTreatedBy\n", 59 | " RETURN v.label\n", 60 | "# How to treat COVID-19 or what medications to use when suffering from COVID-19 or what are the treatments for COVID-19?\n", 61 | "WITH Treatment\n", 62 | "FOR disease IN Disease\n", 63 | " FILTER disease.label == 'COVID-19'\n", 64 | " FOR v, e, s IN 1..1 OUTBOUND disease isTreatedBy\n", 65 | " RETURN v.label\n", 66 | "# What kind of doctor do I look for if I am diagnosed with Asthma or What is the medical specialty for Asthma?\n", 67 | "WITH MedicalSpecialty\n", 68 | "FOR disease IN Disease\n", 69 | " FILTER disease.label == 'Asthma'\n", 70 | " FOR v, e, s IN 1..1 OUTBOUND disease hasSpecialty\n", 71 | " RETURN v.label\n", 72 | "# What kind of doctor do I look for if I am suffering from Macular degeneration or What is the medical specialty for Macular degeneration?\n", 73 | "WITH MedicalSpecialty\n", 74 | "FOR disease IN Disease\n", 75 | " FILTER disease.label == 'Macular degeneration'\n", 76 | " FOR v, e, s IN 1..1 OUTBOUND disease hasSpecialty\n", 77 | " RETURN v.label\n", 78 | "# I have symptoms of frequent urination and increase in thirst what type of disease it could be?\n", 79 | "WITH Symptom, Disease\n", 80 | "FOR symptom IN Symptom\n", 81 | " FILTER symptom.label IN ['Frequent urination', 'Increased thirst']\n", 82 | " FOR v, e, p IN 1..1 INBOUND symptom hasSymptom\n", 83 | " COLLECT disease = v.label INTO symptoms = symptom.label\n", 84 | " FILTER LENGTH(symptoms) == 2\n", 85 | " RETURN disease\n", 86 | "# I have symptoms of loud snoring and excessive daytime sleepiness what type of disease it could be?\n", 87 | "WITH Symptom, Disease\n", 88 | "FOR symptom IN Symptom\n", 89 | " FILTER symptom.label IN ['Loud snoring', 'Excessive daytime sleepiness']\n", 90 | " FOR v, e, p IN 1..1 INBOUND symptom hasSymptom\n", 91 | " COLLECT disease = v.label INTO symptoms = symptom.label\n", 92 | " FILTER LENGTH(symptoms) == 2\n", 93 | " RETURN disease\n", 94 | "# I have symptoms of chest pain and shortness of breath what type of disease it could be?\n", 95 | "WITH Symptom, Disease\n", 96 | "FOR symptom IN Symptom\n", 97 | " FILTER symptom.label IN ['Chest pain', 'Shortness of breath']\n", 98 | " FOR v, e, p IN 1..1 INBOUND symptom hasSymptom\n", 99 | " COLLECT disease = v.label INTO symptoms = symptom.label\n", 100 | " FILTER LENGTH(symptoms) == 2\n", 101 | " RETURN disease\n", 102 | "# I have symptoms of whiteheads and blackheads, papules and pustules what type of disease it could be?\n", 103 | "WITH Symptom, Disease\n", 104 | "FOR symptom IN Symptom\n", 105 | " FILTER symptom.label IN ['Whiteheads and blackheads', 'Papules and pustules']\n", 106 | " FOR v, e, p IN 1..1 INBOUND symptom hasSymptom\n", 107 | " COLLECT disease = v.label INTO symptoms = symptom.label\n", 108 | " FILTER LENGTH(symptoms) >= 1\n", 109 | " RETURN disease\n", 110 | "# I have symptoms of whiteheads and blackheads, papules and pustules what type of disease it could be?\n", 111 | "WITH Symptom, Disease\n", 112 | "FOR symptom IN Symptom\n", 113 | " FILTER symptom.label IN ['Whiteheads and blackheads', 'Papules and pustules']\n", 114 | " FOR v, e, p IN 1..1 INBOUND symptom hasSymptom\n", 115 | " COLLECT disease = v.label INTO symptoms = symptom.label\n", 116 | " FILTER LENGTH(symptoms) >= 1\n", 117 | " RETURN disease\n", 118 | "# To find the disease associated with either one of the symptoms chest pain or shortness of breath.\n", 119 | "WITH Symptom, Disease\n", 120 | "FOR symptom IN Symptom\n", 121 | " FILTER symptom.label IN ['Chest pain', 'Shortness of breath']\n", 122 | " FOR v, e, p IN 1..1 INBOUND symptom hasSymptom\n", 123 | " COLLECT disease = v.label INTO symptoms = symptom.label\n", 124 | " FILTER LENGTH(symptoms) >= 1\n", 125 | " RETURN disease\n", 126 | "# To find the disease associated with symptom of eye pain.\n", 127 | "WITH Symptom, Disease\n", 128 | "FOR symptom IN Symptom\n", 129 | " FILTER symptom.label IN ['Eye pain']\n", 130 | " FOR v, e, p IN 1..1 INBOUND symptom hasSymptom\n", 131 | " COLLECT disease = v.label INTO symptoms = symptom.label\n", 132 | " FILTER LENGTH(symptoms) >= 1\n", 133 | " RETURN disease\n", 134 | "\n", 135 | "\"\"\"" 136 | ] 137 | }, 138 | { 139 | "cell_type": "code", 140 | "execution_count": null, 141 | "metadata": {}, 142 | "outputs": [], 143 | "source": [ 144 | "content_hcb = f\"\"\" You are an AI system specializes in generating ArangoDB AQL queries based on example AQL queries.\n", 145 | "Example ArangoDB AQL queries are: \\n {examples} \\n\n", 146 | "You will refrain from providing explanations or additional information and solely focus on generating the ArangoDB AQL queries.\n", 147 | "You will strictly adhere to generating ArangoDB AQL queries based on the given examples.\n", 148 | "Do not provide any AQL queries that can't be deduced from AQL query examples. \n", 149 | "However, if the context of the conversation is insufficient, you will inform the user and specify the missing context.\n", 150 | "I repeat, if the context of the conversation is insufficient please inform the user and specify the missing context.\n", 151 | "\"\"\"" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "content_hlr = f\"\"\" You are an AI assistant specialized in generating text responses based on the provided information. \n", 161 | "Your role is to generate human-readable responses using the available information from the latest prompt. \n", 162 | "While providing answers, you will maintain the perspective of an AI assistant. \n", 163 | "It is important to note that you will not add any extra information that is not explicitly provided in the given prompt. \n", 164 | "You will strictly adhere to generating responses solely based on the available information. \n", 165 | "Once again, You will refrain from including any additional details that are not explicitly given in the prompt.\n", 166 | "\"\"\"" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "def human_like_response(user_input):\n", 176 | " messages = [\n", 177 | " {\"role\": \"system\", \"content\": content_hlr}\n", 178 | " ]\n", 179 | " messages.append({\"role\": \"user\", \"content\": user_input})\n", 180 | " response = openai.ChatCompletion.create(\n", 181 | " model = \"gpt-3.5-turbo\",\n", 182 | " messages = messages,\n", 183 | " temperature=0.5\n", 184 | " )\n", 185 | " reply = response[\"choices\"][0][\"message\"][\"content\"]\n", 186 | " messages.append({\"role\": \"assistant\", \"content\": reply})\n", 187 | " return reply\n", 188 | " " 189 | ] 190 | }, 191 | { 192 | "cell_type": "code", 193 | "execution_count": null, 194 | "metadata": {}, 195 | "outputs": [], 196 | "source": [ 197 | "# check for valid AQL query\n", 198 | "def is_aql_query(query):\n", 199 | " try:\n", 200 | " db.aql.execute(query) \n", 201 | " return True \n", 202 | " except Exception:\n", 203 | " return False" 204 | ] 205 | }, 206 | { 207 | "cell_type": "code", 208 | "execution_count": null, 209 | "metadata": {}, 210 | "outputs": [], 211 | "source": [ 212 | "def HealthCareChatbot(user_input):\n", 213 | " messages = [{\"role\": \"system\", \"content\": content_hcb}]\n", 214 | " messages.append({\"role\": \"user\", \"content\": user_input})\n", 215 | " response = openai.ChatCompletion.create(\n", 216 | " model = \"gpt-3.5-turbo\",\n", 217 | " messages = messages,\n", 218 | " temperature=0.0\n", 219 | " )\n", 220 | " reply = response[\"choices\"][0][\"message\"][\"content\"]\n", 221 | " messages.append({\"role\": \"assistant\", \"content\": reply})\n", 222 | " if \"`\" in reply:\n", 223 | " reply = reply.split(\"```\")[1].strip(\"`\") \n", 224 | " \n", 225 | " if is_aql_query(reply):\n", 226 | " docs = db.aql.execute(reply)\n", 227 | " response = [doc for doc in docs]\n", 228 | " if len(response) == 0:\n", 229 | " message = f\"Apologise to the user as you don't have an information related to this particular disease, treatments, symptoms, or medical specialty. \"\n", 230 | " response = human_like_response(message)\n", 231 | " else:\n", 232 | " response = human_like_response(\",\".join(response))\n", 233 | " else:\n", 234 | " message = f\"Greet the user and ask more information related to diseases, treatments, symptoms, or medical specialty.\"\n", 235 | " response = human_like_response(message)\n", 236 | " return response\n" 237 | ] 238 | }, 239 | { 240 | "cell_type": "code", 241 | "execution_count": null, 242 | "metadata": {}, 243 | "outputs": [], 244 | "source": [ 245 | "inputs = gradio.inputs.Textbox(lines=7, label=\"Chat with ArangoGPT\")\n", 246 | "outputs = gradio.outputs.Textbox(label=\"ArangoGPT Reply\")\n", 247 | "demo = gradio.Interface(fn=HealthCareChatbot, inputs = inputs, outputs = outputs, title = \"HealthCare Chatbot Backed by ArangoDB\")" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [ 256 | "demo.launch(share=True)" 257 | ] 258 | }, 259 | { 260 | "attachments": {}, 261 | "cell_type": "markdown", 262 | "metadata": {}, 263 | "source": [ 264 | "## Examples\n", 265 | "1. Hello!\n", 266 | "2. What are the symptoms of the disease Asthma?\n", 267 | "3. Which medications to use when suffering from Asthma?\n", 268 | "4. I have symptoms of chest pain and shortness of breath what type of disease it could be?\n", 269 | "5. How to treat COVID-19?" 270 | ] 271 | } 272 | ], 273 | "metadata": { 274 | "kernelspec": { 275 | "display_name": "Python [conda env:llm]", 276 | "language": "python", 277 | "name": "conda-env-llm-py" 278 | }, 279 | "language_info": { 280 | "codemirror_mode": { 281 | "name": "ipython", 282 | "version": 3 283 | }, 284 | "file_extension": ".py", 285 | "mimetype": "text/x-python", 286 | "name": "python", 287 | "nbconvert_exporter": "python", 288 | "pygments_lexer": "ipython3", 289 | "version": "3.10.11" 290 | } 291 | }, 292 | "nbformat": 4, 293 | "nbformat_minor": 4 294 | } 295 | --------------------------------------------------------------------------------