mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-22 23:52:23 +02:00
[Docs] Add ruff linting to .ipynb files (#645)
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
+20
-17
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
+8
-6
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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'] = <your-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\"] = \"<your-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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
},
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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",
|
||||
|
||||
+18
-18
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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,
|
||||
|
||||
+22
-22
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
+13
-12
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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}\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -60,9 +60,10 @@
|
||||
"source": [
|
||||
"### LLMs\n",
|
||||
"import os\n",
|
||||
"os.environ['OPENAI_API_KEY'] = <your-api-key>\n",
|
||||
"os.environ['COHERE_API_KEY'] = <your-api-key>\n",
|
||||
"os.environ['TAVILY_API_KEY'] = <your-api-key>"
|
||||
"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"<your-api-key>\"\n",
|
||||
"os.environ[\"COHERE_API_KEY\"] = \"<your-api-key>\"\n",
|
||||
"os.environ[\"TAVILY_API_KEY\"] = \"<your-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'] = <your-api-key>"
|
||||
"os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||
"os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n",
|
||||
"os.environ[\"LANGCHAIN_API_KEY\"] = \"<your-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",
|
||||
|
||||
@@ -68,7 +68,8 @@
|
||||
"source": [
|
||||
"### LLMs\n",
|
||||
"import os\n",
|
||||
"os.environ['COHERE_API_KEY'] = <your-api-key>"
|
||||
"\n",
|
||||
"os.environ[\"COHERE_API_KEY\"] = \"<your-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'] = <your-api-key>"
|
||||
"# os.environ['LANGCHAIN_API_KEY'] ='<your-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'] = <your-api-key>\n",
|
||||
"# os.environ['TAVILY_API_KEY'] ='<your-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",
|
||||
|
||||
@@ -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'] = <your-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\"] = \"<your-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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -67,7 +67,8 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"os.environ['OPENAI_API_KEY'] = <your-api-key>"
|
||||
"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"<your-api-key>\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -87,7 +88,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"os.environ['TAVILY_API_KEY'] = <your-api-key>"
|
||||
"os.environ[\"TAVILY_API_KEY\"] = \"<your-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'] = <your-api-key>"
|
||||
"os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||
"os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n",
|
||||
"os.environ[\"LANGCHAIN_API_KEY\"] = \"<your-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",
|
||||
|
||||
@@ -99,7 +99,7 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# If using Mistral API\n",
|
||||
"mistral_api_key = <your-api-key>"
|
||||
"mistral_api_key = \"<your-api-key>\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -120,7 +120,8 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"os.environ['TAVILY_API_KEY'] = <your-api-key>"
|
||||
"\n",
|
||||
"os.environ[\"TAVILY_API_KEY\"] = \"<your-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'] = <your-api-key>"
|
||||
"os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||
"os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n",
|
||||
"os.environ[\"LANGCHAIN_API_KEY\"] = \"<your-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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -79,7 +79,8 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"os.environ['OPENAI_API_KEY'] = <your-api-key>"
|
||||
"\n",
|
||||
"os.environ[\"OPENAI_API_KEY\"] = \"<your-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'] = <your-api-key>"
|
||||
"os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
|
||||
"os.environ[\"LANGCHAIN_ENDPOINT\"] = \"https://api.smith.langchain.com\"\n",
|
||||
"os.environ[\"LANGCHAIN_API_KEY\"] = \"<your-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",
|
||||
|
||||
@@ -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'] = <your-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\"] = \"<your-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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
]
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
File diff suppressed because one or more lines are too long
@@ -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",
|
||||
|
||||
+15
-14
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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,
|
||||
|
||||
|
Before Width: | Height: | Size: 501 KiB After Width: | Height: | Size: 501 KiB |
@@ -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",
|
||||
"\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"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
+13
-2
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user