mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-17 21:25:46 +02:00
Migrate langchain deps (#452)
This commit is contained in:
@@ -227,8 +227,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.adapters.openai import convert_message_to_dict\n",
|
||||
"from langchain_core.messages import AIMessage\n",
|
||||
"from langchain_community.adapters.openai import convert_message_to_dict\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def chat_bot_node(messages):\n",
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import functools
|
||||
from typing import Annotated, Any, Callable, Dict, List, Optional, Union
|
||||
|
||||
from langchain_community.adapters.openai import convert_message_to_dict
|
||||
from langchain_core.messages import AIMessage, AnyMessage, BaseMessage, HumanMessage
|
||||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
||||
from langchain_core.runnables import Runnable, RunnableLambda
|
||||
@@ -21,7 +22,6 @@ def langchain_to_openai_messages(messages: List[BaseMessage]):
|
||||
Returns:
|
||||
List[dict]: A list of openai messages.
|
||||
"""
|
||||
from langchain.adapters.openai import convert_message_to_dict # noqa: I001
|
||||
|
||||
return [
|
||||
convert_message_to_dict(m) if isinstance(m, BaseMessage) else m
|
||||
|
||||
@@ -323,9 +323,9 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from operator import itemgetter\n",
|
||||
"from langchain.prompts import PromptTemplate\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",
|
||||
|
||||
@@ -321,13 +321,13 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.chains import create_structured_output_runnable\n",
|
||||
"from langchain.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",
|
||||
"from langchain_core.output_parsers.openai_tools import (\n",
|
||||
" JsonOutputToolsParser,\n",
|
||||
" PydanticToolsParser,\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class Reflection(BaseModel):\n",
|
||||
|
||||
+32
-11
@@ -221,7 +221,7 @@
|
||||
"source": [
|
||||
"# Define the function that determines whether to continue or not\n",
|
||||
"def should_continue(state):\n",
|
||||
" last_message = state['messages'][-1]\n",
|
||||
" last_message = state[\"messages\"][-1]\n",
|
||||
" # If there is no function call, then we finish\n",
|
||||
" if not last_message.tool_calls:\n",
|
||||
" return \"end\"\n",
|
||||
@@ -253,12 +253,15 @@
|
||||
"from typing import TypedDict, Annotated\n",
|
||||
"from langchain_core.messages import AnyMessage, HumanMessage, SystemMessage\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"class BaseState(TypedDict):\n",
|
||||
" messages: Annotated[list[AnyMessage], add_messages]\n",
|
||||
" 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",
|
||||
@@ -271,29 +274,41 @@
|
||||
" return \"Tool Result: ...\"\n",
|
||||
" else:\n",
|
||||
" raise ValueError\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def _render_messages(ms):\n",
|
||||
" m_string = [_render_message(m) for m in ms]\n",
|
||||
" return \"\\n\".join(m_string)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Define a new graph\n",
|
||||
"workflow = StateGraph(BaseState)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def _agent(state: BaseState):\n",
|
||||
" if len(state['examples']) > 0:\n",
|
||||
" _examples = \"\\n\\n\".join([f\"Example {i}: \" + _render_messages(e['messages']) for i, e in enumerate(state['examples'])])\n",
|
||||
" if len(state[\"examples\"]) > 0:\n",
|
||||
" _examples = \"\\n\\n\".join(\n",
|
||||
" [\n",
|
||||
" f\"Example {i}: \" + _render_messages(e[\"messages\"])\n",
|
||||
" for i, e in enumerate(state[\"examples\"])\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
" system_message = \"\"\"You are a helpful assistant. Below are some examples of interactions you had with users. \\\n",
|
||||
"These were good interactions where the final result they got was the desired one. As much as possible, you should learn from these interactions and mimic them in the future. \\\n",
|
||||
"Pay particularly close attention to when tools are called, and what the inputs are.!\n",
|
||||
"\n",
|
||||
"{examples}\n",
|
||||
"\n",
|
||||
"Assist the user as they require!\"\"\".format(examples=_examples)\n",
|
||||
"Assist the user as they require!\"\"\".format(\n",
|
||||
" examples=_examples\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" else:\n",
|
||||
" system_message = \"\"\"You are a helpful assistant\"\"\"\n",
|
||||
" output = model.invoke([SystemMessage(content=system_message)] + state['messages'])\n",
|
||||
" output = model.invoke([SystemMessage(content=system_message)] + state[\"messages\"])\n",
|
||||
" return {\"messages\": [output]}\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Define the two nodes we will cycle between\n",
|
||||
"workflow.add_node(\"agent\", _agent)\n",
|
||||
"workflow.add_node(\"action\", tool_node)\n",
|
||||
@@ -360,7 +375,7 @@
|
||||
"# Finally, we compile it!\n",
|
||||
"# This compiles it into a LangChain Runnable,\n",
|
||||
"# meaning you can use it as you would any other runnable\n",
|
||||
"app = workflow.compile(checkpointer=memory, interrupt_before=['action'])"
|
||||
"app = workflow.compile(checkpointer=memory, interrupt_before=[\"action\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -422,8 +437,10 @@
|
||||
"source": [
|
||||
"from langchain_core.messages import HumanMessage\n",
|
||||
"\n",
|
||||
"thread = {\"configurable\": {\"thread_id\": '1'}}\n",
|
||||
"for event in app.stream({\"messages\": [HumanMessage(content=\"whats the weather in sf?\")]}, thread):\n",
|
||||
"thread = {\"configurable\": {\"thread_id\": \"1\"}}\n",
|
||||
"for event in app.stream(\n",
|
||||
" {\"messages\": [HumanMessage(content=\"whats the weather in sf?\")]}, thread\n",
|
||||
"):\n",
|
||||
" for v in event.values():\n",
|
||||
" print(v)"
|
||||
]
|
||||
@@ -458,7 +475,9 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"current_values.values['messages'][-1].tool_calls[0]['args']['query'] = \"weather in San Francisco, Accuweather\""
|
||||
"current_values.values[\"messages\"][-1].tool_calls[0][\"args\"][\n",
|
||||
" \"query\"\n",
|
||||
"] = \"weather in San Francisco, Accuweather\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -588,8 +607,10 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"thread = {\"configurable\": {\"thread_id\": '7'}}\n",
|
||||
"for event in app.stream({\"messages\": [HumanMessage(content=\"whats the weather in la?\")]}, thread):\n",
|
||||
"thread = {\"configurable\": {\"thread_id\": \"7\"}}\n",
|
||||
"for event in app.stream(\n",
|
||||
" {\"messages\": [HumanMessage(content=\"whats the weather in la?\")]}, thread\n",
|
||||
"):\n",
|
||||
" for v in event.values():\n",
|
||||
" print(v)"
|
||||
]
|
||||
|
||||
@@ -4,12 +4,12 @@ from typing import List, Optional
|
||||
|
||||
import numexpr
|
||||
from langchain.chains.openai_functions import create_structured_output_runnable
|
||||
from langchain_community.chat_models import ChatOpenAI
|
||||
from langchain_core.messages import SystemMessage
|
||||
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
||||
from langchain_core.pydantic_v1 import BaseModel, Field
|
||||
from langchain_core.runnables import RunnableConfig
|
||||
from langchain_core.tools import StructuredTool
|
||||
from langchain_openai import ChatOpenAI
|
||||
|
||||
_MATH_DESCRIPTION = (
|
||||
"math(problem: str, context: Optional[list[str]]) -> float:\n"
|
||||
|
||||
@@ -161,8 +161,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain.output_parsers.openai_functions import JsonOutputFunctionsParser\n",
|
||||
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
|
||||
"from langchain_core.output_parsers.openai_functions import JsonOutputFunctionsParser\n",
|
||||
"\n",
|
||||
"members = [\"Researcher\", \"Coder\"]\n",
|
||||
"system_prompt = (\n",
|
||||
|
||||
@@ -21,9 +21,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%capture --no-stderr\",\n",
|
||||
" \"%pip install\n",
|
||||
"%pip install -U --quiet langchain_community tiktoken langchain-openai langchainhub chromadb langchain langgraph"
|
||||
"%%capture --no-stderr\n",
|
||||
"%pip install -U --quiet langchain-community tiktoken langchain-openai langchainhub chromadb langchain langgraph langchain-text-splitters"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -66,10 +65,10 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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_openai import OpenAIEmbeddings\n",
|
||||
"from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
|
||||
"\n",
|
||||
"urls = [\n",
|
||||
" \"https://lilianweng.github.io/posts/2023-06-23-agent/\",\n",
|
||||
@@ -204,12 +203,12 @@
|
||||
"from typing import Annotated, Literal, Sequence, TypedDict\n",
|
||||
"\n",
|
||||
"from langchain import hub\n",
|
||||
"from langchain.prompts import PromptTemplate\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",
|
||||
"\n",
|
||||
"### Edges\n",
|
||||
"\n",
|
||||
|
||||
@@ -1,15 +1,5 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "8520d840-fcf6-4458-b85c-8a2ff80a34eb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! pip install -U langchain-nomic langchain_community tiktoken langchainhub chromadb langchain langgraph tavily-python gpt4all"
|
||||
]
|
||||
},
|
||||
{
|
||||
"attachments": {
|
||||
"7b00797e-fb85-4474-9a9e-c505b61add81.png": {
|
||||
@@ -50,16 +40,40 @@
|
||||
"\n",
|
||||
"Prompt - \n",
|
||||
"\n",
|
||||
"https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/\n",
|
||||
"https://llama.meta.com/docs/model-cards-and-prompt-formats/meta-llama-3/"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "21e597f9",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%capture --no-stderr\n",
|
||||
"%pip install -U langchain-nomic langchain_community tiktoken langchainhub chromadb langchain langgraph tavily-python gpt4all langchain-text-splitters"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "5bd42c79",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Tracing (optional)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "333cbcf4",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"### Tracing\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"### 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>\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>\""
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -83,10 +97,10 @@
|
||||
"source": [
|
||||
"### Index\n",
|
||||
"\n",
|
||||
"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_text_splitters import RecursiveCharacterTextSplitter\n",
|
||||
"\n",
|
||||
"urls = [\n",
|
||||
" \"https://lilianweng.github.io/posts/2023-06-23-agent/\",\n",
|
||||
@@ -128,9 +142,9 @@
|
||||
"source": [
|
||||
"### Retrieval Grader\n",
|
||||
"\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_community.chat_models import ChatOllama\n",
|
||||
"from langchain_core.output_parsers import JsonOutputParser\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"# LLM\n",
|
||||
"llm = ChatOllama(model=local_llm, format=\"json\", temperature=0)\n",
|
||||
@@ -172,9 +186,9 @@
|
||||
"source": [
|
||||
"### Generate\n",
|
||||
"\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain import hub\n",
|
||||
"from langchain_core.output_parsers import StrOutputParser\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
"prompt = PromptTemplate(\n",
|
||||
@@ -303,9 +317,9 @@
|
||||
"source": [
|
||||
"### Router\n",
|
||||
"\n",
|
||||
"from langchain.prompts import PromptTemplate\n",
|
||||
"from langchain_community.chat_models import ChatOllama\n",
|
||||
"from langchain_core.output_parsers import JsonOutputParser\n",
|
||||
"from langchain_core.prompts import PromptTemplate\n",
|
||||
"\n",
|
||||
"# LLM\n",
|
||||
"llm = ChatOllama(model=local_llm, format=\"json\", temperature=0)\n",
|
||||
@@ -358,6 +372,7 @@
|
||||
"source": [
|
||||
"from typing_extensions import TypedDict\n",
|
||||
"from typing import List\n",
|
||||
"from langchain_core.documents import Document\n",
|
||||
"\n",
|
||||
"### State\n",
|
||||
"\n",
|
||||
@@ -379,8 +394,6 @@
|
||||
" documents: List[str]\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"from langchain.schema import Document\n",
|
||||
"\n",
|
||||
"### Nodes\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -33,8 +33,8 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# %pip install -U --quiet langchain langgraph\n",
|
||||
"# %pip install -U --quiet tavily-python"
|
||||
"%pip install -U --quiet langgraph langchain-fireworks\n",
|
||||
"%pip install -U --quiet tavily-python"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -79,9 +79,9 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from langchain_community.chat_models.fireworks import ChatFireworks\n",
|
||||
"from langchain_core.messages import AIMessage, BaseMessage, HumanMessage\n",
|
||||
"from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
|
||||
"from langchain_fireworks import ChatFireworks\n",
|
||||
"\n",
|
||||
"prompt = ChatPromptTemplate.from_messages(\n",
|
||||
" [\n",
|
||||
|
||||
Reference in New Issue
Block a user