mirror of
https://github.com/langchain-ai/langgraph.git
synced 2026-08-22 23:52:23 +02:00
WIP
This commit is contained in:
+73
-34
@@ -12,7 +12,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 11,
|
||||
"id": "624c452c-ddd5-4390-9065-7ec55dc64b96",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -20,10 +20,14 @@
|
||||
"from operator import itemgetter\n",
|
||||
"\n",
|
||||
"from langchain.chat_models.openai import ChatOpenAI\n",
|
||||
"from langchain.prompts import SystemMessagePromptTemplate, ChatPromptTemplate, PromptTemplate\n",
|
||||
"from langchain.prompts import (\n",
|
||||
" SystemMessagePromptTemplate,\n",
|
||||
" ChatPromptTemplate,\n",
|
||||
" PromptTemplate,\n",
|
||||
")\n",
|
||||
"from langchain.schema.output_parser import StrOutputParser\n",
|
||||
"from langchain.runnables.openai_functions import OpenAIFunctionsRouter\n",
|
||||
"from langchain.schema.runnable import RunnableMap\n",
|
||||
"from langchain.schema.runnable import RunnableMap, RunnablePassthrough\n",
|
||||
"from langchain.schema.document import Document\n",
|
||||
"from langchain.schema import format_document\n",
|
||||
"\n",
|
||||
@@ -59,7 +63,9 @@
|
||||
"source": [
|
||||
"DEFAULT_DOCUMENT_PROMPT = PromptTemplate.from_template(template=\"{page_content}\")\n",
|
||||
"\n",
|
||||
"_combine_documents = RunnableLambda(lambda x: format_document(x, DEFAULT_DOCUMENT_PROMPT)).map() | (lambda x: \"\\n\\n\".join(x))"
|
||||
"_combine_documents = RunnableLambda(\n",
|
||||
" lambda x: format_document(x, DEFAULT_DOCUMENT_PROMPT)\n",
|
||||
").map() | (lambda x: \"\\n\\n\".join(x))"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -69,28 +75,40 @@
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"docs = [Document(page_content=\"Harrison used to work at Kensho\"), Document(page_content=\"Ankush worked at Facebook\")]"
|
||||
"docs = [\n",
|
||||
" Document(page_content=\"Harrison used to work at Kensho\"),\n",
|
||||
" Document(page_content=\"Ankush worked at Facebook\"),\n",
|
||||
"]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 5,
|
||||
"id": "17da58b7-8685-4d0a-9a47-c398c085d477",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"stuff_chain = {\n",
|
||||
" \"question\": lambda x: x[\"question\"],\n",
|
||||
" \"context\": (lambda x: x['docs']) | _combine_documents\n",
|
||||
"} |ChatPromptTemplate.from_messages([\n",
|
||||
" (\"system\", \"Answer user questions based on the following documents:\\n\\n{context}\"),\n",
|
||||
" (\"human\", \"{question}\"),\n",
|
||||
"]) | ChatOpenAI()"
|
||||
"stuff_chain = (\n",
|
||||
" {\n",
|
||||
" \"question\": lambda x: x[\"question\"],\n",
|
||||
" \"context\": (lambda x: x[\"docs\"]) | _combine_documents,\n",
|
||||
" }\n",
|
||||
" | ChatPromptTemplate.from_messages(\n",
|
||||
" [\n",
|
||||
" (\n",
|
||||
" \"system\",\n",
|
||||
" \"Answer user questions based on the following documents:\\n\\n{context}\",\n",
|
||||
" ),\n",
|
||||
" (\"human\", \"{question}\"),\n",
|
||||
" ]\n",
|
||||
" )\n",
|
||||
" | ChatOpenAI()\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": 6,
|
||||
"id": "87295b71-0afc-4901-b57c-a7b945aa4bd9",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -100,7 +118,7 @@
|
||||
"AIMessage(content='Harrison used to work at Kensho.')"
|
||||
]
|
||||
},
|
||||
"execution_count": 9,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -121,7 +139,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 7,
|
||||
"id": "b15f5abb-1cfe-4965-a021-c891506c5dd2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -131,7 +149,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 8,
|
||||
"id": "ccad04a3-fd3f-4e73-b895-29e53535f000",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
@@ -156,7 +174,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 9,
|
||||
"id": "11cfd337-9f3b-4b26-ba30-251e17b18994",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
@@ -175,7 +193,7 @@
|
||||
" Document(page_content='Ankush worked at Facebook')]]"
|
||||
]
|
||||
},
|
||||
"execution_count": 12,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
@@ -188,10 +206,23 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": 10,
|
||||
"id": "8d524ba6-0939-4a5d-8db0-4fa1ef06eaeb",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "LastValue() takes no arguments",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"Cell \u001b[0;32mIn[10], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m input_inbox \u001b[38;5;241m=\u001b[39m \u001b[43mchannels\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mLastValue\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;28;43mstr\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43minput_inbox\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2\u001b[0m reduce_inbox \u001b[38;5;241m=\u001b[39m channels\u001b[38;5;241m.\u001b[39mLastValue[\u001b[38;5;28mstr\u001b[39m](\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mreduce_inbox\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 3\u001b[0m collapse_inbox \u001b[38;5;241m=\u001b[39m channels\u001b[38;5;241m.\u001b[39mLastValue[\u001b[38;5;28mstr\u001b[39m](\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mcollapse_inbox\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n",
|
||||
"File \u001b[0;32m/opt/homebrew/Cellar/python@3.11/3.11.5/Frameworks/Python.framework/Versions/3.11/lib/python3.11/typing.py:1268\u001b[0m, in \u001b[0;36m_BaseGenericAlias.__call__\u001b[0;34m(self, *args, **kwargs)\u001b[0m\n\u001b[1;32m 1265\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_inst:\n\u001b[1;32m 1266\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mTypeError\u001b[39;00m(\u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mType \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_name\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m cannot be instantiated; \u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 1267\u001b[0m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124muse \u001b[39m\u001b[38;5;132;01m{\u001b[39;00m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m__origin__\u001b[38;5;241m.\u001b[39m\u001b[38;5;18m__name__\u001b[39m\u001b[38;5;132;01m}\u001b[39;00m\u001b[38;5;124m() instead\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m-> 1268\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__origin__\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43margs\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[38;5;241;43m*\u001b[39;49m\u001b[43mkwargs\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1269\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 1270\u001b[0m result\u001b[38;5;241m.\u001b[39m__orig_class__ \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\n",
|
||||
"\u001b[0;31mTypeError\u001b[0m: LastValue() takes no arguments"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"input_inbox = channels.LastValue[str](\"input_inbox\")\n",
|
||||
"reduce_inbox = channels.LastValue[str](\"reduce_inbox\")\n",
|
||||
@@ -208,25 +239,31 @@
|
||||
"source": [
|
||||
"# Decide if should finish or should reduce one more step\n",
|
||||
"def decide_end(plan):\n",
|
||||
" if len(plan['docs']) > 1:\n",
|
||||
" if len(plan[\"docs\"]) > 1:\n",
|
||||
" return Pregel.send_to(\"reduce_inbox\")\n",
|
||||
" else:\n",
|
||||
" return {\"docs\": lambda x: x[\"docs\"][0], \"question\": lambda x: x[\"question\"]} | stuff_chain | Pregel.send_to(\"output_inbox\")\n",
|
||||
" return stuff_chain | Pregel.send_to(\"output_inbox\")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"# Chain that collapses documents then chooses end\n",
|
||||
"collapse_chain = Pregel.subscribe_to(input=collapse_inbox) | RunnableMap({\n",
|
||||
" \"docs\": lambda x: _split_list_of_docs(x[\"docs\"]),\n",
|
||||
" \"question\": lambda x: x[\"question\"]\n",
|
||||
"}) | decide_end\n",
|
||||
"collapse_chain = (\n",
|
||||
" Pregel.subscribe_to(docs=collapse_inbox, question=\"question\")\n",
|
||||
" | RunnablePassthrough.assign(docs=lambda x: _split_list_of_docs(x[\"docs\"]))\n",
|
||||
" | decide_end\n",
|
||||
")\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"reduce_chain = (\n",
|
||||
" Pregel.subscribe_to(input=input_inbox)\n",
|
||||
" | (lambda x: [{\"docs\": d, \"question\": x[\"question\"]} for d in x['docs']])\n",
|
||||
" | stuff_chain.map() \n",
|
||||
" | Pregel.send_to({\"collapse_inbox\": {\n",
|
||||
" \"docs\": lambda x: [Document(page_content=m.content) for m in x],\n",
|
||||
" }})\n",
|
||||
" | (lambda x: [{\"docs\": d, \"question\": x[\"question\"]} for d in x[\"docs\"]])\n",
|
||||
" | stuff_chain.map()\n",
|
||||
" | Pregel.send_to(\n",
|
||||
" {\n",
|
||||
" \"collapse_inbox\": {\n",
|
||||
" \"docs\": lambda x: [Document(page_content=m.content) for m in x],\n",
|
||||
" }\n",
|
||||
" }\n",
|
||||
" )\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
@@ -252,7 +289,9 @@
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"pubsub = Pregel(input_inbox, reduce_inbox, collapse_inbox, input=input_inbox, output=output_inbox)\n"
|
||||
"pubsub = Pregel(\n",
|
||||
" input_inbox, reduce_inbox, collapse_inbox, input=input_inbox, output=output_inbox\n",
|
||||
")"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -301,7 +340,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.10.1"
|
||||
"version": "3.11.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
|
||||
Reference in New Issue
Block a user