From 3febbf89cb3007527b12e3af84550629c8e46077 Mon Sep 17 00:00:00 2001 From: Tomaz Bratanic Date: Mon, 16 Sep 2024 12:44:53 +0900 Subject: [PATCH] polish graphreader --- graphreader/graphreader_langgraph.ipynb | 106 ++++++++++++------------ 1 file changed, 54 insertions(+), 52 deletions(-) diff --git a/graphreader/graphreader_langgraph.ipynb b/graphreader/graphreader_langgraph.ipynb index a2624a0..5af4d55 100644 --- a/graphreader/graphreader_langgraph.ipynb +++ b/graphreader/graphreader_langgraph.ipynb @@ -477,13 +477,13 @@ " return data\n", "\n", "# Todo get more neighbors\n", - "def get_neighbors_by_chunk(chunk_id: str) -> List[str]:\n", - " data = neo4j_graph.query(\"\"\"\n", - " MATCH (c:Chunk)-[:HAS_ATOMIC_FACT]->()-[:HAS_KEY_ELEMENT]->(element)\n", - " WHERE c.id = $chunk_id\n", - " RETURN collect(element.id) AS neighbors\n", - " \"\"\", params={\"chunk_id\": chunk_id})\n", - " return data\n", + "#def get_neighbors_by_chunk(chunk_id: str) -> List[str]:\n", + "# data = neo4j_graph.query(\"\"\"\n", + "# MATCH (c:Chunk)-[:HAS_ATOMIC_FACT]->()-[:HAS_KEY_ELEMENT]->(element)\n", + "# WHERE c.id = $chunk_id\n", + "# RETURN collect(element.id) AS neighbors\n", + "# \"\"\", params={\"chunk_id\": chunk_id})\n", + "# return data\n", "\n", "def chunk_check(state: OverallState) -> OverallState:\n", " check_chunks_queue = state.get(\"check_chunks_queue\")\n", @@ -516,8 +516,9 @@ " # Go over to next chunk\n", " if not check_chunks_queue:\n", " response[\"chosen_action\"] = \"search_neighbor\"\n", - " # Get neighbors\n", - " neighbors = get_neighbors_by_chunk(chunk_id)\n", + " # Get neighbors/use vector similarity\n", + " print(f\"Neighbor rational: {read_chunk_results.rational_next_move}\")\n", + " neighbors = get_potential_nodes(read_chunk_results.rational_next_move)\n", " response[\"neighbor_check_queue\"] = neighbors\n", " \n", " response[\"check_chunks_queue\"] = check_chunks_queue\n", @@ -737,7 +738,7 @@ } ], "source": [ - "langgraph = StateGraph(OverallState)\n", + "langgraph = StateGraph(OverallState, input=InputState, output=OutputState)\n", "langgraph.add_node(\"rational_plan_node\", rational_plan_node)\n", "langgraph.add_node(\"initial_node_selection\", initial_node_selection)\n", "langgraph.add_node(\"atomic_fact_check\", atomic_fact_check)\n", @@ -780,15 +781,15 @@ "text": [ "--------------------\n", "Step: rational_plan\n", - "Rational plan: To answer this question, we need to identify the specific battles Joan of Arc participated in during her military career. Then, we must determine the outcomes of these battles to see if any were lost by her forces. This requires gathering information on each battle's result where Joan of Arc had a significant role.\n", + "Rational plan: To answer this question, I need to find information on the specific battles Joan of Arc participated in during her military career and determine whether she experienced any defeats in those battles. This involves identifying the battles she was involved in and checking the outcomes of each.\n", "--------------------\n", "Step: atomic_fact_check\n", - "Reading atomic facts about: ['Joan of Arc', 'siege of Orléans', 'Loire Campaign', 'Compiègne', 'unsuccessful']\n", - "Rational for next action after atomic check: To fully answer the question, we need to explore more details about the battles Joan of Arc lost, specifically the unsuccessful sieges of Paris and La Charité, to determine the context and implications of these losses.\n", + "Reading atomic facts about: ['defeats', 'Joan of Arc', 'siege of Orléans', 'Loire Campaign', 'battle']\n", + "Rational for next action after atomic check: To further confirm and detail the battles Joan of Arc lost, reading the text chunk that contains these atomic facts will provide more comprehensive information.\n", "Chosen action: {'function_name': 'read_chunk', 'arguments': [['3feaa3f3137a5f839bfc207bc5c2ffdb']]}\n", "--------------------\n", "Step: read chunk(3feaa3f3137a5f839bfc207bc5c2ffdb)\n", - "Rational for next action after reading chunks: The current text chunk confirms Joan of Arc's participation in failed sieges at Paris and La Charité, which aligns with the notebook content. This information is sufficient to answer the question about whether Joan of Arc lost any battles. No further information from other chunks is necessary as the question specifically pertains to her battle losses, which have been clearly identified.\n", + "Rational for next action after reading chunks: The current text chunk confirms that Joan of Arc did indeed lose some battles, specifically mentioning her participation in the unsuccessful sieges of Paris and La Charité. This information directly answers the question.\n", "Chosen action: {'function_name': 'termination', 'arguments': []}\n", "--------------------\n", "Step: Answer Reasoning\n" @@ -797,17 +798,13 @@ { "data": { "text/plain": [ - "{'question': 'Did Joan of Arc lose any battles?',\n", - " 'rational_plan': \"To answer this question, we need to identify the specific battles Joan of Arc participated in during her military career. Then, we must determine the outcomes of these battles to see if any were lost by her forces. This requires gathering information on each battle's result where Joan of Arc had a significant role.\",\n", - " 'notebook': \"Joan of Arc participated in several key battles during the Hundred Years' War, including the siege of Orléans and the Loire Campaign, both of which ended in victories. However, she also participated in failed sieges, such as those at Paris and La Charité. Additionally, she was involved in the unsuccessful siege of Paris in September 1429 and the failed siege of La Charité in November. Her role in these defeats reduced the court's faith in her.\",\n", + "{'answer': 'Yes, Joan of Arc did lose some battles, including the sieges of Paris and La Charité.',\n", + " 'analysis': 'The notebook content indicates that Joan of Arc participated in multiple battles and sieges, achieving successes like the siege of Orléans and the Loire Campaign. However, it also mentions her defeats, specifically pointing out the unsuccessful sieges of Paris and La Charité.',\n", " 'previous_actions': ['rational_plan',\n", " 'initial_node_selection',\n", - " \"atomic_fact_check(['Joan of Arc', 'siege of Orléans', 'Loire Campaign', 'Compiègne', 'unsuccessful'])\",\n", + " \"atomic_fact_check(['defeats', 'Joan of Arc', 'siege of Orléans', 'Loire Campaign', 'battle'])\",\n", " 'read_chunks(3feaa3f3137a5f839bfc207bc5c2ffdb)',\n", - " 'answer_reasoning'],\n", - " 'check_atomic_facts_queue': [],\n", - " 'check_chunks_queue': [],\n", - " 'chosen_action': 'termination'}" + " 'answer_reasoning']}" ] }, "execution_count": 14, @@ -831,17 +828,17 @@ "text": [ "--------------------\n", "Step: rational_plan\n", - "Rational plan: To answer the question about the current weather in Spain, I would need to access real-time weather data or forecasts from a reliable meteorological source. This would involve checking the latest weather updates for various regions in Spain, including temperature, precipitation, and any weather warnings.\n", + "Rational plan: To answer the question about the current weather in Spain, we need to access real-time weather data or forecasts from a reliable meteorological source, such as a weather website or application that provides updated information on temperature, precipitation, and other relevant weather conditions across different regions of Spain.\n", "--------------------\n", "Step: atomic_fact_check\n", "Reading atomic facts about: ['Spain']\n", - "Rational for next action after atomic check: Since the atomic facts provided do not contain any relevant information about the current weather in Spain, and given the nature of the question which requires up-to-date meteorological data, it is unlikely that further exploration of the text will yield the necessary information.\n", + "Rational for next action after atomic check: Since there are no atomic facts provided in this step, it indicates that the current node might not contain relevant information regarding the current weather in Spain.\n", "Chosen action: {'function_name': 'stop_and_read_neighbor', 'arguments': []}\n", "Key elements: ['Spain']\n", "--------------------\n", "Step: neighbor select\n", "Possible candidates: [{'possible_candidates': []}]\n", - "Rational for next action after selecting neighbor: Since the neighbor nodes do not contain any possible candidates that would provide information about the current weather in Spain, it is clear that further exploration within this graph will not yield the necessary real-time weather data.\n", + "Rational for next action after selecting neighbor: Since the neighbor nodes list is empty, there are no further nodes to explore that could provide information relevant to the current question about the weather in Spain.\n", "Chosen action: {'function_name': 'termination', 'arguments': []}\n", "--------------------\n", "Step: Answer Reasoning\n" @@ -850,17 +847,13 @@ { "data": { "text/plain": [ - "{'question': 'What is the weather in Spain?',\n", - " 'rational_plan': 'To answer the question about the current weather in Spain, I would need to access real-time weather data or forecasts from a reliable meteorological source. This would involve checking the latest weather updates for various regions in Spain, including temperature, precipitation, and any weather warnings.',\n", - " 'notebook': 'The question seeks current weather information for Spain, which typically requires real-time data or forecasts from meteorological sources.',\n", + "{'answer': 'I cannot provide the current weather in Spain directly. Please check a reliable weather website or application for the most accurate and up-to-date information.',\n", + " 'analysis': 'The notebook content indicates that to determine the current weather in Spain, one must access real-time data from a reliable meteorological source. This suggests that the information cannot be directly provided here without accessing updated weather services.',\n", " 'previous_actions': ['rational_plan',\n", " 'initial_node_selection',\n", " \"atomic_fact_check(['Spain'])\",\n", " 'neighbor_select',\n", - " 'answer_reasoning'],\n", - " 'check_atomic_facts_queue': [],\n", - " 'neighbor_check_queue': [],\n", - " 'chosen_action': 'termination'}" + " 'answer_reasoning']}" ] }, "execution_count": 15, @@ -874,7 +867,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 16, "id": "21900334-d1dc-4761-a576-61c4569f920e", "metadata": {}, "outputs": [ @@ -884,31 +877,43 @@ "text": [ "--------------------\n", "Step: rational_plan\n", - "Rational plan: In order to answer this question, we first need to identify the cities Joan of Arc visited during her early life. Next, we need to list the cities where she won battles. Finally, we compare these two lists to see if there are any cities that appear on both lists, indicating that she visited them in her early life and also won battles there later.\n", + "Rational plan: To answer this question, we need to identify the cities Joan of Arc visited during her early life and then determine if any of these cities were locations where she won battles later in her life. We should look for information about her early life, including her place of birth and any travels she might have undertaken before becoming actively involved in military campaigns. Additionally, we need to compile a list of battles Joan of Arc participated in and the cities associated with these battles. By comparing these two lists, we can determine if there is any overlap.\n", "--------------------\n", "Step: atomic_fact_check\n", - "Reading atomic facts about: ['Joan of Arc', 'Domrémy', 'siege of Orléans', 'Loire Campaign', 'Compiègne']\n", - "Rational for next action after atomic check: Identify the cities Joan of Arc visited during her early life and the cities where she won battles. Compare these lists to determine if there are any cities that appear on both lists.\n", + "Reading atomic facts about: ['Joan of Arc', 'young Joan', 'Orléans', 'Loire Campaign', 'siege of Orléans']\n", + "Rational for next action after atomic check: Review the text chunks to find more detailed information about the cities Joan visited during her early life and the battles she fought in those cities.\n", "Chosen action: {'function_name': 'read_chunk', 'arguments': [['3feaa3f3137a5f839bfc207bc5c2ffdb']]}\n", "--------------------\n", "Step: read chunk(3feaa3f3137a5f839bfc207bc5c2ffdb)\n", - "Rational for next action after reading chunks: The current chunk provides additional details about Joan's military engagements, including her role in the siege of Orléans and other battles. However, it does not offer new information about her early life visits to cities where she later won battles. We need to continue searching for more specific information about the cities she visited during her early life to see if any of these match the cities where she later won battles.\n", + "Rational for next action after reading chunks: We have detailed information about Joan's involvement in various battles and her early life, including her birthplace and initial travels to meet Charles VII. However, we still need to determine if any of the cities she visited early in her life were later battle sites. We should search for more specific information about her early travels and any other cities she might have visited before her military involvement.\n", "Chosen action: {'function_name': 'search_more', 'arguments': []}\n", + "Neighbor rational: We have detailed information about Joan's involvement in various battles and her early life, including her birthplace and initial travels to meet Charles VII. However, we still need to determine if any of the cities she visited early in her life were later battle sites. We should search for more specific information about her early travels and any other cities she might have visited before her military involvement.\n", + "--------------------\n", + "Step: neighbor select\n", + "Possible candidates: ['Joan of Arc', 'The Messenger: The Story of Joan of Arc', 'Charles VII', 'young Joan', 'French war heroine', 'Joan', \"Joan's trial\", 'The Passion of Joan of Arc', 'siege of Orléans', \"Charles VII's coronation\", 'Hundred Years War', 'Loire Campaign', \"Hundred Years' War\", 'French army', 'King of France', 'Burgundian troops', 'French allies', 'Orléans', 'Burgundians', 'Archbishop of Paris', 'Compiègne', 'Saint Catherine', 'French morale', 'early 1430', 'battle', 'French nationalists', 'Bishop Pierre Cauchon', 'French Revolution', 'Rouen prison', 'northeast France', 'save France', 'French nation', 'French silent historical film', 'Saint Margaret', 'Paris', 'France', 'siege', 'French', 'Renée Jeanne Falconetti', 'unsuccessful siege', 'failed siege', 'La Charité', 'September 1429', 'military leader', 'religious martyr', 'medieval architecture', 'April 1429', 'English atrocities', 'Reims', 'birth']\n", + "Rational for next action after selecting neighbor: To find out if Joan of Arc visited any cities in her early life where she later won battles, we need to explore nodes related to her early life travels and the cities she visited before becoming actively involved in military campaigns. The node 'birth' might provide information about her early life locations, which is crucial to answer the question.\n", + "Chosen action: {'function_name': 'read_neighbor_node', 'arguments': ['birth']}\n", + "--------------------\n", + "Step: atomic_fact_check\n", + "Reading atomic facts about: ['birth']\n", + "Rational for next action after atomic check: stop_and_read_neighbor\n", + "Chosen action: {'function_name': 'stop_and_read_neighbor', 'arguments': []}\n", + "Key elements: ['birth']\n", "--------------------\n", "Step: neighbor select\n", - "Possible candidates: [{'neighbors': ['Joan of Arc', 'France', 'patron saint', 'defender', 'French nation', 'siege of Orléans', 'coronation', 'Charles VII', \"Hundred Years' War\", 'divine guidance', 'military leader', 'gender roles', 'savior', 'Joan of Arc', 'Domrémy', 'northeast France', 'propertied peasant family', 'Joan of Arc', 'Charles VII', '1428', 'visions', 'archangel Michael', 'Saint Margaret', 'Saint Catherine', 'English domination', 'Joan of Arc', 'siege of Orléans', 'Charles VII', 'devotion', 'purity', 'relief army', 'seventeen years', 'Joan of Arc', 'siege of Orléans', 'April 1429', 'banner', 'French army', 'English', 'Joan of Arc', 'Charles VII', 'English', 'French', 'Loire Campaign', 'Patay', 'Reims', 'King of France', 'Joan of Arc', 'coronation', 'Charles VII', \"Hundred Years' War\", 'French morale', 'Joan of Arc', 'Paris', 'La Charité', 'siege', \"court's faith\", 'Joan of Arc', 'English', '1430', 'Compiègne', 'Burgundians', 'volunteers', 'Joan of Arc', '23 May', 'Burgundian troops', 'capture', 'captured', 'Joan of Arc', 'English', 'escape attempts', 'November', 'Joan of Arc', 'visions', 'trial', 'Bishop Pierre Cauchon', 'heresy', 'blaspheming', \"men's clothes\", 'demonic', 'church']}]\n", - "Rational for next action after selecting neighbor: To find out if Joan of Arc visited any cities in her early life where she later won battles, we need to explore more about the cities she visited in her early life and where she fought battles. The neighbor node 'Domrémy' could potentially provide more specific information about her early life visits, which is crucial for answering the question.\n", + "Possible candidates: [{'possible_candidates': ['Joan of Arc', 'propertied peasant family', 'Domrémy', 'northeast France']}]\n", + "Rational for next action after selecting neighbor: To determine if Joan of Arc visited any cities in her early life where she later won battles, we need to explore more about her early life, specifically her travels and activities before she became involved in military campaigns. The neighbor node 'Domrémy' is directly related to her place of birth and may provide insights into her early life travels or experiences.\n", "Chosen action: {'function_name': 'read_neighbor_node', 'arguments': ['Domrémy']}\n", "--------------------\n", "Step: atomic_fact_check\n", "Reading atomic facts about: ['Domrémy']\n", - "Rational for next action after atomic check: Since the atomic fact about Joan of Arc being born in Domrémy has already been noted in the notebook, there is no new information to add. The next step should be to continue exploring other nodes or chunks to find more information about cities Joan visited in her early life and where she won battles.\n", + "Rational for next action after atomic check: stop_and_read_neighbor\n", "Chosen action: {'function_name': 'stop_and_read_neighbor', 'arguments': []}\n", "Key elements: ['Domrémy']\n", "--------------------\n", "Step: neighbor select\n", - "Possible candidates: [{'possible_candidates': ['Joan of Arc', 'northeast France', 'propertied peasant family']}]\n", - "Rational for next action after selecting neighbor: The neighbor node 'northeast France' might provide geographical context but is unlikely to offer specific details about cities Joan of Arc visited in her early life or where she won battles. The node 'propertied peasant family' could give background on her family status but does not directly relate to the cities she visited or battles won. Since the current information sufficiently answers the question, further exploration of these nodes is unnecessary.\n", + "Possible candidates: [{'possible_candidates': ['Joan of Arc', 'birth', 'propertied peasant family', 'northeast France']}]\n", + "Rational for next action after selecting neighbor: Since we have already explored the nodes related to Joan of Arc's birth and early life in Domrémy, and we have gathered information about the battles she participated in, the next logical step is to determine if any of these battles occurred in cities she visited during her early life. However, none of the current neighboring nodes ('propertied peasant family', 'northeast France') seem relevant to answering the specific question about cities visited in her early life where she later won battles. Therefore, we should terminate the search as we have sufficient information to conclude that there is no overlap between her early life visits and battle locations based on the explored data.\n", "Chosen action: {'function_name': 'termination', 'arguments': []}\n", "--------------------\n", "Step: Answer Reasoning\n" @@ -917,24 +922,21 @@ { "data": { "text/plain": [ - "{'question': 'Did Joan of Arc visit any cities in early life where she won battles later?',\n", - " 'rational_plan': 'In order to answer this question, we first need to identify the cities Joan of Arc visited during her early life. Next, we need to list the cities where she won battles. Finally, we compare these two lists to see if there are any cities that appear on both lists, indicating that she visited them in her early life and also won battles there later.',\n", - " 'notebook': 'Joan of Arc was born in Domrémy, northeast France. She visited Orléans and participated in battles there, notably lifting the siege of Orléans in April 1429. Joan also encouraged the French during the Loire Campaign, leading to victories such as at Patay. She was involved in the unsuccessful sieges of Paris and La Charité, and organized a relief for Compiègne.',\n", + "{'answer': 'No, Joan of Arc did not visit any cities in her early life where she won battles later. Her early life was spent in Domrémy, and she only visited cities such as Orléans, Paris, La Charité, and Compiègne during her military campaigns.',\n", + " 'analysis': \"The notebook content provides detailed information about Joan of Arc's early life and her military engagements. It mentions that Joan was born in Domrémy, northeast France, and later participated in several key battles and sieges, including Orléans, Paris, La Charité, and a relief effort for Compiègne. However, there is no mention of her visiting any of these cities before she began her military campaigns. The only location connected to her early life before her visions and military involvement is Domrémy.\",\n", " 'previous_actions': ['rational_plan',\n", " 'initial_node_selection',\n", - " \"atomic_fact_check(['Joan of Arc', 'Domrémy', 'siege of Orléans', 'Loire Campaign', 'Compiègne'])\",\n", + " \"atomic_fact_check(['Joan of Arc', 'young Joan', 'Orléans', 'Loire Campaign', 'siege of Orléans'])\",\n", " 'read_chunks(3feaa3f3137a5f839bfc207bc5c2ffdb)',\n", " 'neighbor_select',\n", + " \"atomic_fact_check(['birth'])\",\n", + " 'neighbor_select',\n", " \"atomic_fact_check(['Domrémy'])\",\n", " 'neighbor_select',\n", - " 'answer_reasoning'],\n", - " 'check_atomic_facts_queue': [],\n", - " 'check_chunks_queue': [],\n", - " 'neighbor_check_queue': [],\n", - " 'chosen_action': 'termination'}" + " 'answer_reasoning']}" ] }, - "execution_count": 17, + "execution_count": 16, "metadata": {}, "output_type": "execute_result" }