Skip to content

Convert any text to a graph of knowledge. This can be used for Graph Augmented Generation or Knowledge Graph based QnA

HTML
7.4 KB
Raw Blame
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'modelfile': '# Modelfile generated by \"ollama show\"\\n# To build a new Modelfile based on this one, replace the FROM line with:\\n# FROM zephyr:latest\\n\\nFROM /Users/rahulnayak/.ollama/models/blobs/sha256:0e655574a746a702b8faa09ea5bd779801371cec9e34f69760d0ebb1aec1a6ce\\nTEMPLATE \"\"\"{{- if .System }}\\n<|system|>\\n{{ .System }}\\n</s>\\n{{- end }}\\n<|user|>\\n{{ .Prompt }}\\n</s>\\n<|assistant|>\\n\"\"\"\\nPARAMETER stop \"<|system|>\"\\nPARAMETER stop \"<|user|>\"\\nPARAMETER stop \"<|assistant|>\"\\nPARAMETER stop \"</s>\"',\n",
       " 'parameters': 'stop                           <|system|>\\nstop                           <|user|>\\nstop                           <|assistant|>\\nstop                           </s>',\n",
       " 'template': '{{- if .System }}\\n<|system|>\\n{{ .System }}\\n</s>\\n{{- end }}\\n<|user|>\\n{{ .Prompt }}\\n</s>\\n<|assistant|>\\n'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import sys\n",
    "sys.path.append('..')\n",
    "\n",
    "import json\n",
    "import ollama.client as client\n",
    "\n",
    "model = 'zephyr'\n",
    "\n",
    "# response, _ = client.generate(model_name=model,prompt='prompt' )\n",
    "client.show('zephyr:latest')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def graphPrompt(input: str, metadata={}, model=\"mistral-openorca:latest\"):\n",
    "    if model == None:\n",
    "        model = \"mistral-openorca:latest\"\n",
    "\n",
    "    # model_info = client.show(model_name=model)\n",
    "    # print( chalk.blue(model_info))\n",
    "\n",
    "    SYS_PROMPT = (\n",
    "        \"You are a network graph maker who extracts terms and their relations from a given context. \"\n",
    "        \"You are provided with a context chunk (delimited by ```) Your task is to extract the ontology \"\n",
    "        \"of terms mentioned in the given context. These terms should represent the key concepts as per the context. \\n\"\n",
    "        \"Thought 1: While traversing through each sentence, Think about the key terms mentioned in it.\\n\"\n",
    "            \"\\tTerms may include object, entity, location, organization, person, \\n\"\n",
    "            \"\\tcondition, acronym, documents, service, concept, etc.\\n\"\n",
    "            \"\\tTerms should be as atomistic as possible\\n\\n\"\n",
    "        \"Thought 2: Think about how these terms can have one on one relation with other terms.\\n\"\n",
    "            \"\\tTerms that are mentioned in the same sentence or the same paragraph are typically related to each other.\\n\"\n",
    "            \"\\tTerms can be related to many other terms\\n\\n\"\n",
    "        \"Thought 3: Find out the relation between each such related pair of terms. \\n\\n\"\n",
    "        \"Format your output as a list of json. Each element of the list contains a pair of terms\"\n",
    "        \"and the relation between them, like the follwing: \\n\"\n",
    "        \"[\\n\"\n",
    "        \"   {\\n\"\n",
    "        '       \"node_1\": \"A concept from extracted ontology\",\\n'\n",
    "        '       \"node_2\": \"A related concept from extracted ontology\",\\n'\n",
    "        '       \"edge\": \"relationship between the two concepts, node_1 and node_2 in one or two sentences\"\\n'\n",
    "        \"   }, {...}\\n\"\n",
    "        \"]\"\n",
    "    )\n",
    "\n",
    "    USER_PROMPT = f\"context: ```{input}``` \\n\\n output: \"\n",
    "    response, _ = client.generate(\n",
    "        model_name=model, system=SYS_PROMPT, prompt=USER_PROMPT\n",
    "    )\n",
    "    try:\n",
    "        result = json.loads(response)\n",
    "        result = [dict(item, **metadata) for item in result]\n",
    "    except:\n",
    "        print(\"\\n\\nERROR ### Here is the buggy response: \", response, \"\\n\\n\")\n",
    "        result = None\n",
    "    return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "  {\n",
      "    \"node_1\": \"Mary\",\n",
      "    \"node_2\": \"person\",\n",
      "    \"edge\": \"Mary is a person.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"little lamb\",\n",
      "    \"node_2\": \"animal\",\n",
      "    \"edge\": \"The text mentions a little lamb, which is an animal.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"had\",\n",
      "    \"node_2\": \"action\",\n",
      "    \"edge\": \"Mary had something, which is an action.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"passed\",\n",
      "    \"node_2\": \"action\",\n",
      "    \"edge\": \"Mary passed something, which is an action.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"plate\",\n",
      "    \"node_2\": \"object\",\n",
      "    \"edge\": \"The text mentions a plate, which is an object.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"more\",\n",
      "    \"node_2\": \"quantity\",\n",
      "    \"edge\": \"Mary had more of something, which is a quantity.\"\n",
      "  }\n",
      "]\n",
      "```\n",
      "\n",
      "ERROR ### Here is the buggy response:  [\n",
      "  {\n",
      "    \"node_1\": \"Mary\",\n",
      "    \"node_2\": \"person\",\n",
      "    \"edge\": \"Mary is a person.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"little lamb\",\n",
      "    \"node_2\": \"animal\",\n",
      "    \"edge\": \"The text mentions a little lamb, which is an animal.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"had\",\n",
      "    \"node_2\": \"action\",\n",
      "    \"edge\": \"Mary had something, which is an action.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"passed\",\n",
      "    \"node_2\": \"action\",\n",
      "    \"edge\": \"Mary passed something, which is an action.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"plate\",\n",
      "    \"node_2\": \"object\",\n",
      "    \"edge\": \"The text mentions a plate, which is an object.\"\n",
      "  },\n",
      "  {\n",
      "    \"node_1\": \"more\",\n",
      "    \"node_2\": \"quantity\",\n",
      "    \"edge\": \"Mary had more of something, which is a quantity.\"\n",
      "  }\n",
      "]\n",
      "``` \n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "r = graphPrompt(\n",
    "    \"\"\"\n",
    "text delimited bweteen ``` \\n\n",
    "text: ```\n",
    "Mary had a little lamb,\n",
    "You've heard this tale before;\n",
    "But did you know she passed her plate, \n",
    "And had a little more!\n",
    "```\n",
    "\"\"\", model='zephyr:latest'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "OpenAI@3111",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}