Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
sadiaprodhan committed Apr 19, 2024
1 parent d9d0d38 commit 1c71ef7
Showing 1 changed file with 53 additions and 113 deletions.
166 changes: 53 additions & 113 deletions llm_integration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@
"base_uri": "https://localhost:8080/"
},
"id": "Hd3GQxcKdzzg",
"outputId": "c2eb737d-6859-4a62-e2f2-f399e88cafe4"
"outputId": "a70f2cfd-107c-494e-9196-a0bcc0beddc0"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: openai==0.28 in /usr/local/lib/python3.10/dist-packages (0.28.0)\n",
"Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/dist-packages (from openai==0.28) (2.31.0)\n",
"Requirement already satisfied: tqdm in /usr/local/lib/python3.10/dist-packages (from openai==0.28) (4.66.2)\n",
"Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from openai==0.28) (3.9.3)\n",
"Requirement already satisfied: aiohttp in /usr/local/lib/python3.10/dist-packages (from openai==0.28) (3.9.5)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28) (3.3.2)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28) (3.6)\n",
"Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28) (3.7)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28) (2.0.7)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/dist-packages (from requests>=2.20->openai==0.28) (2024.2.2)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.10/dist-packages (from aiohttp->openai==0.28) (1.3.1)\n",
Expand Down Expand Up @@ -110,7 +110,7 @@
" return f\"\"\"\n",
" Task: Generate Cypher queries to query a Neo4j graph database based on the provided schema definition.\n",
" Instructions:\n",
" Note: Generate a Cypher query that retrieves triples (node-relationship-node) with a limit of 1 and query using the entity or noun in lowercase.\n",
" Note: Generate a Cypher query that retrieves triples (node-relationship-node) with a limit of 1 and query using the entity or noun in lowercase and relationship.\n",
" Do not use any other relationship types or properties that are not provided.\n",
" If you cannot generate a Cypher statement based on the provided schema, explain the reason to the user.\n",
" Schema:\n",
Expand Down Expand Up @@ -148,7 +148,10 @@
" #print(results)\n",
" if(len(results) < 2):\n",
" return \"Could not generate answer\"\n",
"\n",
" print(\"Result from database\")\n",
" print(\"subject:{}\".format(results[0]))\n",
" print(\"relationship:{}\".format(results[1]))\n",
" print(\"object:{}\".format(results[2]))\n",
" prompt = f\"Translate the following triples into a coherent sentence for the question {question} into a sentence:\\n\\n\"\n",
" triples = \"\\n\".join([f\"Source: {results[0]}, Relationship: {results[1]}, Target: {results[2]}\" ])\n",
" messages = [\n",
Expand All @@ -168,6 +171,7 @@
" # Construct Cypher statement\n",
" time.sleep(60)\n",
" cypher = self.construct_cypher(question, history)\n",
" print(\"cypher generated by chatgpt\")\n",
" print(cypher)\n",
" try:\n",
" # time.sleep(60)\n",
Expand All @@ -176,7 +180,7 @@
" #response = \"\"\n",
" if len(a) < 2:\n",
" #print(\"IF- line number 108\")\n",
" response = \"Could not geenrate answer\"\n",
" response = \"Could not generate answer\"\n",
" print(response)\n",
" return response\n",
" else:\n",
Expand Down Expand Up @@ -216,10 +220,10 @@
"outputs": [],
"source": [
"gds_db = Neo4jGPTQuery(\n",
" url=\"neo4j+s://b069e602.databases.neo4j.io\",\n",
" url=\"neo4j+s://b68740ea.databases.neo4j.io\",\n",
" user=\"neo4j\",\n",
" password=\"\",\n",
" openai_api_key=\"\",\n",
" password=\"_lJbg6-mIWPdVXmtjTrAgcmBJFq7iHRJ8iHRBRDuDDc\",\n",
" openai_api_key=\"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\",\n",
")\n"
]
},
Expand All @@ -232,132 +236,68 @@
"height": 1000
},
"id": "4UMcG1RRfrL9",
"outputId": "a7ca0375-4939-4945-b550-bb8257886f6c"
"outputId": "6c5a33bf-9040-4beb-e59e-216cfd710217"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"name": "stdout",
"text": [
"Please write your question: How nucleus is reformed\n",
"MATCH (n1:Entity)-[r:RELATIONSHIP]->(n2:Entity)\n",
"WHERE toLower(n1.name) = 'nucleus' AND toLower(r.type) = 'reformed'\n",
"RETURN n1, r, n2\n",
"Please write your question: how many phases in cell cycle\n",
"cypher generated by chatgpt\n",
"MATCH (e1:Entity)-[r:RELATIONSHIP]->(e2:Entity)\n",
"WHERE toLower(e1.name) = 'cell cycle' AND toLower(r.type) = 'phases'\n",
"RETURN e1.name, r.type, e2.name\n",
"LIMIT 1\n",
"line 105 [['n1', 'r', 'n2'], [<Node element_id='4:29a03e11-e7f2-426a-bd35-32eda489d8ba:224' labels=frozenset({'Entity'}) properties={'name': 'nucleus'}>, <Relationship element_id='5:29a03e11-e7f2-426a-bd35-32eda489d8ba:1152921504606847200' nodes=(<Node element_id='4:29a03e11-e7f2-426a-bd35-32eda489d8ba:224' labels=frozenset({'Entity'}) properties={'name': 'nucleus'}>, <Node element_id='4:29a03e11-e7f2-426a-bd35-32eda489d8ba:225' labels=frozenset({'Entity'}) properties={'name': 'centrosome'}>) type='RELATIONSHIP' properties={'type': 'reformed'}>, <Node element_id='4:29a03e11-e7f2-426a-bd35-32eda489d8ba:225' labels=frozenset({'Entity'}) properties={'name': 'centrosome'}>]]\n",
"[<Node element_id='4:29a03e11-e7f2-426a-bd35-32eda489d8ba:224' labels=frozenset({'Entity'}) properties={'name': 'nucleus'}>, <Relationship element_id='5:29a03e11-e7f2-426a-bd35-32eda489d8ba:1152921504606847200' nodes=(<Node element_id='4:29a03e11-e7f2-426a-bd35-32eda489d8ba:224' labels=frozenset({'Entity'}) properties={'name': 'nucleus'}>, <Node element_id='4:29a03e11-e7f2-426a-bd35-32eda489d8ba:225' labels=frozenset({'Entity'}) properties={'name': 'centrosome'}>) type='RELATIONSHIP' properties={'type': 'reformed'}>, <Node element_id='4:29a03e11-e7f2-426a-bd35-32eda489d8ba:225' labels=frozenset({'Entity'}) properties={'name': 'centrosome'}>]\n",
"ELSE- line number 114\n",
"The nucleus is reformed in relation to the centrosome.\n",
"Please write your question: what makes telophase\n",
"Could not generate answer\n",
"Please write your question: what is interphase\n",
"cypher generated by chatgpt\n",
"MATCH (n:Entity)-[r:RELATIONSHIP]->(m:Entity) \n",
"WHERE toLower(n.name) = 'telophase' \n",
"WHERE toLower(n.name) = 'interphase' \n",
"RETURN n.name, r.type, m.name \n",
"LIMIT 1\n",
"line 105 [['n.name', 'r.type', 'm.name'], ['telophase', 'makes', '10 c ycle duration']]\n",
"['telophase', 'makes', '10 c ycle duration']\n",
"ELSE- line number 114\n",
"Telophase, a stage in cell division, results in a 10 cycle duration.\n",
"Please write your question: How is kinetochores occupied by\n",
"MATCH (n:Entity {name: 'kinetochores'})-[r:RELATIONSHIP]->(m:Entity)\n",
"RETURN n.name, r.type, m.name\n",
"LIMIT 1\n",
"line 105 [['n.name', 'r.type', 'm.name'], ['kinetochores', 'occupied by', 'kinetochore microtubul es']]\n",
"['kinetochores', 'occupied by', 'kinetochore microtubul es']\n",
"ELSE- line number 114\n",
"Kinetochores are occupied by kinetochore microtubules.\n",
"Please write your question: what is equatorial cell\n",
"MATCH (n1:Entity {name: 'equatorial cell'})-[r:RELATIONSHIP]->(n2:Entity)\n",
"RETURN n1, r, n2\n",
"LIMIT 1\n",
"line 105 [['n1', 'r', 'n2']]\n",
"IF- line number 108\n",
"Could not geenrate answer\n",
"Please write your question: How condensation begins\n",
"MATCH (n1:Entity)-[r:RELATIONSHIP]->(n2:Entity)\n",
"WHERE toLower(n1.name) = 'condensation' AND r.type = 'begins'\n",
"RETURN n1.name, r.type, n2.name\n",
"LIMIT 1\n",
"line 105 [['n1.name', 'r.type', 'n2.name'], ['condensation', 'begins', 'chromatin']]\n",
"['condensation', 'begins', 'chromatin']\n",
"ELSE- line number 114\n",
"The beginning of condensation is related to chromatin.\n",
"Please write your question: what destroyed cohesins\n",
"MATCH (n:Entity)-[r:RELATIONSHIP]->(m:Entity)\n",
"WHERE toLower(n.name) = 'cohesins' AND r.type = 'destroyed'\n",
"RETURN n.name, r.type, m.name\n",
"LIMIT 1\n",
"line 105 [['n.name', 'r.type', 'm.name'], ['cohesins', 'destroyed', 'sister chromatids']]\n",
"['cohesins', 'destroyed', 'sister chromatids']\n",
"ELSE- line number 114\n",
"The sister chromatids destroyed cohesins.\n",
"Please write your question: What is S phase?\n",
"Could not generate answer\n",
"Please write your question: what is M-phase\n",
"cypher generated by chatgpt\n",
"MATCH (n:Entity)-[r:RELATIONSHIP]->(m:Entity) \n",
"WHERE toLower(n.name) = 's phase' \n",
"WHERE toLower(n.name) = 'm-phase' \n",
"RETURN n.name, r.type, m.name LIMIT 1\n",
"line 105 [['n.name', 'r.type', 'm.name']]\n",
"IF- line number 108\n",
"Could not geenrate answer\n",
"Please write your question: How many phases mitosis is divided into\n",
"MATCH (n:Entity {name: 'mitosis'})-[r:RELATIONSHIP]->(m:Entity)\n",
"RETURN n.name, r.type, m.name LIMIT 1\n",
"line 105 [['n.name', 'r.type', 'm.name'], ['mitosis', 'is indirect', 'g2 cell cyc']]\n",
"['mitosis', 'is indirect', 'g2 cell cyc']\n",
"ELSE- line number 114\n",
"Mitosis, which is indirectly related to the G2 phase of the cell cycle, is divided into several phases.\n",
"Please write your question: How many phases mitosis divided\n",
"MATCH (e1:Entity)-[r:RELATIONSHIP]->(e2:Entity)\n",
"WHERE toLower(e1.name) = 'mitosis' AND toLower(r.type) = 'divided into'\n",
"RETURN e1.name, r.type, e2.name\n",
"LIMIT 1\n",
"line 105 [['e1.name', 'r.type', 'e2.name'], ['mitosis', 'divided into', 'five phases']]\n",
"['mitosis', 'divided into', 'five phases']\n",
"ELSE- line number 114\n",
"Mitosis is divided into five phases.\n",
"Please write your question: How many phases mitosis divided into\n",
"MATCH (e1:Entity)-[r:RELATIONSHIP]->(e2:Entity)\n",
"WHERE toLower(e1.name) = 'mitosis' AND toLower(r.type) = 'divided into'\n",
"RETURN e1.name, r.type, e2.name\n",
"Could not generate answer\n",
"Please write your question: how nucleus is reformed\n",
"cypher generated by chatgpt\n",
"MATCH (n:Entity)-[r:RELATIONSHIP]->(m:Entity)\n",
"WHERE toLower(n.name) = 'nucleus' AND toLower(r.type) = 'reformed'\n",
"RETURN n, r, m\n",
"LIMIT 1\n",
"line 105 [['e1.name', 'r.type', 'e2.name'], ['mitosis', 'divided into', 'five phases']]\n",
"['mitosis', 'divided into', 'five phases']\n",
"ELSE- line number 114\n",
"Mitosis is divided into five phases.\n",
"Please write your question: How sister chromatids are destroyed\n",
"Result from database\n",
"subject:<Node element_id='4:e23f223b-b311-4c32-9b95-03cbea8a6985:224' labels=frozenset({'Entity'}) properties={'name': 'nucleus'}>\n",
"relationship:<Relationship element_id='5:e23f223b-b311-4c32-9b95-03cbea8a6985:1152921504606847200' nodes=(<Node element_id='4:e23f223b-b311-4c32-9b95-03cbea8a6985:224' labels=frozenset({'Entity'}) properties={'name': 'nucleus'}>, <Node element_id='4:e23f223b-b311-4c32-9b95-03cbea8a6985:225' labels=frozenset({'Entity'}) properties={'name': 'centrosome'}>) type='RELATIONSHIP' properties={'type': 'reformed'}>\n",
"object:<Node element_id='4:e23f223b-b311-4c32-9b95-03cbea8a6985:225' labels=frozenset({'Entity'}) properties={'name': 'centrosome'}>\n",
"The nucleus is reformed through a process involving the centrosome.\n",
"Please write your question: what is an equatorial cell\n",
"cypher generated by chatgpt\n",
"MATCH (n1:Entity)-[r:RELATIONSHIP]->(n2:Entity)\n",
"WHERE toLower(n1.name) = 'sister chromatids' AND toLower(r.type) = 'destroyed'\n",
"RETURN n1.name, r.type, n2.name\n",
"LIMIT 1\n",
"line 105 [['n1.name', 'r.type', 'n2.name']]\n",
"IF- line number 108\n",
"Could not geenrate answer\n",
"Please write your question: what begins after g1 phase\n",
"MATCH (n1:Entity {name: 'g1 phase'})-[r:RELATIONSHIP]->(n2:Entity)\n",
"WHERE toLower(n1.name) = 'equatorial cell'\n",
"RETURN n1.name, r.type, n2.name\n",
"LIMIT 1\n",
"line 105 [['n1.name', 'r.type', 'n2.name'], ['g1 phase', 'begins after', 'previous mitosis']]\n",
"['g1 phase', 'begins after', 'previous mitosis']\n",
"ELSE- line number 114\n",
"The phase that begins after the G1 phase is the previous mitosis.\n",
"Please write your question: how a plant cell is formed and how it functions \n",
"I'm sorry, but I can't assist with that.\n",
"Retrying\n",
"MATCH (source:Entity)-[r:RELATIONSHIP]->(target:Entity)\n",
"WHERE toLower(source.name) = 'plant cell' OR toLower(target.name) = 'plant cell'\n",
"RETURN source.name, r.type, target.name\n",
"Could not generate answer\n",
"Please write your question: what is s phase?\n",
"cypher generated by chatgpt\n",
"MATCH (n:Entity)-[r:RELATIONSHIP]->(m:Entity)\n",
"WHERE toLower(n.name) = 's phase'\n",
"RETURN n.name, r.type, m.name\n",
"LIMIT 1\n",
"line 105 [['source.name', 'r.type', 'target.name']]\n",
"IF- line number 108\n",
"Could not geenrate answer\n"
"Could not generate answer\n"
]
},
{
"output_type": "error",
"ename": "KeyboardInterrupt",
"evalue": "Interrupted by user",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-5e91856c9c74>\u001b[0m in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mselected_word\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Please write your question: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mselected_word\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0moptions\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m query = f\"\"\"\n",
"\u001b[0;32m<ipython-input-4-dde5114071e1>\u001b[0m in \u001b[0;36m<cell line: 3>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 4\u001b[0;31m \u001b[0mselected_word\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34mf\"Please write your question: \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 5\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mselected_word\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32min\u001b[0m \u001b[0moptions\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 6\u001b[0m query = f\"\"\"\n",
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 849\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 850\u001b[0m )\n\u001b[0;32m--> 851\u001b[0;31m return self._input_request(str(prompt),\n\u001b[0m\u001b[1;32m 852\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 853\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/local/lib/python3.10/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 893\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 894\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 895\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 896\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 897\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
Expand Down Expand Up @@ -402,4 +342,4 @@
},
"nbformat": 4,
"nbformat_minor": 0
}
}

0 comments on commit 1c71ef7

Please sign in to comment.