Skip to content

Commit

Permalink
Rename RunnableMap to RunnableParallel (#11487)
Browse files Browse the repository at this point in the history
- keep alias for RunnableMap
- update docs to use RunnableParallel and RunnablePassthrough.assign

<!-- Thank you for contributing to LangChain!

Replace this entire comment with:
  - **Description:** a description of the change, 
  - **Issue:** the issue # it fixes (if applicable),
  - **Dependencies:** any dependencies required for this change,
- **Tag maintainer:** for a quicker response, tag the relevant
maintainer (see below),
- **Twitter handle:** we announce bigger features on Twitter. If your PR
gets announced, and you'd like a mention, we'll gladly shout you out!

Please make sure your PR is passing linting and testing before
submitting. Run `make format`, `make lint` and `make test` to check this
locally.

See contribution guidelines for more information on how to write/run
tests, lint, etc:

https://github.com/langchain-ai/langchain/blob/master/.github/CONTRIBUTING.md

If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use. It lives in `docs/extras`
directory.

If no one reviews your PR within a few days, please @-mention one of
@baskaryan, @eyurtsev, @hwchase17.
 -->
  • Loading branch information
nfcampos authored Oct 9, 2023
1 parent 6a10e8e commit 628cc4c
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 213 deletions.
27 changes: 12 additions & 15 deletions docs/docs_skeleton/docs/expression_language/cookbook/memory.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
"metadata": {},
"outputs": [],
"source": [
"from operator import itemgetter\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.memory import ConversationBufferMemory\n",
"from langchain.schema.runnable import RunnableMap\n",
"from langchain.schema.runnable import RunnablePassthrough\n",
"from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
"\n",
"model = ChatOpenAI()\n",
"prompt = ChatPromptTemplate.from_messages([\n",
" (\"system\", \"You are a helpful chatbot\"),\n",
" MessagesPlaceholder(variable_name=\"history\"),\n",
" (\"human\", \"{input}\")\n",
"])"
"])\n"
]
},
{
Expand All @@ -37,7 +38,7 @@
"metadata": {},
"outputs": [],
"source": [
"memory = ConversationBufferMemory(return_messages=True)"
"memory = ConversationBufferMemory(return_messages=True)\n"
]
},
{
Expand All @@ -58,7 +59,7 @@
}
],
"source": [
"memory.load_memory_variables({})"
"memory.load_memory_variables({})\n"
]
},
{
Expand All @@ -68,13 +69,9 @@
"metadata": {},
"outputs": [],
"source": [
"chain = RunnableMap({\n",
" \"input\": lambda x: x[\"input\"],\n",
" \"memory\": memory.load_memory_variables\n",
"}) | {\n",
" \"input\": lambda x: x[\"input\"],\n",
" \"history\": lambda x: x[\"memory\"][\"history\"]\n",
"} | prompt | model"
"chain = RunnablePassthrough.assign(\n",
" memory=memory.load_memory_variables | itemgetter(\"history\")\n",
") | prompt | model\n"
]
},
{
Expand All @@ -97,7 +94,7 @@
"source": [
"inputs = {\"input\": \"hi im bob\"}\n",
"response = chain.invoke(inputs)\n",
"response"
"response\n"
]
},
{
Expand All @@ -107,7 +104,7 @@
"metadata": {},
"outputs": [],
"source": [
"memory.save_context(inputs, {\"output\": response.content})"
"memory.save_context(inputs, {\"output\": response.content})\n"
]
},
{
Expand All @@ -129,7 +126,7 @@
}
],
"source": [
"memory.load_memory_variables({})"
"memory.load_memory_variables({})\n"
]
},
{
Expand All @@ -152,7 +149,7 @@
"source": [
"inputs = {\"input\": \"whats my name\"}\n",
"response = chain.invoke(inputs)\n",
"response"
"response\n"
]
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"---\n",
"sidebar_position: 0\n",
"title: Prompt + LLM\n",
"---"
"---\n"
]
},
{
Expand Down Expand Up @@ -47,7 +47,7 @@
"\n",
"prompt = ChatPromptTemplate.from_template(\"tell me a joke about {foo}\")\n",
"model = ChatOpenAI()\n",
"chain = prompt | model"
"chain = prompt | model\n"
]
},
{
Expand All @@ -68,7 +68,7 @@
}
],
"source": [
"chain.invoke({\"foo\": \"bears\"})"
"chain.invoke({\"foo\": \"bears\"})\n"
]
},
{
Expand All @@ -94,7 +94,7 @@
"metadata": {},
"outputs": [],
"source": [
"chain = prompt | model.bind(stop=[\"\\n\"])"
"chain = prompt | model.bind(stop=[\"\\n\"])\n"
]
},
{
Expand All @@ -115,7 +115,7 @@
}
],
"source": [
"chain.invoke({\"foo\": \"bears\"})"
"chain.invoke({\"foo\": \"bears\"})\n"
]
},
{
Expand Down Expand Up @@ -153,7 +153,7 @@
" }\n",
" }\n",
" ]\n",
"chain = prompt | model.bind(function_call= {\"name\": \"joke\"}, functions= functions)"
"chain = prompt | model.bind(function_call= {\"name\": \"joke\"}, functions= functions)\n"
]
},
{
Expand All @@ -174,7 +174,7 @@
}
],
"source": [
"chain.invoke({\"foo\": \"bears\"}, config={})"
"chain.invoke({\"foo\": \"bears\"}, config={})\n"
]
},
{
Expand All @@ -196,7 +196,7 @@
"source": [
"from langchain.schema.output_parser import StrOutputParser\n",
"\n",
"chain = prompt | model | StrOutputParser()"
"chain = prompt | model | StrOutputParser()\n"
]
},
{
Expand Down Expand Up @@ -225,7 +225,7 @@
}
],
"source": [
"chain.invoke({\"foo\": \"bears\"})"
"chain.invoke({\"foo\": \"bears\"})\n"
]
},
{
Expand All @@ -251,7 +251,7 @@
" prompt \n",
" | model.bind(function_call= {\"name\": \"joke\"}, functions= functions) \n",
" | JsonOutputFunctionsParser()\n",
")"
")\n"
]
},
{
Expand All @@ -273,7 +273,7 @@
}
],
"source": [
"chain.invoke({\"foo\": \"bears\"})"
"chain.invoke({\"foo\": \"bears\"})\n"
]
},
{
Expand All @@ -289,7 +289,7 @@
" prompt \n",
" | model.bind(function_call= {\"name\": \"joke\"}, functions= functions) \n",
" | JsonKeyOutputFunctionsParser(key_name=\"setup\")\n",
")"
")\n"
]
},
{
Expand All @@ -310,7 +310,7 @@
}
],
"source": [
"chain.invoke({\"foo\": \"bears\"})"
"chain.invoke({\"foo\": \"bears\"})\n"
]
},
{
Expand All @@ -332,13 +332,13 @@
"source": [
"from langchain.schema.runnable import RunnableMap, RunnablePassthrough\n",
"\n",
"map_ = RunnableMap({\"foo\": RunnablePassthrough()})\n",
"map_ = RunnableMap(foo=RunnablePassthrough())\n",
"chain = (\n",
" map_ \n",
" | prompt\n",
" | model.bind(function_call= {\"name\": \"joke\"}, functions= functions) \n",
" | JsonKeyOutputFunctionsParser(key_name=\"setup\")\n",
")"
")\n"
]
},
{
Expand All @@ -359,7 +359,7 @@
}
],
"source": [
"chain.invoke(\"bears\")"
"chain.invoke(\"bears\")\n"
]
},
{
Expand All @@ -382,7 +382,7 @@
" | prompt\n",
" | model.bind(function_call= {\"name\": \"joke\"}, functions= functions) \n",
" | JsonKeyOutputFunctionsParser(key_name=\"setup\")\n",
")"
")\n"
]
},
{
Expand All @@ -403,7 +403,7 @@
}
],
"source": [
"chain.invoke(\"bears\")"
"chain.invoke(\"bears\")\n"
]
}
],
Expand Down
Loading

0 comments on commit 628cc4c

Please sign in to comment.