diff --git a/docs/docs/expression_language/interface.ipynb b/docs/docs/expression_language/interface.ipynb index ffc9225ac41..6837a73532a 100644 --- a/docs/docs/expression_language/interface.ipynb +++ b/docs/docs/expression_language/interface.ipynb @@ -30,6 +30,7 @@ "- [`ainvoke`](#async-invoke): call the chain on an input async\n", "- [`abatch`](#async-batch): call the chain on a list of inputs async\n", "- [`astream_log`](#async-stream-intermediate-steps): stream back intermediate steps as they happen, in addition to the final response\n", + "- [`astream_events`](#async-stream-events): **beta** stream events as they happen in the chain (introduced in `langchain-core` 0.2.0)\n", "\n", "The **input type** and **output type** varies by component:\n", "\n", @@ -87,7 +88,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 2, "id": "25e146d4-60da-40a2-9026-b5dfee106a3f", "metadata": {}, "outputs": [ @@ -99,7 +100,7 @@ " 'properties': {'topic': {'title': 'Topic', 'type': 'string'}}}" ] }, - "execution_count": 13, + "execution_count": 2, "metadata": {}, "output_type": "execute_result" } @@ -111,7 +112,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 3, "id": "ad130546-4c14-4f6c-95af-c56ea19b12ac", "metadata": {}, "outputs": [ @@ -123,7 +124,7 @@ " 'properties': {'topic': {'title': 'Topic', 'type': 'string'}}}" ] }, - "execution_count": 16, + "execution_count": 3, "metadata": {}, "output_type": "execute_result" } @@ -134,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 4, "id": "49d34744-d6db-4fdf-a0d6-261522b7f251", "metadata": {}, "outputs": [ @@ -150,7 +151,8 @@ " {'$ref': '#/definitions/HumanMessage'},\n", " {'$ref': '#/definitions/ChatMessage'},\n", " {'$ref': '#/definitions/SystemMessage'},\n", - " {'$ref': '#/definitions/FunctionMessage'}]}}],\n", + " {'$ref': '#/definitions/FunctionMessage'},\n", + " {'$ref': '#/definitions/ToolMessage'}]}}],\n", " 'definitions': {'StringPromptValue': {'title': 'StringPromptValue',\n", " 'description': 'String prompt value.',\n", " 'type': 'object',\n", @@ -163,7 +165,10 @@ " 'AIMessage': {'title': 'AIMessage',\n", " 'description': 'A Message from an AI.',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", " 'default': 'ai',\n", @@ -174,7 +179,10 @@ " 'HumanMessage': {'title': 'HumanMessage',\n", " 'description': 'A Message from a human.',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", " 'default': 'human',\n", @@ -185,7 +193,10 @@ " 'ChatMessage': {'title': 'ChatMessage',\n", " 'description': 'A Message that can be assigned an arbitrary speaker (i.e. role).',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", " 'default': 'chat',\n", @@ -196,7 +207,10 @@ " 'SystemMessage': {'title': 'SystemMessage',\n", " 'description': 'A Message for priming AI behavior, usually passed in as the first of a sequence\\nof input messages.',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", " 'default': 'system',\n", @@ -206,7 +220,10 @@ " 'FunctionMessage': {'title': 'FunctionMessage',\n", " 'description': 'A Message for passing the result of executing a function back to a model.',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", " 'default': 'function',\n", @@ -214,6 +231,20 @@ " 'type': 'string'},\n", " 'name': {'title': 'Name', 'type': 'string'}},\n", " 'required': ['content', 'name']},\n", + " 'ToolMessage': {'title': 'ToolMessage',\n", + " 'description': 'A Message for passing the result of executing a tool back to a model.',\n", + " 'type': 'object',\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", + " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", + " 'type': {'title': 'Type',\n", + " 'default': 'tool',\n", + " 'enum': ['tool'],\n", + " 'type': 'string'},\n", + " 'tool_call_id': {'title': 'Tool Call Id', 'type': 'string'}},\n", + " 'required': ['content', 'tool_call_id']},\n", " 'ChatPromptValueConcrete': {'title': 'ChatPromptValueConcrete',\n", " 'description': 'Chat prompt value which explicitly lists out the message types it accepts.\\nFor use in external schemas.',\n", " 'type': 'object',\n", @@ -223,7 +254,8 @@ " {'$ref': '#/definitions/HumanMessage'},\n", " {'$ref': '#/definitions/ChatMessage'},\n", " {'$ref': '#/definitions/SystemMessage'},\n", - " {'$ref': '#/definitions/FunctionMessage'}]}},\n", + " {'$ref': '#/definitions/FunctionMessage'},\n", + " {'$ref': '#/definitions/ToolMessage'}]}},\n", " 'type': {'title': 'Type',\n", " 'default': 'ChatPromptValueConcrete',\n", " 'enum': ['ChatPromptValueConcrete'],\n", @@ -231,7 +263,7 @@ " 'required': ['messages']}}}" ] }, - "execution_count": 15, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } @@ -254,7 +286,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 5, "id": "a0e41fd3-77d8-4911-af6a-d4d3aad5f77b", "metadata": {}, "outputs": [ @@ -262,26 +294,19 @@ "data": { "text/plain": [ "{'title': 'ChatOpenAIOutput',\n", - " 'anyOf': [{'$ref': '#/definitions/HumanMessage'},\n", - " {'$ref': '#/definitions/AIMessage'},\n", + " 'anyOf': [{'$ref': '#/definitions/AIMessage'},\n", + " {'$ref': '#/definitions/HumanMessage'},\n", " {'$ref': '#/definitions/ChatMessage'},\n", + " {'$ref': '#/definitions/SystemMessage'},\n", " {'$ref': '#/definitions/FunctionMessage'},\n", - " {'$ref': '#/definitions/SystemMessage'}],\n", - " 'definitions': {'HumanMessage': {'title': 'HumanMessage',\n", - " 'description': 'A Message from a human.',\n", - " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", - " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", - " 'type': {'title': 'Type',\n", - " 'default': 'human',\n", - " 'enum': ['human'],\n", - " 'type': 'string'},\n", - " 'example': {'title': 'Example', 'default': False, 'type': 'boolean'}},\n", - " 'required': ['content']},\n", - " 'AIMessage': {'title': 'AIMessage',\n", + " {'$ref': '#/definitions/ToolMessage'}],\n", + " 'definitions': {'AIMessage': {'title': 'AIMessage',\n", " 'description': 'A Message from an AI.',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", " 'default': 'ai',\n", @@ -289,10 +314,27 @@ " 'type': 'string'},\n", " 'example': {'title': 'Example', 'default': False, 'type': 'boolean'}},\n", " 'required': ['content']},\n", + " 'HumanMessage': {'title': 'HumanMessage',\n", + " 'description': 'A Message from a human.',\n", + " 'type': 'object',\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", + " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", + " 'type': {'title': 'Type',\n", + " 'default': 'human',\n", + " 'enum': ['human'],\n", + " 'type': 'string'},\n", + " 'example': {'title': 'Example', 'default': False, 'type': 'boolean'}},\n", + " 'required': ['content']},\n", " 'ChatMessage': {'title': 'ChatMessage',\n", " 'description': 'A Message that can be assigned an arbitrary speaker (i.e. role).',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", " 'default': 'chat',\n", @@ -300,10 +342,26 @@ " 'type': 'string'},\n", " 'role': {'title': 'Role', 'type': 'string'}},\n", " 'required': ['content', 'role']},\n", + " 'SystemMessage': {'title': 'SystemMessage',\n", + " 'description': 'A Message for priming AI behavior, usually passed in as the first of a sequence\\nof input messages.',\n", + " 'type': 'object',\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", + " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", + " 'type': {'title': 'Type',\n", + " 'default': 'system',\n", + " 'enum': ['system'],\n", + " 'type': 'string'}},\n", + " 'required': ['content']},\n", " 'FunctionMessage': {'title': 'FunctionMessage',\n", " 'description': 'A Message for passing the result of executing a function back to a model.',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", " 'default': 'function',\n", @@ -311,19 +369,23 @@ " 'type': 'string'},\n", " 'name': {'title': 'Name', 'type': 'string'}},\n", " 'required': ['content', 'name']},\n", - " 'SystemMessage': {'title': 'SystemMessage',\n", - " 'description': 'A Message for priming AI behavior, usually passed in as the first of a sequence\\nof input messages.',\n", + " 'ToolMessage': {'title': 'ToolMessage',\n", + " 'description': 'A Message for passing the result of executing a tool back to a model.',\n", " 'type': 'object',\n", - " 'properties': {'content': {'title': 'Content', 'type': 'string'},\n", + " 'properties': {'content': {'title': 'Content',\n", + " 'anyOf': [{'type': 'string'},\n", + " {'type': 'array',\n", + " 'items': {'anyOf': [{'type': 'string'}, {'type': 'object'}]}}]},\n", " 'additional_kwargs': {'title': 'Additional Kwargs', 'type': 'object'},\n", " 'type': {'title': 'Type',\n", - " 'default': 'system',\n", - " 'enum': ['system'],\n", - " 'type': 'string'}},\n", - " 'required': ['content']}}}" + " 'default': 'tool',\n", + " 'enum': ['tool'],\n", + " 'type': 'string'},\n", + " 'tool_call_id': {'title': 'Tool Call Id', 'type': 'string'}},\n", + " 'required': ['content', 'tool_call_id']}}}" ] }, - "execution_count": 17, + "execution_count": 5, "metadata": {}, "output_type": "execute_result" } @@ -343,7 +405,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 6, "id": "bea9639d", "metadata": {}, "outputs": [ @@ -351,6 +413,8 @@ "name": "stdout", "output_type": "stream", "text": [ + "Sure, here's a bear-themed joke for you:\n", + "\n", "Why don't bears wear shoes?\n", "\n", "Because they already have bear feet!" @@ -372,17 +436,17 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 7, "id": "470e483f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they already have bear feet!\")" + "AIMessage(content=\"Why don't bears wear shoes? \\n\\nBecause they have bear feet!\")" ] }, - "execution_count": 21, + "execution_count": 7, "metadata": {}, "output_type": "execute_result" } @@ -401,18 +465,18 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 8, "id": "9685de67", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they have bear feet!\"),\n", + "[AIMessage(content=\"Sure, here's a bear joke for you:\\n\\nWhy don't bears wear shoes?\\n\\nBecause they already have bear feet!\"),\n", " AIMessage(content=\"Why don't cats play poker in the wild?\\n\\nToo many cheetahs!\")]" ] }, - "execution_count": 22, + "execution_count": 8, "metadata": {}, "output_type": "execute_result" } @@ -431,18 +495,18 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 9, "id": "a08522f6", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[AIMessage(content=\"Why don't bears wear shoes? \\n\\nBecause they have bear feet!\"),\n", - " AIMessage(content=\"Why don't cats play poker in the wild?\\n\\nToo many cheetahs!\")]" + "[AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they have bear feet!\"),\n", + " AIMessage(content=\"Why don't cats play poker in the wild? Too many cheetahs!\")]" ] }, - "execution_count": 23, + "execution_count": 9, "metadata": {}, "output_type": "execute_result" } @@ -461,7 +525,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 10, "id": "ea35eee4", "metadata": {}, "outputs": [ @@ -469,11 +533,9 @@ "name": "stdout", "output_type": "stream", "text": [ - "Sure, here's a bear-themed joke for you:\n", - "\n", "Why don't bears wear shoes?\n", "\n", - "Because they already have bear feet!" + "Because they have bear feet!" ] } ], @@ -492,17 +554,17 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 11, "id": "ef8c9b20", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content=\"Why don't bears wear shoes? \\n\\nBecause they have bear feet!\")" + "AIMessage(content=\"Why don't bears ever wear shoes?\\n\\nBecause they already have bear feet!\")" ] }, - "execution_count": 25, + "execution_count": 11, "metadata": {}, "output_type": "execute_result" } @@ -521,7 +583,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 12, "id": "eba2a103", "metadata": {}, "outputs": [ @@ -531,7 +593,7 @@ "[AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they have bear feet!\")]" ] }, - "execution_count": 26, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -540,6 +602,192 @@ "await chain.abatch([{\"topic\": \"bears\"}])" ] }, + { + "cell_type": "markdown", + "id": "c2d58e3f-2b2e-4dac-820b-5e9c263b1868", + "metadata": {}, + "source": [ + "## Async Stream Events (beta)" + ] + }, + { + "cell_type": "markdown", + "id": "53d365e5-dc14-4bb7-aa6a-7762c3af16a4", + "metadata": {}, + "source": [ + "Event Streaming is a **beta** API, and may change a bit based on feedback.\n", + "\n", + "Note: Introduced in langchain-core 0.2.0\n", + "\n", + "For now, when using the astream_events API, for everything to work properly please:\n", + "\n", + "* Use `async` throughout the code (including async tools etc)\n", + "* Propagate callbacks if defining custom functions / runnables. \n", + "* Whenever using runnables without LCEL, make sure to call `.astream()` on LLMs rather than `.ainvoke` to force the LLM to stream tokens.\n", + "\n", + "### Event Reference\n", + "\n", + "\n", + "Here is a reference table that shows some events that might be emitted by the various Runnable objects.\n", + "Definitions for some of the Runnable are included after the table.\n", + "\n", + "⚠️ When streaming the inputs for the runnable will not be available until the input stream has been entirely consumed This means that the inputs will be available at for the corresponding `end` hook rather than `start` event.\n", + "\n", + "\n", + "| event | name | chunk | input | output |\n", + "|----------------------|------------------|---------------------------------|-----------------------------------------------|-------------------------------------------------|\n", + "| on_chat_model_start | [model name] | | {\"messages\": [[SystemMessage, HumanMessage]]} | |\n", + "| on_chat_model_stream | [model name] | AIMessageChunk(content=\"hello\") | | |\n", + "| on_chat_model_end | [model name] | | {\"messages\": [[SystemMessage, HumanMessage]]} | {\"generations\": [...], \"llm_output\": None, ...} |\n", + "| on_llm_start | [model name] | | {'input': 'hello'} | |\n", + "| on_llm_stream | [model name] | 'Hello' | | |\n", + "| on_llm_end | [model name] | | 'Hello human!' |\n", + "| on_chain_start | format_docs | | | |\n", + "| on_chain_stream | format_docs | \"hello world!, goodbye world!\" | | |\n", + "| on_chain_end | format_docs | | [Document(...)] | \"hello world!, goodbye world!\" |\n", + "| on_tool_start | some_tool | | {\"x\": 1, \"y\": \"2\"} | |\n", + "| on_tool_stream | some_tool | {\"x\": 1, \"y\": \"2\"} | | |\n", + "| on_tool_end | some_tool | | | {\"x\": 1, \"y\": \"2\"} |\n", + "| on_retriever_start | [retriever name] | | {\"query\": \"hello\"} | |\n", + "| on_retriever_chunk | [retriever name] | {documents: [...]} | | |\n", + "| on_retriever_end | [retriever name] | | {\"query\": \"hello\"} | {documents: [...]} |\n", + "| on_prompt_start | [template_name] | | {\"question\": \"hello\"} | |\n", + "| on_prompt_end | [template_name] | | {\"question\": \"hello\"} | ChatPromptValue(messages: [SystemMessage, ...]) |\n", + "\n", + "\n", + "Here are declarations associated with the events shown above:\n", + "\n", + "`format_docs`:\n", + "\n", + "```python\n", + "def format_docs(docs: List[Document]) -> str:\n", + " '''Format the docs.'''\n", + " return \", \".join([doc.page_content for doc in docs])\n", + "\n", + "format_docs = RunnableLambda(format_docs)\n", + "```\n", + "\n", + "`some_tool`:\n", + "\n", + "```python\n", + "@tool\n", + "def some_tool(x: int, y: str) -> dict:\n", + " '''Some_tool.'''\n", + " return {\"x\": x, \"y\": y}\n", + "```\n", + "\n", + "`prompt`:\n", + "\n", + "```python\n", + "template = ChatPromptTemplate.from_messages(\n", + " [(\"system\", \"You are Cat Agent 007\"), (\"human\", \"{question}\")]\n", + ").with_config({\"run_name\": \"my_template\", \"tags\": [\"my_template\"]})\n", + "```\n", + "\n" + ] + }, + { + "cell_type": "markdown", + "id": "108cf792-a372-4626-bbef-9d7be23dde33", + "metadata": {}, + "source": [ + "Let's define a new chain to make it more interesting to show off the `astream_events` interface (and later the `astream_log` interface)." + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "92eeb4da-0aae-457b-bd8f-8c35a024d4d1", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain_community.vectorstores import FAISS\n", + "from langchain_core.output_parsers import StrOutputParser\n", + "from langchain_core.runnables import RunnablePassthrough\n", + "from langchain_openai import OpenAIEmbeddings\n", + "\n", + "template = \"\"\"Answer the question based only on the following context:\n", + "{context}\n", + "\n", + "Question: {question}\n", + "\"\"\"\n", + "prompt = ChatPromptTemplate.from_template(template)\n", + "\n", + "vectorstore = FAISS.from_texts(\n", + " [\"harrison worked at kensho\"], embedding=OpenAIEmbeddings()\n", + ")\n", + "retriever = vectorstore.as_retriever()\n", + "\n", + "retrieval_chain = (\n", + " {\n", + " \"context\": retriever.with_config(run_name=\"Docs\"),\n", + " \"question\": RunnablePassthrough(),\n", + " }\n", + " | prompt\n", + " | model.with_config(run_name=\"my_llm\")\n", + " | StrOutputParser()\n", + ")" + ] + }, + { + "cell_type": "markdown", + "id": "1167e8f2-cab7-45b4-8922-7518b58a7d8d", + "metadata": {}, + "source": [ + "Now let's use `astream_events` to get events from the retriever and the LLM." + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "0742d723-5b00-4a44-961e-dd4a3ec6d557", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/home/eugene/src/langchain/libs/core/langchain_core/_api/beta_decorator.py:86: LangChainBetaWarning: This API is in beta and may change in the future.\n", + " warn_beta(\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "--\n", + "Retrieved the following documents:\n", + "[Document(page_content='harrison worked at kensho')]\n", + "\n", + "Streaming LLM:\n", + "|H|arrison| worked| at| Kens|ho|.||\n", + "Done streaming LLM.\n" + ] + } + ], + "source": [ + "async for event in retrieval_chain.astream_events(\n", + " \"where did harrison work?\", version=\"v1\", include_names=[\"Docs\", \"my_llm\"]\n", + "):\n", + " kind = event[\"event\"]\n", + " if kind == \"on_chat_model_stream\":\n", + " print(event[\"data\"][\"chunk\"].content, end=\"|\")\n", + " elif kind in {\"on_chat_model_start\"}:\n", + " print()\n", + " print(\"Streaming LLM:\")\n", + " elif kind in {\"on_chat_model_end\"}:\n", + " print()\n", + " print(\"Done streaming LLM.\")\n", + " elif kind == \"on_retriever_end\":\n", + " print(\"--\")\n", + " print(\"Retrieved the following documents:\")\n", + " print(event[\"data\"][\"output\"][\"documents\"])\n", + " elif kind == \"on_tool_end\":\n", + " print(f\"Ended tool: {event['name']}\")\n", + " else:\n", + " pass" + ] + }, { "cell_type": "markdown", "id": "f9cef104", @@ -607,7 +855,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 15, "id": "21c9019e", "metadata": {}, "outputs": [ @@ -619,20 +867,23 @@ "RunLogPatch({'op': 'replace',\n", " 'path': '',\n", " 'value': {'final_output': None,\n", - " 'id': 'e2f2cc72-eb63-4d20-8326-237367482efb',\n", + " 'id': '82e9b4b1-3dd6-4732-8db9-90e79c4da48c',\n", " 'logs': {},\n", - " 'streamed_output': []}})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': [],\n", + " 'type': 'chain'}})\n", "----------------------------------------\n", "RunLogPatch({'op': 'add',\n", " 'path': '/logs/Docs',\n", " 'value': {'end_time': None,\n", " 'final_output': None,\n", - " 'id': '8da492cc-4492-4e74-b8b0-9e60e8693390',\n", + " 'id': '9206e94a-57bd-48ee-8c5e-fdd1c52a6da2',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:50:13.526',\n", + " 'start_time': '2024-01-19T22:33:55.902+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}})\n", "----------------------------------------\n", "RunLogPatch({'op': 'add',\n", @@ -640,60 +891,41 @@ " 'value': {'documents': [Document(page_content='harrison worked at kensho')]}},\n", " {'op': 'add',\n", " 'path': '/logs/Docs/end_time',\n", - " 'value': '2023-10-19T17:50:13.713'})\n", + " 'value': '2024-01-19T22:33:56.064+00:00'})\n", "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ''})\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ''},\n", + " {'op': 'replace', 'path': '/final_output', 'value': ''})\n", "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'H'})\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'H'},\n", + " {'op': 'replace', 'path': '/final_output', 'value': 'H'})\n", "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'arrison'})\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'arrison'},\n", + " {'op': 'replace', 'path': '/final_output', 'value': 'Harrison'})\n", "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' worked'})\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' worked'},\n", + " {'op': 'replace', 'path': '/final_output', 'value': 'Harrison worked'})\n", "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' at'})\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' at'},\n", + " {'op': 'replace', 'path': '/final_output', 'value': 'Harrison worked at'})\n", "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' Kens'})\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ' Kens'},\n", + " {'op': 'replace', 'path': '/final_output', 'value': 'Harrison worked at Kens'})\n", "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'ho'})\n", - "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': '.'})\n", - "----------------------------------------\n", - "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ''})\n", - "----------------------------------------\n", - "RunLogPatch({'op': 'replace',\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': 'ho'},\n", + " {'op': 'replace',\n", " 'path': '/final_output',\n", - " 'value': {'output': 'Harrison worked at Kensho.'}})\n" + " 'value': 'Harrison worked at Kensho'})\n", + "----------------------------------------\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': '.'},\n", + " {'op': 'replace',\n", + " 'path': '/final_output',\n", + " 'value': 'Harrison worked at Kensho.'})\n", + "----------------------------------------\n", + "RunLogPatch({'op': 'add', 'path': '/streamed_output/-', 'value': ''})\n" ] } ], "source": [ - "from langchain_community.vectorstores import FAISS\n", - "from langchain_core.output_parsers import StrOutputParser\n", - "from langchain_core.runnables import RunnablePassthrough\n", - "from langchain_openai import OpenAIEmbeddings\n", - "\n", - "template = \"\"\"Answer the question based only on the following context:\n", - "{context}\n", - "\n", - "Question: {question}\n", - "\"\"\"\n", - "prompt = ChatPromptTemplate.from_template(template)\n", - "\n", - "vectorstore = FAISS.from_texts(\n", - " [\"harrison worked at kensho\"], embedding=OpenAIEmbeddings()\n", - ")\n", - "retriever = vectorstore.as_retriever()\n", - "\n", - "retrieval_chain = (\n", - " {\n", - " \"context\": retriever.with_config(run_name=\"Docs\"),\n", - " \"question\": RunnablePassthrough(),\n", - " }\n", - " | prompt\n", - " | model\n", - " | StrOutputParser()\n", - ")\n", - "\n", "async for chunk in retrieval_chain.astream_log(\n", " \"where did harrison work?\", include_names=[\"Docs\"]\n", "):\n", @@ -714,7 +946,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 16, "id": "5c26b731-b4eb-4967-a42a-dec813249ecb", "metadata": {}, "outputs": [ @@ -724,151 +956,185 @@ "text": [ "----------------------------------------------------------------------\n", "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", " 'logs': {},\n", - " 'streamed_output': []})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': [],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", " 'logs': {'Docs': {'end_time': None,\n", " 'final_output': None,\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': []})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': [],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': []})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': [],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': '',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': ['']})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': [''],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': 'H',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': ['', 'H']})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': ['', 'H'],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': 'Harrison',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': ['', 'H', 'arrison']})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': ['', 'H', 'arrison'],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': 'Harrison worked',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': ['', 'H', 'arrison', ' worked']})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': ['', 'H', 'arrison', ' worked'],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': 'Harrison worked at',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': ['', 'H', 'arrison', ' worked', ' at']})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': ['', 'H', 'arrison', ' worked', ' at'],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': 'Harrison worked at Kens',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens']})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens'],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': 'Harrison worked at Kensho',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens', 'ho']})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens', 'ho'],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': 'Harrison worked at Kensho.',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", - " 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens', 'ho', '.']})\n", + " 'name': 'RunnableSequence',\n", + " 'streamed_output': ['', 'H', 'arrison', ' worked', ' at', ' Kens', 'ho', '.'],\n", + " 'type': 'chain'})\n", "----------------------------------------------------------------------\n", - "RunLog({'final_output': None,\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", + "RunLog({'final_output': 'Harrison worked at Kensho.',\n", + " 'id': '431d1c55-7c50-48ac-b3a2-2f5ba5f35172',\n", + " 'logs': {'Docs': {'end_time': '2024-01-19T22:33:57.120+00:00',\n", " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", + " 'id': '8de10b49-d6af-4cb7-a4e7-fbadf6efa01e',\n", " 'metadata': {},\n", " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", + " 'start_time': '2024-01-19T22:33:56.939+00:00',\n", + " 'streamed_output': [],\n", " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", + " 'tags': ['map:key:context', 'FAISS', 'OpenAIEmbeddings'],\n", " 'type': 'retriever'}},\n", + " 'name': 'RunnableSequence',\n", " 'streamed_output': ['',\n", " 'H',\n", " 'arrison',\n", @@ -877,28 +1143,8 @@ " ' Kens',\n", " 'ho',\n", " '.',\n", - " '']})\n", - "----------------------------------------------------------------------\n", - "RunLog({'final_output': {'output': 'Harrison worked at Kensho.'},\n", - " 'id': 'afe66178-d75f-4c2d-b348-b1d144239cd6',\n", - " 'logs': {'Docs': {'end_time': '2023-10-19T17:52:15.738',\n", - " 'final_output': {'documents': [Document(page_content='harrison worked at kensho')]},\n", - " 'id': '88d51118-5756-4891-89c5-2f6a5e90cc26',\n", - " 'metadata': {},\n", - " 'name': 'Docs',\n", - " 'start_time': '2023-10-19T17:52:15.438',\n", - " 'streamed_output_str': [],\n", - " 'tags': ['map:key:context', 'FAISS'],\n", - " 'type': 'retriever'}},\n", - " 'streamed_output': ['',\n", - " 'H',\n", - " 'arrison',\n", - " ' worked',\n", - " ' at',\n", - " ' Kens',\n", - " 'ho',\n", - " '.',\n", - " '']})\n" + " ''],\n", + " 'type': 'chain'})\n" ] } ], @@ -923,7 +1169,7 @@ }, { "cell_type": "code", - "execution_count": 32, + "execution_count": 17, "id": "0a1c409d", "metadata": {}, "outputs": [], @@ -940,7 +1186,7 @@ }, { "cell_type": "code", - "execution_count": 43, + "execution_count": 18, "id": "08044c0a", "metadata": {}, "outputs": [ @@ -948,8 +1194,8 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 54.3 ms, sys: 0 ns, total: 54.3 ms\n", - "Wall time: 2.29 s\n" + "CPU times: user 18 ms, sys: 1.27 ms, total: 19.3 ms\n", + "Wall time: 692 ms\n" ] }, { @@ -958,7 +1204,7 @@ "AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they already have bear feet!\")" ] }, - "execution_count": 43, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -970,7 +1216,7 @@ }, { "cell_type": "code", - "execution_count": 44, + "execution_count": 19, "id": "22c56804", "metadata": {}, "outputs": [ @@ -978,17 +1224,17 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 7.8 ms, sys: 0 ns, total: 7.8 ms\n", - "Wall time: 1.43 s\n" + "CPU times: user 10.5 ms, sys: 166 µs, total: 10.7 ms\n", + "Wall time: 579 ms\n" ] }, { "data": { "text/plain": [ - "AIMessage(content=\"In wild embrace,\\nNature's strength roams with grace.\")" + "AIMessage(content=\"In forest's embrace,\\nMajestic bears pace.\")" ] }, - "execution_count": 44, + "execution_count": 19, "metadata": {}, "output_type": "execute_result" } @@ -1000,7 +1246,7 @@ }, { "cell_type": "code", - "execution_count": 45, + "execution_count": 20, "id": "4fff4cbb", "metadata": {}, "outputs": [ @@ -1008,18 +1254,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 167 ms, sys: 921 µs, total: 168 ms\n", - "Wall time: 1.56 s\n" + "CPU times: user 32 ms, sys: 2.59 ms, total: 34.6 ms\n", + "Wall time: 816 ms\n" ] }, { "data": { "text/plain": [ - "{'joke': AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they already have bear feet!\"),\n", - " 'poem': AIMessage(content=\"Fierce and wild, nature's might,\\nBears roam the woods, shadows of the night.\")}" + "{'joke': AIMessage(content=\"Sure, here's a bear-related joke for you:\\n\\nWhy did the bear bring a ladder to the bar?\\n\\nBecause he heard the drinks were on the house!\"),\n", + " 'poem': AIMessage(content=\"In wilderness they roam,\\nMajestic strength, nature's throne.\")}" ] }, - "execution_count": 45, + "execution_count": 20, "metadata": {}, "output_type": "execute_result" } @@ -1042,7 +1288,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 21, "id": "f67d2268-c766-441b-8d64-57b8219ccb34", "metadata": {}, "outputs": [ @@ -1050,18 +1296,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 159 ms, sys: 3.66 ms, total: 163 ms\n", - "Wall time: 1.34 s\n" + "CPU times: user 17.3 ms, sys: 4.84 ms, total: 22.2 ms\n", + "Wall time: 628 ms\n" ] }, { "data": { "text/plain": [ - "[AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they already have bear feet!\"),\n", - " AIMessage(content=\"Sure, here's a cat joke for you:\\n\\nWhy don't cats play poker in the wild?\\n\\nBecause there are too many cheetahs!\")]" + "[AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they have bear feet!\"),\n", + " AIMessage(content=\"Why don't cats play poker in the wild?\\n\\nToo many cheetahs!\")]" ] }, - "execution_count": 40, + "execution_count": 21, "metadata": {}, "output_type": "execute_result" } @@ -1073,7 +1319,7 @@ }, { "cell_type": "code", - "execution_count": 41, + "execution_count": 22, "id": "83c8d511-9563-403e-9c06-cae986cf5dee", "metadata": {}, "outputs": [ @@ -1081,18 +1327,18 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 165 ms, sys: 0 ns, total: 165 ms\n", - "Wall time: 1.73 s\n" + "CPU times: user 15.8 ms, sys: 3.83 ms, total: 19.7 ms\n", + "Wall time: 718 ms\n" ] }, { "data": { "text/plain": [ - "[AIMessage(content=\"Silent giants roam,\\nNature's strength, love's emblem shown.\"),\n", - " AIMessage(content='Whiskers aglow, paws tiptoe,\\nGraceful hunters, hearts aglow.')]" + "[AIMessage(content='In the wild, bears roam,\\nMajestic guardians of ancient home.'),\n", + " AIMessage(content='Whiskers grace, eyes gleam,\\nCats dance through the moonbeam.')]" ] }, - "execution_count": 41, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } @@ -1104,7 +1350,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": 23, "id": "07a81230-8db8-4b96-bdcb-99ae1d171f2f", "metadata": {}, "outputs": [ @@ -1112,20 +1358,20 @@ "name": "stdout", "output_type": "stream", "text": [ - "CPU times: user 507 ms, sys: 125 ms, total: 632 ms\n", - "Wall time: 1.49 s\n" + "CPU times: user 44.8 ms, sys: 3.17 ms, total: 48 ms\n", + "Wall time: 721 ms\n" ] }, { "data": { "text/plain": [ - "[{'joke': AIMessage(content=\"Why don't bears wear shoes?\\n\\nBecause they already have bear feet!\"),\n", - " 'poem': AIMessage(content=\"Majestic bears roam,\\nNature's wild guardians of home.\")},\n", - " {'joke': AIMessage(content=\"Sure, here's a cat joke for you:\\n\\nWhy did the cat sit on the computer?\\n\\nBecause it wanted to keep an eye on the mouse!\"),\n", - " 'poem': AIMessage(content='Whiskers twitch, eyes gleam,\\nGraceful creatures, feline dream.')}]" + "[{'joke': AIMessage(content=\"Sure, here's a bear joke for you:\\n\\nWhy don't bears wear shoes?\\n\\nBecause they have bear feet!\"),\n", + " 'poem': AIMessage(content=\"Majestic bears roam,\\nNature's strength, beauty shown.\")},\n", + " {'joke': AIMessage(content=\"Why don't cats play poker in the wild?\\n\\nToo many cheetahs!\"),\n", + " 'poem': AIMessage(content=\"Whiskers dance, eyes aglow,\\nCats embrace the night's gentle flow.\")}]" ] }, - "execution_count": 42, + "execution_count": 23, "metadata": {}, "output_type": "execute_result" } @@ -1134,14 +1380,6 @@ "%%time\n", "combined.batch([{\"topic\": \"bears\"}, {\"topic\": \"cats\"}])" ] - }, - { - "cell_type": "code", - "execution_count": null, - "id": "5218cafd-370a-4e3a-85a0-452e570092fe", - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -1160,7 +1398,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.1" + "version": "3.11.4" } }, "nbformat": 4,