From ba0e5c1762b1f2d1543b4997f58317f514f6e2bc Mon Sep 17 00:00:00 2001 From: Ackermann Yuriy Date: Sun, 14 Jan 2024 19:17:49 +1300 Subject: [PATCH] :lipstick: --- S7.ipynb | 72 ++++++++++- S8.ipynb | 322 ++++++++++++++++++++++++++++++++++++++++------- requirements.txt | 2 + 3 files changed, 352 insertions(+), 44 deletions(-) diff --git a/S7.ipynb b/S7.ipynb index 6a22b82..e3867a6 100644 --- a/S7.ipynb +++ b/S7.ipynb @@ -3687,11 +3687,79 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 47, "metadata": {}, "outputs": [], "source": [ - "llm = ChatOpenAI(temperature=0)" + "llm = ChatOpenAI(temperature=0)\n", + "\n", + "chain = RetrievalQA.from_chain_type(\n", + " llm=llm,\n", + " retriever=retriever,\n", + " verbose=True,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "\"LLaVA is a model that is specifically designed for instruction-following on multimodal data. It accurately follows user instructions and provides comprehensive responses, rather than simply describing the scene. LLaVA is trained with a small multimodal instruction-following dataset and demonstrates similar reasoning results to multimodal GPT-4. In contrast, GPT-4 focuses more on chat capabilities and generating responses based on textual input. LLaVA's ability to understand visual content that is not covered in its training data and its impressive OCR (optical character recognition) ability are some of its notable features.\"" + ] + }, + "execution_count": 48, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.run(\"What makes LLava different from GPT-4?\")" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new RetrievalQA chain...\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'The architecture of LLaVA consists of two main components: a Language Model (LM) and a Vision-Language Model (VLM). The LM is responsible for processing and generating text, while the VLM is responsible for understanding and generating visual content. The LM is pretrained on a large corpus of text data, while the VLM is pretrained on a large dataset of image-text pairs. These pretrained models are then fine-tuned on a specific task or dataset to improve their performance. The combination of the LM and VLM allows LLaVA to understand and generate responses that incorporate both textual and visual information.'" + ] + }, + "execution_count": 49, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.run(\"What is the architecture of LLava?\")" ] } ], diff --git a/S8.ipynb b/S8.ipynb index ed90773..feceede 100644 --- a/S8.ipynb +++ b/S8.ipynb @@ -535,29 +535,9 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 59, "metadata": {}, - "outputs": [ - { - "ename": "ImportError", - "evalue": "Could not import wikipedia python package. Please install it with `pip install wikipedia`.", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", - "File \u001b[0;32m~/Github/learning/langchain-udemy/venv/lib/python3.10/site-packages/langchain_community/utilities/wikipedia.py:33\u001b[0m, in \u001b[0;36mWikipediaAPIWrapper.validate_environment\u001b[0;34m(cls, values)\u001b[0m\n\u001b[1;32m 32\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m---> 33\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mwikipedia\u001b[39;00m\n\u001b[1;32m 35\u001b[0m wikipedia\u001b[38;5;241m.\u001b[39mset_lang(values[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlang\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n", - "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'wikipedia'", - "\nDuring handling of the above exception, another exception occurred:\n", - "\u001b[0;31mImportError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[56], line 5\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mlangchain\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mdocument_loaders\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mwikipedia\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m WikipediaLoader\n\u001b[1;32m 3\u001b[0m query \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWarren Buffett\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m----> 5\u001b[0m raw_docs \u001b[38;5;241m=\u001b[39m \u001b[43mWikipediaLoader\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mquery\u001b[49m\u001b[43m)\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Github/learning/langchain-udemy/venv/lib/python3.10/site-packages/langchain_community/document_loaders/wikipedia.py:53\u001b[0m, in \u001b[0;36mWikipediaLoader.load\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mload\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m List[Document]:\n\u001b[1;32m 46\u001b[0m \u001b[38;5;250m \u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 47\u001b[0m \u001b[38;5;124;03m Loads the query result from Wikipedia into a list of Documents.\u001b[39;00m\n\u001b[1;32m 48\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 51\u001b[0m \u001b[38;5;124;03m Wikipedia pages.\u001b[39;00m\n\u001b[1;32m 52\u001b[0m \u001b[38;5;124;03m \"\"\"\u001b[39;00m\n\u001b[0;32m---> 53\u001b[0m client \u001b[38;5;241m=\u001b[39m \u001b[43mWikipediaAPIWrapper\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 54\u001b[0m \u001b[43m \u001b[49m\u001b[43mlang\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mlang\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 55\u001b[0m \u001b[43m \u001b[49m\u001b[43mtop_k_results\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload_max_docs\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 56\u001b[0m \u001b[43m \u001b[49m\u001b[43mload_all_available_meta\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mload_all_available_meta\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 57\u001b[0m \u001b[43m \u001b[49m\u001b[43mdoc_content_chars_max\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdoc_content_chars_max\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 58\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 59\u001b[0m docs \u001b[38;5;241m=\u001b[39m client\u001b[38;5;241m.\u001b[39mload(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mquery)\n\u001b[1;32m 60\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m docs\n", - "File \u001b[0;32m~/Github/learning/langchain-udemy/venv/lib/python3.10/site-packages/pydantic/v1/main.py:339\u001b[0m, in \u001b[0;36mBaseModel.__init__\u001b[0;34m(__pydantic_self__, **data)\u001b[0m\n\u001b[1;32m 333\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 334\u001b[0m \u001b[38;5;124;03mCreate a new model by parsing and validating input data from keyword arguments.\u001b[39;00m\n\u001b[1;32m 335\u001b[0m \n\u001b[1;32m 336\u001b[0m \u001b[38;5;124;03mRaises ValidationError if the input data cannot be parsed to form a valid model.\u001b[39;00m\n\u001b[1;32m 337\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 338\u001b[0m \u001b[38;5;66;03m# Uses something other than `self` the first arg to allow \"self\" as a settable attribute\u001b[39;00m\n\u001b[0;32m--> 339\u001b[0m values, fields_set, validation_error \u001b[38;5;241m=\u001b[39m \u001b[43mvalidate_model\u001b[49m\u001b[43m(\u001b[49m\u001b[43m__pydantic_self__\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[38;5;18;43m__class__\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdata\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 340\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m validation_error:\n\u001b[1;32m 341\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m validation_error\n", - "File \u001b[0;32m~/Github/learning/langchain-udemy/venv/lib/python3.10/site-packages/pydantic/v1/main.py:1102\u001b[0m, in \u001b[0;36mvalidate_model\u001b[0;34m(model, input_data, cls)\u001b[0m\n\u001b[1;32m 1100\u001b[0m \u001b[38;5;28;01mcontinue\u001b[39;00m\n\u001b[1;32m 1101\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1102\u001b[0m values \u001b[38;5;241m=\u001b[39m \u001b[43mvalidator\u001b[49m\u001b[43m(\u001b[49m\u001b[43mcls_\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mvalues\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1103\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m (\u001b[38;5;167;01mValueError\u001b[39;00m, \u001b[38;5;167;01mTypeError\u001b[39;00m, \u001b[38;5;167;01mAssertionError\u001b[39;00m) \u001b[38;5;28;01mas\u001b[39;00m exc:\n\u001b[1;32m 1104\u001b[0m errors\u001b[38;5;241m.\u001b[39mappend(ErrorWrapper(exc, loc\u001b[38;5;241m=\u001b[39mROOT_KEY))\n", - "File \u001b[0;32m~/Github/learning/langchain-udemy/venv/lib/python3.10/site-packages/langchain_community/utilities/wikipedia.py:38\u001b[0m, in \u001b[0;36mWikipediaAPIWrapper.validate_environment\u001b[0;34m(cls, values)\u001b[0m\n\u001b[1;32m 36\u001b[0m values[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mwiki_client\u001b[39m\u001b[38;5;124m\"\u001b[39m] \u001b[38;5;241m=\u001b[39m wikipedia\n\u001b[1;32m 37\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m:\n\u001b[0;32m---> 38\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mImportError\u001b[39;00m(\n\u001b[1;32m 39\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCould not import wikipedia python package. \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 40\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mPlease install it with `pip install wikipedia`.\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 41\u001b[0m )\n\u001b[1;32m 42\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m values\n", - "\u001b[0;31mImportError\u001b[0m: Could not import wikipedia python package. Please install it with `pip install wikipedia`." - ] - } - ], + "outputs": [], "source": [ "from langchain.document_loaders import WikipediaLoader\n", "\n", @@ -566,6 +546,105 @@ "raw_docs = WikipediaLoader(query=query).load()" ] }, + { + "cell_type": "code", + "execution_count": 60, + "metadata": {}, + "outputs": [], + "source": [ + "graph_docs = diffbot_nlp.convert_to_graph_documents(raw_docs)" + ] + }, + { + "cell_type": "code", + "execution_count": 61, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[Relationship(source=Node(id='http://www.wikidata.org/entity/Q217583', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='CHIEF_EXECUTIVE_OFFICER', properties={'evidence': 'Warren Edward Buffett ( BUF-it; born August 30, 1930) is an American businessman, investor, and philanthropist who currently serves as the co-founder, chairman and CEO of Berkshire Hathaway.', 'isCurrent': 'true'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q217583', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='FOUNDED_BY', properties={'evidence': 'Warren Edward Buffett ( BUF-it; born August 30, 1930) is an American businessman, investor, and philanthropist who currently serves as the co-founder, chairman and CEO of Berkshire Hathaway.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), target=Node(id='Leila', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), target=Node(id='Leila', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Leila', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='HAS_CHILD', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Leila', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Leila', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q5284', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q203807', type='Organization'), type='EMPLOYEE_OR_MEMBER_OF', properties={'evidence': 'He founded the Giving Pledge in 2010 with Bill Gates, whereby billionaires pledge to give away at least half of their fortunes.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), target=Node(id='Stahl', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), target=Node(id='Stahl', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q16254414', type='Organization'), type='EDUCATED_AT', properties={'evidence': 'He attended New York Institute of Finance to focus on his economics background and soon pursued a business career.', 'isNotCurrent': 'true'}),\n", + " Relationship(source=Node(id='Buffett Partnership Ltd.', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q217583', type='Organization'), type='ACQUIRED_BY', properties={'evidence': 'He created Buffett Partnership Ltd. in 1956 and his investment firm eventually acquired a textile manufacturing firm, Berkshire Hathaway, assuming its name to create a diversified holding company.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q203807', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q5284', type='Person'), type='FOUNDED_BY', properties={'evidence': 'He founded the Giving Pledge in 2010 with Bill Gates, whereby billionaires pledge to give away at least half of their fortunes.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q8033204', type='Organization'), type='EDUCATED_AT', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".', 'isNotCurrent': 'true', 'endTime': '1947'}),\n", + " Relationship(source=Node(id='Leila', type='Person'), target=Node(id='Stahl', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='Alice Deal Junior High School', type='Organization'), type='EDUCATED_AT', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".', 'isNotCurrent': 'true'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q1553', type='Location'), type='PERSON_LOCATION', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q1553', type='Location'), type='PLACE_OF_BIRTH', properties={'evidence': 'As of January 2024, he had a net worth of $122 billion, making him the tenth-richest person in the world.Buffett was born in Omaha, Nebraska.'}),\n", + " Relationship(source=Node(id='Leila', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Leila', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q61', type='Location'), type='PERSON_LOCATION', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q61', type='Location'), type='PERSON_LOCATION', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q30', type='Location'), type='NATIONALITY', properties={'evidence': 'Warren Edward Buffett ( BUF-it; born August 30, 1930) is an American businessman, investor, and philanthropist who currently serves as the co-founder, chairman and CEO of Berkshire Hathaway.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q30', type='Location'), type='PERSON_LOCATION', properties={'evidence': 'Warren Edward Buffett ( BUF-it; born August 30, 1930) is an American businessman, investor, and philanthropist who currently serves as the co-founder, chairman and CEO of Berkshire Hathaway.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q217583', type='Organization'), type='EMPLOYEE_OR_MEMBER_OF', properties={'evidence': \"Since 1970, Buffett has presided as the chairman and largest shareholder of Berkshire Hathaway, one of America's foremost holding companies and world's leading corporate conglomerates.\", 'isCurrent': 'true', 'positionHeld': 'CEO'}),\n", + " Relationship(source=Node(id='Stahl', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Stahl', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='Leila', type='Person'), type='HAS_PARENT', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='Leila', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='Leila', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Alice Deal Junior High School', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q61', type='Location'), type='ORGANIZATION_LOCATIONS', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q918630', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='WORK_RELATIONSHIP', properties={'evidence': 'In 1978, fellow investor and long-time business associate Charlie Munger joined Buffett as vice-chairman.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q5284', type='Person'), type='WORK_RELATIONSHIP', properties={'evidence': 'He founded the Giving Pledge in 2010 with Bill Gates, whereby billionaires pledge to give away at least half of their fortunes.'}),\n", + " Relationship(source=Node(id='Warren', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q8033204', type='Organization'), type='EDUCATED_AT', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".', 'isNotCurrent': 'true', 'endTime': '1947'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q203807', type='Organization'), type='EMPLOYEE_OR_MEMBER_OF', properties={'evidence': 'He founded the Giving Pledge in 2010 with Bill Gates, whereby billionaires pledge to give away at least half of their fortunes.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q43199', type='Location'), type='PERSON_LOCATION', properties={'evidence': 'As of January 2024, he had a net worth of $122 billion, making him the tenth-richest person in the world.Buffett was born in Omaha, Nebraska.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q43199', type='Location'), type='PLACE_OF_BIRTH', properties={'evidence': 'As of January 2024, he had a net worth of $122 billion, making him the tenth-richest person in the world.Buffett was born in Omaha, Nebraska.'}),\n", + " Relationship(source=Node(id='Warren', type='Person'), target=Node(id='Alice Deal Junior High School', type='Organization'), type='EDUCATED_AT', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".', 'isNotCurrent': 'true'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q8033204', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q61', type='Location'), type='ORGANIZATION_LOCATIONS', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".', 'isCurrent': 'true'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), type='HAS_PARENT', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Buffett Partnership Ltd.', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q1505660', type='Skill'), type='INDUSTRY', properties={'evidence': 'He created Buffett Partnership Ltd. in 1956 and his investment firm eventually acquired a textile manufacturing firm, Berkshire Hathaway, assuming its name to create a diversified holding company.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q5284', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='WORK_RELATIONSHIP', properties={'evidence': 'He founded the Giving Pledge in 2010 with Bill Gates, whereby billionaires pledge to give away at least half of their fortunes.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='Stahl', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='Stahl', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='Stahl', type='Person'), type='HAS_PARENT', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q918630', type='Person'), type='WORK_RELATIONSHIP', properties={'evidence': 'In 1978, fellow investor and long-time business associate Charlie Munger joined Buffett as vice-chairman.'}),\n", + " Relationship(source=Node(id='Warren', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q61', type='Location'), type='PERSON_LOCATION', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".'}),\n", + " Relationship(source=Node(id='Warren', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q61', type='Location'), type='PERSON_LOCATION', properties={'evidence': 'In 1942, his father was elected to the first of four terms in the United States Congress, and after moving with his family to Washington, D.C., Warren finished elementary school, attended Alice Deal Junior High School and graduated from what was then Woodrow Wilson High School in 1947, where his senior yearbook picture reads: \"likes math; a future stockbroker\".'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='Buffett Partnership Ltd.', type='Organization'), type='EMPLOYEE_OR_MEMBER_OF', properties={'evidence': 'He created Buffett Partnership Ltd. in 1956 and his investment firm eventually acquired a textile manufacturing firm, Berkshire Hathaway, assuming its name to create a diversified holding company.', 'isNotCurrent': 'true', 'startTime': '1956'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q217583', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q1505660', type='Skill'), type='INDUSTRY', properties={'evidence': 'He created Buffett Partnership Ltd. in 1956 and his investment firm eventually acquired a textile manufacturing firm, Berkshire Hathaway, assuming its name to create a diversified holding company.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q723488', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='HAS_CHILD', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q8134', type='Skill'), type='INTERESTED_IN', properties={'evidence': 'He attended New York Institute of Finance to focus on his economics background and soon pursued a business career.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q8134', type='Skill'), type='INTERESTED_IN', properties={'evidence': 'He attended New York Institute of Finance to focus on his economics background and soon pursued a business career.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q8134', type='Skill'), type='FIELD_OF_WORK', properties={'evidence': 'He attended New York Institute of Finance to focus on his economics background and soon pursued a business career.'}),\n", + " Relationship(source=Node(id='http://www.wikidata.org/entity/Q203807', type='Organization'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='FOUNDED_BY', properties={'evidence': 'He founded the Giving Pledge in 2010 with Bill Gates, whereby billionaires pledge to give away at least half of their fortunes.'}),\n", + " Relationship(source=Node(id='Stahl', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='FAMILY_MEMBER', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Stahl', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='SOCIAL_RELATIONSHIP', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'}),\n", + " Relationship(source=Node(id='Stahl', type='Person'), target=Node(id='http://www.wikidata.org/entity/Q47213', type='Person'), type='HAS_CHILD', properties={'evidence': 'Warren Edward Buffett was born on August 30, 1930 in Omaha, Nebraska, as the second of three children and the only son of Leila (née Stahl) and Congressman Howard Buffett.'})]" + ] + }, + "execution_count": 61, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "graph_docs[0].relationships" + ] + }, + { + "cell_type": "code", + "execution_count": 62, + "metadata": {}, + "outputs": [], + "source": [ + "book_graph = diffbot_nlp.convert_to_graph_documents(book_data)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -575,13 +654,13 @@ }, { "cell_type": "code", - "execution_count": 52, + "execution_count": 63, "metadata": {}, "outputs": [], "source": [ "from langchain.graphs import Neo4jGraph\n", "\n", - "graph = Neo4jGraph(\n", + "graph_db = Neo4jGraph(\n", " url=\"bolt://localhost:7687\",\n", " username=\"neo4j\",\n", " password=\"pleaseletmein\"\n", @@ -590,24 +669,20 @@ }, { "cell_type": "code", - "execution_count": 53, + "execution_count": 64, "metadata": {}, - "outputs": [ - { - "ename": "AttributeError", - "evalue": "'Document' object has no attribute 'nodes'", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[0;32mIn[53], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[43mgraph\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43madd_graph_documents\u001b[49m\u001b[43m(\u001b[49m\u001b[43mbook_data\u001b[49m\u001b[43m)\u001b[49m\n", - "File \u001b[0;32m~/Github/learning/langchain-udemy/venv/lib/python3.10/site-packages/langchain_community/graphs/neo4j_graph.py:188\u001b[0m, in \u001b[0;36mNeo4jGraph.add_graph_documents\u001b[0;34m(self, graph_documents, include_source)\u001b[0m\n\u001b[1;32m 171\u001b[0m include_docs_query \u001b[38;5;241m=\u001b[39m (\n\u001b[1;32m 172\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCREATE (d:Document) \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 173\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSET d.text = $document.page_content \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 174\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mSET d += $document.metadata \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 175\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mWITH d \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 176\u001b[0m )\n\u001b[1;32m 177\u001b[0m \u001b[38;5;66;03m# Import nodes\u001b[39;00m\n\u001b[1;32m 178\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mquery(\n\u001b[1;32m 179\u001b[0m (\n\u001b[1;32m 180\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00minclude_docs_query\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mif\u001b[39;00m\u001b[38;5;250m \u001b[39minclude_source\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01melse\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 181\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUNWIND $data AS row \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 182\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCALL apoc.merge.node([row.type], \u001b[39m\u001b[38;5;124m{\u001b[39m\u001b[38;5;124mid: row.id}, \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 183\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mrow.properties, \u001b[39m\u001b[38;5;132;01m{}\u001b[39;00m\u001b[38;5;124m) YIELD node \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 184\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mMERGE (d)-[:MENTIONS]->(node) \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01mif\u001b[39;00m\u001b[38;5;250m \u001b[39minclude_source\u001b[38;5;250m \u001b[39m\u001b[38;5;28;01melse\u001b[39;00m\u001b[38;5;250m \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 185\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRETURN distinct \u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124mdone\u001b[39m\u001b[38;5;124m'\u001b[39m\u001b[38;5;124m AS result\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 186\u001b[0m ),\n\u001b[1;32m 187\u001b[0m {\n\u001b[0;32m--> 188\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdata\u001b[39m\u001b[38;5;124m\"\u001b[39m: [el\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__dict__\u001b[39m \u001b[38;5;28;01mfor\u001b[39;00m el \u001b[38;5;129;01min\u001b[39;00m \u001b[43mdocument\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mnodes\u001b[49m],\n\u001b[1;32m 189\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mdocument\u001b[39m\u001b[38;5;124m\"\u001b[39m: document\u001b[38;5;241m.\u001b[39msource\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__dict__\u001b[39m,\n\u001b[1;32m 190\u001b[0m },\n\u001b[1;32m 191\u001b[0m )\n\u001b[1;32m 192\u001b[0m \u001b[38;5;66;03m# Import relationships\u001b[39;00m\n\u001b[1;32m 193\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mquery(\n\u001b[1;32m 194\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mUNWIND $data AS row \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 195\u001b[0m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mCALL apoc.merge.node([row.source_label], \u001b[39m\u001b[38;5;124m{\u001b[39m\u001b[38;5;124mid: row.source},\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 214\u001b[0m },\n\u001b[1;32m 215\u001b[0m )\n", - "\u001b[0;31mAttributeError\u001b[0m: 'Document' object has no attribute 'nodes'" - ] - } - ], + "outputs": [], + "source": [ + "graph_db.add_graph_documents(book_graph)" + ] + }, + { + "cell_type": "code", + "execution_count": 65, + "metadata": {}, + "outputs": [], "source": [ - "graph.add_graph_documents(book_graph)" + "graph_db.refresh_schema()" ] }, { @@ -619,10 +694,173 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 66, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "from langchain.chains import GraphCypherQAChain\n", + "\n", + "cypher_llm = ChatOpenAI(temperature=0, model_name=\"gpt-4\")\n", + "qa_llm = ChatOpenAI(temperature=0, model_name=\"gpt-3.5-turbo\")\n", + "\n", + "chain = GraphCypherQAChain.from_llm(\n", + " cypher_llm=cypher_llm,\n", + " qa_llm=qa_llm,\n", + " graph=graph_db,\n", + " verbose=True,\n", + ")" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", + "Prompt after formatting:\n", + "\u001b[32;1m\u001b[1;3mTask:Generate Cypher statement to query a graph database.\n", + "Instructions:\n", + "Use only the provided relationship types and properties in the schema.\n", + "Do not use any other relationship types or properties that are not provided.\n", + "Schema:\n", + "Node properties are the following:\n", + "Person {id: STRING, name: STRING},Location {id: STRING, name: STRING},Skill {id: STRING, name: STRING},Organization {name: STRING, id: STRING}\n", + "Relationship properties are the following:\n", + "PERSON_LOCATION {evidence: STRING},INTERESTED_IN {evidence: STRING},SOCIAL_RELATIONSHIP {evidence: STRING},ROMANTIC_RELATIONSHIP {evidence: STRING},EMPLOYEE_OR_MEMBER_OF {evidence: STRING, isCurrent: STRING}\n", + "The relationships are the following:\n", + "(:Person)-[:PERSON_LOCATION]->(:Location),(:Person)-[:INTERESTED_IN]->(:Skill),(:Person)-[:ROMANTIC_RELATIONSHIP]->(:Person),(:Person)-[:SOCIAL_RELATIONSHIP]->(:Person),(:Person)-[:EMPLOYEE_OR_MEMBER_OF]->(:Organization)\n", + "Note: Do not include any explanations or apologies in your responses.\n", + "Do not respond to any questions that might ask anything else than for you to construct a Cypher statement.\n", + "Do not include any text except the generated Cypher statement.\n", + "\n", + "The question is:\n", + "What is happening in the North Pole?\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "Generated Cypher:\n", + "\u001b[32;1m\u001b[1;3mMATCH (p:Person)-[r:PERSON_LOCATION]->(l:Location) WHERE l.name = \"North Pole\" RETURN p,r,l;\u001b[0m\n", + "Full Context:\n", + "\u001b[32;1m\u001b[1;3m[]\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", + "Prompt after formatting:\n", + "\u001b[32;1m\u001b[1;3mYou are an assistant that helps to form nice and human understandable answers.\n", + "The information part contains the provided information that you must use to construct an answer.\n", + "The provided information is authoritative, you must never doubt it or try to use your internal knowledge to correct it.\n", + "Make the answer sound as a response to the question. Do not mention that you based the result on the given information.\n", + "If the provided information is empty, say that you don't know the answer.\n", + "Information:\n", + "[]\n", + "\n", + "Question: What is happening in the North Pole?\n", + "Helpful Answer:\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "\"I'm sorry, but I don't have any information on what is currently happening in the North Pole.\"" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from langchain.callbacks import StdOutCallbackHandler\n", + "\n", + "\n", + "chain.run(\"What is happening in the North Pole?\", callbacks=[StdOutCallbackHandler()])" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new GraphCypherQAChain chain...\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", + "Prompt after formatting:\n", + "\u001b[32;1m\u001b[1;3mTask:Generate Cypher statement to query a graph database.\n", + "Instructions:\n", + "Use only the provided relationship types and properties in the schema.\n", + "Do not use any other relationship types or properties that are not provided.\n", + "Schema:\n", + "Node properties are the following:\n", + "Person {id: STRING, name: STRING},Location {id: STRING, name: STRING},Skill {id: STRING, name: STRING},Organization {name: STRING, id: STRING}\n", + "Relationship properties are the following:\n", + "PERSON_LOCATION {evidence: STRING},INTERESTED_IN {evidence: STRING},SOCIAL_RELATIONSHIP {evidence: STRING},ROMANTIC_RELATIONSHIP {evidence: STRING},EMPLOYEE_OR_MEMBER_OF {evidence: STRING, isCurrent: STRING}\n", + "The relationships are the following:\n", + "(:Person)-[:PERSON_LOCATION]->(:Location),(:Person)-[:INTERESTED_IN]->(:Skill),(:Person)-[:ROMANTIC_RELATIONSHIP]->(:Person),(:Person)-[:SOCIAL_RELATIONSHIP]->(:Person),(:Person)-[:EMPLOYEE_OR_MEMBER_OF]->(:Organization)\n", + "Note: Do not include any explanations or apologies in your responses.\n", + "Do not respond to any questions that might ask anything else than for you to construct a Cypher statement.\n", + "Do not include any text except the generated Cypher statement.\n", + "\n", + "The question is:\n", + "Who is Timmy?\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "Generated Cypher:\n", + "\u001b[32;1m\u001b[1;3mMATCH (p:Person {name: 'Timmy'}) RETURN p\u001b[0m\n", + "Full Context:\n", + "\u001b[32;1m\u001b[1;3m[{'p': {'name': 'Timmy', 'id': 'Timmy'}}]\u001b[0m\n", + "\n", + "\n", + "\u001b[1m> Entering new LLMChain chain...\u001b[0m\n", + "Prompt after formatting:\n", + "\u001b[32;1m\u001b[1;3mYou are an assistant that helps to form nice and human understandable answers.\n", + "The information part contains the provided information that you must use to construct an answer.\n", + "The provided information is authoritative, you must never doubt it or try to use your internal knowledge to correct it.\n", + "Make the answer sound as a response to the question. Do not mention that you based the result on the given information.\n", + "If the provided information is empty, say that you don't know the answer.\n", + "Information:\n", + "[{'p': {'name': 'Timmy', 'id': 'Timmy'}}]\n", + "\n", + "Question: Who is Timmy?\n", + "Helpful Answer:\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + }, + { + "data": { + "text/plain": [ + "'Timmy is a person whose name is Timmy.'" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "chain.run(\"Who is Timmy?\", callbacks=[StdOutCallbackHandler()])" + ] } ], "metadata": { diff --git a/requirements.txt b/requirements.txt index e8cbcba..81f504c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -95,6 +95,7 @@ monotonic==1.6 mpmath==1.3.0 multidict==6.0.4 mypy-extensions==1.0.0 +neo4j==5.16.0 nest-asyncio==1.5.8 networkx==3.2.1 newsapi-python==0.2.7 @@ -202,6 +203,7 @@ watchfiles==0.21.0 wcwidth==0.2.12 websocket-client==1.7.0 websockets==12.0 +wikipedia==1.4.0 wrapt==1.16.0 yarl==1.9.4 youtube-dl==2021.12.17