Skip to content

Commit

Permalink
update more notebooks for pydantic
Browse files Browse the repository at this point in the history
  • Loading branch information
vbarda committed Sep 12, 2024
1 parent 6f65852 commit 8781caa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
16 changes: 12 additions & 4 deletions docs/docs/how-tos/human_in_the_loop/wait-user-input.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -374,14 +374,22 @@
"model = ChatAnthropic(model=\"claude-3-5-sonnet-20240620\")\n",
"model = ChatOpenAI(model=\"gpt-4o\")\n",
"\n",
"# NOTE:\n",
"# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
"# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
"from langchain_core import __version__ as core_version\n",
"from packaging import version\n",
"\n",
"core_version = version.parse(core_version)\n",
"if (core_version.major, core_version.minor) < (0, 3):\n",
" from pydantic.v1 import BaseModel\n",
"else:\n",
" from pydantic import BaseModel\n",
"\n",
"# We are going \"bind\" all tools to the model\n",
"# We have the ACTUAL tools from above, but we also need a mock tool to ask a human\n",
"# Since `bind_tools` takes in tools but also just tool definitions,\n",
"# We can define a tool definition for `ask_human`\n",
"\n",
"from pydantic import BaseModel\n",
"\n",
"\n",
"class AskHuman(BaseModel):\n",
" \"\"\"Ask the human a question\"\"\"\n",
"\n",
Expand Down
13 changes: 12 additions & 1 deletion docs/docs/how-tos/many-tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,18 @@
"outputs": [],
"source": [
"from langchain_core.messages import HumanMessage, SystemMessage, ToolMessage\n",
"from pydantic import BaseModel, Field\n",
"\n",
"# NOTE:\n",
"# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
"# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
"from langchain_core import __version__ as core_version\n",
"from packaging import version\n",
"\n",
"core_version = version.parse(core_version)\n",
"if (core_version.major, core_version.minor) < (0, 3):\n",
" from pydantic.v1 import BaseModel, Field\n",
"else:\n",
" from pydantic import BaseModel, Field\n",
"\n",
"\n",
"class QueryForTools(BaseModel):\n",
Expand Down
13 changes: 12 additions & 1 deletion docs/docs/how-tos/map-reduce.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,23 @@
"import operator\n",
"from typing import Annotated, TypedDict\n",
"\n",
"from pydantic import BaseModel, Field\n",
"from langchain_anthropic import ChatAnthropic\n",
"\n",
"from langgraph.constants import Send\n",
"from langgraph.graph import END, StateGraph, START\n",
"\n",
"# NOTE:\n",
"# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
"# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
"from langchain_core import __version__ as core_version\n",
"from packaging import version\n",
"\n",
"core_version = version.parse(core_version)\n",
"if (core_version.major, core_version.minor) < (0, 3):\n",
" from pydantic.v1 import BaseModel, Field\n",
"else:\n",
" from pydantic import BaseModel, Field\n",
"\n",
"# Model and prompts\n",
"# Define model and prompts we will use\n",
"subjects_prompt = \"\"\"Generate a comma separated list of between 2 and 5 examples related to: {topic}.\"\"\"\n",
Expand Down
14 changes: 12 additions & 2 deletions docs/docs/how-tos/pass-run-time-values-to-tools.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,21 @@
"from typing_extensions import Annotated\n",
"\n",
"from langchain_core.documents import Document\n",
"from pydantic import BaseModel\n",
"from langchain_core.tools import tool\n",
"\n",
"from langgraph.prebuilt import InjectedState\n",
"\n",
"# NOTE:\n",
"# - if you're using langchain-core >= 0.3, you need to use pydantic v2\n",
"# - if you're using langchain-core >= 0.2,<0.3, you need to use pydantic v1\n",
"from langchain_core import __version__ as core_version\n",
"from packaging import version\n",
"\n",
"core_version = version.parse(core_version)\n",
"if (core_version.major, core_version.minor) < (0, 3):\n",
" from pydantic.v1 import BaseModel\n",
"else:\n",
" from pydantic import BaseModel\n",
"\n",
"\n",
"@tool(parse_docstring=True, response_format=\"content_and_artifact\")\n",
"def get_context(question: List[str]) -> Tuple[str, List[Document]]:\n",
Expand Down

0 comments on commit 8781caa

Please sign in to comment.