{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"id": "e9f83b0b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"import json\n",
"import re\n",
"from dotenv import load_dotenv\n",
"\n",
"from IPython.display import Image, display\n",
"from langgraph.graph import StateGraph, START, END\n",
"from langchain_google_genai import ChatGoogleGenerativeAI\n",
"from langchain_groq import ChatGroq\n",
"from langchain_openai import ChatOpenAI\n",
"from langchain_anthropic import ChatAnthropic\n",
"from langchain_mistralai.chat_models import ChatMistralAI\n",
"from langchain_cohere import ChatCohere\n",
"\n",
"\n",
"from typing_extensions import TypedDict\n",
"\n",
"from langchain_community.tools.tavily_search import TavilySearchResults\n",
"from langchain_community.document_loaders import WikipediaLoader\n",
"\n",
"import networkx as nx\n",
"from pyvis.network import Network\n",
"\n",
"load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "be96faeb",
"metadata": {},
"outputs": [],
"source": [
"# May add that later (have to ask for user tavily api too)\n",
"def web_search(query: str) -> str:\n",
" \"\"\"\n",
" Search Tavily for a query to answer questions about recent events or general knowledge.\n",
" \n",
" Args:\n",
" query: The search query.\n",
" \"\"\"\n",
" search_tool = TavilySearchResults(max_results=2)\n",
" \n",
" # The tool now returns a dictionary, and the actual results are in the 'results' key\n",
" search_output = search_tool.invoke({\"query\": query})\n",
" \n",
" formatted_output = \"\\n\\n\".join([doc[\"content\"] for doc in search_output]) \n",
" print(search_output)\n",
" \n",
" # We need to loop through the list of result dictionaries and extract the 'content' from each\n",
" return formatted_output\n",
"\n",
"def wiki_search(query: str) -> str:\n",
" \"\"\"\n",
" Search Wikipedia for a query and return maximum 1 results.\n",
" \n",
" Args:\n",
" query: The search query.\n",
" \"\"\"\n",
" search_docs = WikipediaLoader(query=query, load_max_docs=1).load()\n",
" \n",
" formatted_search_docs = \"\\n\\n---\\n\\n\".join(\n",
" [\n",
" f'
No graph data could be generated from the text.
\"}\n", "\n", " # 1. Create a NetworkX graph to hold the data structure (same as before)\n", " G = nx.DiGraph()\n", "\n", " # Add nodes with attributes that pyvis can use, like 'size' and 'title' (for hover text)\n", " for keyword in keywords:\n", " concept = keyword.get(\"concept\")\n", " importance = keyword.get(\"importance\", 5)\n", " if concept:\n", " G.add_node(\n", " concept,\n", " size=15 + (importance * 2), # Calculate node size based on importance\n", " title=f\"Importance: {importance}\", # This creates a tooltip on hover\n", " importance_val=importance # Store for coloring later\n", " )\n", "\n", " # Add edges with labels (same as before)\n", " for conn in connections:\n", " source = conn.get(\"source\")\n", " target = conn.get(\"target\")\n", " relation = conn.get(\"relation\", \"\")\n", " if source in G and target in G:\n", " G.add_edge(source, target, label=relation)\n", "\n", " # 2. Initialize a pyvis Network for visualization\n", " # Set a specific height, a dark background, and white font for a modern look.\n", " net = Network(height=\"750px\", width=\"100%\", bgcolor=\"#222222\", font_color=\"white\", directed=True)\n", "\n", " # 3. Transfer the graph structure from NetworkX to pyvis\n", " net.from_nx(G)\n", "\n", " # 4. Apply custom styling to nodes after they've been added to the pyvis net\n", " for node in net.nodes:\n", " importance = node.get(\"importance_val\", 5)\n", " if importance >= 9:\n", " node[\"color\"] = \"#ff4757\" # Red for highest importance\n", " elif importance >= 7:\n", " node[\"color\"] = \"#ffa502\" # Orange for high importance\n", " elif importance >= 5:\n", " node[\"color\"] = \"#2ed573\" # Green for medium importance\n", " else:\n", " node[\"color\"] = \"#1e90ff\" # Blue for lower importance\n", "\n", " # 5. Add physics layout options for a more dynamic and readable graph\n", " # These settings prevent nodes from overlapping and create a nice \"springy\" effect.\n", " net.set_options(\"\"\"\n", " var options = {\n", " \"physics\": {\n", " \"repulsion\": {\n", " \"centralGravity\": 0.2,\n", " \"springLength\": 100,\n", " \"springConstant\": 0.05,\n", " \"nodeDistance\": 150,\n", " \"damping\": 0.09\n", " },\n", " \"maxVelocity\": 50,\n", " \"minVelocity\": 0.1,\n", " \"solver\": \"repulsion\"\n", " }\n", " }\n", " \"\"\")\n", "\n", " # 6. Generate the HTML and return its content as a string\n", " try:\n", " # save_graph writes the complete HTML structure to a file\n", " net.save_graph(output_filename)\n", " \n", " # Read the content of the generated file\n", " with open(output_filename, 'r', encoding='utf-8') as f:\n", " html_content = f.read()\n", " \n", " print(f\"Interactive conceptual map HTML generated successfully.\")\n", " # The Gradio gr.HTML component can directly render this string\n", " return {\"image_path\": html_content}\n", " \n", " except Exception as e:\n", " print(f\"Error generating graph image with pyvis: {e}\")\n", " return {\"image_path\": f\"An error occurred while generating the graph: {e}
\"}\n" ] }, { "cell_type": "code", "execution_count": 13, "id": "a3b535f4", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "class KnowledgeMapperAgent: initialized with API: 1 and Model: 3\n" ] } ], "source": [ "agent = KnowledgeMapperAgent(1, 2, 3)" ] }, { "cell_type": "code", "execution_count": 14, "id": "f465073a", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "---NODE: LLM DECIDING PATH---\n", "Decide Path Input (first 50 chars): 'search for the most impactful event at 2025...'\n", "LLM Raw Router Response: 'web_search'\n", "LLM Processed Router Choice: 'web_search' (length: 10)\n", "Decision: 'web_search'. Setting chosen_path to 'perform_web_search'.\n", "---ROUTING: Based on chosen_path 'perform_web_search'---\n", "---NODE: PERFORMING WEB SEARCH---\n", "[{'title': '15 Events to Watch in 2025 | Opus Agency', 'url': 'https://www.opusagency.com/article/15-events-to-watch-in-2025', 'content': 'For a truly exclusive experience, consider [WSJ Tech Live](https://techlive.wsj.com/) in Laguna Beach. Slated for October 2025, this invite-only event brings together a select group of tech visionaries, disruptors, and industry leaders to discuss the impact of technology on business regulation and innovation. And they don’t just talk about it; they offer amazing offsite excursions like golfing, kayaking, and even a beach book camp, creating a more relaxed and informal networking and [...] ### **Money 20/20 — Las Vegas, October 26-29, 2025**\\n\\nNext, we’re headed to the glitz and glamor of Las Vegas for Money 20/20, “where money does business.” This premier event is the world’s biggest and most influential gathering of the global money ecosystem, including banks, payments, tech, startups, retail, fintech, financial services, policy, and more. [...] 1. **Blurred Lines:**Many events emphasize experience design across various sectors and formats, merging business discussions with highly creative, entertainment-driven formats.\\n2. **Tech-Driven, Human-Centered**: Many events focus on the impact of technology on society, tackling topics such as AI, metaverse, and tech-driven social transformation. There is a clear emphasis on new tech applications and the ethical, human-centered implications of these advancements.', 'score': 0.7077487}, {'title': 'Biggest Events of 2025 so Far IMO (US POV) : r/decadeology - Reddit', 'url': 'https://www.reddit.com/r/decadeology/comments/1jvj9ks/biggest_events_of_2025_so_far_imo_us_pov/', 'content': 'Myanmar Earthquake, thousands of people dead, during a civil war where there are still air strikes during time of crisis.\\n\\nTrump administration fired the US aid workers while they were over there assessing how to help with earthquake relief.\\n\\n9\\n\\nNew to Reddit?Create your account and connect with a world of communities.\\n\\nContinue with GoogleContinue with Google\\n\\nContinue with Email\\n\\nContinue With Phone Number [...] It’s crazy how that event is basically forgotten about\\n\\n34\\n\\n 1 reply\\n\\n1 reply[Continue this thread](https://www.reddit.com/r/decadeology/comments/1jvj9ks/comment/mmaqh4q/)\\n\\n[](https://www.reddit.com/user/statebirdsnest/)\\n\\n[statebirdsnest](https://www.reddit.com/user/statebirdsnest/)\\n\\n• [1mo ago](https://www.reddit.com/r/decadeology/comments/1jvj9ks/comment/mmas4k3/)\\n\\nThe DC plane crash and Philadelphia plane crash.\\n\\nSignal Chat Leak\\n\\nDOGE and Treasury overtake (+other federal agencies)\\n\\n43 [...] * [Amazing](https://reddit.com/t/amazing/)* [Animals & Pets](https://reddit.com/t/animals_and_pets/)* [Cringe & Facepalm](https://reddit.com/t/cringe_and_facepalm/)* [Funny](https://reddit.com/t/funny/)* [Interesting](https://reddit.com/t/interesting/)* [Memes](https://reddit.com/t/memes/)* [Oddly Satisfying](https://reddit.com/t/oddly_satisfying/)* [Reddit Meta](https://reddit.com/t/reddit_meta/)* [Wholesome & Heartwarming](https://reddit.com/t/wholesome_and_heartwarming/)', 'score': 0.6222074}]\n", "---NODE: LLM EXTRACTION ---\n", "Raw LLM response: \n", "'```json\n", "[\n", " {\n", " \"concept\": \"WSJ Tech Live\",\n", " \"importance\": 7\n", " },\n", " {\n", " \"concept\": \"Money 20/20\",\n", " \"importance\": 8\n", " },\n", " {\n", " \"concept\": \"Technology Impact on Business\",\n", " \"importance\": 9\n", " },\n", " {\n", " \"concept\": \"Fintech\",\n", " \"importance\": 7\n", " },\n", " {\n", " \"concept\": \"Networking\",\n", " \"importance\": 6\n", " },\n", " {\n", " \"concept\": \"Experience Design\",\n", " \"importance\": 6\n", " },\n", " {\n", " \"concept\": \"AI\",\n", " \"importance\": 8\n", " },\n", " {\n", " \"concept\": \"Metaverse\",\n", " \"importance\": 7\n", " },\n", " {\n", " \"concept\": \"Ethical Implications of Technology\",\n", " \"importance\": 8\n", " },\n", " {\n", " \"concept\": \"Myanmar Earthquake\",\n", " \"importance\": 9\n", " },\n", " {\n", " \"concept\": \"Civil War\",\n", " \"importance\": 8\n", " },\n", " {\n", " \"concept\": \"US Aid Workers Fired\",\n", " \"importance\": 7\n", " },\n", " {\n", " \"concept\": \"Plane Crashes\",\n", " \"importance\": 6\n", " },\n", " {\n", " \"concept\": \"Signal Chat Leak\",\n", " \"importance\": 5\n", " },\n", " {\n", " \"concept\": \"DOGE and Treasury\",\n", " \"importance\": 5\n", " }\n", "]\n", "```'\n", "Extracted JSON string: \n", "'[\n", " {\n", " \"concept\": \"WSJ Tech Live\",\n", " \"importance\": 7\n", " },\n", " {\n", " \"concept\": \"Money 20/20\",\n", " \"importance\": 8\n", " },\n", " {\n", " \"concept\": \"Technology Impact on Business\",\n", " \"importance\": 9\n", " },\n", " {\n", " \"concept\": \"Fintech\",\n", " \"importance\": 7\n", " },\n", " {\n", " \"concept\": \"Networking\",\n", " \"importance\": 6\n", " },\n", " {\n", " \"concept\": \"Experience Design\",\n", " \"importance\": 6\n", " },\n", " {\n", " \"concept\": \"AI\",\n", " \"importance\": 8\n", " },\n", " {\n", " \"concept\": \"Metaverse\",\n", " \"importance\": 7\n", " },\n", " {\n", " \"concept\": \"Ethical Implications of Technology\",\n", " \"importance\": 8\n", " },\n", " {\n", " \"concept\": \"Myanmar Earthquake\",\n", " \"importance\": 9\n", " },\n", " {\n", " \"concept\": \"Civil War\",\n", " \"importance\": 8\n", " },\n", " {\n", " \"concept\": \"US Aid Workers Fired\",\n", " \"importance\": 7\n", " },\n", " {\n", " \"concept\": \"Plane Crashes\",\n", " \"importance\": 6\n", " },\n", " {\n", " \"concept\": \"Signal Chat Leak\",\n", " \"importance\": 5\n", " },\n", " {\n", " \"concept\": \"DOGE and Treasury\",\n", " \"importance\": 5\n", " }\n", "]'\n", "---NODE: LLM CONNECTING KEYWORDS ---\n", "Raw LLM response (connections): \n", "'```json\n", "[\n", " {\"source\": \"WSJ Tech Live\", \"relation\": \"discusses\", \"target\": \"Technology Impact on Business\"},\n", " {\"source\": \"WSJ Tech Live\", \"relation\": \"facilitates\", \"target\": \"Networking\"},\n", " {\"source\": \"Money 20/20\", \"relation\": \"is related to\", \"target\": \"Fintech\"},\n", " {\"source\": \"Money 20/20\", \"relation\": \"includes\", \"target\": \"Financial Services\"},\n", " {\"source\": \"Experience Design\", \"relation\": \"is emphasized at\", \"target\": \"Many events\"},\n", " {\"source\": \"AI\", \"relation\": \"is a topic of\", \"target\": \"Many events\"},\n", " {\"source\": \"Metaverse\", \"relation\": \"is a topic of\", \"target\": \"Many events\"},\n", " {\"source\": \"Ethical Implications of Technology\", \"relation\": \"is a focus of\", \"target\": \"Many events\"},\n", " {\"source\": \"Myanmar Earthquake\", \"relation\": \"occurred during\", \"target\": \"Civil War\"},\n", " {\"source\": \"US Aid Workers Fired\", \"relation\": \"happened during\", \"target\": \"Myanmar Earthquake\"}\n", "]\n", "```'\n", "Extracted JSON string (connections): \n", "'[\n", " {\"source\": \"WSJ Tech Live\", \"relation\": \"discusses\", \"target\": \"Technology Impact on Business\"},\n", " {\"source\": \"WSJ Tech Live\", \"relation\": \"facilitates\", \"target\": \"Networking\"},\n", " {\"source\": \"Money 20/20\", \"relation\": \"is related to\", \"target\": \"Fintech\"},\n", " {\"source\": \"Money 20/20\", \"relation\": \"includes\", \"target\": \"Financial Services\"},\n", " {\"source\": \"Experience Design\", \"relation\": \"is emphasized at\", \"target\": \"Many events\"},\n", " {\"source\": \"AI\", \"relation\": \"is a topic of\", \"target\": \"Many events\"},\n", " {\"source\": \"Metaverse\", \"relation\": \"is a topic of\", \"target\": \"Many events\"},\n", " {\"source\": \"Ethical Implications of Technology\", \"relation\": \"is a focus of\", \"target\": \"Many events\"},\n", " {\"source\": \"Myanmar Earthquake\", \"relation\": \"occurred during\", \"target\": \"Civil War\"},\n", " {\"source\": \"US Aid Workers Fired\", \"relation\": \"happened during\", \"target\": \"Myanmar Earthquake\"}\n", "]'\n", "---NODE: GENERATING INTERACTIVE GRAPH (pyvis) ---\n", "Interactive conceptual map HTML generated successfully.\n", "---NODE: SUMMARIZING MAP---\n", "Generated Summary: \n", "'The search focuses on identifying the most impactful event of 2025. This involves considering both technology-focused conferences and significant world events. On the technology side, events like WSJ Tech Live, which emphasizes the impact of technology on business and facilitates networking, and Money 20/20, centered around Fintech, are key contenders. These events often feature discussions on emerging technologies like AI and the Metaverse, as well as the ethical implications of technology and experience design. However, the search also considers impactful real-world events, such as the Myanmar Earthquake, which occurred during a Civil War and was further complicated by the firing of US Aid Workers. The goal is to weigh the impact of these technological and geopolitical events to determine which had the most significant consequences in 2025.'\n" ] } ], "source": [ "html = agent(\"search for the most impactful event at 2025\")" ] }, { "cell_type": "code", "execution_count": 15, "id": "00c4e8e3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'original_input': 'search for the most impactful event at 2025', 'text_to_map': 'For a truly exclusive experience, consider [WSJ Tech Live](https://techlive.wsj.com/) in Laguna Beach. Slated for October 2025, this invite-only event brings together a select group of tech visionaries, disruptors, and industry leaders to discuss the impact of technology on business regulation and innovation. And they don’t just talk about it; they offer amazing offsite excursions like golfing, kayaking, and even a beach book camp, creating a more relaxed and informal networking and [...] ### **Money 20/20 — Las Vegas, October 26-29, 2025**\\n\\nNext, we’re headed to the glitz and glamor of Las Vegas for Money 20/20, “where money does business.” This premier event is the world’s biggest and most influential gathering of the global money ecosystem, including banks, payments, tech, startups, retail, fintech, financial services, policy, and more. [...] 1. **Blurred Lines:**Many events emphasize experience design across various sectors and formats, merging business discussions with highly creative, entertainment-driven formats.\\n2. **Tech-Driven, Human-Centered**: Many events focus on the impact of technology on society, tackling topics such as AI, metaverse, and tech-driven social transformation. There is a clear emphasis on new tech applications and the ethical, human-centered implications of these advancements.\\n\\nMyanmar Earthquake, thousands of people dead, during a civil war where there are still air strikes during time of crisis.\\n\\nTrump administration fired the US aid workers while they were over there assessing how to help with earthquake relief.\\n\\n9\\n\\nNew to Reddit?Create your account and connect with a world of communities.\\n\\nContinue with GoogleContinue with Google\\n\\nContinue with Email\\n\\nContinue With Phone Number [...] It’s crazy how that event is basically forgotten about\\n\\n34\\n\\n 1 reply\\n\\n1 reply[Continue this thread](https://www.reddit.com/r/decadeology/comments/1jvj9ks/comment/mmaqh4q/)\\n\\n[](https://www.reddit.com/user/statebirdsnest/)\\n\\n[statebirdsnest](https://www.reddit.com/user/statebirdsnest/)\\n\\n• [1mo ago](https://www.reddit.com/r/decadeology/comments/1jvj9ks/comment/mmas4k3/)\\n\\nThe DC plane crash and Philadelphia plane crash.\\n\\nSignal Chat Leak\\n\\nDOGE and Treasury overtake (+other federal agencies)\\n\\n43 [...] * [Amazing](https://reddit.com/t/amazing/)* [Animals & Pets](https://reddit.com/t/animals_and_pets/)* [Cringe & Facepalm](https://reddit.com/t/cringe_and_facepalm/)* [Funny](https://reddit.com/t/funny/)* [Interesting](https://reddit.com/t/interesting/)* [Memes](https://reddit.com/t/memes/)* [Oddly Satisfying](https://reddit.com/t/oddly_satisfying/)* [Reddit Meta](https://reddit.com/t/reddit_meta/)* [Wholesome & Heartwarming](https://reddit.com/t/wholesome_and_heartwarming/)', 'keywords': [{'concept': 'WSJ Tech Live', 'importance': 7}, {'concept': 'Money 20/20', 'importance': 8}, {'concept': 'Technology Impact on Business', 'importance': 9}, {'concept': 'Fintech', 'importance': 7}, {'concept': 'Networking', 'importance': 6}, {'concept': 'Experience Design', 'importance': 6}, {'concept': 'AI', 'importance': 8}, {'concept': 'Metaverse', 'importance': 7}, {'concept': 'Ethical Implications of Technology', 'importance': 8}, {'concept': 'Myanmar Earthquake', 'importance': 9}, {'concept': 'Civil War', 'importance': 8}, {'concept': 'US Aid Workers Fired', 'importance': 7}, {'concept': 'Plane Crashes', 'importance': 6}, {'concept': 'Signal Chat Leak', 'importance': 5}, {'concept': 'DOGE and Treasury', 'importance': 5}], 'connections': [{'source': 'WSJ Tech Live', 'relation': 'discusses', 'target': 'Technology Impact on Business'}, {'source': 'WSJ Tech Live', 'relation': 'facilitates', 'target': 'Networking'}, {'source': 'Money 20/20', 'relation': 'is related to', 'target': 'Fintech'}, {'source': 'Money 20/20', 'relation': 'includes', 'target': 'Financial Services'}, {'source': 'Experience Design', 'relation': 'is emphasized at', 'target': 'Many events'}, {'source': 'AI', 'relation': 'is a topic of', 'target': 'Many events'}, {'source': 'Metaverse', 'relation': 'is a topic of', 'target': 'Many events'}, {'source': 'Ethical Implications of Technology', 'relation': 'is a focus of', 'target': 'Many events'}, {'source': 'Myanmar Earthquake', 'relation': 'occurred during', 'target': 'Civil War'}, {'source': 'US Aid Workers Fired', 'relation': 'happened during', 'target': 'Myanmar Earthquake'}], 'summary': 'The search focuses on identifying the most impactful event of 2025. This involves considering both technology-focused conferences and significant world events. On the technology side, events like WSJ Tech Live, which emphasizes the impact of technology on business and facilitates networking, and Money 20/20, centered around Fintech, are key contenders. These events often feature discussions on emerging technologies like AI and the Metaverse, as well as the ethical implications of technology and experience design. However, the search also considers impactful real-world events, such as the Myanmar Earthquake, which occurred during a Civil War and was further complicated by the firing of US Aid Workers. The goal is to weigh the impact of these technological and geopolitical events to determine which had the most significant consequences in 2025.', 'image_path': '\\n \\n \\n \\n \\n \\n \\n \\n \\n