diff --git a/docs/_scripts/copy_notebooks.py b/docs/_scripts/copy_notebooks.py index 4b9b42a34..d925f5412 100644 --- a/docs/_scripts/copy_notebooks.py +++ b/docs/_scripts/copy_notebooks.py @@ -33,6 +33,7 @@ _MANUAL = { "tutorials": [ "introduction.ipynb", "customer-support/customer-support.ipynb", + "tutorials/tnt-llm/tnt-llm.ipynb", ], } _MANUAL_INVERSE = {v: docs_dir / k for k, vs in _MANUAL.items() for v in vs} @@ -113,6 +114,9 @@ def copy_notebooks(): dst_path = os.path.join( overridden_dir, os.path.relpath(src_path, examples_dir) ) + dst_path = dst_path.replace( + "tutorials/tutorials", "tutorials" + ).replace("how-tos/how-tos", "how-tos") print(f"Overriding: {src_path} to {dst_path}") break diff --git a/docs/docs/tutorials/index.md b/docs/docs/tutorials/index.md index 7682346e6..937575315 100644 --- a/docs/docs/tutorials/index.md +++ b/docs/docs/tutorials/index.md @@ -55,6 +55,10 @@ Learn from example implementations of graphs designed for specific scenarios and - [Agent-based](chatbot-simulation-evaluation/agent-simulation-evaluation.ipynb): Evaluating chatbots via simulated user interactions - [Within LangSmith](chatbot-simulation-evaluation/langsmith-agent-simulation-evaluation.ipynb): Evaluating chatbots in LangSmith over a dialog dataset +#### Text Mining + +- [TNT-LLM](tnt-llm/tnt-llm.ipynb): learn to build rich, interpretable taxonomies of user intentand using the classification system developed by Microsoft for their Bing Copilot application. + #### Competitive Programming - [Can Language Models Solve Olympiad Programming?](usaco/usaco.ipynb): Build an agent with few-shot "episodic memory" and human-in-the-loop collaboration to solve problems from the USA Computing Olympiad; adapted from the [paper of the same name](https://arxiv.org/abs/2404.10952v1) by Shi, Tang, Narasimhan, and Yao. diff --git a/docs/mkdocs.yml b/docs/mkdocs.yml index e8870acbd..03b15f54b 100644 --- a/docs/mkdocs.yml +++ b/docs/mkdocs.yml @@ -129,6 +129,8 @@ nav: - Chatbot Eval via Sim: - Agent-based: tutorials/chatbot-simulation-evaluation/agent-simulation-evaluation.ipynb - In LangSmith: tutorials/chatbot-simulation-evaluation/langsmith-agent-simulation-evaluation.ipynb + - Text Mining: + - TNT-LLM: tutorials/tnt-llm/tnt-llm.ipynb - Web Navigation: tutorials/web-navigation/web_voyager.ipynb - Competitive Programming: tutorials/usaco/usaco.ipynb diff --git a/examples/agent_executor/base.ipynb b/examples/agent_executor/base.ipynb index 561c7b4ce..460e9d5f7 100644 --- a/examples/agent_executor/base.ipynb +++ b/examples/agent_executor/base.ipynb @@ -45,8 +45,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -90,8 +90,8 @@ "source": [ "from langchain import hub\n", "from langchain.agents import create_openai_functions_agent\n", - "from langchain_openai.chat_models import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "from langchain_openai.chat_models import ChatOpenAI\n", "\n", "tools = [TavilySearchResults(max_results=1)]\n", "\n", @@ -127,10 +127,11 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, List, Union\n", + "import operator\n", + "from typing import Annotated, TypedDict, Union\n", + "\n", "from langchain_core.agents import AgentAction, AgentFinish\n", "from langchain_core.messages import BaseMessage\n", - "import operator\n", "\n", "\n", "class AgentState(TypedDict):\n", @@ -182,6 +183,7 @@ "outputs": [], "source": [ "from langchain_core.agents import AgentFinish\n", + "\n", "from langgraph.prebuilt.tool_executor import ToolExecutor\n", "\n", "# This a helper class we have that is useful for running tools\n", diff --git a/examples/agent_executor/force-calling-a-tool-first.ipynb b/examples/agent_executor/force-calling-a-tool-first.ipynb index 93994e9f4..264d27810 100644 --- a/examples/agent_executor/force-calling-a-tool-first.ipynb +++ b/examples/agent_executor/force-calling-a-tool-first.ipynb @@ -50,8 +50,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -95,8 +95,8 @@ "source": [ "from langchain import hub\n", "from langchain.agents import create_openai_functions_agent\n", - "from langchain_openai.chat_models import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "from langchain_openai.chat_models import ChatOpenAI\n", "\n", "tools = [TavilySearchResults(max_results=1)]\n", "\n", @@ -132,10 +132,11 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, List, Union\n", + "import operator\n", + "from typing import Annotated, TypedDict, Union\n", + "\n", "from langchain_core.agents import AgentAction, AgentFinish\n", "from langchain_core.messages import BaseMessage\n", - "import operator\n", "\n", "\n", "class AgentState(TypedDict):\n", @@ -187,6 +188,7 @@ "outputs": [], "source": [ "from langchain_core.agents import AgentFinish\n", + "\n", "from langgraph.prebuilt.tool_executor import ToolExecutor\n", "\n", "# This a helper class we have that is useful for running tools\n", diff --git a/examples/agent_executor/human-in-the-loop.ipynb b/examples/agent_executor/human-in-the-loop.ipynb index d89c5257a..630859f61 100644 --- a/examples/agent_executor/human-in-the-loop.ipynb +++ b/examples/agent_executor/human-in-the-loop.ipynb @@ -50,8 +50,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -95,8 +95,8 @@ "source": [ "from langchain import hub\n", "from langchain.agents import create_openai_functions_agent\n", - "from langchain_openai.chat_models import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "from langchain_openai.chat_models import ChatOpenAI\n", "\n", "tools = [TavilySearchResults(max_results=1)]\n", "\n", @@ -132,10 +132,11 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, List, Union\n", + "import operator\n", + "from typing import Annotated, TypedDict, Union\n", + "\n", "from langchain_core.agents import AgentAction, AgentFinish\n", "from langchain_core.messages import BaseMessage\n", - "import operator\n", "\n", "\n", "class AgentState(TypedDict):\n", @@ -187,6 +188,7 @@ "outputs": [], "source": [ "from langchain_core.agents import AgentFinish\n", + "\n", "from langgraph.prebuilt.tool_executor import ToolExecutor\n", "\n", "# This a helper class we have that is useful for running tools\n", diff --git a/examples/agent_executor/managing-agent-steps.ipynb b/examples/agent_executor/managing-agent-steps.ipynb index c4cac640b..014ad747e 100644 --- a/examples/agent_executor/managing-agent-steps.ipynb +++ b/examples/agent_executor/managing-agent-steps.ipynb @@ -50,8 +50,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -95,8 +95,8 @@ "source": [ "from langchain import hub\n", "from langchain.agents import create_openai_functions_agent\n", - "from langchain_openai.chat_models import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "from langchain_openai.chat_models import ChatOpenAI\n", "\n", "tools = [TavilySearchResults(max_results=1)]\n", "\n", @@ -132,10 +132,11 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, List, Union\n", + "import operator\n", + "from typing import Annotated, TypedDict, Union\n", + "\n", "from langchain_core.agents import AgentAction, AgentFinish\n", "from langchain_core.messages import BaseMessage\n", - "import operator\n", "\n", "\n", "class AgentState(TypedDict):\n", @@ -187,6 +188,7 @@ "outputs": [], "source": [ "from langchain_core.agents import AgentFinish\n", + "\n", "from langgraph.prebuilt.tool_executor import ToolExecutor\n", "\n", "# This a helper class we have that is useful for running tools\n", diff --git a/examples/async.ipynb b/examples/async.ipynb index 147ffdb68..6dea1383e 100644 --- a/examples/async.ipynb +++ b/examples/async.ipynb @@ -57,8 +57,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -113,8 +113,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "# Add messages essentially does this with more\n", @@ -306,7 +308,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(State)\n", diff --git a/examples/branching.ipynb b/examples/branching.ipynb index da29a87e9..6b5a85256 100644 --- a/examples/branching.ipynb +++ b/examples/branching.ipynb @@ -40,10 +40,12 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph\n", - "from typing_extensions import TypedDict\n", - "from typing import Annotated\n", "import operator\n", + "from typing import Annotated, Any\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", + "from langgraph.graph import StateGraph\n", "\n", "\n", "class State(TypedDict):\n", @@ -51,9 +53,6 @@ " aggregate: Annotated[list, operator.add]\n", "\n", "\n", - "from typing import Any\n", - "\n", - "\n", "class ReturnNodeValue:\n", " def __init__(self, node_secret: str):\n", " self._value = node_secret\n", @@ -164,10 +163,12 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph\n", - "from typing_extensions import TypedDict\n", - "from typing import Annotated\n", "import operator\n", + "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", + "from langgraph.graph import StateGraph\n", "\n", "\n", "class State(TypedDict):\n", @@ -264,11 +265,12 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Sequence\n", - "from langgraph.graph import StateGraph, END, START\n", - "from typing_extensions import TypedDict\n", - "from typing import Annotated\n", "import operator\n", + "from typing import Annotated, Sequence\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", + "from langgraph.graph import END, START, StateGraph\n", "\n", "\n", "class State(TypedDict):\n", @@ -412,11 +414,12 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Sequence\n", - "from langgraph.graph import StateGraph\n", - "from typing_extensions import TypedDict\n", - "from typing import Annotated\n", "import operator\n", + "from typing import Annotated, Sequence\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", + "from langgraph.graph import StateGraph\n", "\n", "\n", "def reduce_fanouts(left, right):\n", diff --git a/examples/chat_agent_executor_with_function_calling/anthropic.ipynb b/examples/chat_agent_executor_with_function_calling/anthropic.ipynb index f09940c3f..ec7e1c0e9 100644 --- a/examples/chat_agent_executor_with_function_calling/anthropic.ipynb +++ b/examples/chat_agent_executor_with_function_calling/anthropic.ipynb @@ -47,8 +47,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -165,8 +165,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -252,7 +253,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", diff --git a/examples/chat_agent_executor_with_function_calling/base.ipynb b/examples/chat_agent_executor_with_function_calling/base.ipynb index f066030bd..658dbc14f 100644 --- a/examples/chat_agent_executor_with_function_calling/base.ipynb +++ b/examples/chat_agent_executor_with_function_calling/base.ipynb @@ -46,8 +46,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -193,8 +193,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -236,9 +237,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", "from langchain_core.messages import ToolMessage\n", "\n", + "from langgraph.prebuilt import ToolInvocation\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state):\n", @@ -299,7 +301,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", @@ -365,7 +367,7 @@ "\n", "try:\n", " display(Image(app.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/chat_agent_executor_with_function_calling/dynamically-returning-directly.ipynb b/examples/chat_agent_executor_with_function_calling/dynamically-returning-directly.ipynb index 172aecd75..7377fe85d 100644 --- a/examples/chat_agent_executor_with_function_calling/dynamically-returning-directly.ipynb +++ b/examples/chat_agent_executor_with_function_calling/dynamically-returning-directly.ipynb @@ -58,8 +58,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -230,8 +230,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -273,8 +274,9 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", - "from langchain_core.messages import ToolMessage" + "from langchain_core.messages import ToolMessage\n", + "\n", + "from langgraph.prebuilt import ToolInvocation" ] }, { @@ -390,7 +392,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", @@ -460,7 +462,7 @@ "\n", "try:\n", " display(Image(app.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/chat_agent_executor_with_function_calling/force-calling-a-tool-first.ipynb b/examples/chat_agent_executor_with_function_calling/force-calling-a-tool-first.ipynb index aa0490453..5ba9e1901 100644 --- a/examples/chat_agent_executor_with_function_calling/force-calling-a-tool-first.ipynb +++ b/examples/chat_agent_executor_with_function_calling/force-calling-a-tool-first.ipynb @@ -50,8 +50,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -208,8 +208,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -251,9 +252,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", "from langchain_core.messages import ToolMessage\n", "\n", + "from langgraph.prebuilt import ToolInvocation\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state: AgentState):\n", @@ -375,7 +377,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", diff --git a/examples/chat_agent_executor_with_function_calling/high-level-tools.ipynb b/examples/chat_agent_executor_with_function_calling/high-level-tools.ipynb index d97a49c4d..bf1f9c029 100644 --- a/examples/chat_agent_executor_with_function_calling/high-level-tools.ipynb +++ b/examples/chat_agent_executor_with_function_calling/high-level-tools.ipynb @@ -30,10 +30,11 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_openai import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langgraph.prebuilt import create_react_agent\n", - "from langchain_core.messages import HumanMessage" + "from langchain_core.messages import HumanMessage\n", + "from langchain_openai import ChatOpenAI\n", + "\n", + "from langgraph.prebuilt import create_react_agent" ] }, { diff --git a/examples/chat_agent_executor_with_function_calling/human-in-the-loop.ipynb b/examples/chat_agent_executor_with_function_calling/human-in-the-loop.ipynb index 72c91c494..11aa66b8c 100644 --- a/examples/chat_agent_executor_with_function_calling/human-in-the-loop.ipynb +++ b/examples/chat_agent_executor_with_function_calling/human-in-the-loop.ipynb @@ -59,8 +59,8 @@ } ], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -214,8 +214,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -257,9 +258,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", "from langchain_core.messages import ToolMessage\n", "\n", + "from langgraph.prebuilt import ToolInvocation\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state):\n", @@ -344,8 +346,8 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", "from langgraph.checkpoint.memory import MemorySaver\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", @@ -411,7 +413,7 @@ "\n", "try:\n", " display(Image(app.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/chat_agent_executor_with_function_calling/managing-agent-steps.ipynb b/examples/chat_agent_executor_with_function_calling/managing-agent-steps.ipynb index d698e4225..26b391a29 100644 --- a/examples/chat_agent_executor_with_function_calling/managing-agent-steps.ipynb +++ b/examples/chat_agent_executor_with_function_calling/managing-agent-steps.ipynb @@ -50,8 +50,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -197,8 +197,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -240,9 +241,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", "from langchain_core.messages import ToolMessage\n", "\n", + "from langgraph.prebuilt import ToolInvocation\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state):\n", @@ -340,7 +342,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", @@ -406,7 +408,7 @@ "\n", "try:\n", " display(Image(app.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/chat_agent_executor_with_function_calling/prebuilt-tool-node.ipynb b/examples/chat_agent_executor_with_function_calling/prebuilt-tool-node.ipynb index 2d629af4b..a232adcbe 100644 --- a/examples/chat_agent_executor_with_function_calling/prebuilt-tool-node.ipynb +++ b/examples/chat_agent_executor_with_function_calling/prebuilt-tool-node.ipynb @@ -47,8 +47,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -174,8 +174,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -261,7 +262,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", diff --git a/examples/chat_agent_executor_with_function_calling/respond-in-format.ipynb b/examples/chat_agent_executor_with_function_calling/respond-in-format.ipynb index b77000f66..dbfdb7586 100644 --- a/examples/chat_agent_executor_with_function_calling/respond-in-format.ipynb +++ b/examples/chat_agent_executor_with_function_calling/respond-in-format.ipynb @@ -39,8 +39,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "# Add messages essentially does this with more\n", @@ -190,8 +192,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -352,8 +354,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -399,10 +402,12 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", - "from langchain_core.messages import ToolMessage\n", "from typing import Literal\n", "\n", + "from langchain_core.messages import ToolMessage\n", + "\n", + "from langgraph.prebuilt import ToolInvocation\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state) -> Literal[\"continue\", \"end\"]:\n", @@ -478,7 +483,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", @@ -544,7 +549,7 @@ "\n", "try:\n", " display(Image(app.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/chatbot-simulation-evaluation/agent-simulation-evaluation.ipynb b/examples/chatbot-simulation-evaluation/agent-simulation-evaluation.ipynb index cbb1d9433..044572c7b 100644 --- a/examples/chatbot-simulation-evaluation/agent-simulation-evaluation.ipynb +++ b/examples/chatbot-simulation-evaluation/agent-simulation-evaluation.ipynb @@ -40,7 +40,6 @@ "source": [ "import getpass\n", "import os\n", - "import uuid\n", "\n", "\n", "def _set_if_undefined(var: str):\n", @@ -138,7 +137,6 @@ "outputs": [], "source": [ "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", - "from langchain_core.runnables import chain\n", "from langchain_openai import ChatOpenAI\n", "\n", "system_prompt_template = \"\"\"You are a customer of an airline company. \\\n", @@ -185,7 +183,6 @@ "source": [ "from langchain_core.messages import HumanMessage\n", "\n", - "\n", "messages = [HumanMessage(content=\"Hi! How can I help you?\")]\n", "simulated_user.invoke({\"messages\": messages})" ] @@ -227,8 +224,8 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.messages import AIMessage\n", "from langchain_community.adapters.openai import convert_message_to_dict\n", + "from langchain_core.messages import AIMessage\n", "\n", "\n", "def chat_bot_node(messages):\n", @@ -324,7 +321,6 @@ "source": [ "from langgraph.graph import END, MessageGraph\n", "\n", - "\n", "graph_builder = MessageGraph()\n", "graph_builder.add_node(\"user\", simulated_user_node)\n", "graph_builder.add_node(\"chat_bot\", chat_bot_node)\n", diff --git a/examples/chatbots/information-gather-prompting.ipynb b/examples/chatbots/information-gather-prompting.ipynb index 3986886ef..7255b72f1 100644 --- a/examples/chatbots/information-gather-prompting.ipynb +++ b/examples/chatbots/information-gather-prompting.ipynb @@ -33,10 +33,11 @@ "metadata": {}, "outputs": [], "source": [ + "from typing import List\n", + "\n", "from langchain_core.messages import SystemMessage\n", - "from langchain_openai import ChatOpenAI\n", "from langchain_core.pydantic_v1 import BaseModel\n", - "from typing import List" + "from langchain_openai import ChatOpenAI" ] }, { @@ -97,7 +98,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.messages import HumanMessage, AIMessage, ToolMessage\n", + "from langchain_core.messages import AIMessage, HumanMessage, ToolMessage\n", "\n", "# New system prompt\n", "prompt_system = \"\"\"Based on the following requirements, write a good prompt template:\n", @@ -145,6 +146,7 @@ "outputs": [], "source": [ "from typing import Literal\n", + "\n", "from langgraph.graph import END\n", "\n", "\n", @@ -174,8 +176,8 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import MessageGraph, START\n", "from langgraph.checkpoint.sqlite import SqliteSaver\n", + "from langgraph.graph import START, MessageGraph\n", "\n", "memory = SqliteSaver.from_conn_string(\":memory:\")\n", "workflow = MessageGraph()\n", @@ -215,7 +217,7 @@ } ], "source": [ - "from IPython.display import display, Image\n", + "from IPython.display import Image, display\n", "\n", "display(Image(graph.get_graph().draw_mermaid_png()))" ] @@ -296,7 +298,6 @@ "source": [ "import uuid\n", "\n", - "\n", "config = {\"configurable\": {\"thread_id\": str(uuid.uuid4())}}\n", "while True:\n", " user = input(\"User (q/Q to quit): \")\n", diff --git a/examples/code_assistant/langgraph_code_assistant.ipynb b/examples/code_assistant/langgraph_code_assistant.ipynb index 909c0e420..8f63d7863 100644 --- a/examples/code_assistant/langgraph_code_assistant.ipynb +++ b/examples/code_assistant/langgraph_code_assistant.ipynb @@ -94,9 +94,9 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_openai import ChatOpenAI\n", "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", + "from langchain_openai import ChatOpenAI\n", "\n", "### OpenAI\n", "\n", @@ -125,6 +125,7 @@ " code: str = Field(description=\"Code block not including import statements\")\n", " description = \"Schema for code solutions to questions about LCEL.\"\n", "\n", + "\n", "expt_llm = \"gpt-4-0125-preview\"\n", "llm = ChatOpenAI(temperature=0, model=expt_llm)\n", "code_gen_chain = code_gen_prompt | llm.with_structured_output(code)\n", @@ -181,6 +182,7 @@ "\n", "structured_llm_claude = llm.with_structured_output(code, include_raw=True)\n", "\n", + "\n", "# Optional: Check for errors in case tool use is flaky\n", "def check_claude_output(tool_output):\n", " \"\"\"Check for parse error or failure to call the tool\"\"\"\n", @@ -189,7 +191,7 @@ " if tool_output[\"parsing_error\"]:\n", " # Report back output and parsing errors\n", " print(\"Parsing error!\")\n", - " raw_output = str(code_output[\"raw\"].content)\n", + " raw_output = str(tool_output[\"raw\"].content)\n", " error = tool_output[\"parsing_error\"]\n", " raise ValueError(\n", " f\"Error parsing your output! Be sure to invoke the tool. Output: {raw_output}. \\n Parse error: {error}\"\n", @@ -199,15 +201,17 @@ " elif not tool_output[\"parsed\"]:\n", " print(\"Failed to invoke tool!\")\n", " raise ValueError(\n", - " f\"You did not use the provided tool! Be sure to invoke the tool to structure the output.\"\n", + " \"You did not use the provided tool! Be sure to invoke the tool to structure the output.\"\n", " )\n", " return tool_output\n", "\n", + "\n", "# Chain with output check\n", "code_chain_claude_raw = (\n", " code_gen_prompt_claude | structured_llm_claude | check_claude_output\n", ")\n", "\n", + "\n", "def insert_errors(inputs):\n", " \"\"\"Insert errors for tool parsing in the messages\"\"\"\n", "\n", @@ -240,6 +244,7 @@ "\n", " return solution[\"parsed\"]\n", "\n", + "\n", "# Optional: With re-try to correct for failure to invoke tool\n", "code_gen_chain = code_gen_chain_re_try | parse_output\n", "\n", @@ -281,7 +286,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Dict, TypedDict, List\n", + "from typing import List, TypedDict\n", "\n", "\n", "class GraphState(TypedDict):\n", @@ -318,10 +323,7 @@ "metadata": {}, "outputs": [], "source": [ - "from operator import itemgetter\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", - "from langchain_core.runnables import RunnablePassthrough\n", - "from langchain_core.prompts import PromptTemplate\n", "\n", "### Parameter\n", "\n", @@ -396,7 +398,6 @@ " iterations = state[\"iterations\"]\n", "\n", " # Get solution components\n", - " prefix = code_solution.prefix\n", " imports = code_solution.imports\n", " code = code_solution.code\n", "\n", @@ -457,14 +458,6 @@ " code_solution = state[\"generation\"]\n", "\n", " # Prompt reflection\n", - " reflection_message = [\n", - " (\n", - " \"user\",\n", - " \"\"\"You tried to solve this problem and failed a unit test. Reflect on this failure\n", - " given the provided documentation. Write a few key suggestions based on the \n", - " documentation to avoid making this mistake again.\"\"\",\n", - " )\n", - " ]\n", "\n", " # Add reflection\n", " reflections = code_gen_chain.invoke(\n", @@ -613,7 +606,7 @@ " try:\n", " exec(imports)\n", " return {\"key\": \"import_check\", \"score\": 1}\n", - " except:\n", + " except Exception:\n", " return {\"key\": \"import_check\", \"score\": 0}\n", "\n", "\n", @@ -623,7 +616,7 @@ " try:\n", " exec(imports + \"\\n\" + code)\n", " return {\"key\": \"code_execution_check\", \"score\": 1}\n", - " except:\n", + " except Exception:\n", " return {\"key\": \"code_execution_check\", \"score\": 0}" ] }, @@ -754,7 +747,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.8" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/examples/code_assistant/langgraph_code_assistant_mistral.ipynb b/examples/code_assistant/langgraph_code_assistant_mistral.ipynb index a2161f76b..0d6d7b586 100644 --- a/examples/code_assistant/langgraph_code_assistant_mistral.ipynb +++ b/examples/code_assistant/langgraph_code_assistant_mistral.ipynb @@ -55,8 +55,9 @@ "outputs": [], "source": [ "import os\n", - "os.environ['TOKENIZERS_PARALLELISM'] = 'true'\n", - "mistral_api_key = os.getenv(\"MISTRAL_API_KEY\") # Ensure this is set" + "\n", + "os.environ[\"TOKENIZERS_PARALLELISM\"] = \"true\"\n", + "mistral_api_key = os.getenv(\"MISTRAL_API_KEY\") # Ensure this is set" ] }, { @@ -76,19 +77,9 @@ "metadata": {}, "outputs": [], "source": [ - "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", - "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", - "os.environ['LANGCHAIN_API_KEY'] = " - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "id": "949efd30-44c7-4a4c-a05f-eca4e2769a61", - "metadata": {}, - "outputs": [], - "source": [ - "import os\n", + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = \"\"\n", "os.environ[\"LANGCHAIN_PROJECT\"] = \"Mistral-code-gen-testing\"" ] }, @@ -110,18 +101,18 @@ "outputs": [], "source": [ "# Select LLM\n", - "from langchain_mistralai import ChatMistralAI\n", "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", + "from langchain_mistralai import ChatMistralAI\n", "\n", "mistral_model = \"mistral-large-latest\"\n", "llm = ChatMistralAI(model=mistral_model, temperature=0)\n", "\n", - "# Prompt \n", + "# Prompt\n", "code_gen_prompt_claude = ChatPromptTemplate.from_messages(\n", " [\n", " (\n", - " \"system\", \n", + " \"system\",\n", " \"\"\"You are a coding assistant. Ensure any code you provide can be executed with all required imports and variables \\n\n", " defined. Structure your answer: 1) a prefix describing the code solution, 2) the imports, 3) the functioning code block.\n", " \\n Here is the user question:\"\"\",\n", @@ -130,6 +121,7 @@ " ]\n", ")\n", "\n", + "\n", "# Data model\n", "class code(BaseModel):\n", " \"\"\"Code output\"\"\"\n", @@ -139,6 +131,7 @@ " code: str = Field(description=\"Code block not including import statements\")\n", " description = \"Schema for code solutions to questions about LCEL.\"\n", "\n", + "\n", "# LLM\n", "code_gen_chain = llm.with_structured_output(code, include_raw=False)" ] @@ -192,10 +185,11 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated\n", - "from typing import Dict, TypedDict, List\n", + "from typing import Annotated, TypedDict\n", + "\n", "from langgraph.graph.message import AnyMessage, add_messages\n", "\n", + "\n", "class GraphState(TypedDict):\n", " \"\"\"\n", " Represents the state of our graph.\n", @@ -228,14 +222,14 @@ "metadata": {}, "outputs": [], "source": [ - "from operator import itemgetter\n", + "import uuid\n", + "\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", - "from langchain_core.runnables import RunnablePassthrough\n", - "from langchain_core.prompts import PromptTemplate\n", "\n", "### Parameters\n", "max_iterations = 3\n", "\n", + "\n", "### Nodes\n", "def generate(state: GraphState):\n", " \"\"\"\n", @@ -253,7 +247,6 @@ " # State\n", " messages = state[\"messages\"]\n", " iterations = state[\"iterations\"]\n", - " error = state[\"error\"]\n", "\n", " # Solution\n", " code_solution = code_gen_chain.invoke(messages)\n", @@ -268,6 +261,7 @@ " iterations = iterations + 1\n", " return {\"generation\": code_solution, \"messages\": messages, \"iterations\": iterations}\n", "\n", + "\n", "def code_check(state: GraphState):\n", " \"\"\"\n", " Check code\n", @@ -287,7 +281,6 @@ " iterations = state[\"iterations\"]\n", "\n", " # Get solution components\n", - " prefix = code_solution.prefix\n", " imports = code_solution.imports\n", " code = code_solution.code\n", "\n", @@ -296,7 +289,12 @@ " exec(imports)\n", " except Exception as e:\n", " print(\"---CODE IMPORT CHECK: FAILED---\")\n", - " error_message = [(\"user\", f\"Your solution failed the import test. Here is the error: {e}. Reflect on this error and your prior attempt to solve the problem. (1) State what you think went wrong with the prior solution and (2) try to solve this problem again. Return the FULL SOLUTION. Use the code tool to structure the output with a prefix, imports, and code block:\")]\n", + " error_message = [\n", + " (\n", + " \"user\",\n", + " f\"Your solution failed the import test. Here is the error: {e}. Reflect on this error and your prior attempt to solve the problem. (1) State what you think went wrong with the prior solution and (2) try to solve this problem again. Return the FULL SOLUTION. Use the code tool to structure the output with a prefix, imports, and code block:\",\n", + " )\n", + " ]\n", " messages += error_message\n", " return {\n", " \"generation\": code_solution,\n", @@ -314,7 +312,12 @@ " exec(combined_code, global_scope)\n", " except Exception as e:\n", " print(\"---CODE BLOCK CHECK: FAILED---\")\n", - " error_message = [(\"user\", f\"Your solution failed the code execution test: {e}) Reflect on this error and your prior attempt to solve the problem. (1) State what you think went wrong with the prior solution and (2) try to solve this problem again. Return the FULL SOLUTION. Use the code tool to structure the output with a prefix, imports, and code block:\")]\n", + " error_message = [\n", + " (\n", + " \"user\",\n", + " f\"Your solution failed the code execution test: {e}) Reflect on this error and your prior attempt to solve the problem. (1) State what you think went wrong with the prior solution and (2) try to solve this problem again. Return the FULL SOLUTION. Use the code tool to structure the output with a prefix, imports, and code block:\",\n", + " )\n", + " ]\n", " messages += error_message\n", " return {\n", " \"generation\": code_solution,\n", @@ -332,8 +335,10 @@ " \"error\": \"no\",\n", " }\n", "\n", + "\n", "### Conditional edges\n", "\n", + "\n", "def decide_to_finish(state: GraphState):\n", " \"\"\"\n", " Determines whether to finish.\n", @@ -354,14 +359,14 @@ " print(\"---DECISION: RE-TRY SOLUTION---\")\n", " return \"generate\"\n", "\n", + "\n", "### Utilities\n", "\n", - "import uuid \n", "\n", "def _print_event(event: dict, _printed: set, max_length=1500):\n", " current_state = event.get(\"dialog_state\")\n", " if current_state:\n", - " print(f\"Currently in: \", current_state[-1])\n", + " print(\"Currently in: \", current_state[-1])\n", " message = event.get(\"messages\")\n", " if message:\n", " if isinstance(message, list):\n", @@ -428,7 +433,7 @@ "\n", "try:\n", " display(Image(graph.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -554,6 +559,7 @@ "outputs": [], "source": [ "import uuid\n", + "\n", "_printed = set()\n", "thread_id = str(uuid.uuid4())\n", "config = {\n", @@ -563,7 +569,7 @@ " }\n", "}\n", "\n", - "question = '''I want to vectorize a function\n", + "question = \"\"\"I want to vectorize a function\n", "\n", " frame = np.zeros((out_h, out_w, 3), dtype=np.uint8)\n", " for i, val1 in enumerate(rows):\n", @@ -574,7 +580,7 @@ "\n", " out.write(np.array(frame))\n", "\n", - "with a simple numpy function that does something like this what is it called. Show me a test case with this working.'''\n", + "with a simple numpy function that does something like this what is it called. Show me a test case with this working.\"\"\"\n", "\n", "events = graph.stream(\n", " {\"messages\": [(\"user\", question)], \"iterations\": 0}, config, stream_mode=\"values\"\n", diff --git a/examples/configuration.ipynb b/examples/configuration.ipynb index f075aee2f..fb1bee442 100644 --- a/examples/configuration.ipynb +++ b/examples/configuration.ipynb @@ -29,12 +29,13 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", - "from langchain_anthropic import ChatAnthropic\n", - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", + "from langchain_anthropic import ChatAnthropic\n", "from langchain_core.messages import BaseMessage, HumanMessage\n", "\n", + "from langgraph.graph import END, StateGraph\n", "\n", "model = ChatAnthropic(model_name=\"claude-2.1\")\n", "\n", diff --git a/examples/customer-support/customer-support.ipynb b/examples/customer-support/customer-support.ipynb index ee7c57086..f3828cc5b 100644 --- a/examples/customer-support/customer-support.ipynb +++ b/examples/customer-support/customer-support.ipynb @@ -903,8 +903,8 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.runnables import RunnableLambda\n", "from langchain_core.messages import ToolMessage\n", + "from langchain_core.runnables import RunnableLambda\n", "\n", "from langgraph.prebuilt import ToolNode\n", "\n", @@ -932,7 +932,7 @@ "def _print_event(event: dict, _printed: set, max_length=1500):\n", " current_state = event.get(\"dialog_state\")\n", " if current_state:\n", - " print(f\"Currently in: \", current_state[-1])\n", + " print(\"Currently in: \", current_state[-1])\n", " message = event.get(\"messages\")\n", " if message:\n", " if isinstance(message, list):\n", @@ -1107,7 +1107,7 @@ "source": [ "from langgraph.checkpoint.sqlite import SqliteSaver\n", "from langgraph.graph import END, StateGraph\n", - "from langgraph.prebuilt import ToolNode, tools_condition\n", + "from langgraph.prebuilt import tools_condition\n", "\n", "builder = StateGraph(State)\n", "\n", @@ -1151,7 +1151,7 @@ "\n", "try:\n", " display(Image(part_1_graph.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -1806,7 +1806,7 @@ "\n", "#### State & Assistant\n", "\n", - "Our graph state and LLM calling is nearly identical to Part 1 except:\n", + "Our graph state and LLM calling is nearly identical to Part 1 except Exception:\n", "\n", "- We've added a `user_info` field that will be eagerly populated by our graph\n", "- We can use the state directly in the `Assistant` object rather than using the configurable params" @@ -1924,8 +1924,8 @@ "outputs": [], "source": [ "from langgraph.checkpoint.sqlite import SqliteSaver\n", - "from langgraph.graph import END, StateGraph\n", - "from langgraph.prebuilt import ToolNode, tools_condition\n", + "from langgraph.graph import StateGraph\n", + "from langgraph.prebuilt import tools_condition\n", "\n", "builder = StateGraph(State)\n", "\n", @@ -1979,7 +1979,7 @@ "\n", "try:\n", " display(Image(part_2_graph.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -2512,7 +2512,7 @@ "from typing import Literal\n", "\n", "from langgraph.checkpoint.sqlite import SqliteSaver\n", - "from langgraph.graph import END, StateGraph\n", + "from langgraph.graph import StateGraph\n", "from langgraph.prebuilt import tools_condition\n", "\n", "builder = StateGraph(State)\n", @@ -2588,7 +2588,7 @@ "\n", "try:\n", " display(Image(part_3_graph.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -3442,7 +3442,7 @@ "from typing import Literal\n", "\n", "from langgraph.checkpoint.sqlite import SqliteSaver\n", - "from langgraph.graph import END, StateGraph\n", + "from langgraph.graph import StateGraph\n", "from langgraph.prebuilt import tools_condition\n", "\n", "builder = StateGraph(State)\n", @@ -3842,7 +3842,7 @@ "\n", "try:\n", " display(Image(part_4_graph.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/docs/quickstart.ipynb b/examples/docs/quickstart.ipynb index b42f5101d..e130c27f4 100644 --- a/examples/docs/quickstart.ipynb +++ b/examples/docs/quickstart.ipynb @@ -23,8 +23,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "if not os.environ.get(\"OPENAI_API_KEY\"):\n", " os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")" @@ -36,8 +36,9 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_openai import ChatOpenAI\n", "from langchain_core.messages import BaseMessage, HumanMessage\n", + "from langchain_openai import ChatOpenAI\n", + "\n", "from langgraph.graph import END, MessageGraph\n", "\n", "model = ChatOpenAI(temperature=0)\n", @@ -73,7 +74,7 @@ "\n", "try:\n", " display(Image(runnable.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -105,11 +106,13 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.tools import tool\n", - "from langgraph.prebuilt import ToolNode\n", - "from langgraph.graph import END, START\n", "from typing import Literal\n", "\n", + "from langchain_core.tools import tool\n", + "\n", + "from langgraph.graph import END, START\n", + "from langgraph.prebuilt import ToolNode\n", + "\n", "\n", "@tool\n", "def multiply(first_number: int, second_number: int):\n", @@ -161,7 +164,7 @@ "source": [ "try:\n", " display(Image(runnable.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/dynamically-returning-directly.ipynb b/examples/dynamically-returning-directly.ipynb index e3b133af7..c7b81c1ab 100644 --- a/examples/dynamically-returning-directly.ipynb +++ b/examples/dynamically-returning-directly.ipynb @@ -48,8 +48,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -227,8 +227,8 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated\n", "import operator\n", + "from typing import Annotated, TypedDict\n", "\n", "\n", "class AgentState(TypedDict):\n", @@ -269,8 +269,9 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", - "from langchain_core.messages import ToolMessage" + "from langchain_core.messages import ToolMessage\n", + "\n", + "from langgraph.prebuilt import ToolInvocation" ] }, { @@ -386,7 +387,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", diff --git a/examples/extraction/retries.ipynb b/examples/extraction/retries.ipynb index 36e4ee280..afff58742 100644 --- a/examples/extraction/retries.ipynb +++ b/examples/extraction/retries.ipynb @@ -420,8 +420,8 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_anthropic import ChatAnthropic\n", + "from langchain_core.prompts import ChatPromptTemplate\n", "\n", "# Or you can use ChatGroq, ChatOpenAI, ChatGoogleGemini, ChatCohere, etc.\n", "# See https://python.langchain.com/v0.1/docs/integrations/chat/ for more info on tool calling\n", @@ -953,7 +953,7 @@ "\n", "try:\n", " display(Image(bound_llm.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " pass" ] }, diff --git a/examples/force-calling-a-tool-first.ipynb b/examples/force-calling-a-tool-first.ipynb index ffc104f8a..b50f09c9e 100644 --- a/examples/force-calling-a-tool-first.ipynb +++ b/examples/force-calling-a-tool-first.ipynb @@ -46,8 +46,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -193,8 +193,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -236,9 +237,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", "from langchain_core.messages import ToolMessage\n", "\n", + "from langgraph.prebuilt import ToolInvocation\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state):\n", @@ -357,7 +359,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", @@ -429,7 +431,7 @@ "\n", "try:\n", " display(Image(app.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/human-in-the-loop.ipynb b/examples/human-in-the-loop.ipynb index a71ae9b80..efe1f5cf1 100644 --- a/examples/human-in-the-loop.ipynb +++ b/examples/human-in-the-loop.ipynb @@ -59,8 +59,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -107,8 +107,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "# `add_messages`` essentially does this\n", @@ -257,9 +259,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", "from langchain_core.messages import ToolMessage\n", "\n", + "from langgraph.prebuilt import ToolInvocation\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state):\n", @@ -320,7 +323,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(State)\n", @@ -611,7 +614,7 @@ " indent=2,\n", " )\n", " return AIMessage(\n", - " content = (\n", + " content=(\n", " \"I plan to invoke the following tools, do you approve?\\n\\n\"\n", " \"Type 'y' if you do, anything else to stop.\\n\\n\"\n", " f\"{serialized_tool_calls}\"\n", diff --git a/examples/introduction.ipynb b/examples/introduction.ipynb index 6caea9cd8..68fe172e5 100644 --- a/examples/introduction.ipynb +++ b/examples/introduction.ipynb @@ -245,7 +245,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -653,7 +653,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -775,7 +775,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Union\n", + "from typing import Annotated\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -873,7 +873,7 @@ } ], "source": [ - "from typing import Annotated, Union\n", + "from typing import Annotated\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -964,7 +964,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -1179,7 +1179,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Union\n", + "from typing import Annotated\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -1253,7 +1253,7 @@ } ], "source": [ - "from typing import Annotated, Union\n", + "from typing import Annotated\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -1487,7 +1487,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Union\n", + "from typing import Annotated\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -1495,7 +1495,7 @@ "from typing_extensions import TypedDict\n", "\n", "from langgraph.checkpoint.sqlite import SqliteSaver\n", - "from langgraph.graph import MessageGraph, StateGraph\n", + "from langgraph.graph import StateGraph\n", "from langgraph.graph.message import add_messages\n", "from langgraph.prebuilt import ToolNode\n", "\n", @@ -1569,7 +1569,7 @@ } ], "source": [ - "from typing import Annotated, Union\n", + "from typing import Annotated\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -1577,7 +1577,7 @@ "from typing_extensions import TypedDict\n", "\n", "from langgraph.checkpoint.sqlite import SqliteSaver\n", - "from langgraph.graph import MessageGraph, StateGraph\n", + "from langgraph.graph import StateGraph\n", "from langgraph.graph.message import add_messages\n", "from langgraph.prebuilt import ToolNode, tools_condition\n", "\n", @@ -1688,7 +1688,7 @@ } ], "source": [ - "from langchain_core.messages import AIMessage, ToolMessage\n", + "from langchain_core.messages import AIMessage\n", "\n", "answer = (\n", " \"LangGraph is a library for building stateful, multi-actor applications with LLMs.\"\n", @@ -1790,7 +1790,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -2068,7 +2068,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Union\n", + "from typing import Annotated\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -2310,7 +2310,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -2517,7 +2517,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Union\n", + "from typing import Annotated\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -2644,7 +2644,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Union, Literal\n", + "from typing import Annotated, Literal\n", "\n", "from langchain_anthropic import ChatAnthropic\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", @@ -2768,7 +2768,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/lats/lats.ipynb b/examples/lats/lats.ipynb index 858761d38..3fd1a8f03 100644 --- a/examples/lats/lats.ipynb +++ b/examples/lats/lats.ipynb @@ -49,6 +49,8 @@ "metadata": {}, "outputs": [], "source": [ + "from __future__ import annotations\n", + "\n", "import getpass\n", "import os\n", "\n", @@ -91,13 +93,11 @@ "metadata": {}, "outputs": [], "source": [ - "from __future__ import annotations\n", - "\n", "import math\n", + "from collections import deque\n", "from typing import Optional\n", "\n", "from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage\n", - "from collections import deque\n", "\n", "\n", "class Node:\n", @@ -321,13 +321,13 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", - "from langchain_core.pydantic_v1 import BaseModel, Field\n", - "from langchain_core.runnables import chain as as_runnable\n", "from langchain_core.output_parsers.openai_tools import (\n", " JsonOutputToolsParser,\n", " PydanticToolsParser,\n", ")\n", + "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", + "from langchain_core.pydantic_v1 import BaseModel, Field\n", + "from langchain_core.runnables import chain as as_runnable\n", "\n", "\n", "class Reflection(BaseModel):\n", @@ -637,6 +637,7 @@ "outputs": [], "source": [ "from typing import Literal\n", + "\n", "from langgraph.graph import END, StateGraph\n", "\n", "\n", @@ -925,7 +926,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/examples/learning.ipynb b/examples/learning.ipynb index e545fb27c..ac6ceec47 100644 --- a/examples/learning.ipynb +++ b/examples/learning.ipynb @@ -9,7 +9,7 @@ "\n", "When running LangGraph agents, you can easily save good threads and use them in the future.\n", "\n", - "**Note:** this requires passing in a checkpointer." + "**Note:** this requires passing in a checkpointer.\n" ] }, { @@ -19,7 +19,7 @@ "source": [ "## Setup\n", "\n", - "First we need to install the packages required" + "First we need to install the packages required\n" ] }, { @@ -47,7 +47,7 @@ "id": "0abe11f4-62ed-4dc4-8875-3db21e260d1d", "metadata": {}, "source": [ - "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" + "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)\n" ] }, { @@ -66,8 +66,8 @@ } ], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")\n", "os.environ[\"TAVILY_API_KEY\"] = getpass.getpass(\"Tavily API Key:\")" @@ -78,7 +78,7 @@ "id": "f0ed46a8-effe-4596-b0e1-a6a29ee16f5c", "metadata": {}, "source": [ - "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability." + "Optionally, we can set API key for [LangSmith tracing](https://smith.langchain.com/), which will give us best-in-class observability.\n" ] }, { @@ -150,7 +150,7 @@ "1. It should work with messages. We will represent all agent state in the form of messages, so it needs to be able to work well with them.\n", "2. It should work with OpenAI function calling. This means it should either be an OpenAI model or a model that exposes a similar interface.\n", "\n", - "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example." + "Note: these model requirements are not requirements for using LangGraph - they are just requirements for this one example.\n" ] }, { @@ -170,7 +170,6 @@ "id": "a77995c0-bae2-4cee-a036-8688a90f05b9", "metadata": {}, "source": [ - "\n", "After we've done this, we should make sure the model knows that it has these tools available to call.\n", "We can do this using the `.bind_tools()` method, common to many of LangChain's chat models.\n" ] @@ -209,7 +208,7 @@ " b. If the agent said that it was finished, then it should finish\n", "2. Normal Edge: after the tools are invoked, it should always go back to the agent to decide what to do next\n", "\n", - "Let's define the nodes, as well as a function to decide how what conditional edge to take." + "Let's define the nodes, as well as a function to decide how what conditional edge to take.\n" ] }, { @@ -237,7 +236,7 @@ "source": [ "## Define the graph\n", "\n", - "We can now put it all together and define the graph!" + "We can now put it all together and define the graph!\n" ] }, { @@ -247,11 +246,19 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from typing import Annotated, TypedDict\n", + "\n", + "from langchain_core.messages import (\n", + " AIMessage,\n", + " AnyMessage,\n", + " HumanMessage,\n", + " SystemMessage,\n", + " ToolMessage,\n", + ")\n", + "\n", + "from langgraph.graph import END, StateGraph\n", "from langgraph.graph.message import add_messages\n", "from langgraph.managed.few_shot import FewShotExamples\n", - "from typing import TypedDict, Annotated\n", - "from langchain_core.messages import AnyMessage, HumanMessage, SystemMessage\n", "\n", "\n", "class BaseState(TypedDict):\n", @@ -259,9 +266,6 @@ " examples: Annotated[list, FewShotExamples]\n", "\n", "\n", - "from langchain_core.messages import AIMessage, ToolMessage\n", - "\n", - "\n", "def _render_message(m):\n", " if isinstance(m, HumanMessage):\n", " return \"Human: \" + m.content\n", @@ -299,9 +303,7 @@ "\n", "{examples}\n", "\n", - "Assist the user as they require!\"\"\".format(\n", - " examples=_examples\n", - " )\n", + "Assist the user as they require!\"\"\".format(examples=_examples)\n", "\n", " else:\n", " system_message = \"\"\"You are a helpful assistant\"\"\"\n", @@ -350,7 +352,7 @@ "source": [ "**Persistence**\n", "\n", - "To add in persistence, we pass in a checkpoint when compiling the graph" + "To add in persistence, we pass in a checkpoint when compiling the graph\n" ] }, { @@ -383,7 +385,7 @@ "id": "e8aff75b-563e-42b1-969b-742201514fc3", "metadata": {}, "source": [ - "## Preview the graph" + "## Preview the graph\n" ] }, { @@ -435,8 +437,6 @@ } ], "source": [ - "from langchain_core.messages import HumanMessage\n", - "\n", "thread = {\"configurable\": {\"thread_id\": \"1\"}}\n", "for event in app.stream(\n", " {\"messages\": [HumanMessage(content=\"whats the weather in sf?\")]}, thread\n", diff --git a/examples/llm-compiler/LLMCompiler.ipynb b/examples/llm-compiler/LLMCompiler.ipynb index b3d30e5dd..2530cca78 100644 --- a/examples/llm-compiler/LLMCompiler.ipynb +++ b/examples/llm-compiler/LLMCompiler.ipynb @@ -43,8 +43,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _get_pass(var: str):\n", @@ -78,8 +78,8 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_openai import ChatOpenAI\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "from langchain_openai import ChatOpenAI\n", "\n", "# Imported from the https://github.com/langchain-ai/langgraph/tree/main/examples/plan-and-execute repo\n", "from math_tools import get_math_tool\n", @@ -191,21 +191,19 @@ "source": [ "from typing import Sequence\n", "\n", + "from langchain import hub\n", "from langchain_core.language_models import BaseChatModel\n", - "from langchain_core.prompts import ChatPromptTemplate\n", - "from langchain_core.runnables import RunnableBranch\n", - "from langchain_core.tools import BaseTool\n", "from langchain_core.messages import (\n", " BaseMessage,\n", " FunctionMessage,\n", " HumanMessage,\n", " SystemMessage,\n", ")\n", - "\n", - "from output_parser import LLMCompilerPlanParser, Task\n", - "from langchain import hub\n", + "from langchain_core.prompts import ChatPromptTemplate\n", + "from langchain_core.runnables import RunnableBranch\n", + "from langchain_core.tools import BaseTool\n", "from langchain_openai import ChatOpenAI\n", - "\n", + "from output_parser import LLMCompilerPlanParser, Task\n", "\n", "prompt = hub.pull(\"wfh/llm-compiler\")\n", "print(prompt.pretty_print())" @@ -340,16 +338,15 @@ }, "outputs": [], "source": [ - "from typing import Any, Union, Iterable, List, Tuple, Dict\n", - "from typing_extensions import TypedDict\n", "import re\n", + "import time\n", + "from concurrent.futures import ThreadPoolExecutor, wait\n", + "from typing import Any, Dict, Iterable, List, Union\n", "\n", "from langchain_core.runnables import (\n", " chain as as_runnable,\n", ")\n", - "\n", - "from concurrent.futures import ThreadPoolExecutor, wait\n", - "import time\n", + "from typing_extensions import TypedDict\n", "\n", "\n", "def _get_observations(messages: List[BaseMessage]) -> Dict[int, Any]:\n", @@ -472,8 +469,7 @@ " args_for_tasks[task[\"idx\"]] = task[\"args\"]\n", " if (\n", " # Depends on other tasks\n", - " deps\n", - " and (any([dep not in observations for dep in deps]))\n", + " deps and (any([dep not in observations for dep in deps]))\n", " ):\n", " futures.append(\n", " executor.submit(\n", @@ -597,9 +593,9 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.pydantic_v1 import BaseModel, Field\n", "from langchain.chains.openai_functions import create_structured_output_runnable\n", "from langchain_core.messages import AIMessage\n", + "from langchain_core.pydantic_v1 import BaseModel, Field\n", "\n", "\n", "class FinalResponse(BaseModel):\n", @@ -724,9 +720,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import MessageGraph, END\n", "from typing import Dict\n", "\n", + "from langgraph.graph import END, MessageGraph\n", + "\n", "graph_builder = MessageGraph()\n", "\n", "# 1. Define vertices\n", diff --git a/examples/managing-agent-steps.ipynb b/examples/managing-agent-steps.ipynb index a8650defb..48e733708 100644 --- a/examples/managing-agent-steps.ipynb +++ b/examples/managing-agent-steps.ipynb @@ -48,8 +48,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -104,8 +104,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "# Add messages essentially does this with more\n", @@ -317,7 +319,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(State)\n", diff --git a/examples/managing-conversation-history.ipynb b/examples/managing-conversation-history.ipynb index 65719acac..8a7495a67 100644 --- a/examples/managing-conversation-history.ipynb +++ b/examples/managing-conversation-history.ipynb @@ -46,8 +46,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -92,18 +92,18 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", - "from typing import Annotated\n", - "from langgraph.graph import MessagesState\n", - "from langchain_core.tools import tool\n", - "from langgraph.prebuilt import ToolNode\n", - "from langchain_anthropic import ChatAnthropic\n", "from typing import Literal\n", - "from langgraph.graph import StateGraph, END\n", + "\n", + "from langchain_anthropic import ChatAnthropic\n", + "from langchain_core.tools import tool\n", + "\n", "from langgraph.checkpoint.sqlite import SqliteSaver\n", + "from langgraph.graph import MessagesState, StateGraph\n", + "from langgraph.prebuilt import ToolNode\n", "\n", "memory = SqliteSaver.from_conn_string(\":memory:\")\n", "\n", + "\n", "@tool\n", "def search(query: str):\n", " \"\"\"Call to surf the web.\"\"\"\n", @@ -119,6 +119,7 @@ "model = ChatAnthropic(model_name=\"claude-3-haiku-20240307\")\n", "bound_model = model.bind_tools(tools)\n", "\n", + "\n", "def should_continue(state: MessagesState) -> Literal[\"action\", \"__end__\"]:\n", " \"\"\"Return the next node to execute.\"\"\"\n", " last_message = state[\"messages\"][-1]\n", @@ -222,18 +223,18 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", - "from typing import Annotated\n", - "from langgraph.graph import MessagesState\n", - "from langchain_core.tools import tool\n", - "from langgraph.prebuilt import ToolNode\n", - "from langchain_anthropic import ChatAnthropic\n", "from typing import Literal\n", - "from langgraph.graph import StateGraph, END\n", + "\n", + "from langchain_anthropic import ChatAnthropic\n", + "from langchain_core.tools import tool\n", + "\n", "from langgraph.checkpoint.sqlite import SqliteSaver\n", + "from langgraph.graph import MessagesState, StateGraph\n", + "from langgraph.prebuilt import ToolNode\n", "\n", "memory = SqliteSaver.from_conn_string(\":memory:\")\n", "\n", + "\n", "@tool\n", "def search(query: str):\n", " \"\"\"Call to surf the web.\"\"\"\n", @@ -249,6 +250,7 @@ "model = ChatAnthropic(model_name=\"claude-3-haiku-20240307\")\n", "bound_model = model.bind_tools(tools)\n", "\n", + "\n", "def should_continue(state: MessagesState) -> Literal[\"action\", \"__end__\"]:\n", " \"\"\"Return the next node to execute.\"\"\"\n", " last_message = state[\"messages\"][-1]\n", @@ -335,7 +337,7 @@ "for event in app.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n", " event[\"messages\"][-1].pretty_print()\n", "\n", - "# This will now not remember the previous messages \n", + "# This will now not remember the previous messages\n", "# (because we set `messages[-1:]` in the filter messages argument)\n", "input_message = HumanMessage(content=\"whats my name?\")\n", "for event in app.stream({\"messages\": [input_message]}, config, stream_mode=\"values\"):\n", diff --git a/examples/map-reduce.ipynb b/examples/map-reduce.ipynb index 237ad240c..eb9a254ef 100644 --- a/examples/map-reduce.ipynb +++ b/examples/map-reduce.ipynb @@ -35,12 +35,14 @@ } ], "source": [ + "import operator\n", + "from typing import Annotated, TypedDict\n", + "\n", + "from langchain_core.pydantic_v1 import BaseModel\n", + "from langchain_openai import ChatOpenAI\n", + "\n", "from langgraph.constants import Send\n", "from langgraph.graph import END, StateGraph\n", - "import operator\n", - "from typing import TypedDict, Annotated\n", - "from langchain_openai import ChatOpenAI\n", - "from langchain_core.pydantic_v1 import BaseModel\n", "\n", "# Model and prompts\n", "# Define model and prompts we will use\n", @@ -67,6 +69,7 @@ "\n", "# Graph components: define the components that will make up the graph\n", "\n", + "\n", "# This will be the overall state of the main graph.\n", "# It will contain a topic (which we expect the user to provide)\n", "# and then will generate a list of subjects, and then a joke for\n", @@ -90,14 +93,14 @@ "\n", "# This is the function we will use to generate the subjects of the jokes\n", "def generate_topics(state: OverallState):\n", - " prompt = subjects_prompt.format(topic=state['topic'])\n", + " prompt = subjects_prompt.format(topic=state[\"topic\"])\n", " response = model.with_structured_output(Subjects).invoke(prompt)\n", " return {\"subjects\": response.subjects}\n", "\n", "\n", "# Here we generate a joke, given a subject\n", "def generate_joke(state: JokeState):\n", - " prompt = joke_prompt.format(subject=state['subject'])\n", + " prompt = joke_prompt.format(subject=state[\"subject\"])\n", " response = model.with_structured_output(Joke).invoke(prompt)\n", " return {\"jokes\": [response.joke]}\n", "\n", @@ -108,17 +111,15 @@ " # We will return a list of `Send` objects\n", " # Each `Send` object consists of the name of a node in the graph\n", " # as well as the state to send to that node\n", - " return [Send(\"generate_joke\", {\"subject\": s}) for s in state['subjects']]\n", + " return [Send(\"generate_joke\", {\"subject\": s}) for s in state[\"subjects\"]]\n", "\n", "\n", "# Here we will judge the best joke\n", "def best_joke(state: OverallState):\n", - " jokes = \"\\n\\n\".format(\"Joke {i}: {j}\" for i, j in enumerate(state['jokes']))\n", - " prompt = best_joke_prompt.format(topic=state['topic'], jokes=jokes)\n", + " jokes = \"\\n\\n\".format()\n", + " prompt = best_joke_prompt.format(topic=state[\"topic\"], jokes=jokes)\n", " response = model.with_structured_output(BestJoke).invoke(prompt)\n", - " return {\"best_selected_joke\": state['jokes'][response.id]}\n", - " \n", - "\n", + " return {\"best_selected_joke\": state[\"jokes\"][response.id]}\n", "\n", "\n", "# Construct the graph: here we put everything together to construct our graph\n", diff --git a/examples/multi_agent/agent_supervisor.ipynb b/examples/multi_agent/agent_supervisor.ipynb index 38746a70f..089c1c58c 100644 --- a/examples/multi_agent/agent_supervisor.ipynb +++ b/examples/multi_agent/agent_supervisor.ipynb @@ -73,10 +73,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, List, Tuple, Union\n", + "from typing import Annotated\n", "\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", - "from langchain_core.tools import tool\n", "from langchain_experimental.tools import PythonREPLTool\n", "\n", "tavily_tool = TavilySearchResults(max_results=5)\n", @@ -161,8 +160,8 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", "from langchain_core.output_parsers.openai_functions import JsonOutputFunctionsParser\n", + "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", "\n", "members = [\"Researcher\", \"Coder\"]\n", "system_prompt = (\n", @@ -231,12 +230,13 @@ "metadata": {}, "outputs": [], "source": [ - "import operator\n", - "from typing import Annotated, Any, Dict, List, Optional, Sequence, TypedDict\n", "import functools\n", + "import operator\n", + "from typing import Sequence, TypedDict\n", "\n", "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", - "from langgraph.graph import StateGraph, END\n", + "\n", + "from langgraph.graph import END, StateGraph\n", "\n", "\n", "# The agent state is the input to each node in the graph\n", diff --git a/examples/multi_agent/hierarchical_agent_teams.ipynb b/examples/multi_agent/hierarchical_agent_teams.ipynb index 9c1e5e551..bb9609df3 100644 --- a/examples/multi_agent/hierarchical_agent_teams.ipynb +++ b/examples/multi_agent/hierarchical_agent_teams.ipynb @@ -59,7 +59,6 @@ "source": [ "import getpass\n", "import os\n", - "import uuid\n", "\n", "\n", "def _set_if_undefined(var: str):\n", @@ -105,13 +104,11 @@ }, "outputs": [], "source": [ - "from typing import Annotated, List, Tuple, Union\n", + "from typing import Annotated, List\n", "\n", - "import matplotlib.pyplot as plt\n", "from langchain_community.document_loaders import WebBaseLoader\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", "from langchain_core.tools import tool\n", - "from langsmith import trace\n", "\n", "tavily_tool = TavilySearchResults(max_results=5)\n", "\n", @@ -236,7 +233,7 @@ "\n", "@tool\n", "def python_repl(\n", - " code: Annotated[str, \"The python code to execute to generate your chart.\"]\n", + " code: Annotated[str, \"The python code to execute to generate your chart.\"],\n", "):\n", " \"\"\"Use this to execute python code. If you want to see the output of a value,\n", " you should print it out with `print(...)`. This is visible to the user.\"\"\"\n", @@ -274,13 +271,11 @@ }, "outputs": [], "source": [ - "from typing import Any, Callable, List, Optional, TypedDict, Union\n", + "from typing import List, Optional\n", "\n", "from langchain.agents import AgentExecutor, create_openai_functions_agent\n", "from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser\n", "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", - "from langchain_core.runnables import Runnable\n", - "from langchain_core.tools import BaseTool\n", "from langchain_openai import ChatOpenAI\n", "\n", "from langgraph.graph import END, StateGraph\n", @@ -383,9 +378,8 @@ "import functools\n", "import operator\n", "\n", - "from langchain_core.messages import AIMessage, BaseMessage, HumanMessage\n", + "from langchain_core.messages import BaseMessage, HumanMessage\n", "from langchain_openai.chat_models import ChatOpenAI\n", - "import functools\n", "\n", "\n", "# ResearchTeam graph state\n", @@ -586,7 +580,7 @@ " written_files = [\n", " f.relative_to(WORKING_DIRECTORY) for f in WORKING_DIRECTORY.rglob(\"*\")\n", " ]\n", - " except:\n", + " except Exception:\n", " pass\n", " if not written_files:\n", " return {**state, \"current_files\": \"No files written.\"}\n", @@ -785,10 +779,9 @@ }, "outputs": [], "source": [ - "from langchain_core.messages import AIMessage, BaseMessage, HumanMessage\n", + "from langchain_core.messages import BaseMessage\n", "from langchain_openai.chat_models import ChatOpenAI\n", "\n", - "\n", "llm = ChatOpenAI(model=\"gpt-4-1106-preview\")\n", "\n", "supervisor_node = create_team_supervisor(\n", diff --git a/examples/multi_agent/multi-agent-collaboration.ipynb b/examples/multi_agent/multi-agent-collaboration.ipynb index 1bef08bfd..04603d01c 100644 --- a/examples/multi_agent/multi-agent-collaboration.ipynb +++ b/examples/multi_agent/multi-agent-collaboration.ipynb @@ -77,10 +77,11 @@ "source": [ "from langchain_core.messages import (\n", " BaseMessage,\n", - " ToolMessage,\n", " HumanMessage,\n", + " ToolMessage,\n", ")\n", "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", + "\n", "from langgraph.graph import END, StateGraph\n", "\n", "\n", @@ -123,10 +124,11 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.tools import tool\n", "from typing import Annotated\n", - "from langchain_experimental.utilities import PythonREPL\n", + "\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", + "from langchain_core.tools import tool\n", + "from langchain_experimental.utilities import PythonREPL\n", "\n", "tavily_tool = TavilySearchResults(max_results=5)\n", "\n", @@ -137,7 +139,7 @@ "\n", "@tool\n", "def python_repl(\n", - " code: Annotated[str, \"The python code to execute to generate your chart.\"]\n", + " code: Annotated[str, \"The python code to execute to generate your chart.\"],\n", "):\n", " \"\"\"Use this to execute python code. If you want to see the output of a value,\n", " you should print it out with `print(...)`. This is visible to the user.\"\"\"\n", @@ -182,7 +184,6 @@ "from typing import Annotated, Sequence, TypedDict\n", "\n", "from langchain_openai import ChatOpenAI\n", - "from typing_extensions import TypedDict\n", "\n", "\n", "# This defines the object that is passed between each node\n", @@ -210,6 +211,7 @@ "outputs": [], "source": [ "import functools\n", + "\n", "from langchain_core.messages import AIMessage\n", "\n", "\n", @@ -377,7 +379,7 @@ "\n", "try:\n", " display(Image(graph.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/pass-run-time-values-to-tools.ipynb b/examples/pass-run-time-values-to-tools.ipynb index 8e1a3a0c2..2d474f115 100644 --- a/examples/pass-run-time-values-to-tools.ipynb +++ b/examples/pass-run-time-values-to-tools.ipynb @@ -55,10 +55,10 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", - "if 'OPENAI_API_KEY' not in os.environ:\n", + "if \"OPENAI_API_KEY\" not in os.environ:\n", " os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"OpenAI API Key:\")" ] }, @@ -79,7 +79,7 @@ "source": [ "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", "\n", - "if 'LANGCHAIN_API_KEY' not in os.environ:\n", + "if \"LANGCHAIN_API_KEY\" not in os.environ:\n", " os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass(\"LangSmith API Key:\")" ] }, @@ -104,25 +104,27 @@ "outputs": [], "source": [ "from typing import List\n", - "from langchain_core.tools import tool, BaseTool\n", + "\n", + "from langchain_core.tools import BaseTool, tool\n", "\n", "# A global dict that the tools will be updating in this example.\n", "user_to_pets = {}\n", "\n", + "\n", "def generate_tools_for_user(user_id: str) -> List[BaseTool]:\n", " \"\"\"Generate a set of tools that have a user id associated with them.\"\"\"\n", - " \n", + "\n", " @tool\n", " def update_favorite_pets(pets: List[str]) -> None:\n", " \"\"\"Add the list of favorite pets.\"\"\"\n", " user_to_pets[user_id] = pets\n", - " \n", + "\n", " @tool\n", " def delete_favorite_pets() -> None:\n", " \"\"\"Delete the list of favorite pets.\"\"\"\n", " if user_id in user_to_pets:\n", " del user_to_pets[user_id]\n", - " \n", + "\n", " @tool\n", " def list_favorite_pets() -> None:\n", " \"\"\"List favorite pets if any.\"\"\"\n", @@ -186,8 +188,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", @@ -229,9 +232,9 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", "from langchain_core.messages import ToolMessage\n", - "from langgraph.prebuilt import ToolExecutor\n", + "\n", + "from langgraph.prebuilt import ToolExecutor, ToolInvocation\n", "\n", "\n", "# Define the function that determines whether to continue or not\n", @@ -249,7 +252,7 @@ "# Define the function that calls the model\n", "def call_model(state, config):\n", " messages = state[\"messages\"]\n", - " tools = generate_tools_for_user(config['user_id'])\n", + " tools = generate_tools_for_user(config[\"user_id\"])\n", " model_with_tools = model.bind_tools(tools)\n", " response = model_with_tools.invoke(messages)\n", " # We return a list, because this will get added to the existing list\n", @@ -275,7 +278,7 @@ " # We can now wrap these tools in a simple ToolExecutor.\n", " # This is a real simple class that takes in a ToolInvocation and calls that tool, returning the output.\n", " # A ToolInvocation is any class with `tool` and `tool_input` attribute.\n", - " tools = generate_tools_for_user(config['user_id'])\n", + " tools = generate_tools_for_user(config[\"user_id\"])\n", " tool_executor = ToolExecutor(tools)\n", " responses = tool_executor.batch(tool_invocations, return_exceptions=True)\n", " # We use the response to create tool messages\n", @@ -309,7 +312,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", @@ -375,7 +378,7 @@ "\n", "try:\n", " display(Image(app.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -427,12 +430,12 @@ "source": [ "from langchain_core.messages import HumanMessage\n", "\n", - "user_to_pets.clear() # Clear the state\n", + "user_to_pets.clear() # Clear the state\n", "\n", - "print(f'User information prior to run: {user_to_pets}')\n", + "print(f\"User information prior to run: {user_to_pets}\")\n", "\n", "inputs = {\"messages\": [HumanMessage(content=\"my favorite pets are cats and dogs\")]}\n", - "for output in app.stream(inputs, {'user_id': 'eugene'}):\n", + "for output in app.stream(inputs, {\"user_id\": \"eugene\"}):\n", " # stream() yields dictionaries with output keyed by node name\n", " for key, value in output.items():\n", " print(f\"Output from node '{key}':\")\n", @@ -440,7 +443,7 @@ " print(value)\n", " print(\"\\n---\\n\")\n", "\n", - "print(f'User information prior to run: {user_to_pets}')" + "print(f\"User information prior to run: {user_to_pets}\")" ] }, { @@ -477,11 +480,11 @@ } ], "source": [ - "print(f'User information prior to run: {user_to_pets}')\n", + "print(f\"User information prior to run: {user_to_pets}\")\n", "\n", "\n", "inputs = {\"messages\": [HumanMessage(content=\"what are my favorite pets?\")]}\n", - "for output in app.stream(inputs, {'user_id': 'eugene'}):\n", + "for output in app.stream(inputs, {\"user_id\": \"eugene\"}):\n", " # stream() yields dictionaries with output keyed by node name\n", " for key, value in output.items():\n", " print(f\"Output from node '{key}':\")\n", @@ -490,7 +493,7 @@ " print(\"\\n---\\n\")\n", "\n", "\n", - "print(f'User information prior to run: {user_to_pets}')" + "print(f\"User information prior to run: {user_to_pets}\")" ] }, { @@ -527,11 +530,15 @@ } ], "source": [ - "print(f'User information prior to run: {user_to_pets}')\n", + "print(f\"User information prior to run: {user_to_pets}\")\n", "\n", "\n", - "inputs = {\"messages\": [HumanMessage(content=\"please forget what i told you about my favorite animals\")]}\n", - "for output in app.stream(inputs, {'user_id': 'eugene'}):\n", + "inputs = {\n", + " \"messages\": [\n", + " HumanMessage(content=\"please forget what i told you about my favorite animals\")\n", + " ]\n", + "}\n", + "for output in app.stream(inputs, {\"user_id\": \"eugene\"}):\n", " # stream() yields dictionaries with output keyed by node name\n", " for key, value in output.items():\n", " print(f\"Output from node '{key}':\")\n", @@ -540,7 +547,7 @@ " print(\"\\n---\\n\")\n", "\n", "\n", - "print(f'User information prior to run: {user_to_pets}')" + "print(f\"User information prior to run: {user_to_pets}\")" ] } ], diff --git a/examples/persistence.ipynb b/examples/persistence.ipynb index c8f8bf913..7ead6c69c 100644 --- a/examples/persistence.ipynb +++ b/examples/persistence.ipynb @@ -74,8 +74,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -122,8 +122,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "# Add messages essentially does this with more\n", @@ -315,7 +317,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(State)\n", @@ -399,7 +401,7 @@ "\n", "try:\n", " display(Image(app.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -566,7 +568,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/examples/plan-and-execute/plan-and-execute.ipynb b/examples/plan-and-execute/plan-and-execute.ipynb index 9ead0eac4..13c26b44a 100644 --- a/examples/plan-and-execute/plan-and-execute.ipynb +++ b/examples/plan-and-execute/plan-and-execute.ipynb @@ -65,8 +65,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -154,6 +154,7 @@ "source": [ "from langchain import hub\n", "from langchain_openai import ChatOpenAI\n", + "\n", "from langgraph.prebuilt import create_react_agent\n", "\n", "# Get the prompt to use - you can modify this!\n", @@ -212,8 +213,8 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import List, Tuple, Annotated, TypedDict\n", "import operator\n", + "from typing import Annotated, List, Tuple, TypedDict\n", "\n", "\n", "class PlanExecute(TypedDict):\n", diff --git a/examples/rag/langgraph_adaptive_rag.ipynb b/examples/rag/langgraph_adaptive_rag.ipynb index 51326f9d8..c4da78907 100644 --- a/examples/rag/langgraph_adaptive_rag.ipynb +++ b/examples/rag/langgraph_adaptive_rag.ipynb @@ -60,9 +60,10 @@ "source": [ "### LLMs\n", "import os\n", - "os.environ['OPENAI_API_KEY'] = \n", - "os.environ['COHERE_API_KEY'] = \n", - "os.environ['TAVILY_API_KEY'] = " + "\n", + "os.environ[\"OPENAI_API_KEY\"] = \"\"\n", + "os.environ[\"COHERE_API_KEY\"] = \"\"\n", + "os.environ[\"TAVILY_API_KEY\"] = \"\"" ] }, { @@ -83,9 +84,9 @@ "outputs": [], "source": [ "### Tracing (optional)\n", - "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", - "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", - "os.environ['LANGCHAIN_API_KEY'] = " + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = \"\"" ] }, { @@ -475,9 +476,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import List\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", "\n", "class GraphState(TypedDict):\n", " \"\"\"\n", @@ -659,7 +661,7 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " filtered_documents = state[\"documents\"]\n", "\n", " if not filtered_documents:\n", diff --git a/examples/rag/langgraph_adaptive_rag_cohere.ipynb b/examples/rag/langgraph_adaptive_rag_cohere.ipynb index 241db3f6a..a95fe0ab1 100644 --- a/examples/rag/langgraph_adaptive_rag_cohere.ipynb +++ b/examples/rag/langgraph_adaptive_rag_cohere.ipynb @@ -68,7 +68,8 @@ "source": [ "### LLMs\n", "import os\n", - "os.environ['COHERE_API_KEY'] = " + "\n", + "os.environ[\"COHERE_API_KEY\"] = \"\"" ] }, { @@ -83,7 +84,7 @@ "# ### Tracing (optional)\n", "# os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", "# os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", - "# os.environ['LANGCHAIN_API_KEY'] = " + "# os.environ['LANGCHAIN_API_KEY'] =''" ] }, { @@ -108,9 +109,9 @@ "### Build Index\n", "\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", + "from langchain_cohere import CohereEmbeddings\n", "from langchain_community.document_loaders import WebBaseLoader\n", "from langchain_community.vectorstores import Chroma\n", - "from langchain_cohere import CohereEmbeddings\n", "\n", "# Set embeddings\n", "embd = CohereEmbeddings()\n", @@ -187,11 +188,10 @@ ], "source": [ "### Router\n", - "from typing import Literal\n", "\n", + "from langchain_cohere import ChatCohere\n", "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", - "from langchain_cohere import ChatCohere\n", "\n", "\n", "# Data model\n", @@ -329,11 +329,8 @@ "source": [ "### Generate\n", "\n", - "from langchain import hub\n", - "from langchain_core.output_parsers import StrOutputParser\n", - "import langchain\n", "from langchain_core.messages import HumanMessage\n", - "\n", + "from langchain_core.output_parsers import StrOutputParser\n", "\n", "# Preamble\n", "preamble = \"\"\"You are an assistant for question-answering tasks. Use the following pieces of retrieved context to answer the question. If you don't know the answer, just say that you don't know. Use three sentences maximum and keep the answer concise.\"\"\"\n", @@ -341,15 +338,18 @@ "# LLM\n", "llm = ChatCohere(model_name=\"command-r\", temperature=0).bind(preamble=preamble)\n", "\n", + "\n", "# Prompt\n", - "prompt = lambda x: ChatPromptTemplate.from_messages(\n", - " [\n", - " HumanMessage(\n", - " f\"Question: {x['question']} \\nAnswer: \",\n", - " additional_kwargs={\"documents\": x[\"documents\"]},\n", - " )\n", - " ]\n", - ")\n", + "def prompt(x):\n", + " return ChatPromptTemplate.from_messages(\n", + " [\n", + " HumanMessage(\n", + " f\"Question: {x['question']} \\nAnswer: \",\n", + " additional_kwargs={\"documents\": x[\"documents\"]},\n", + " )\n", + " ]\n", + " )\n", + "\n", "\n", "# Chain\n", "rag_chain = prompt | llm | StrOutputParser()\n", @@ -376,11 +376,7 @@ "source": [ "### LLM fallback\n", "\n", - "from langchain import hub\n", "from langchain_core.output_parsers import StrOutputParser\n", - "import langchain\n", - "from langchain_core.messages import HumanMessage\n", - "\n", "\n", "# Preamble\n", "preamble = \"\"\"You are an assistant for question-answering tasks. Answer the question based upon your knowledge. Use three sentences maximum and keep the answer concise.\"\"\"\n", @@ -388,10 +384,13 @@ "# LLM\n", "llm = ChatCohere(model_name=\"command-r\", temperature=0).bind(preamble=preamble)\n", "\n", + "\n", "# Prompt\n", - "prompt = lambda x: ChatPromptTemplate.from_messages(\n", - " [HumanMessage(f\"Question: {x['question']} \\nAnswer: \")]\n", - ")\n", + "def prompt(x):\n", + " return ChatPromptTemplate.from_messages(\n", + " [HumanMessage(f\"Question: {x['question']} \\nAnswer: \")]\n", + " )\n", + "\n", "\n", "# Chain\n", "llm_chain = prompt | llm | StrOutputParser()\n", @@ -535,7 +534,7 @@ "outputs": [], "source": [ "### Search\n", - "# os.environ['TAVILY_API_KEY'] = \n", + "# os.environ['TAVILY_API_KEY'] =''\n", "\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", "\n", @@ -565,9 +564,10 @@ }, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import List\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", "\n", "class GraphState(TypedDict):\n", " \"\"\"|\n", @@ -764,7 +764,7 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " filtered_documents = state[\"documents\"]\n", "\n", " if not filtered_documents:\n", diff --git a/examples/rag/langgraph_adaptive_rag_local.ipynb b/examples/rag/langgraph_adaptive_rag_local.ipynb index 1f2decad1..de32ae9ba 100644 --- a/examples/rag/langgraph_adaptive_rag_local.ipynb +++ b/examples/rag/langgraph_adaptive_rag_local.ipynb @@ -45,7 +45,8 @@ "metadata": {}, "outputs": [], "source": [ - "! pip install -U langchain-nomic langchain_community tiktoken langchainhub chromadb langchain langgraph tavily-python" + "%capture --no-stderr\n", + "%pip install -U langchain-nomic langchain_community tiktoken langchainhub chromadb langchain langgraph tavily-python" ] }, { @@ -100,9 +101,11 @@ "metadata": {}, "outputs": [], "source": [ - "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", - "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", - "os.environ['LANGCHAIN_API_KEY'] = " + "import os\n", + "\n", + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = \"\"" ] }, { @@ -122,8 +125,8 @@ "source": [ "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "from langchain_community.document_loaders import WebBaseLoader\n", - "from langchain_community.vectorstores import Chroma\n", "from langchain_community.embeddings import GPT4AllEmbeddings\n", + "from langchain_community.vectorstores import Chroma\n", "\n", "urls = [\n", " \"https://lilianweng.github.io/posts/2023-06-23-agent/\",\n", @@ -439,9 +442,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import List\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", "\n", "class GraphState(TypedDict):\n", " \"\"\"\n", @@ -620,7 +624,7 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " filtered_documents = state[\"documents\"]\n", "\n", " if not filtered_documents:\n", diff --git a/examples/rag/langgraph_agentic_rag.ipynb b/examples/rag/langgraph_agentic_rag.ipynb index 646a45cda..0eefd0744 100644 --- a/examples/rag/langgraph_agentic_rag.ipynb +++ b/examples/rag/langgraph_agentic_rag.ipynb @@ -32,8 +32,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(key: str):\n", @@ -116,11 +116,7 @@ " \"Search and return information about Lilian Weng blog posts on LLM agents, prompt engineering, and adversarial attacks on LLMs.\",\n", ")\n", "\n", - "tools = [retriever_tool]\n", - "\n", - "from langgraph.prebuilt import ToolExecutor\n", - "\n", - "tool_executor = ToolExecutor(tools)" + "tools = [retriever_tool]" ] }, { @@ -149,6 +145,7 @@ "from typing import Annotated, Sequence, TypedDict\n", "\n", "from langchain_core.messages import BaseMessage\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "\n", @@ -204,11 +201,12 @@ "\n", "from langchain import hub\n", "from langchain_core.messages import BaseMessage, HumanMessage\n", - "from langchain_core.pydantic_v1 import BaseModel, Field\n", - "from langchain_openai import ChatOpenAI\n", - "from langgraph.prebuilt import tools_condition\n", "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_core.prompts import PromptTemplate\n", + "from langchain_core.pydantic_v1 import BaseModel, Field\n", + "from langchain_openai import ChatOpenAI\n", + "\n", + "from langgraph.prebuilt import tools_condition\n", "\n", "### Edges\n", "\n", @@ -451,7 +449,7 @@ "\n", "try:\n", " display(Image(graph.get_graph(xray=True).draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -529,7 +527,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/examples/rag/langgraph_crag.ipynb b/examples/rag/langgraph_crag.ipynb index 75e20a8db..70940d173 100644 --- a/examples/rag/langgraph_crag.ipynb +++ b/examples/rag/langgraph_crag.ipynb @@ -67,7 +67,8 @@ "outputs": [], "source": [ "import os\n", - "os.environ['OPENAI_API_KEY'] = " + "\n", + "os.environ[\"OPENAI_API_KEY\"] = \"\"" ] }, { @@ -87,7 +88,7 @@ "metadata": {}, "outputs": [], "source": [ - "os.environ['TAVILY_API_KEY'] = " + "os.environ[\"TAVILY_API_KEY\"] = \"\"" ] }, { @@ -107,9 +108,9 @@ "metadata": {}, "outputs": [], "source": [ - "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", - "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", - "os.environ['LANGCHAIN_API_KEY'] = " + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = \"\"" ] }, { @@ -182,9 +183,9 @@ "source": [ "### Retrieval Grader\n", "\n", - "from langchain_openai import ChatOpenAI\n", "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", + "from langchain_openai import ChatOpenAI\n", "\n", "\n", "# Data model\n", @@ -339,9 +340,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import List\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", "\n", "class GraphState(TypedDict):\n", " \"\"\"\n", @@ -499,9 +501,9 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " web_search = state[\"web_search\"]\n", - " filtered_documents = state[\"documents\"]\n", + " state[\"documents\"]\n", "\n", " if web_search == \"Yes\":\n", " # All documents have been filtered check_relevance\n", diff --git a/examples/rag/langgraph_crag_local.ipynb b/examples/rag/langgraph_crag_local.ipynb index bc94e65b9..70ed86f7d 100644 --- a/examples/rag/langgraph_crag_local.ipynb +++ b/examples/rag/langgraph_crag_local.ipynb @@ -99,7 +99,7 @@ "outputs": [], "source": [ "# If using Mistral API\n", - "mistral_api_key = " + "mistral_api_key = \"\"" ] }, { @@ -120,7 +120,8 @@ "outputs": [], "source": [ "import os\n", - "os.environ['TAVILY_API_KEY'] = " + "\n", + "os.environ[\"TAVILY_API_KEY\"] = \"\"" ] }, { @@ -140,9 +141,9 @@ "metadata": {}, "outputs": [], "source": [ - "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", - "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", - "os.environ['LANGCHAIN_API_KEY'] = " + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = \"\"" ] }, { @@ -183,10 +184,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_community.document_loaders import WebBaseLoader\n", - "from langchain_community.vectorstores import Chroma\n", - "from langchain_community.embeddings import GPT4AllEmbeddings\n", "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", + "from langchain_community.document_loaders import WebBaseLoader\n", + "from langchain_community.embeddings import GPT4AllEmbeddings\n", + "from langchain_community.vectorstores import Chroma\n", "from langchain_mistralai import MistralAIEmbeddings\n", "\n", "# Load\n", @@ -243,8 +244,8 @@ "\n", "from langchain.prompts import PromptTemplate\n", "from langchain_community.chat_models import ChatOllama\n", - "from langchain_mistralai.chat_models import ChatMistralAI\n", "from langchain_core.output_parsers import JsonOutputParser\n", + "from langchain_mistralai.chat_models import ChatMistralAI\n", "\n", "# LLM\n", "if run_local == \"Yes\":\n", @@ -398,9 +399,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import List\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", "\n", "class GraphState(TypedDict):\n", " \"\"\"\n", @@ -558,9 +560,9 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " web_search = state[\"web_search\"]\n", - " filtered_documents = state[\"documents\"]\n", + " state[\"documents\"]\n", "\n", " if web_search == \"Yes\":\n", " # All documents have been filtered check_relevance\n", diff --git a/examples/rag/langgraph_rag_agent_llama3_local.ipynb b/examples/rag/langgraph_rag_agent_llama3_local.ipynb index c9e05b08b..ce7dfc3c7 100644 --- a/examples/rag/langgraph_rag_agent_llama3_local.ipynb +++ b/examples/rag/langgraph_rag_agent_llama3_local.ipynb @@ -98,8 +98,8 @@ "### Index\n", "\n", "from langchain_community.document_loaders import WebBaseLoader\n", - "from langchain_community.vectorstores import Chroma\n", "from langchain_community.embeddings import GPT4AllEmbeddings\n", + "from langchain_community.vectorstores import Chroma\n", "from langchain_text_splitters import RecursiveCharacterTextSplitter\n", "\n", "urls = [\n", @@ -186,7 +186,6 @@ "source": [ "### Generate\n", "\n", - "from langchain import hub\n", "from langchain_core.output_parsers import StrOutputParser\n", "from langchain_core.prompts import PromptTemplate\n", "\n", @@ -349,7 +348,6 @@ "outputs": [], "source": [ "### Search\n", - "\n", "from langchain_community.tools.tavily_search import TavilySearchResults\n", "\n", "web_search_tool = TavilySearchResults(k=3)" @@ -370,9 +368,13 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", + "from pprint import pprint\n", "from typing import List\n", + "\n", "from langchain_core.documents import Document\n", + "from typing_extensions import TypedDict\n", + "\n", + "from langgraph.graph import END, StateGraph\n", "\n", "### State\n", "\n", @@ -538,9 +540,9 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " web_search = state[\"web_search\"]\n", - " filtered_documents = state[\"documents\"]\n", + " state[\"documents\"]\n", "\n", " if web_search == \"Yes\":\n", " # All documents have been filtered check_relevance\n", @@ -597,8 +599,6 @@ " return \"not supported\"\n", "\n", "\n", - "from langgraph.graph import END, StateGraph\n", - "\n", "workflow = StateGraph(GraphState)\n", "\n", "# Define the nodes\n", @@ -703,7 +703,6 @@ "app = workflow.compile()\n", "\n", "# Test\n", - "from pprint import pprint\n", "\n", "inputs = {\"question\": \"What are the types of agent memory?\"}\n", "for output in app.stream(inputs):\n", @@ -751,12 +750,10 @@ } ], "source": [ - "# Compile\n", - "app = workflow.compile()\n", - "\n", - "# Test\n", "from pprint import pprint\n", "\n", + "# Compile\n", + "app = workflow.compile()\n", "inputs = {\"question\": \"Who are the Bears expected to draft first in the NFL draft?\"}\n", "for output in app.stream(inputs):\n", " for key, value in output.items():\n", diff --git a/examples/rag/langgraph_self_rag.ipynb b/examples/rag/langgraph_self_rag.ipynb index a10fb5928..d27eb3478 100644 --- a/examples/rag/langgraph_self_rag.ipynb +++ b/examples/rag/langgraph_self_rag.ipynb @@ -79,7 +79,8 @@ "outputs": [], "source": [ "import os\n", - "os.environ['OPENAI_API_KEY'] = " + "\n", + "os.environ[\"OPENAI_API_KEY\"] = \"\"" ] }, { @@ -99,9 +100,9 @@ "metadata": {}, "outputs": [], "source": [ - "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", - "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", - "os.environ['LANGCHAIN_API_KEY'] = " + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = \"\"" ] }, { @@ -182,7 +183,6 @@ "source": [ "### Retrieval Grader\n", "\n", - "from typing import Literal\n", "\n", "from langchain_core.prompts import ChatPromptTemplate\n", "from langchain_core.pydantic_v1 import BaseModel, Field\n", @@ -416,9 +416,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import List\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", "\n", "class GraphState(TypedDict):\n", " \"\"\"\n", @@ -444,8 +445,6 @@ "source": [ "### Nodes\n", "\n", - "from langchain.schema import Document\n", - "\n", "\n", "def retrieve(state):\n", " \"\"\"\n", @@ -550,7 +549,7 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " filtered_documents = state[\"documents\"]\n", "\n", " if not filtered_documents:\n", diff --git a/examples/rag/langgraph_self_rag_local.ipynb b/examples/rag/langgraph_self_rag_local.ipynb index 1f1a920e5..dce12a9cf 100644 --- a/examples/rag/langgraph_self_rag_local.ipynb +++ b/examples/rag/langgraph_self_rag_local.ipynb @@ -60,7 +60,8 @@ "metadata": {}, "outputs": [], "source": [ - "! pip install -U langchain-nomic langchain_community tiktoken langchainhub chromadb langchain langgraph" + "%capture --no-stderr\n", + "%pip install -U langchain-nomic langchain_community tiktoken langchainhub chromadb langchain langgraph" ] }, { @@ -115,9 +116,11 @@ "metadata": {}, "outputs": [], "source": [ - "os.environ['LANGCHAIN_TRACING_V2'] = 'true'\n", - "os.environ['LANGCHAIN_ENDPOINT'] = 'https://api.smith.langchain.com'\n", - "os.environ['LANGCHAIN_API_KEY'] = " + "import os\n", + "\n", + "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n", + "os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n", + "os.environ[\"LANGCHAIN_API_KEY\"] = \"\"" ] }, { @@ -139,8 +142,8 @@ "source": [ "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", "from langchain_community.document_loaders import WebBaseLoader\n", - "from langchain_community.vectorstores import Chroma\n", "from langchain_community.embeddings import GPT4AllEmbeddings\n", + "from langchain_community.vectorstores import Chroma\n", "\n", "urls = [\n", " \"https://lilianweng.github.io/posts/2023-06-23-agent/\",\n", @@ -389,9 +392,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import List\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", "\n", "class GraphState(TypedDict):\n", " \"\"\"\n", @@ -417,8 +421,6 @@ "source": [ "### Nodes\n", "\n", - "from langchain.schema import Document\n", - "\n", "\n", "def retrieve(state):\n", " \"\"\"\n", @@ -523,7 +525,7 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " filtered_documents = state[\"documents\"]\n", "\n", " if not filtered_documents:\n", diff --git a/examples/rag/langgraph_self_rag_pinecone_movies.ipynb b/examples/rag/langgraph_self_rag_pinecone_movies.ipynb index 99e194c97..a21c95c5c 100644 --- a/examples/rag/langgraph_self_rag_pinecone_movies.ipynb +++ b/examples/rag/langgraph_self_rag_pinecone_movies.ipynb @@ -231,7 +231,6 @@ "\n", "from langchain import hub\n", "from langchain_core.output_parsers import StrOutputParser\n", - "from langchain_core.runnables import RunnablePassthrough\n", "\n", "# Prompt\n", "prompt = hub.pull(\"rlm/rag-prompt\")\n", @@ -404,9 +403,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import List\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", "\n", "class GraphState(TypedDict):\n", " \"\"\"\n", @@ -543,7 +543,7 @@ " \"\"\"\n", "\n", " print(\"---ASSESS GRADED DOCUMENTS---\")\n", - " question = state[\"question\"]\n", + " state[\"question\"]\n", " filtered_documents = state[\"documents\"]\n", "\n", " if not filtered_documents:\n", diff --git a/examples/reflexion/reflexion.ipynb b/examples/reflexion/reflexion.ipynb index b0e8e45a6..3c4ca26dc 100644 --- a/examples/reflexion/reflexion.ipynb +++ b/examples/reflexion/reflexion.ipynb @@ -133,10 +133,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", - "from langchain_core.pydantic_v1 import BaseModel, Field, ValidationError\n", "from langchain_core.messages import HumanMessage, ToolMessage\n", "from langchain_core.output_parsers.openai_tools import PydanticToolsParser\n", + "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", + "from langchain_core.pydantic_v1 import BaseModel, Field, ValidationError\n", "\n", "\n", "class Reflection(BaseModel):\n", @@ -346,6 +346,7 @@ "outputs": [], "source": [ "from langchain_core.tools import StructuredTool\n", + "\n", "from langgraph.prebuilt import ToolNode\n", "\n", "\n", @@ -381,8 +382,8 @@ "outputs": [], "source": [ "from typing import Literal\n", - "from langgraph.graph import END, MessageGraph\n", "\n", + "from langgraph.graph import END, MessageGraph\n", "\n", "MAX_ITERATIONS = 5\n", "builder = MessageGraph()\n", @@ -444,7 +445,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] diff --git a/examples/respond-in-format.ipynb b/examples/respond-in-format.ipynb index d60e85ea0..6d71adeed 100644 --- a/examples/respond-in-format.ipynb +++ b/examples/respond-in-format.ipynb @@ -50,8 +50,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -106,8 +106,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "# Add messages essentially does this with more\n", @@ -262,8 +264,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence, TypedDict\n", + "\n", "from langchain_core.messages import BaseMessage\n", "\n", "\n", diff --git a/examples/rewoo/rewoo.ipynb b/examples/rewoo/rewoo.ipynb index c668f7513..818882380 100644 --- a/examples/rewoo/rewoo.ipynb +++ b/examples/rewoo/rewoo.ipynb @@ -58,8 +58,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_if_undefined(var: str):\n", @@ -91,7 +91,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import TypedDict, List\n", + "from typing import List, TypedDict\n", "\n", "\n", "class ReWOO(TypedDict):\n", @@ -233,6 +233,7 @@ "outputs": [], "source": [ "import re\n", + "\n", "from langchain_core.prompts import ChatPromptTemplate\n", "\n", "# Regex to match expressions of the form E#... = ...[...]\n", @@ -383,7 +384,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "graph = StateGraph(ReWOO)\n", "graph.add_node(\"plan\", get_plan)\n", diff --git a/examples/self-discover/self-discover.ipynb b/examples/self-discover/self-discover.ipynb index 3c5f44b2c..f626c0d34 100644 --- a/examples/self-discover/self-discover.ipynb +++ b/examples/self-discover/self-discover.ipynb @@ -9,7 +9,10 @@ "\n", "An implementation of the [Self-Discover paper](https://arxiv.org/pdf/2402.03620.pdf).\n", "\n", - "Based on [this implementation from @catid](https://github.com/catid/self-discover/tree/main?tab=readme-ov-file)" + "Based on [this implementation from @catid](https://github.com/catid/self-discover/tree/main?tab=readme-ov-file)\n", + "\n", + "\n", + "## Define the prompts" ] }, { @@ -17,52 +20,12 @@ "execution_count": 1, "id": "a18d8f24-5d9a-45c5-9739-6f3c4ed6c9c9", "metadata": {}, - "outputs": [], - "source": [ - "from langchain_openai import ChatOpenAI" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "id": "9f554045-6e79-42d3-be4b-835bbbd0b78c", - "metadata": {}, - "outputs": [], - "source": [ - "model = ChatOpenAI(temperature=0, model=\"gpt-4-turbo-preview\")" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "id": "9e9925aa-638a-4862-823e-9803402b8f82", - "metadata": {}, - "outputs": [], - "source": [ - "from langchain import hub\n", - "from langchain_core.prompts import PromptTemplate" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "id": "c4cc5c8c-f6a5-42c7-9ed5-780d79b3b29a", - "metadata": {}, - "outputs": [], - "source": [ - "select_prompt = hub.pull(\"hwchase17/self-discovery-select\")" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "id": "a5b53d29-f5b6-4f39-af97-bb6b133e1d18", - "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ + "Self-Discovery Select Prompt:\n", "Select several reasoning modules that are crucial to utilize in order to solve the given task:\n", "\n", "All reasoning module descriptions:\n", @@ -71,34 +34,8 @@ "Task: \u001b[33;1m\u001b[1;3m{task_description}\u001b[0m\n", "\n", "Select several modules are crucial for solving the task above:\n", - "\n" - ] - } - ], - "source": [ - "select_prompt.pretty_print()" - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "id": "26eaa6bc-5202-4b22-9522-33f227c8eb55", - "metadata": {}, - "outputs": [], - "source": [ - "adapt_prompt = hub.pull(\"hwchase17/self-discovery-adapt\")" - ] - }, - { - "cell_type": "code", - "execution_count": 41, - "id": "dc30afb9-180d-417b-9935-f7ef166710b8", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ + "\n", + "Self-Discovery Select Response:\n", "Rephrase and specify each reasoning module so that it better helps solving the task:\n", "\n", "SELECTED module descriptions:\n", @@ -107,34 +44,8 @@ "Task: \u001b[33;1m\u001b[1;3m{task_description}\u001b[0m\n", "\n", "Adapt each reasoning module description to better solve the task:\n", - "\n" - ] - } - ], - "source": [ - "adapt_prompt.pretty_print()" - ] - }, - { - "cell_type": "code", - "execution_count": 28, - "id": "a93253a9-8f50-49dd-8815-c3927bae1905", - "metadata": {}, - "outputs": [], - "source": [ - "structured_prompt = hub.pull(\"hwchase17/self-discovery-structure\")" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "id": "8ea8dd78-4285-400b-83d2-c4a241903a79", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ + "\n", + "Self-Discovery Structured Prompt:\n", "Operationalize the reasoning modules into a step-by-step reasoning plan in JSON format:\n", "\n", "Here's an example:\n", @@ -159,34 +70,8 @@ "\n", "Implement a reasoning structure for solvers to follow step-by-step and arrive at correct answer.\n", "\n", - "Note: do NOT actually arrive at a conclusion in this pass. Your job is to generate a PLAN so that in the future you can fill it out and arrive at the correct conclusion for tasks like this\n" - ] - } - ], - "source": [ - "structured_prompt.pretty_print()" - ] - }, - { - "cell_type": "code", - "execution_count": 45, - "id": "f3d4d79d-f414-4588-b476-4a35b3ba6fbf", - "metadata": {}, - "outputs": [], - "source": [ - "reasoning_prompt = hub.pull(\"hwchase17/self-discovery-reasoning\")" - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "id": "23d1e32e-d12e-454a-8484-c08e250e3262", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ + "Note: do NOT actually arrive at a conclusion in this pass. Your job is to generate a PLAN so that in the future you can fill it out and arrive at the correct conclusion for tasks like this\n", + "Self-Discovery Structured Response:\n", "Follow the step-by-step reasoning plan in JSON to correctly solve the task. Fill in the values following the keys by reasoning specifically about the task given. Do not simply rephrase the keys.\n", " \n", "Reasoning Structure:\n", @@ -197,163 +82,113 @@ } ], "source": [ + "from langchain import hub\n", + "\n", + "select_prompt = hub.pull(\"hwchase17/self-discovery-select\")\n", + "print(\"Self-Discovery Select Prompt:\")\n", + "select_prompt.pretty_print()\n", + "print(\"Self-Discovery Select Response:\")\n", + "adapt_prompt = hub.pull(\"hwchase17/self-discovery-adapt\")\n", + "adapt_prompt.pretty_print()\n", + "structured_prompt = hub.pull(\"hwchase17/self-discovery-structure\")\n", + "print(\"Self-Discovery Structured Prompt:\")\n", + "structured_prompt.pretty_print()\n", + "reasoning_prompt = hub.pull(\"hwchase17/self-discovery-reasoning\")\n", + "print(\"Self-Discovery Structured Response:\")\n", "reasoning_prompt.pretty_print()" ] }, { - "cell_type": "code", - "execution_count": 42, - "id": "7b9af01d-da28-4785-b069-efea61905cfa", + "cell_type": "markdown", + "id": "bce1135e", "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "PromptTemplate(input_variables=['reasoning_structure', 'task_instance'], template='Follow the step-by-step reasoning plan in JSON to correctly solve the task. Fill in the values following the keys by reasoning specifically about the task given. Do not simply rephrase the keys.\\n \\nReasoning Structure:\\n{reasoning_structure}\\n\\nTask: {task_instance}')" - ] - }, - "execution_count": 42, - "metadata": {}, - "output_type": "execute_result" - } - ], "source": [ - "reasoning_prompt" + "## Define the graph" ] }, { "cell_type": "code", - "execution_count": 43, - "id": "399bf160-e257-429f-b27e-66d4063f195f", - "metadata": {}, - "outputs": [], - "source": [ - "_prompt = \"\"\"Follow the step-by-step reasoning plan in JSON to correctly solve the task. Fill in the values following the keys by reasoning specifically about the task given. Do not simply rephrase the keys.\\n \\nReasoning Structure:\\n{reasoning_structure}\\n\\nTask: {task_description}\"\"\"\n", - "_prompt = PromptTemplate.from_template(_prompt)" - ] - }, - { - "cell_type": "code", - "execution_count": 44, - "id": "6e047e3a-b1c2-4a3b-abc7-eb84de6874e4", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'https://smith.langchain.com/hub/hwchase17/self-discovery-reasoning/48340707'" - ] - }, - "execution_count": 44, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "hub.push(\"hwchase17/self-discovery-reasoning\", _prompt)" - ] - }, - { - "cell_type": "code", - "execution_count": 72, - "id": "7f66d61e-6dcd-4462-b67b-29c5de56e238", + "execution_count": 2, + "id": "9f554045-6e79-42d3-be4b-835bbbd0b78c", "metadata": {}, "outputs": [], "source": [ + "from typing import Optional, TypedDict\n", + "\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_openai import ChatOpenAI\n", + "\n", + "from langgraph.graph import END, START, StateGraph\n", + "\n", + "\n", "class SelfDiscoverState(TypedDict):\n", " reasoning_modules: str\n", " task_description: str\n", " selected_modules: Optional[str]\n", " adapted_modules: Optional[str]\n", " reasoning_structure: Optional[str]\n", - " answer: Optional[str]" - ] - }, - { - "cell_type": "code", - "execution_count": 73, - "id": "5c3bd203-7dc1-457e-813f-283aaf059ec0", - "metadata": {}, - "outputs": [], - "source": [ + " answer: Optional[str]\n", + "\n", + "\n", + "model = ChatOpenAI(temperature=0, model=\"gpt-4-turbo-preview\")\n", + "\n", + "\n", "def select(inputs):\n", " select_chain = select_prompt | model | StrOutputParser()\n", - " return {\"selected_modules\": select_chain.invoke(inputs)}" - ] - }, - { - "cell_type": "code", - "execution_count": 74, - "id": "86420da0-7cc2-4659-853e-9c3ef808e47c", - "metadata": {}, - "outputs": [], - "source": [ + " return {\"selected_modules\": select_chain.invoke(inputs)}\n", + "\n", + "\n", "def adapt(inputs):\n", " adapt_chain = adapt_prompt | model | StrOutputParser()\n", - " return {\"adapted_modules\": adapt_chain.invoke(inputs)}" - ] - }, - { - "cell_type": "code", - "execution_count": 75, - "id": "270a3905-58a3-4650-96ca-e8254040285f", - "metadata": {}, - "outputs": [], - "source": [ + " return {\"adapted_modules\": adapt_chain.invoke(inputs)}\n", + "\n", + "\n", "def structure(inputs):\n", " structure_chain = structured_prompt | model | StrOutputParser()\n", - " return {\"reasoning_structure\": structure_chain.invoke(inputs)}" - ] - }, - { - "cell_type": "code", - "execution_count": 76, - "id": "55b486cc-36be-497e-9eba-9c8dc228f2d1", - "metadata": {}, - "outputs": [], - "source": [ + " return {\"reasoning_structure\": structure_chain.invoke(inputs)}\n", + "\n", + "\n", "def reason(inputs):\n", " reasoning_chain = reasoning_prompt | model | StrOutputParser()\n", - " return {\"answer\": reasoning_chain.invoke(inputs)}" - ] - }, - { - "cell_type": "code", - "execution_count": 77, - "id": "d9e2e4ce-dc9d-45a5-a218-34ab4fb62bc4", - "metadata": {}, - "outputs": [], - "source": [ - "from langgraph.graph import StateGraph, END\n", - "from typing import TypedDict, Optional" - ] - }, - { - "cell_type": "code", - "execution_count": 79, - "id": "26276404-0134-40a0-a796-cc9927c153ee", - "metadata": {}, - "outputs": [], - "source": [ + " return {\"answer\": reasoning_chain.invoke(inputs)}\n", + "\n", + "\n", "graph = StateGraph(SelfDiscoverState)\n", - "graph.add_node(\"select\", select)\n", - "graph.add_node(\"adapt\", adapt)\n", - "graph.add_node(\"structure\", structure)\n", - "graph.add_node(\"reason\", reason)\n", + "graph.add_node(select)\n", + "graph.add_node(adapt)\n", + "graph.add_node(structure)\n", + "graph.add_node(reason)\n", + "graph.add_edge(START, \"select\")\n", "graph.add_edge(\"select\", \"adapt\")\n", "graph.add_edge(\"adapt\", \"structure\")\n", "graph.add_edge(\"structure\", \"reason\")\n", "graph.add_edge(\"reason\", END)\n", - "graph.set_entry_point(\"select\")\n", "app = graph.compile()" ] }, { - "cell_type": "code", - "execution_count": 80, + "cell_type": "markdown", "id": "29fe385b-cf5d-4581-80e7-55462f5628bb", "metadata": {}, - "outputs": [], + "source": [ + "## Invoke the graph" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "6cbfbe81-f751-42da-843a-f9003ace663d", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'select': {'selected_modules': 'To solve the task of identifying the shape drawn by the SVG path element, the following reasoning modules are crucial:\\n\\n1. **Critical Thinking (10):** This involves analyzing the provided SVG path commands to understand how they contribute to forming a shape. It requires questioning assumptions (e.g., not assuming the shape is simple or common) and evaluating the information given in the path data.\\n\\n2. **Creative Thinking (11):** While the task seems straightforward, creative thinking can help in visualizing the shape described by the path commands without immediately drawing it. This involves imagining the transitions and connections between the points defined in the path.\\n\\n3. **Systems Thinking (13):** Understanding the SVG path as a system of coordinates and lines that connect to form a shape. This includes recognizing the interconnectedness of the start and end points of each line segment and how they contribute to the overall shape.\\n\\n4. **Analytical Problem Solving (29):** This task requires data analysis skills to interpret the SVG path commands and deduce the shape they form. Analyzing the coordinates and the movements (lines and moves) can reveal the structure of the shape.\\n\\n5. **Design Challenge (30):** Interpreting and visualizing SVG paths can be seen as a design challenge, requiring an understanding of how individual parts (line segments) come together to create a whole (shape).\\n\\n6. **Step-by-Step Planning and Implementation (39):** Formulating a plan to sequentially interpret each segment of the SVG path and understanding how each segment contributes to the overall shape. This could involve sketching the path based on the commands to better visualize the shape.\\n\\nThese modules collectively enable a comprehensive approach to solving the task, from understanding and analyzing the SVG path data to creatively and systematically deducing the shape it represents.'}}\n", + "{'adapt': {'adapted_modules': \"To enhance the process of identifying the shape drawn by the SVG path element, the reasoning modules can be adapted and specified as follows:\\n\\n1. **Enhanced Critical Analysis (10):** This module focuses on a detailed examination of the SVG path commands, challenging initial perceptions and critically assessing each command's role in shaping the figure. It involves a deep dive into the syntax and semantics of the path data, ensuring no detail is overlooked, especially in recognizing less obvious or complex shapes.\\n\\n2. **Visual Creative Thinking (11):** Leveraging imagination to mentally construct the shape from the path commands, this module emphasizes the ability to visualize the sequential flow and connection of points without physical drawing. It encourages innovative approaches to mentally piecing together the described shape, enhancing the ability to predict the outcome based on abstract data.\\n\\n3. **Integrated Systems Analysis (13):** This module treats the SVG path as a complex system where each command and coordinate plays a critical role in the final shape. It focuses on understanding the relationship between individual path segments and their collective contribution to forming a coherent structure, emphasizing the holistic view of the path's construction.\\n\\n4. **Targeted Analytical Problem Solving (29):** Specializing in dissecting the SVG path's commands to systematically uncover the represented shape, this module applies precise analytical techniques to decode the sequence of movements and coordinates. It involves a methodical breakdown of the path data to reveal the underlying geometric figure.\\n\\n5. **Design Synthesis Challenge (30):** Approaching the task as a problem of synthesizing a coherent design from segmented inputs, this module requires an adept understanding of how discrete line segments interconnect to form a unified shape. It challenges one to think like a designer, piecing together the puzzle of path commands into a complete and recognizable form.\\n\\n6. **Sequential Interpretation and Visualization (39):** This module involves developing a step-by-step strategy for interpreting and visualizing the SVG path, focusing on the incremental construction of the shape from the path commands. It advocates for a systematic approach to translating the abstract commands into a tangible visual representation, potentially through sketching or mentally mapping the path's progression.\\n\\nBy refining these modules, the approach to solving the task becomes more targeted, enhancing the ability to accurately identify the shape described by the SVG path element.\"}}\n" + ] + } + ], "source": [ "reasoning_modules = [\n", " \"1. How could I devise an experiment to help solve that problem?\",\n", @@ -402,38 +237,10 @@ "\n", "task_example = \"\"\"This SVG path element draws a:\n", - "(A) circle (B) heptagon (C) hexagon (D) kite (E) line (F) octagon (G) pentagon(H) rectangle (I) sector (J) triangle\"\"\"" - ] - }, - { - "cell_type": "code", - "execution_count": 81, - "id": "6cbfbe81-f751-42da-843a-f9003ace663d", - "metadata": {}, - "outputs": [], - "source": [ - "reasoning_modules_str = \"\\n\".join(reasoning_modules)" - ] - }, - { - "cell_type": "code", - "execution_count": 82, - "id": "d411c7aa-7017-4d67-88b5-43b5d161c34c", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'select': {'selected_modules': \"To solve the task of identifying the shape drawn by the given SVG path element, the following reasoning modules are crucial:\\n\\n1. **Critical Thinking (10)**: This involves analyzing the SVG path commands and coordinates logically to understand the shape they form. It requires questioning assumptions (e.g., not assuming the shape based on a quick glance at the coordinates but rather analyzing the path commands) and evaluating the information given in the SVG path data.\\n\\n2. **Simplification (4)**: Simplifying the problem by breaking down the SVG path commands can make it easier to visualize and understand the shape being drawn. This might involve sketching the path based on the commands and coordinates or using a tool to render the SVG path.\\n\\n3. **Systems Thinking (13)**: Understanding the SVG path as part of a larger system (in this case, the SVG coordinate system and how path commands work) helps in comprehending how the individual commands come together to form a complete shape.\\n\\n4. **Analytical Problem Solving (29)**: This task requires data analysis skills to interpret the SVG path commands and coordinates. Understanding how 'M' (moveto), 'L' (lineto), and other commands work is essential for determining the shape.\\n\\n5. **Creative Thinking (11)**: While not as directly applicable as the other modules, creative thinking can aid in visualizing the shape that the path commands are intended to draw, especially if the shape is complex or if the path commands are not immediately clear.\\n\\n6. **Visualization (30)**: Although not explicitly listed, a module focused on visualization would be highly relevant here. Visualizing the path that the 'M' and 'L' commands create from the given coordinates can directly lead to identifying the shape.\\n\\nGiven the task's nature, modules focused on experimentation, risk analysis, stakeholder perspectives, and long-term implications (e.g., 1, 14, 21, 8) are less relevant. The task is primarily analytical and technical, requiring an understanding of SVG path syntax and geometry rather than broader problem-solving or decision-making strategies.\"}}\n", - "{'adapt': {'adapted_modules': \"1. **Detailed Path Analysis (10)**: This module focuses on a thorough examination of the SVG path commands and their corresponding coordinates to accurately deduce the shape they outline. It involves a critical approach where assumptions are set aside in favor of a detailed analysis of each command (e.g., 'M' for moveto, 'L' for lineto) and how these commands connect points in the SVG coordinate system to form a specific shape.\\n\\n2. **Path Decomposition (4)**: This involves breaking down the SVG path into more manageable segments or components to facilitate a clearer understanding of the overall shape. Techniques might include manually sketching the path as described by the commands and coordinates or utilizing digital tools to render the SVG path, thereby making the shape more apparent and easier to identify.\\n\\n3. **SVG System Analysis (13)**: Emphasizes the importance of understanding the SVG coordinate system and the functionality of path commands within this framework. This module is about seeing the SVG path not just as a series of commands but as part of the broader system of SVG graphics, where each command plays a specific role in shaping the final image.\\n\\n4. **Command Interpretation and Geometry (29)**: This module requires a deep dive into the syntax and semantics of SVG path commands, coupled with geometric reasoning to interpret the shape formed by these commands. Knowledge of how different commands like 'M' (moveto) and 'L' (lineto) contribute to the construction of geometric shapes is crucial for accurately identifying the shape in question.\\n\\n5. **Imaginative Visualization (11)**: While analytical skills are paramount, this module recognizes the role of creative thinking in visualizing the potential shapes that complex or ambiguous path commands might represent. It encourages thinking beyond the obvious and considering multiple geometric possibilities that fit the given path data.\\n\\n6. **Explicit Visualization (30)**: Directly focuses on the ability to visualize the trajectory formed by executing the SVG path commands, particularly 'M' and 'L'. This module is about using visualization techniques, whether mental or through software tools, to trace the path and see the resulting shape, thereby facilitating its identification.\\n\\nBy refining these modules to more directly address the task of interpreting SVG path elements, the process of identifying the drawn shape becomes more structured and focused on the specific skills and knowledge areas that are most relevant to the task.\"}}\n", - "{'structure': {'reasoning_structure': '```json\\n{\\n \"Step 1: Detailed Path Analysis\": {\\n \"Description\": \"Examine each SVG path command and its coordinates to understand the shape outline.\",\\n \"Actions\": [\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"55.57,80.69\",\\n \"Purpose\": \"Move to starting point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"57.38,65.80\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"57.38,65.80\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"48.90,57.46\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"48.90,57.46\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"45.58,47.78\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"45.58,47.78\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"53.25,36.07\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"66.29,48.90\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"78.69,61.09\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"55.57,80.69\",\\n \"Purpose\": \"Draw line to this point to close the shape.\"\\n }\\n ]\\n },\\n \"Step 2: Path Decomposition\": {\\n \"Description\": \"Break down the path into segments to simplify analysis.\",\\n \"Segments\": [\\n \"Segment 1: Move from (55.57,80.69) to (57.38,65.80)\",\\n \"Segment 2: Move from (57.38,65.80) to (48.90,57.46)\",\\n \"Segment 3: Move from (48.90,57.46) to (45.58,47.78)\",\\n \"Segment 4: Move from (45.58,47.78) to (53.25,36.07)\",\\n \"Segment 5: Move from (53.25,36.07) to (66.29,48.90)\",\\n \"Segment 6: Move from (66.29,48.90) to (78.69,61.09)\",\\n \"Segment 7: Move from (78.69,61.09) to (55.57,80.69)\"\\n ]\\n },\\n \"Step 3: SVG System Analysis\": {\\n \"Description\": \"Understand the role of each command within the SVG coordinate system.\",\\n \"Analysis\": [\\n {\\n \"Command\": \"M\",\\n \"Role\": \"Defines starting points for new sub-paths.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Role\": \"Creates straight lines between points.\"\\n }\\n ]\\n },\\n \"Step 4: Command Interpretation and Geometry\": {\\n \"Description\": \"Interpret the geometric shape formed by the path commands.\",\\n \"Geometric Principles\": [\\n \"Identify angles and lines created by \\'L\\' commands.\",\\n \"Determine the number of sides from the number of \\'L\\' commands.\"\\n ]\\n },\\n \"Step 5: Imaginative Visualization\": {\\n \"Description\": \"Visualize potential shapes that the path commands might represent.\",\\n \"Visualization Techniques\": [\\n \"Sketching the path based on command coordinates.\",\\n \"Mentally visualizing the path progression.\"\\n ]\\n },\\n \"Step 6: Explicit Visualization\": {\\n \"Description\": \"Use visualization tools to trace the path and see the resulting shape.\",\\n \"Tools\": [\\n \"Digital drawing software\",\\n \"SVG rendering tools\"\\n ]\\n },\\n \"Conclusion\": {\\n \"Description\": \"Based on the analysis and visualization, identify the shape.\",\\n \"Options\": [\\n \"Circle\",\\n \"Heptagon\",\\n \"Hexagon\",\\n \"Kite\",\\n \"Line\",\\n \"Octagon\",\\n \"Pentagon\",\\n \"Rectangle\",\\n \"Sector\",\\n \"Triangle\"\\n ],\\n \"Selected Option\": \"\"\\n }\\n}\\n```'}}\n", - "{'reason': {'answer': '```json\\n{\\n \"Step 1: Detailed Path Analysis\": {\\n \"Description\": \"Examine each SVG path command and its coordinates to understand the shape outline.\",\\n \"Actions\": [\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"55.57,80.69\",\\n \"Purpose\": \"Move to starting point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"57.38,65.80\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"57.38,65.80\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"48.90,57.46\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"48.90,57.46\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"45.58,47.78\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"45.58,47.78\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"53.25,36.07\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"66.29,48.90\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"78.69,61.09\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"55.57,80.69\",\\n \"Purpose\": \"Draw line to this point to close the shape.\"\\n }\\n ]\\n },\\n \"Step 2: Path Decomposition\": {\\n \"Description\": \"Break down the path into segments to simplify analysis.\",\\n \"Segments\": [\\n \"Segment 1: Move from (55.57,80.69) to (57.38,65.80)\",\\n \"Segment 2: Move from (57.38,65.80) to (48.90,57.46)\",\\n \"Segment 3: Move from (48.90,57.46) to (45.58,47.78)\",\\n \"Segment 4: Move from (45.58,47.78) to (53.25,36.07)\",\\n \"Segment 5: Move from (53.25,36.07) to (66.29,48.90)\",\\n \"Segment 6: Move from (66.29,48.90) to (78.69,61.09)\",\\n \"Segment 7: Move from (78.69,61.09) to (55.57,80.69)\"\\n ]\\n },\\n \"Step 3: SVG System Analysis\": {\\n \"Description\": \"Understand the role of each command within the SVG coordinate system.\",\\n \"Analysis\": [\\n {\\n \"Command\": \"M\",\\n \"Role\": \"Defines starting points for new sub-paths.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Role\": \"Creates straight lines between points.\"\\n }\\n ]\\n },\\n \"Step 4: Command Interpretation and Geometry\": {\\n \"Description\": \"Interpret the geometric shape formed by the path commands.\",\\n \"Geometric Principles\": [\\n \"Identify angles and lines created by \\'L\\' commands.\",\\n \"Determine the number of sides from the number of \\'L\\' commands.\"\\n ]\\n },\\n \"Step 5: Imaginative Visualization\": {\\n \"Description\": \"Visualize potential shapes that the path commands might represent.\",\\n \"Visualization Techniques\": [\\n \"Sketching the path based on command coordinates.\",\\n \"Mentally visualizing the path progression.\"\\n ]\\n },\\n \"Step 6: Explicit Visualization\": {\\n \"Description\": \"Use visualization tools to trace the path and see the resulting shape.\",\\n \"Tools\": [\\n \"Digital drawing software\",\\n \"SVG rendering tools\"\\n ]\\n },\\n \"Conclusion\": {\\n \"Description\": \"Based on the analysis and visualization, identify the shape.\",\\n \"Options\": [\\n \"Circle\",\\n \"Heptagon\",\\n \"Hexagon\",\\n \"Kite\",\\n \"Line\",\\n \"Octagon\",\\n \"Pentagon\",\\n \"Rectangle\",\\n \"Sector\",\\n \"Triangle\"\\n ],\\n \"Selected Option\": \"Pentagon\"\\n }\\n}\\n```'}}\n", - "{'__end__': {'reasoning_modules': '1. How could I devise an experiment to help solve that problem?\\n2. Make a list of ideas for solving this problem, and apply them one by one to the problem to see if any progress can be made.\\n4. How can I simplify the problem so that it is easier to solve?\\n5. What are the key assumptions underlying this problem?\\n6. What are the potential risks and drawbacks of each solution?\\n7. What are the alternative perspectives or viewpoints on this problem?\\n8. What are the long-term implications of this problem and its solutions?\\n9. How can I break down this problem into smaller, more manageable parts?\\n10. Critical Thinking: This style involves analyzing the problem from different perspectives, questioning assumptions, and evaluating the evidence or information available. It focuses on logical reasoning, evidence-based decision-making, and identifying potential biases or flaws in thinking.\\n11. Try creative thinking, generate innovative and out-of-the-box ideas to solve the problem. Explore unconventional solutions, thinking beyond traditional boundaries, and encouraging imagination and originality.\\n13. Use systems thinking: Consider the problem as part of a larger system and understanding the interconnectedness of various elements. Focuses on identifying the underlying causes, feedback loops, and interdependencies that influence the problem, and developing holistic solutions that address the system as a whole.\\n14. Use Risk Analysis: Evaluate potential risks, uncertainties, and tradeoffs associated with different solutions or approaches to a problem. Emphasize assessing the potential consequences and likelihood of success or failure, and making informed decisions based on a balanced analysis of risks and benefits.\\n16. What is the core issue or problem that needs to be addressed?\\n17. What are the underlying causes or factors contributing to the problem?\\n18. Are there any potential solutions or strategies that have been tried before? If yes, what were the outcomes and lessons learned?\\n19. What are the potential obstacles or challenges that might arise in solving this problem?\\n20. Are there any relevant data or information that can provide insights into the problem? If yes, what data sources are available, and how can they be analyzed?\\n21. Are there any stakeholders or individuals who are directly affected by the problem? What are their perspectives and needs?\\n22. What resources (financial, human, technological, etc.) are needed to tackle the problem effectively?\\n23. How can progress or success in solving the problem be measured or evaluated?\\n24. What indicators or metrics can be used?\\n25. Is the problem a technical or practical one that requires a specific expertise or skill set? Or is it more of a conceptual or theoretical problem?\\n26. Does the problem involve a physical constraint, such as limited resources, infrastructure, or space?\\n27. Is the problem related to human behavior, such as a social, cultural, or psychological issue?\\n28. Does the problem involve decision-making or planning, where choices need to be made under uncertainty or with competing objectives?\\n29. Is the problem an analytical one that requires data analysis, modeling, or optimization techniques?\\n30. Is the problem a design challenge that requires creative solutions and innovation?\\n31. Does the problem require addressing systemic or structural issues rather than just individual instances?\\n32. Is the problem time-sensitive or urgent, requiring immediate attention and action?\\n33. What kinds of solution typically are produced for this kind of problem specification?\\n34. Given the problem specification and the current best solution, have a guess about other possible solutions.35. Let’s imagine the current best solution is totally wrong, what other ways are there to think about the problem specification?36. What is the best way to modify this current best solution, given what you know about these kinds of problem specification?37. Ignoring the current best solution, create an entirely new solution to the problem.39. Let’s make a step by step plan and implement it with good notation and explanation.', 'task_description': 'This SVG path element draws a:\\n(A) circle (B) heptagon (C) hexagon (D) kite (E) line (F) octagon (G) pentagon(H) rectangle (I) sector (J) triangle', 'selected_modules': \"To solve the task of identifying the shape drawn by the given SVG path element, the following reasoning modules are crucial:\\n\\n1. **Critical Thinking (10)**: This involves analyzing the SVG path commands and coordinates logically to understand the shape they form. It requires questioning assumptions (e.g., not assuming the shape based on a quick glance at the coordinates but rather analyzing the path commands) and evaluating the information given in the SVG path data.\\n\\n2. **Simplification (4)**: Simplifying the problem by breaking down the SVG path commands can make it easier to visualize and understand the shape being drawn. This might involve sketching the path based on the commands and coordinates or using a tool to render the SVG path.\\n\\n3. **Systems Thinking (13)**: Understanding the SVG path as part of a larger system (in this case, the SVG coordinate system and how path commands work) helps in comprehending how the individual commands come together to form a complete shape.\\n\\n4. **Analytical Problem Solving (29)**: This task requires data analysis skills to interpret the SVG path commands and coordinates. Understanding how 'M' (moveto), 'L' (lineto), and other commands work is essential for determining the shape.\\n\\n5. **Creative Thinking (11)**: While not as directly applicable as the other modules, creative thinking can aid in visualizing the shape that the path commands are intended to draw, especially if the shape is complex or if the path commands are not immediately clear.\\n\\n6. **Visualization (30)**: Although not explicitly listed, a module focused on visualization would be highly relevant here. Visualizing the path that the 'M' and 'L' commands create from the given coordinates can directly lead to identifying the shape.\\n\\nGiven the task's nature, modules focused on experimentation, risk analysis, stakeholder perspectives, and long-term implications (e.g., 1, 14, 21, 8) are less relevant. The task is primarily analytical and technical, requiring an understanding of SVG path syntax and geometry rather than broader problem-solving or decision-making strategies.\", 'adapted_modules': \"1. **Detailed Path Analysis (10)**: This module focuses on a thorough examination of the SVG path commands and their corresponding coordinates to accurately deduce the shape they outline. It involves a critical approach where assumptions are set aside in favor of a detailed analysis of each command (e.g., 'M' for moveto, 'L' for lineto) and how these commands connect points in the SVG coordinate system to form a specific shape.\\n\\n2. **Path Decomposition (4)**: This involves breaking down the SVG path into more manageable segments or components to facilitate a clearer understanding of the overall shape. Techniques might include manually sketching the path as described by the commands and coordinates or utilizing digital tools to render the SVG path, thereby making the shape more apparent and easier to identify.\\n\\n3. **SVG System Analysis (13)**: Emphasizes the importance of understanding the SVG coordinate system and the functionality of path commands within this framework. This module is about seeing the SVG path not just as a series of commands but as part of the broader system of SVG graphics, where each command plays a specific role in shaping the final image.\\n\\n4. **Command Interpretation and Geometry (29)**: This module requires a deep dive into the syntax and semantics of SVG path commands, coupled with geometric reasoning to interpret the shape formed by these commands. Knowledge of how different commands like 'M' (moveto) and 'L' (lineto) contribute to the construction of geometric shapes is crucial for accurately identifying the shape in question.\\n\\n5. **Imaginative Visualization (11)**: While analytical skills are paramount, this module recognizes the role of creative thinking in visualizing the potential shapes that complex or ambiguous path commands might represent. It encourages thinking beyond the obvious and considering multiple geometric possibilities that fit the given path data.\\n\\n6. **Explicit Visualization (30)**: Directly focuses on the ability to visualize the trajectory formed by executing the SVG path commands, particularly 'M' and 'L'. This module is about using visualization techniques, whether mental or through software tools, to trace the path and see the resulting shape, thereby facilitating its identification.\\n\\nBy refining these modules to more directly address the task of interpreting SVG path elements, the process of identifying the drawn shape becomes more structured and focused on the specific skills and knowledge areas that are most relevant to the task.\", 'reasoning_structure': '```json\\n{\\n \"Step 1: Detailed Path Analysis\": {\\n \"Description\": \"Examine each SVG path command and its coordinates to understand the shape outline.\",\\n \"Actions\": [\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"55.57,80.69\",\\n \"Purpose\": \"Move to starting point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"57.38,65.80\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"57.38,65.80\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"48.90,57.46\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"48.90,57.46\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"45.58,47.78\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"45.58,47.78\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"53.25,36.07\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"66.29,48.90\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"78.69,61.09\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"55.57,80.69\",\\n \"Purpose\": \"Draw line to this point to close the shape.\"\\n }\\n ]\\n },\\n \"Step 2: Path Decomposition\": {\\n \"Description\": \"Break down the path into segments to simplify analysis.\",\\n \"Segments\": [\\n \"Segment 1: Move from (55.57,80.69) to (57.38,65.80)\",\\n \"Segment 2: Move from (57.38,65.80) to (48.90,57.46)\",\\n \"Segment 3: Move from (48.90,57.46) to (45.58,47.78)\",\\n \"Segment 4: Move from (45.58,47.78) to (53.25,36.07)\",\\n \"Segment 5: Move from (53.25,36.07) to (66.29,48.90)\",\\n \"Segment 6: Move from (66.29,48.90) to (78.69,61.09)\",\\n \"Segment 7: Move from (78.69,61.09) to (55.57,80.69)\"\\n ]\\n },\\n \"Step 3: SVG System Analysis\": {\\n \"Description\": \"Understand the role of each command within the SVG coordinate system.\",\\n \"Analysis\": [\\n {\\n \"Command\": \"M\",\\n \"Role\": \"Defines starting points for new sub-paths.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Role\": \"Creates straight lines between points.\"\\n }\\n ]\\n },\\n \"Step 4: Command Interpretation and Geometry\": {\\n \"Description\": \"Interpret the geometric shape formed by the path commands.\",\\n \"Geometric Principles\": [\\n \"Identify angles and lines created by \\'L\\' commands.\",\\n \"Determine the number of sides from the number of \\'L\\' commands.\"\\n ]\\n },\\n \"Step 5: Imaginative Visualization\": {\\n \"Description\": \"Visualize potential shapes that the path commands might represent.\",\\n \"Visualization Techniques\": [\\n \"Sketching the path based on command coordinates.\",\\n \"Mentally visualizing the path progression.\"\\n ]\\n },\\n \"Step 6: Explicit Visualization\": {\\n \"Description\": \"Use visualization tools to trace the path and see the resulting shape.\",\\n \"Tools\": [\\n \"Digital drawing software\",\\n \"SVG rendering tools\"\\n ]\\n },\\n \"Conclusion\": {\\n \"Description\": \"Based on the analysis and visualization, identify the shape.\",\\n \"Options\": [\\n \"Circle\",\\n \"Heptagon\",\\n \"Hexagon\",\\n \"Kite\",\\n \"Line\",\\n \"Octagon\",\\n \"Pentagon\",\\n \"Rectangle\",\\n \"Sector\",\\n \"Triangle\"\\n ],\\n \"Selected Option\": \"\"\\n }\\n}\\n```', 'answer': '```json\\n{\\n \"Step 1: Detailed Path Analysis\": {\\n \"Description\": \"Examine each SVG path command and its coordinates to understand the shape outline.\",\\n \"Actions\": [\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"55.57,80.69\",\\n \"Purpose\": \"Move to starting point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"57.38,65.80\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"57.38,65.80\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"48.90,57.46\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"48.90,57.46\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"45.58,47.78\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"M\",\\n \"Coordinate\": \"45.58,47.78\",\\n \"Purpose\": \"Move to this point without drawing.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"53.25,36.07\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"66.29,48.90\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"78.69,61.09\",\\n \"Purpose\": \"Draw line to this point.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Coordinate\": \"55.57,80.69\",\\n \"Purpose\": \"Draw line to this point to close the shape.\"\\n }\\n ]\\n },\\n \"Step 2: Path Decomposition\": {\\n \"Description\": \"Break down the path into segments to simplify analysis.\",\\n \"Segments\": [\\n \"Segment 1: Move from (55.57,80.69) to (57.38,65.80)\",\\n \"Segment 2: Move from (57.38,65.80) to (48.90,57.46)\",\\n \"Segment 3: Move from (48.90,57.46) to (45.58,47.78)\",\\n \"Segment 4: Move from (45.58,47.78) to (53.25,36.07)\",\\n \"Segment 5: Move from (53.25,36.07) to (66.29,48.90)\",\\n \"Segment 6: Move from (66.29,48.90) to (78.69,61.09)\",\\n \"Segment 7: Move from (78.69,61.09) to (55.57,80.69)\"\\n ]\\n },\\n \"Step 3: SVG System Analysis\": {\\n \"Description\": \"Understand the role of each command within the SVG coordinate system.\",\\n \"Analysis\": [\\n {\\n \"Command\": \"M\",\\n \"Role\": \"Defines starting points for new sub-paths.\"\\n },\\n {\\n \"Command\": \"L\",\\n \"Role\": \"Creates straight lines between points.\"\\n }\\n ]\\n },\\n \"Step 4: Command Interpretation and Geometry\": {\\n \"Description\": \"Interpret the geometric shape formed by the path commands.\",\\n \"Geometric Principles\": [\\n \"Identify angles and lines created by \\'L\\' commands.\",\\n \"Determine the number of sides from the number of \\'L\\' commands.\"\\n ]\\n },\\n \"Step 5: Imaginative Visualization\": {\\n \"Description\": \"Visualize potential shapes that the path commands might represent.\",\\n \"Visualization Techniques\": [\\n \"Sketching the path based on command coordinates.\",\\n \"Mentally visualizing the path progression.\"\\n ]\\n },\\n \"Step 6: Explicit Visualization\": {\\n \"Description\": \"Use visualization tools to trace the path and see the resulting shape.\",\\n \"Tools\": [\\n \"Digital drawing software\",\\n \"SVG rendering tools\"\\n ]\\n },\\n \"Conclusion\": {\\n \"Description\": \"Based on the analysis and visualization, identify the shape.\",\\n \"Options\": [\\n \"Circle\",\\n \"Heptagon\",\\n \"Hexagon\",\\n \"Kite\",\\n \"Line\",\\n \"Octagon\",\\n \"Pentagon\",\\n \"Rectangle\",\\n \"Sector\",\\n \"Triangle\"\\n ],\\n \"Selected Option\": \"Pentagon\"\\n }\\n}\\n```'}}\n" - ] - } - ], - "source": [ + "(A) circle (B) heptagon (C) hexagon (D) kite (E) line (F) octagon (G) pentagon(H) rectangle (I) sector (J) triangle\"\"\"\n", + "\n", + "reasoning_modules_str = \"\\n\".join(reasoning_modules)\n", + "\n", "for s in app.stream(\n", " {\"task_description\": task_example, \"reasoning_modules\": reasoning_modules_str}\n", "):\n", @@ -443,15 +250,7 @@ { "cell_type": "code", "execution_count": null, - "id": "ea8568d5-bdb6-45cd-8d04-1ab305786caa", - "metadata": {}, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "c14a291c-7c1b-43bc-807e-11180290985e", + "id": "20cac598", "metadata": {}, "outputs": [], "source": [] @@ -473,7 +272,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.1" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/examples/state-model.ipynb b/examples/state-model.ipynb index 01f993d6c..31b818821 100644 --- a/examples/state-model.ipynb +++ b/examples/state-model.ipynb @@ -52,8 +52,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -212,8 +212,9 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Sequence\n", "import operator\n", + "from typing import Annotated, Sequence\n", + "\n", "from langchain_core.messages import BaseMessage\n", "from langchain_core.pydantic_v1 import BaseModel\n", "\n", @@ -260,9 +261,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.prebuilt import ToolInvocation\n", "from langchain_core.messages import ToolMessage\n", "\n", + "from langgraph.prebuilt import ToolInvocation\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state):\n", @@ -323,7 +325,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(AgentState)\n", diff --git a/examples/storm/storm.ipynb b/examples/storm/storm.ipynb index 32c060627..49998a021 100644 --- a/examples/storm/storm.ipynb +++ b/examples/storm/storm.ipynb @@ -71,8 +71,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -104,7 +104,6 @@ "outputs": [], "source": [ "from langchain_openai import ChatOpenAI\n", - "from langchain_fireworks import ChatFireworks\n", "\n", "fast_llm = ChatOpenAI(model=\"gpt-3.5-turbo\")\n", "# Uncomment for a Fireworks model\n", @@ -137,9 +136,10 @@ } ], "source": [ - "from langchain_core.pydantic_v1 import BaseModel, Field\n", "from typing import List, Optional\n", + "\n", "from langchain_core.prompts import ChatPromptTemplate\n", + "from langchain_core.pydantic_v1 import BaseModel, Field\n", "\n", "direct_gen_outline_prompt = ChatPromptTemplate.from_messages(\n", " [\n", @@ -360,7 +360,8 @@ "outputs": [], "source": [ "from langchain_community.retrievers import WikipediaRetriever\n", - "from langchain_core.runnables import RunnableLambda, chain as as_runnable\n", + "from langchain_core.runnables import RunnableLambda\n", + "from langchain_core.runnables import chain as as_runnable\n", "\n", "wikipedia_retriever = WikipediaRetriever(load_all_available_meta=True, top_k_results=1)\n", "\n", @@ -455,10 +456,12 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", - "from typing_extensions import TypedDict\n", + "from typing import Annotated\n", + "\n", "from langchain_core.messages import AnyMessage\n", - "from typing import Annotated, Sequence\n", + "from typing_extensions import TypedDict\n", + "\n", + "from langgraph.graph import END, StateGraph\n", "\n", "\n", "def add_messages(left, right):\n", @@ -504,9 +507,8 @@ "metadata": {}, "outputs": [], "source": [ + "from langchain_core.messages import AIMessage, HumanMessage, ToolMessage\n", "from langchain_core.prompts import MessagesPlaceholder\n", - "from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage\n", - "\n", "\n", "gen_qn_prompt = ChatPromptTemplate.from_messages(\n", " [\n", @@ -692,7 +694,6 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_community.tools.tavily_search import TavilySearchResults\n", "from langchain_community.utilities.duckduckgo_search import DuckDuckGoSearchAPIWrapper\n", "from langchain_core.tools import tool\n", "\n", @@ -724,9 +725,10 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.runnables import RunnableConfig\n", "import json\n", "\n", + "from langchain_core.runnables import RunnableConfig\n", + "\n", "\n", "async def gen_answer(\n", " state: InterviewState,\n", @@ -1047,9 +1049,8 @@ "metadata": {}, "outputs": [], "source": [ - "from langchain_core.documents import Document\n", - "\n", "from langchain_community.vectorstores import SKLearnVectorStore\n", + "from langchain_core.documents import Document\n", "from langchain_openai import OpenAIEmbeddings\n", "\n", "embeddings = OpenAIEmbeddings(model=\"text-embedding-3-small\")\n", @@ -1565,7 +1566,7 @@ " {\n", " \"topic\": \"Groq, NVIDIA, Llamma.cpp and the future of LLM Inference\",\n", " },\n", - " config\n", + " config,\n", "):\n", " name = next(iter(step))\n", " print(name)\n", diff --git a/examples/streaming-tokens.ipynb b/examples/streaming-tokens.ipynb index bed3dbe4e..d91594246 100644 --- a/examples/streaming-tokens.ipynb +++ b/examples/streaming-tokens.ipynb @@ -63,8 +63,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -119,8 +119,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "# Add messages essentially does this with more\n", @@ -278,10 +280,12 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END, START\n", - "from langchain_core.runnables import RunnableConfig\n", "from typing import Literal\n", "\n", + "from langchain_core.runnables import RunnableConfig\n", + "\n", + "from langgraph.graph import END, START, StateGraph\n", + "\n", "\n", "# Define the function that determines whether to continue or not\n", "def should_continue(state: State) -> Literal[\"__end__\", \"tools\"]:\n", @@ -370,7 +374,6 @@ "source": [ "from IPython.display import Image, display\n", "\n", - "\n", "display(Image(app.get_graph().draw_mermaid_png()))" ] }, @@ -458,18 +461,31 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END, START\n", - "from langchain_core.runnables import RunnableGenerator\n", "from langchain_core.messages import AIMessage\n", + "from langchain_core.runnables import RunnableGenerator\n", + "\n", + "from langgraph.graph import START, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(State)\n", "\n", + "\n", "async def my_generator(state: State):\n", - " messages = [\"Four\", \"score\", \"and\", \"seven\", \"years\", \"ago\", \"our\", \"fathers\", \"...\"]\n", + " messages = [\n", + " \"Four\",\n", + " \"score\",\n", + " \"and\",\n", + " \"seven\",\n", + " \"years\",\n", + " \"ago\",\n", + " \"our\",\n", + " \"fathers\",\n", + " \"...\",\n", + " ]\n", " for message in messages:\n", " yield message\n", "\n", + "\n", "async def my_node(state: State, config: RunnableConfig):\n", " messages = []\n", " # Tagging a node makes it easy to filter out which events to include in your stream\n", diff --git a/examples/subgraph.ipynb b/examples/subgraph.ipynb index 826ac2c06..8df91b956 100644 --- a/examples/subgraph.ipynb +++ b/examples/subgraph.ipynb @@ -40,8 +40,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -70,10 +70,12 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", - "from langgraph.graph import StateGraph\n", "from typing import Annotated\n", "\n", + "from typing_extensions import TypedDict\n", + "\n", + "from langgraph.graph import StateGraph\n", + "\n", "\n", "def reduce_list(left: list | None, right: list | None) -> list:\n", " if not left:\n", diff --git a/examples/time-travel.ipynb b/examples/time-travel.ipynb index 4cc6a70c6..244f48681 100644 --- a/examples/time-travel.ipynb +++ b/examples/time-travel.ipynb @@ -66,8 +66,8 @@ "metadata": {}, "outputs": [], "source": [ - "import os\n", "import getpass\n", + "import os\n", "\n", "\n", "def _set_env(var: str):\n", @@ -114,8 +114,10 @@ "metadata": {}, "outputs": [], "source": [ - "from typing_extensions import TypedDict\n", "from typing import Annotated\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph.message import add_messages\n", "\n", "# `add_messages`` essentially does this\n", @@ -294,7 +296,7 @@ "metadata": {}, "outputs": [], "source": [ - "from langgraph.graph import StateGraph, END\n", + "from langgraph.graph import END, StateGraph\n", "\n", "# Define a new graph\n", "workflow = StateGraph(State)\n", @@ -403,7 +405,7 @@ "\n", "try:\n", " display(Image(app.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -1112,7 +1114,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.2" + "version": "3.12.2" } }, "nbformat": 4, diff --git a/examples/tnt-llm/img/tnt_llm.png b/examples/tutorials/tnt-llm/img/tnt_llm.png similarity index 100% rename from examples/tnt-llm/img/tnt_llm.png rename to examples/tutorials/tnt-llm/img/tnt_llm.png diff --git a/examples/tnt-llm/tnt-llm.ipynb b/examples/tutorials/tnt-llm/tnt-llm.ipynb similarity index 98% rename from examples/tnt-llm/tnt-llm.ipynb rename to examples/tutorials/tnt-llm/tnt-llm.ipynb index c98bf5598..e07a07840 100644 --- a/examples/tnt-llm/tnt-llm.ipynb +++ b/examples/tutorials/tnt-llm/tnt-llm.ipynb @@ -7,7 +7,7 @@ "source": [ "# TNT-LLM: Text Mining at Scale\n", "\n", - "[TNT-LLM](https://arxiv.org/abs/2403.12173) by Wan, et. al describes a taxonomy generation and classification system developed by Microsoft for their Bing Copilot application.\n", + "[TNT-LLM](https://arxiv.org/abs/2403.12173) by Wan, et. al describes a taxonomy generation and classification system developed by Microsoft for their Bing Copilot application.\n", "\n", "It generates a rich, interpretable taxonomy of user intents (or other categories) from raw conversation logs. This taxonomy can then be used downstream by LLMs to label logs, which in turn can be used as training data to adapt a cheap classifier (such as logistic regression classifier on embeddings) that can be deployed in your app.\n", "\n", @@ -17,7 +17,6 @@ "2. Label Training Data\n", "3. Finetune classifier + deploy\n", "\n", - "\n", "When applying LangGraph in this notebook, we will focus on the first phase: taxonomy generation (blue in the diagram below). We then show how to label and fit the classifier in subsequent steps below.\n", "\n", "![TNT LLM Diagram](./img/tnt_llm.png)\n", @@ -30,8 +29,7 @@ "4. **Update** the taxonomy on each subsequent minibatch via a ritique and revise prompt\n", "5. **Review** the final taxonomy, scoring its quality and generating a final value using a final sample.\n", "\n", - "\n", - "## Prerequisites" + "## Prerequisites\n" ] }, { @@ -77,7 +75,7 @@ "\n", "Since each node of a StateGraph accepts the state (and returns an updated state), we'll define that at the outset.\n", "\n", - "Our flow takes in a list of documents, batches them, and then generates and refines candidate taxonomies as interpretable \"clusters\"." + "Our flow takes in a list of documents, batches them, and then generates and refines candidate taxonomies as interpretable \"clusters\".\n" ] }, { @@ -119,7 +117,7 @@ "source": [ "#### 1. Summarize Docs\n", "\n", - "Chat logs can get quite long. Our taxonomy generation step needs to see large, diverse minibatches to be able to adequately capture the distribution of categories. To ensure they can all fit efficiently into the context window, we first summarize each chat log. Downstream steps will use these summaries instead of the raw doc content." + "Chat logs can get quite long. Our taxonomy generation step needs to see large, diverse minibatches to be able to adequately capture the distribution of categories. To ensure they can all fit efficiently into the context window, we first summarize each chat log. Downstream steps will use these summaries instead of the raw doc content.\n" ] }, { @@ -155,9 +153,7 @@ "\n", "\n", "summary_llm_chain = (\n", - " summary_prompt\n", - " | ChatAnthropic(model=\"claude-3-haiku-20240307\")\n", - " | StrOutputParser()\n", + " summary_prompt | ChatAnthropic(model=\"claude-3-haiku-20240307\") | StrOutputParser()\n", " # Customize the tracing name for easier organization\n", ").with_config(run_name=\"GenerateSummary\")\n", "summary_chain = summary_llm_chain | parse_summary\n", @@ -207,7 +203,7 @@ "source": [ "#### 2. Split into Minibatches\n", "\n", - "Each minibatch contains a random sample of docs. This lets the flow identify inadequacies in the current taxonomy using new data." + "Each minibatch contains a random sample of docs. This lets the flow identify inadequacies in the current taxonomy using new data.\n" ] }, { @@ -217,6 +213,9 @@ "metadata": {}, "outputs": [], "source": [ + "import random\n", + "\n", + "\n", "def get_minibatches(state: TaxonomyGenerationState, config: RunnableConfig):\n", " batch_size = config[\"configurable\"].get(\"batch_size\", 200)\n", " original = state[\"documents\"]\n", @@ -251,8 +250,7 @@ "source": [ "#### 3.a Taxonomy Generation Utilities\n", "\n", - "\n", - "This section of the graph is a generate -> update 🔄 -> review cycle. Each node shares a LOT of logic, which we have factored out into the shared functions below." + "This section of the graph is a generate -> update 🔄 -> review cycle. Each node shares a LOT of logic, which we have factored out into the shared functions below.\n" ] }, { @@ -262,7 +260,6 @@ "metadata": {}, "outputs": [], "source": [ - "import random\n", "from typing import Dict\n", "\n", "from langchain_core.runnables import Runnable\n", @@ -342,7 +339,7 @@ "id": "2e2a2723-d350-4871-83e8-88f081ab4c8b", "metadata": {}, "source": [ - "#### 3. Generate initial taxonomy" + "#### 3. Generate initial taxonomy\n" ] }, { @@ -387,7 +384,7 @@ "source": [ "#### 4. Update Taxonomy\n", "\n", - "This is a \"critique -> revise\" step that is repeated N times." + "This is a \"critique -> revise\" step that is repeated N times.\n" ] }, { @@ -423,7 +420,7 @@ "source": [ "#### 5. Review Taxonomy\n", "\n", - "This runs once we've processed all the minibatches." + "This runs once we've processed all the minibatches.\n" ] }, { @@ -462,7 +459,7 @@ "source": [ "## Define the Graph\n", "\n", - "With all the functionality defined, we can define the graph!" + "With all the functionality defined, we can define the graph!\n" ] }, { @@ -537,11 +534,11 @@ "source": [ "## Usage\n", "\n", - "The docs can contain __any__ content, but we've found it works really well on chat bot logs, such as those captured by [LangSmith](https://smith.langchain.com).\n", + "The docs can contain **any** content, but we've found it works really well on chat bot logs, such as those captured by [LangSmith](https://smith.langchain.com).\n", "\n", "We will use that as an example below. Update the `project_name` to your own LangSmith project.\n", "\n", - "You will likely have to customize the `run_to_doc` function below, since your expected keys may differ from those of this notebook's author." + "You will likely have to customize the `run_to_doc` function below, since your expected keys may differ from those of this notebook's author.\n" ] }, { @@ -551,7 +548,6 @@ "metadata": {}, "outputs": [], "source": [ - "import random\n", "from datetime import datetime, timedelta\n", "\n", "from langsmith import Client\n", @@ -604,7 +600,7 @@ "source": [ "#### Invoke\n", "\n", - "Now convert the runs to docs and kick off your graph flow. This will take some time! The summary step takes the longest. If you want to speed things up, you could try splitting the load across model providers." + "Now convert the runs to docs and kick off your graph flow. This will take some time! The summary step takes the longest. If you want to speed things up, you could try splitting the load across model providers.\n" ] }, { @@ -614,11 +610,12 @@ "metadata": {}, "outputs": [], "source": [ - "# Optional. If you are running into errors or rate limits and want to avoid repeated computation,\n", - "# you can set this while debugging\n", "from langchain.cache import InMemoryCache\n", "from langchain.globals import set_llm_cache\n", "\n", + "# Optional. If you are running into errors or rate limits and want to avoid repeated computation,\n", + "# you can set this while debugging\n", + "\n", "set_llm_cache(InMemoryCache())" ] }, @@ -638,31 +635,28 @@ " \" that would benefit the user.\"\n", ")\n", "\n", - "from langchain_core.tracers.context import tracing_v2_enabled\n", - "\n", - "with tracing_v2_enabled(client=Client(api_key=\"ls__eaec6db115fe4ad2af4fdf26fa553645\")):\n", - " stream = app.stream(\n", - " {\"documents\": docs},\n", - " {\n", - " \"configurable\": {\n", - " \"use_case\": use_case,\n", - " # Optional:\n", - " \"batch_size\": 400,\n", - " \"suggestion_length\": 30,\n", - " \"cluster_name_length\": 10,\n", - " \"cluster_description_length\": 30,\n", - " \"explanation_length\": 20,\n", - " \"max_num_clusters\": 25,\n", - " },\n", - " # We batch summarize the docs. To avoid getting errors, we will limit the\n", - " # degree of parallelism to permit.\n", - " \"max_concurrency\": 2,\n", + "stream = app.stream(\n", + " {\"documents\": docs},\n", + " {\n", + " \"configurable\": {\n", + " \"use_case\": use_case,\n", + " # Optional:\n", + " \"batch_size\": 400,\n", + " \"suggestion_length\": 30,\n", + " \"cluster_name_length\": 10,\n", + " \"cluster_description_length\": 30,\n", + " \"explanation_length\": 20,\n", + " \"max_num_clusters\": 25,\n", " },\n", - " )\n", + " # We batch summarize the docs. To avoid getting errors, we will limit the\n", + " # degree of parallelism to permit.\n", + " \"max_concurrency\": 2,\n", + " },\n", + ")\n", "\n", - " for step in stream:\n", - " node, state = next(iter(step.items()))\n", - " print(node, str(state)[:20] + \" ...\")" + "for step in stream:\n", + " node, state = next(iter(step.items()))\n", + " print(node, str(state)[:20] + \" ...\")" ] }, { @@ -672,7 +666,7 @@ "source": [ "## Final Result\n", "\n", - "Below, render the final result as markdown:" + "Below, render the final result as markdown:\n" ] }, { @@ -724,6 +718,9 @@ } ], "source": [ + "from IPython.display import Markdown\n", + "\n", + "\n", "def format_taxonomy_md(clusters):\n", " md = \"## Final Taxonomy\\n\\n\"\n", " md += \"| ID | Name | Description |\\n\"\n", @@ -743,8 +740,6 @@ " return md\n", "\n", "\n", - "from IPython.display import Markdown\n", - "\n", "Markdown(format_taxonomy_md(step[\"__end__\"][\"clusters\"][-1]))" ] }, @@ -761,13 +756,13 @@ "\n", "The problem is that LLM-based tagging can be expensive.\n", "\n", - "Embeddings can be ~100x cheaper to compute, and a simple logistic regression classifier on top of that would add negligible cost. \n", + "Embeddings can be ~100x cheaper to compute, and a simple logistic regression classifier on top of that would add negligible cost.\n", "\n", "Let's tag and train a classifier!\n", "\n", "#### Label Training Data\n", "\n", - "Use an LLM to label the data in a fully-automated fashion. For beter accuracy, you can sample a portion of the results to label by hand as well to verify the quality." + "Use an LLM to label the data in a fully-automated fashion. For beter accuracy, you can sample a portion of the results to label by hand as well to verify the quality.\n" ] }, { @@ -860,7 +855,7 @@ "source": [ "#### Train Classifier\n", "\n", - "Now that we've extracted the features from the text, we can generate the classifier on them." + "Now that we've extracted the features from the text, we can generate the classifier on them.\n" ] }, { @@ -883,9 +878,8 @@ "source": [ "import numpy as np\n", "from sklearn.linear_model import LogisticRegression\n", - "from sklearn.metrics import accuracy_score, f1_score, roc_auc_score\n", + "from sklearn.metrics import accuracy_score, f1_score\n", "from sklearn.model_selection import train_test_split\n", - "from sklearn.preprocessing import label_binarize\n", "from sklearn.utils import class_weight\n", "\n", "# Create a dictionary mapping category names to their indices in the taxonomy\n", @@ -934,7 +928,7 @@ "source": [ "## Phase 3: Deploy\n", "\n", - "Now that you have your classifier, you can easily deploy it and apply to future runs! All you need is to embed the input and apply your LogisticRegression classifier. Let's try it. We will use python's [joblib](https://joblib.readthedocs.io/en/stable/) library to serialize our sklearn classifier. Below is an example:" + "Now that you have your classifier, you can easily deploy it and apply to future runs! All you need is to embed the input and apply your LogisticRegression classifier. Let's try it. We will use python's [joblib](https://joblib.readthedocs.io/en/stable/) library to serialize our sklearn classifier. Below is an example:\n" ] }, { @@ -960,7 +954,7 @@ "source": [ "#### To deploy\n", "\n", - "When deploying, you can load the classifier and initialize your embeddings encoder. They fit together easily using LCEL:" + "When deploying, you can load the classifier and initialize your embeddings encoder. They fit together easily using LCEL:\n" ] }, { @@ -995,7 +989,7 @@ "source": [ "#### Example:\n", "\n", - "Assuming you've had some more data come in, you can fetch it and apply it below" + "Assuming you've had some more data come in, you can fetch it and apply it below\n" ] }, { @@ -1056,7 +1050,7 @@ "\n", "Congrats on implementing TNT-LLM! While most folks use clustering-based approachs like LDA, k-means, etc. it can often be hard to really interpret what each cluster represents. TNT-LLM generates human-interpretable labels you can use downstream to monitor and improve your application.\n", "\n", - "The technique also lends itself to hierarchical sub-categorizing: once you have the above taxonomy, use it to label your data, then on each sub-category, generate a new taxonomy using a similar technique to the one described above!" + "The technique also lends itself to hierarchical sub-categorizing: once you have the above taxonomy, use it to label your data, then on each sub-category, generate a new taxonomy using a similar technique to the one described above!\n" ] } ], diff --git a/examples/usaco/usaco.ipynb b/examples/usaco/usaco.ipynb index 68382db2c..aa9b55d70 100644 --- a/examples/usaco/usaco.ipynb +++ b/examples/usaco/usaco.ipynb @@ -168,7 +168,7 @@ " except subprocess.TimeoutExpired:\n", " process.kill()\n", " q.put(\"timed out\")\n", - " except Exception as e:\n", + " except Exception:\n", " q.put(f\"failed: {traceback.format_exc()}\")\n", "\n", "\n", @@ -266,7 +266,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Optional\n", + "from typing import Annotated\n", "\n", "from typing_extensions import TypedDict\n", "\n", @@ -483,7 +483,6 @@ "\n", "def evaluate(state: State):\n", " test_cases = state[\"test_cases\"]\n", - " runtime_limit = state[\"runtime_limit\"]\n", " ai_message: AIMessage = state[\"messages\"][-1]\n", " if not ai_message.tool_calls:\n", " return {\n", @@ -586,7 +585,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -736,7 +735,6 @@ } ], "source": [ - "from langchain_core.messages import BaseMessage\n", "from langchain_core.tracers.context import tracing_v2_enabled\n", "from langsmith import Client\n", "\n", @@ -745,7 +743,7 @@ "def _hide_test_cases(inputs):\n", " copied = inputs.copy()\n", " # These are tens of MB in size. No need to send them up\n", - " copied[\"test_cases\"] = f\"...\"\n", + " copied[\"test_cases\"] = \"...\"\n", " return copied\n", "\n", "\n", @@ -830,7 +828,7 @@ "metadata": {}, "outputs": [], "source": [ - "from typing import Annotated, Optional\n", + "from typing import Annotated\n", "\n", "from typing_extensions import TypedDict\n", "\n", @@ -1059,7 +1057,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -1379,7 +1377,7 @@ "\n", "try:\n", " display(Image(graph.get_graph().draw_mermaid_png()))\n", - "except:\n", + "except Exception:\n", " # This requires some extra dependencies and is optional\n", " pass" ] @@ -1664,7 +1662,7 @@ " \"messages\": [\n", " (\n", " \"user\",\n", - " f\"\"\"Consider breaking down the algorithm into separate parts: reading inputs, detecting cycles using the tortoise and hare algorithm, and determining Bessie's final position by skipping ahead K steps.\n", + " \"\"\"Consider breaking down the algorithm into separate parts: reading inputs, detecting cycles using the tortoise and hare algorithm, and determining Bessie's final position by skipping ahead K steps.\n", "\n", "Read the inputs into three arrays:\n", "- Two arrays L and R for the ports (adjust for 0-based indexing)\n", diff --git a/examples/visualization.ipynb b/examples/visualization.ipynb index 1dafc6667..c20d0ea6d 100644 --- a/examples/visualization.ipynb +++ b/examples/visualization.ipynb @@ -39,10 +39,12 @@ "outputs": [], "source": [ "import random\n", + "from typing import Annotated, Literal\n", + "\n", + "from typing_extensions import TypedDict\n", + "\n", "from langgraph.graph import StateGraph\n", "from langgraph.graph.message import add_messages\n", - "from typing_extensions import TypedDict\n", - "from typing import Annotated, Literal\n", "\n", "\n", "class State(TypedDict):\n", @@ -265,8 +267,8 @@ } ], "source": [ - "from langchain_core.runnables.graph import CurveStyle, NodeColors, MermaidDrawMethod\n", - "from IPython.display import display, HTML, Image\n", + "from IPython.display import Image, display\n", + "from langchain_core.runnables.graph import CurveStyle, MermaidDrawMethod, NodeColors\n", "\n", "display(\n", " Image(\n", diff --git a/examples/web-navigation/web_voyager.ipynb b/examples/web-navigation/web_voyager.ipynb index 4c4937a0e..620d133bf 100644 --- a/examples/web-navigation/web_voyager.ipynb +++ b/examples/web-navigation/web_voyager.ipynb @@ -199,10 +199,10 @@ " bbox_id = int(bbox_id)\n", " try:\n", " bbox = state[\"bboxes\"][bbox_id]\n", - " except:\n", + " except Exception:\n", " return f\"Error: no bbox for : {bbox_id}\"\n", " x, y = bbox[\"x\"], bbox[\"y\"]\n", - " res = await page.mouse.click(x, y)\n", + " await page.mouse.click(x, y)\n", " # TODO: In the paper, they automatically parse any downloaded PDFs\n", " # We could add something similar here as well and generally\n", " # improve response format.\n", @@ -308,7 +308,6 @@ "metadata": {}, "outputs": [], "source": [ - "import asyncio\n", "import base64\n", "\n", "from langchain_core.runnables import chain as chain_decorator\n", @@ -327,7 +326,7 @@ " try:\n", " bboxes = await page.evaluate(\"markPage()\")\n", " break\n", - " except:\n", + " except Exception:\n", " # May be loading...\n", " asyncio.sleep(3)\n", " screenshot = await page.screenshot()\n", @@ -358,7 +357,6 @@ "source": [ "from langchain import hub\n", "from langchain_core.output_parsers import StrOutputParser\n", - "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n", "from langchain_core.runnables import RunnablePassthrough\n", "from langchain_openai import ChatOpenAI\n", "\n", @@ -470,6 +468,7 @@ "outputs": [], "source": [ "from langchain_core.runnables import RunnableLambda\n", + "\n", "from langgraph.graph import END, StateGraph\n", "\n", "graph_builder = StateGraph(AgentState)\n", @@ -538,7 +537,6 @@ "metadata": {}, "outputs": [], "source": [ - "import playwright\n", "from IPython import display\n", "from playwright.async_api import async_playwright\n", "\n", diff --git a/pyproject.toml b/pyproject.toml index 3b27e36f2..364c4f601 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,8 +35,19 @@ langchain-anthropic = ">=0.1.8" optional = true [tool.ruff] -select = [ "E", "F", "I" ] -ignore = [ "E501" ] +lint.select = [ "E", "F", "I" ] +lint.ignore = [ "E501" ] +line-length = 88 +indent-width = 4 +extend-include = ["*.ipynb"] + +[tool.ruff.format] +quote-style = "double" +indent-style = "space" +skip-magic-trailing-comma = false +line-ending = "auto" +docstring-code-format = false +docstring-code-line-length = "dynamic" [tool.mypy] ignore_missing_imports = "True"