mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-23 16:12:25 +02:00
Merge branch 'main' into eugene/langgraph_nav
This commit is contained in:
@@ -275,6 +275,13 @@ async def on_assistants(
|
||||
status_code=403,
|
||||
detail="User lacks the required permissions.",
|
||||
)
|
||||
|
||||
# Assumes you organize information in store like (user_id, resource_type, resource_id)
|
||||
@auth.on.store()
|
||||
async def authorize_store(ctx: Auth.types.AuthContext, value: dict):
|
||||
# The "namespace" field for each store item is a tuple you can think of as the directory of an item.
|
||||
namespace: tuple = value["namespace"]
|
||||
assert namespace[0] == ctx.user.identity, "Not authorized"
|
||||
```
|
||||
|
||||
Notice that instead of one global handler, we now have specific handlers for:
|
||||
|
||||
+1
-1
@@ -582,7 +582,7 @@
|
||||
")\n",
|
||||
"\n",
|
||||
"evaluator = prompt | ChatOpenAI(model=\"gpt-4-turbo-preview\").with_structured_output(\n",
|
||||
" RedTeamingResult\n",
|
||||
" RedTeamingResult, method=\"function_calling\"\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
|
||||
@@ -246,7 +246,7 @@
|
||||
"\n",
|
||||
"Define the (`fetch_user_flight_information`) tool to let the agent see the current user's flight information. Then define tools to search for flights and manage the passenger's bookings stored in the SQL database.\n",
|
||||
"\n",
|
||||
"We the can [access the RunnableConfig](https://python.langchain.com/docs/how_to/tool_configure/#inferring-by-parameter-type) for a given run to check the `passenger_id` of the user accessing this application. The LLM never has to provide these explicitly, they are provided for a given invocation of the graph so that each user cannot access other passengers' booking information.\n",
|
||||
"We then can [access the RunnableConfig](https://python.langchain.com/docs/how_to/tool_configure/#inferring-by-parameter-type) for a given run to check the `passenger_id` of the user accessing this application. The LLM never has to provide these explicitly, they are provided for a given invocation of the graph so that each user cannot access other passengers' booking information.\n",
|
||||
"\n",
|
||||
"<div class=\"admonition warning\">\n",
|
||||
" <p class=\"admonition-title\">Compatibility</p>\n",
|
||||
@@ -3423,7 +3423,7 @@
|
||||
"\n",
|
||||
"#### Utility\n",
|
||||
"\n",
|
||||
"Create a function to make an \"entry\" node for each workflow, stating \"the current assistant ix `assistant_name`\"."
|
||||
"Create a function to make an \"entry\" node for each workflow, stating \"the current assistant is `assistant_name`\"."
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -1032,7 +1032,9 @@
|
||||
") # You can optionally add examples\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4-turbo-preview\")\n",
|
||||
"\n",
|
||||
"runnable = joiner_prompt | llm.with_structured_output(JoinOutputs)"
|
||||
"runnable = joiner_prompt | llm.with_structured_output(\n",
|
||||
" JoinOutputs, method=\"function_calling\"\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -114,7 +114,9 @@ def get_math_tool(llm: ChatOpenAI):
|
||||
MessagesPlaceholder(variable_name="context", optional=True),
|
||||
]
|
||||
)
|
||||
extractor = prompt | llm.with_structured_output(ExecuteCode)
|
||||
extractor = prompt | llm.with_structured_output(
|
||||
ExecuteCode, method="function_calling"
|
||||
)
|
||||
|
||||
def calculate_expression(
|
||||
problem: str,
|
||||
|
||||
@@ -42,13 +42,13 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"id": "53d1a740-9fea-4a6e-8f95-fb9dbf1c80a1",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%capture --no-stderr\n",
|
||||
"! pip install -U langchain_community tiktoken langchain-openai langchain-cohere langchainhub chromadb langchain langgraph tavily-python"
|
||||
"%pip install -U langchain_community tiktoken langchain-openai langchain-cohere langchainhub chromadb langchain langgraph tavily-python"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -68,7 +68,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"_set_env(\"OPENAI_API_KEY\")\n",
|
||||
"_set_env(\"COHERE_API_KEY\")\n",
|
||||
"# _set_env(\"COHERE_API_KEY\")\n",
|
||||
"_set_env(\"TAVILY_API_KEY\")"
|
||||
]
|
||||
},
|
||||
@@ -95,7 +95,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": null,
|
||||
"id": "b224e5ba-50ca-495a-a7fa-0f75a080e03c",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -161,7 +161,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 4,
|
||||
"id": "4dec9d98-f3dc-4b7f-abc0-9d01c754f2be",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -196,7 +196,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"# LLM with function call\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"structured_llm_router = llm.with_structured_output(RouteQuery)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
@@ -221,7 +221,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"id": "856801cb-f42a-44e7-956f-47845e3664ca",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -229,7 +229,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"binary_score='no'\n"
|
||||
"binary_score='yes'\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -247,7 +247,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"# LLM with function call\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"structured_llm_grader = llm.with_structured_output(GradeDocuments)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
@@ -271,7 +271,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 6,
|
||||
"id": "2272333e-50b2-42ab-b472-e1055a3b94a8",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -279,7 +279,7 @@
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"The design of generative agents combines LLM with memory, planning, and reflection mechanisms to enable agents to behave based on past experience and interact with other agents. Memory stream is a long-term memory module that records agents' experiences in natural language. The retrieval model surfaces context to inform the agent's behavior based on relevance, recency, and importance.\n"
|
||||
"Agent memory in LLM-powered autonomous systems consists of short-term and long-term memory. Short-term memory utilizes in-context learning for immediate tasks, while long-term memory allows agents to retain and recall information over extended periods, often using external storage for efficient retrieval. This memory structure supports the agent's ability to reflect on past actions and improve future performance.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -293,7 +293,7 @@
|
||||
"prompt = hub.pull(\"rlm/rag-prompt\")\n",
|
||||
"\n",
|
||||
"# LLM\n",
|
||||
"llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model_name=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Post-processing\n",
|
||||
@@ -311,7 +311,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"id": "f0c08d14-77a0-4eed-b882-2d636abb22a3",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -321,7 +321,7 @@
|
||||
"GradeHallucinations(binary_score='yes')"
|
||||
]
|
||||
},
|
||||
"execution_count": 6,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -340,7 +340,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"# LLM with function call\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"structured_llm_grader = llm.with_structured_output(GradeHallucinations)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
@@ -359,7 +359,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 8,
|
||||
"id": "ded99680-437a-4c9d-b860-619c88949d84",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -369,7 +369,7 @@
|
||||
"GradeAnswer(binary_score='yes')"
|
||||
]
|
||||
},
|
||||
"execution_count": 7,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -388,7 +388,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"# LLM with function call\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"structured_llm_grader = llm.with_structured_output(GradeAnswer)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
@@ -407,17 +407,17 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 9,
|
||||
"id": "9d75f1d7-a47a-4577-bb0d-84b504b0867e",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"\"What is the role of memory in an agent's functioning?\""
|
||||
"'What are the key concepts and techniques related to agent memory in artificial intelligence?'"
|
||||
]
|
||||
},
|
||||
"execution_count": 8,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -426,7 +426,7 @@
|
||||
"### Question Re-writer\n",
|
||||
"\n",
|
||||
"# LLM\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
"system = \"\"\"You a question re-writer that converts an input question to a better version that is optimized \\n \n",
|
||||
@@ -455,7 +455,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 10,
|
||||
"id": "01d829bb-1074-4976-b650-ead41dcb9788",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -481,7 +481,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 11,
|
||||
"id": "e723fcdb-06e6-402d-912e-899795b78408",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -516,7 +516,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": 12,
|
||||
"id": "b76b5ec3-0720-443d-85b1-c0e79659ca0a",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -736,7 +736,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 13,
|
||||
"id": "67854e07-9293-4c3c-bf9a-bc9a605570ee",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -796,7 +796,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 14,
|
||||
"id": "29acc541-d726-4b75-84d1-a215845fe88a",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -816,11 +816,9 @@
|
||||
"---DECISION: GENERATION ADDRESSES QUESTION---\n",
|
||||
"\"Node 'generate':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('It is expected that the Chicago Bears could have the opportunity to draft '\n",
|
||||
" 'the first defensive player in the 2024 NFL draft. The Bears have the first '\n",
|
||||
" 'overall pick in the draft, giving them a prime position to select top '\n",
|
||||
" 'talent. The top wide receiver Marvin Harrison Jr. from Ohio State is also '\n",
|
||||
" 'mentioned as a potential pick for the Cardinals.')\n"
|
||||
"('The Chicago Bears are expected to draft quarterback Caleb Williams first '\n",
|
||||
" 'overall in the 2024 NFL Draft. They also have a second first-round pick, '\n",
|
||||
" 'where they selected wide receiver Rome Odunze.')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -843,19 +841,9 @@
|
||||
"pprint(value[\"generation\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "11fddd00-58bf-4910-bf36-be9e5bfba778",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Trace: \n",
|
||||
"\n",
|
||||
"https://smith.langchain.com/public/7e3aa7e5-c51f-45c2-bc66-b34f17ff2263/r"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": 15,
|
||||
"id": "69a985dd-03c6-45af-a67b-b15746a2cb5f",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -869,7 +857,7 @@
|
||||
"\"Node 'retrieve':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"---CHECK DOCUMENT RELEVANCE TO QUESTION---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT NOT RELEVANT---\n",
|
||||
"---GRADE: DOCUMENT RELEVANT---\n",
|
||||
@@ -884,11 +872,11 @@
|
||||
"---DECISION: GENERATION ADDRESSES QUESTION---\n",
|
||||
"\"Node 'generate':\"\n",
|
||||
"'\\n---\\n'\n",
|
||||
"('The types of agent memory include Sensory Memory, Short-Term Memory (STM) or '\n",
|
||||
" 'Working Memory, and Long-Term Memory (LTM) with subtypes of Explicit / '\n",
|
||||
" 'declarative memory and Implicit / procedural memory. Sensory memory retains '\n",
|
||||
" 'sensory information briefly, STM stores information for cognitive tasks, and '\n",
|
||||
" 'LTM stores information for a long time with different types of memories.')\n"
|
||||
"('The types of agent memory include short-term memory, long-term memory, and '\n",
|
||||
" 'sensory memory. Short-term memory is utilized for in-context learning, while '\n",
|
||||
" 'long-term memory allows for the retention and recall of information over '\n",
|
||||
" 'extended periods. Sensory memory involves learning embedding representations '\n",
|
||||
" 'for various raw inputs, such as text and images.')\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@@ -906,16 +894,6 @@
|
||||
"# Final generation\n",
|
||||
"pprint(value[\"generation\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "ebf41097-fc4c-4072-95b3-e7e07731ada1",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Trace: \n",
|
||||
"\n",
|
||||
"https://smith.langchain.com/public/fdf0a180-6d15-4d09-bb92-f84f2105ca51/r"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
@@ -934,7 +912,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.4"
|
||||
"version": "3.12.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -261,7 +261,7 @@
|
||||
" binary_score: str = Field(description=\"Relevance score 'yes' or 'no'\")\n",
|
||||
"\n",
|
||||
" # LLM\n",
|
||||
" model = ChatOpenAI(temperature=0, model=\"gpt-4-0125-preview\", streaming=True)\n",
|
||||
" model = ChatOpenAI(temperature=0, model=\"gpt-4o\", streaming=True)\n",
|
||||
"\n",
|
||||
" # LLM with tool and validation\n",
|
||||
" llm_with_tool = model.with_structured_output(grade)\n",
|
||||
@@ -376,7 +376,7 @@
|
||||
" prompt = hub.pull(\"rlm/rag-prompt\")\n",
|
||||
"\n",
|
||||
" # LLM\n",
|
||||
" llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0, streaming=True)\n",
|
||||
" llm = ChatOpenAI(model_name=\"gpt-4o-mini\", temperature=0, streaming=True)\n",
|
||||
"\n",
|
||||
" # Post-processing\n",
|
||||
" def format_docs(docs):\n",
|
||||
@@ -548,7 +548,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.9"
|
||||
"version": "3.12.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
@@ -62,7 +62,7 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! pip install -U langchain_community tiktoken langchain-openai langchainhub chromadb langchain langgraph"
|
||||
"%pip install -U langchain_community tiktoken langchain-openai langchainhub chromadb langchain langgraph"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -197,7 +197,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"# LLM with function call\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"structured_llm_grader = llm.with_structured_output(GradeDocuments)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
@@ -243,7 +243,7 @@
|
||||
"prompt = hub.pull(\"rlm/rag-prompt\")\n",
|
||||
"\n",
|
||||
"# LLM\n",
|
||||
"llm = ChatOpenAI(model_name=\"gpt-3.5-turbo\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model_name=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Post-processing\n",
|
||||
@@ -290,7 +290,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"# LLM with function call\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"structured_llm_grader = llm.with_structured_output(GradeHallucinations)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
@@ -338,7 +338,7 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"# LLM with function call\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"structured_llm_grader = llm.with_structured_output(GradeAnswer)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
@@ -376,7 +376,7 @@
|
||||
"### Question Re-writer\n",
|
||||
"\n",
|
||||
"# LLM\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-3.5-turbo-0125\", temperature=0)\n",
|
||||
"llm = ChatOpenAI(model=\"gpt-4o-mini\", temperature=0)\n",
|
||||
"\n",
|
||||
"# Prompt\n",
|
||||
"system = \"\"\"You a question re-writer that converts an input question to a better version that is optimized \\n \n",
|
||||
@@ -760,18 +760,6 @@
|
||||
"# Final generation\n",
|
||||
"pprint(value[\"generation\"])"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"id": "548f1c5b-4108-4aae-8abb-ec171b511b92",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"LangSmith Traces - \n",
|
||||
" \n",
|
||||
"* https://smith.langchain.com/public/55d6180f-aab8-42bc-8799-dadce6247d9b/r\n",
|
||||
"\n",
|
||||
"* https://smith.langchain.com/public/1c6bf654-61b2-4fc5-9889-054b020c78aa/r"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
@@ -790,7 +778,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.11.9"
|
||||
"version": "3.12.3"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user