{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "4ef893cf-eac1-45e6-9eb6-72e9ca043200",
   "metadata": {},
   "source": [
    "# How to stream results from your RAG application\n",
    "\n",
    "This guide explains how to stream results from a RAG application. It covers streaming tokens from the final output as well as intermediate steps of a chain (e.g., from query re-writing).\n",
    "\n",
    "We'll work off of the Q&A app with sources we built over the [LLM Powered Autonomous Agents](https://lilianweng.github.io/posts/2023-06-23-agent/) blog post by Lilian Weng in the [RAG tutorial](/docs/tutorials/rag)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "487d8d79-5ee9-4aa4-9fdf-cd5f4303e099",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "### Dependencies\n",
    "\n",
    "We'll use OpenAI embeddings and a Chroma vector store in this walkthrough, but everything shown here works with any [Embeddings](/docs/concepts#embedding-models), [VectorStore](/docs/concepts#vectorstores) or [Retriever](/docs/concepts#retrievers). \n",
    "\n",
    "We'll use the following packages:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "28d272cd-4e31-40aa-bbb4-0be0a1f49a14",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install --upgrade --quiet  langchain langchain-community langchainhub langchain-openai langchain-chroma bs4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51ef48de-70b6-4f43-8e0b-ab9b84c9c02a",
   "metadata": {},
   "source": [
    "We need to set environment variable `OPENAI_API_KEY`, which can be done directly or loaded from a `.env` file like so:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "143787ca-d8e6-4dc9-8281-4374f4d71720",
   "metadata": {},
   "outputs": [],
   "source": [
    "import getpass\n",
    "import os\n",
    "\n",
    "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass()\n",
    "\n",
    "# import dotenv\n",
    "\n",
    "# dotenv.load_dotenv()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1665e740-ce01-4f09-b9ed-516db0bd326f",
   "metadata": {},
   "source": [
    "### LangSmith\n",
    "\n",
    "Many of the applications you build with LangChain will contain multiple steps with multiple invocations of LLM calls. As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your chain or agent. The best way to do this is with [LangSmith](https://smith.langchain.com).\n",
    "\n",
    "Note that LangSmith is not needed, but it is helpful. If you do want to use LangSmith, after you sign up at the link above, make sure to set your environment variables to start logging traces:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "07411adb-3722-4f65-ab7f-8f6f57663d11",
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"LANGCHAIN_TRACING_V2\"] = \"true\"\n",
    "os.environ[\"LANGCHAIN_API_KEY\"] = getpass.getpass()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e2a72ca8-f8c8-4c0e-929a-223946c63f12",
   "metadata": {},
   "source": [
    "## RAG chain\n",
    "\n",
    "Let's first select a LLM:\n",
    "\n",
    "```{=mdx}\n",
    "import ChatModelTabs from \"@theme/ChatModelTabs\";\n",
    "\n",
    "<ChatModelTabs customVarName=\"llm\" />\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "accc4c35-e17c-4bf0-8a11-cd9e53436a3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# | output: false\n",
    "# | echo: false\n",
    "\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "llm = ChatOpenAI()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa6ba684-26cf-4860-904e-a4d51380c134",
   "metadata": {},
   "source": [
    "Here is Q&A app with sources we built over the [LLM Powered Autonomous Agents](https://lilianweng.github.io/posts/2023-06-23-agent/) blog post by Lilian Weng in the [RAG tutorial](/docs/tutorials/rag):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "820244ae-74b4-4593-b392-822979dd91b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bs4\n",
    "from langchain.chains import create_retrieval_chain\n",
    "from langchain.chains.combine_documents import create_stuff_documents_chain\n",
    "from langchain_chroma import Chroma\n",
    "from langchain_community.document_loaders import WebBaseLoader\n",
    "from langchain_core.prompts import ChatPromptTemplate\n",
    "from langchain_openai import OpenAIEmbeddings\n",
    "from langchain_text_splitters import RecursiveCharacterTextSplitter\n",
    "\n",
    "# 1. Load, chunk and index the contents of the blog to create a retriever.\n",
    "loader = WebBaseLoader(\n",
    "    web_paths=(\"https://lilianweng.github.io/posts/2023-06-23-agent/\",),\n",
    "    bs_kwargs=dict(\n",
    "        parse_only=bs4.SoupStrainer(\n",
    "            class_=(\"post-content\", \"post-title\", \"post-header\")\n",
    "        )\n",
    "    ),\n",
    ")\n",
    "docs = loader.load()\n",
    "\n",
    "text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)\n",
    "splits = text_splitter.split_documents(docs)\n",
    "vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())\n",
    "retriever = vectorstore.as_retriever()\n",
    "\n",
    "\n",
    "# 2. Incorporate the retriever into a question-answering chain.\n",
    "system_prompt = (\n",
    "    \"You are an assistant for question-answering tasks. \"\n",
    "    \"Use the following pieces of retrieved context to answer \"\n",
    "    \"the question. If you don't know the answer, say that you \"\n",
    "    \"don't know. Use three sentences maximum and keep the \"\n",
    "    \"answer concise.\"\n",
    "    \"\\n\\n\"\n",
    "    \"{context}\"\n",
    ")\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", system_prompt),\n",
    "        (\"human\", \"{input}\"),\n",
    "    ]\n",
    ")\n",
    "\n",
    "question_answer_chain = create_stuff_documents_chain(llm, prompt)\n",
    "rag_chain = create_retrieval_chain(retriever, question_answer_chain)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c2f99b5-80b4-4178-bf30-c1c0a152638f",
   "metadata": {},
   "source": [
    "## Streaming final outputs\n",
    "\n",
    "The chain constructed by `create_retrieval_chain` returns a dict with keys `\"input\"`, `\"context\"`, and `\"answer\"`. The `.stream` method will by default stream each key in a sequence.\n",
    "\n",
    "Note that here only the `\"answer\"` key is streamed token-by-token, as the other components-- such as retrieval-- do not support token-level streaming."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "ded41680-b749-4e2a-9daa-b1165d74783b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'input': 'What is Task Decomposition?'}\n",
      "{'context': [Document(page_content='Fig. 1. Overview of a LLM-powered autonomous agent system.\\nComponent One: Planning#\\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\\nTask Decomposition#\\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}), Document(page_content='Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}), Document(page_content='Resources:\\n1. Internet access for searches and information gathering.\\n2. Long Term memory management.\\n3. GPT-3.5 powered Agents for delegation of simple tasks.\\n4. File output.\\n\\nPerformance Evaluation:\\n1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\\n2. Constructively self-criticize your big-picture behavior constantly.\\n3. Reflect on past decisions and strategies to refine your approach.\\n4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}), Document(page_content=\"(3) Task execution: Expert models execute on the specific tasks and log results.\\nInstruction:\\n\\nWith the input and the inference results, the AI assistant needs to describe the process and results. The previous stages can be formed as - User Input: {{ User Input }}, Task Planning: {{ Tasks }}, Model Selection: {{ Model Assignment }}, Task Execution: {{ Predictions }}. You must first answer the user's request in a straightforward manner. Then describe the task process and show your analysis and model inference results to the user in the first person. If inference results contain a file path, must tell the user the complete file path.\", metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'})]}\n",
      "{'answer': ''}\n",
      "{'answer': 'Task'}\n",
      "{'answer': ' decomposition'}\n",
      "{'answer': ' involves'}\n",
      "{'answer': ' breaking'}\n",
      "{'answer': ' down'}\n",
      "{'answer': ' complex'}\n",
      "{'answer': ' tasks'}\n",
      "{'answer': ' into'}\n",
      "{'answer': ' smaller'}\n",
      "{'answer': ' and'}\n",
      "{'answer': ' simpler'}\n",
      "{'answer': ' steps'}\n",
      "{'answer': ' to'}\n",
      "{'answer': ' make'}\n",
      "{'answer': ' them'}\n",
      "{'answer': ' more'}\n",
      "{'answer': ' manageable'}\n",
      "{'answer': '.'}\n",
      "{'answer': ' This'}\n",
      "{'answer': ' process'}\n",
      "{'answer': ' can'}\n",
      "{'answer': ' be'}\n",
      "{'answer': ' facilitated'}\n",
      "{'answer': ' by'}\n",
      "{'answer': ' techniques'}\n",
      "{'answer': ' like'}\n",
      "{'answer': ' Chain'}\n",
      "{'answer': ' of'}\n",
      "{'answer': ' Thought'}\n",
      "{'answer': ' ('}\n",
      "{'answer': 'Co'}\n",
      "{'answer': 'T'}\n",
      "{'answer': ')'}\n",
      "{'answer': ' and'}\n",
      "{'answer': ' Tree'}\n",
      "{'answer': ' of'}\n",
      "{'answer': ' Thoughts'}\n",
      "{'answer': ','}\n",
      "{'answer': ' which'}\n",
      "{'answer': ' help'}\n",
      "{'answer': ' agents'}\n",
      "{'answer': ' plan'}\n",
      "{'answer': ' and'}\n",
      "{'answer': ' execute'}\n",
      "{'answer': ' tasks'}\n",
      "{'answer': ' effectively'}\n",
      "{'answer': ' by'}\n",
      "{'answer': ' dividing'}\n",
      "{'answer': ' them'}\n",
      "{'answer': ' into'}\n",
      "{'answer': ' sub'}\n",
      "{'answer': 'goals'}\n",
      "{'answer': ' or'}\n",
      "{'answer': ' multiple'}\n",
      "{'answer': ' reasoning'}\n",
      "{'answer': ' possibilities'}\n",
      "{'answer': '.'}\n",
      "{'answer': ' Task'}\n",
      "{'answer': ' decomposition'}\n",
      "{'answer': ' can'}\n",
      "{'answer': ' be'}\n",
      "{'answer': ' initiated'}\n",
      "{'answer': ' through'}\n",
      "{'answer': ' simple'}\n",
      "{'answer': ' prompts'}\n",
      "{'answer': ','}\n",
      "{'answer': ' task'}\n",
      "{'answer': '-specific'}\n",
      "{'answer': ' instructions'}\n",
      "{'answer': ','}\n",
      "{'answer': ' or'}\n",
      "{'answer': ' human'}\n",
      "{'answer': ' inputs'}\n",
      "{'answer': ' to'}\n",
      "{'answer': ' guide'}\n",
      "{'answer': ' the'}\n",
      "{'answer': ' agent'}\n",
      "{'answer': ' in'}\n",
      "{'answer': ' achieving'}\n",
      "{'answer': ' its'}\n",
      "{'answer': ' goals'}\n",
      "{'answer': ' efficiently'}\n",
      "{'answer': '.'}\n",
      "{'answer': ''}\n"
     ]
    }
   ],
   "source": [
    "for chunk in rag_chain.stream({\"input\": \"What is Task Decomposition?\"}):\n",
    "    print(chunk)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "72380afa-965d-4715-aac4-6049cce56313",
   "metadata": {},
   "source": [
    "We are free to process chunks as they are streamed out. If we just want to stream the answer tokens, for example, we can select chunks with the corresponding key:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "738eb33e-6ccd-4b26-b563-beef216fb113",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Task| decomposition| is| a| technique| used| to| break| down| complex| tasks| into| smaller| and| more| manageable| steps|.| This| process| helps| agents| or| models| handle| intricate| tasks| by| dividing| them| into| simpler| sub|tasks|.| By| decom|posing| tasks|,| the| model| can| effectively| plan| and| execute| each| step| towards| achieving| the| overall| goal|.|"
     ]
    }
   ],
   "source": [
    "for chunk in rag_chain.stream({\"input\": \"What is Task Decomposition?\"}):\n",
    "    if answer_chunk := chunk.get(\"answer\"):\n",
    "        print(f\"{answer_chunk}|\", end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b2d224d-2a82-418b-b562-01ea210b86ef",
   "metadata": {},
   "source": [
    "More simply, we can use the [.pick](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.base.Runnable.html#langchain_core.runnables.base.Runnable.pick) method to select only the desired key:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "16c20971-a6fd-4b57-83cd-7b2b453f97c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|Task| decomposition| involves| breaking| down| complex| tasks| into| smaller| and| simpler| steps| to| make| them| more| manageable| for| an| agent| or| model| to| handle|.| This| process| helps| in| planning| and| executing| tasks| efficiently| by| dividing| them| into| a| series| of| sub|goals| or| actions|.| Task| decomposition| can| be| achieved| through| techniques| like| Chain| of| Thought| (|Co|T|)| or| Tree| of| Thoughts|,| which| enhance| model| performance| on| intricate| tasks| by| guiding| them| through| step|-by|-step| thinking| processes|.||"
     ]
    }
   ],
   "source": [
    "chain = rag_chain.pick(\"answer\")\n",
    "\n",
    "for chunk in chain.stream({\"input\": \"What is Task Decomposition?\"}):\n",
    "    print(f\"{chunk}|\", end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdee7ae6-4a81-46ab-8efd-d2310b596f8c",
   "metadata": {},
   "source": [
    "## Streaming intermediate steps\n",
    "\n",
    "Suppose we want to stream not only the final outputs of the chain, but also some intermediate steps. As an example let's take our [Conversational RAG](/docs/tutorials/qa_chat_history) chain. Here we reformulate the user question before passing it to the retriever. This reformulated question is not returned as part of the final output. We could modify our chain to return the new question, but for demonstration purposes we'll leave it as is."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "f4d7714e-bdca-419d-a6c6-7c1a70a69297",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import create_history_aware_retriever\n",
    "from langchain_core.prompts import MessagesPlaceholder\n",
    "\n",
    "### Contextualize question ###\n",
    "contextualize_q_system_prompt = (\n",
    "    \"Given a chat history and the latest user question \"\n",
    "    \"which might reference context in the chat history, \"\n",
    "    \"formulate a standalone question which can be understood \"\n",
    "    \"without the chat history. Do NOT answer the question, \"\n",
    "    \"just reformulate it if needed and otherwise return it as is.\"\n",
    ")\n",
    "contextualize_q_prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", contextualize_q_system_prompt),\n",
    "        MessagesPlaceholder(\"chat_history\"),\n",
    "        (\"human\", \"{input}\"),\n",
    "    ]\n",
    ")\n",
    "contextualize_q_llm = llm.with_config(tags=[\"contextualize_q_llm\"])\n",
    "history_aware_retriever = create_history_aware_retriever(\n",
    "    contextualize_q_llm, retriever, contextualize_q_prompt\n",
    ")\n",
    "\n",
    "\n",
    "### Answer question ###\n",
    "system_prompt = (\n",
    "    \"You are an assistant for question-answering tasks. \"\n",
    "    \"Use the following pieces of retrieved context to answer \"\n",
    "    \"the question. If you don't know the answer, say that you \"\n",
    "    \"don't know. Use three sentences maximum and keep the \"\n",
    "    \"answer concise.\"\n",
    "    \"\\n\\n\"\n",
    "    \"{context}\"\n",
    ")\n",
    "qa_prompt = ChatPromptTemplate.from_messages(\n",
    "    [\n",
    "        (\"system\", system_prompt),\n",
    "        MessagesPlaceholder(\"chat_history\"),\n",
    "        (\"human\", \"{input}\"),\n",
    "    ]\n",
    ")\n",
    "question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)\n",
    "\n",
    "rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad306179-b6f0-4ade-9ec5-06e04fbb8d69",
   "metadata": {},
   "source": [
    "Note that above we use `.with_config` to assign a tag to the LLM that is used for the question re-phrasing step. This is not necessary but will make it more convenient to stream output from that specific step.\n",
    "\n",
    "To demonstrate, we will pass in an artificial message history:\n",
    "```\n",
    "Human: What is task decomposition?\n",
    "\n",
    "AI: Task decomposition involves breaking up a complex task into smaller and simpler steps.\n",
    "```\n",
    "We then ask a follow up question: \"What are some common ways of doing it?\" Leading into the retrieval step, our `history_aware_retriever` will rephrase this question using the conversation's context to ensure that the retrieval is meaningful.\n",
    "\n",
    "To stream intermediate output, we recommend use of the async `.astream_events` method. This method will stream output from all \"events\" in the chain, and can be quite verbose. We can filter using tags, event types, and other criteria, as we do here.\n",
    "\n",
    "Below we show a typical `.astream_events` loop, where we pass in the chain input and emit desired results. See the [API reference](https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.base.Runnable.html#langchain_core.runnables.base.Runnable.astream_events) and [streaming guide](/docs/how_to/streaming) for more detail."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "3ef2af40-e6ce-42a3-ad6a-ee405ad7f8ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|What| are| some| typical| methods| used| for| task| decomposition|?||"
     ]
    }
   ],
   "source": [
    "first_question = \"What is task decomposition?\"\n",
    "first_answer = (\n",
    "    \"Task decomposition involves breaking up \"\n",
    "    \"a complex task into smaller and simpler \"\n",
    "    \"steps.\"\n",
    ")\n",
    "follow_up_question = \"What are some common ways of doing it?\"\n",
    "\n",
    "chat_history = [\n",
    "    (\"human\", first_question),\n",
    "    (\"ai\", first_answer),\n",
    "]\n",
    "\n",
    "\n",
    "async for event in rag_chain.astream_events(\n",
    "    {\n",
    "        \"input\": follow_up_question,\n",
    "        \"chat_history\": chat_history,\n",
    "    },\n",
    "    version=\"v1\",\n",
    "):\n",
    "    if (\n",
    "        event[\"event\"] == \"on_chat_model_stream\"\n",
    "        and \"contextualize_q_llm\" in event[\"tags\"]\n",
    "    ):\n",
    "        ai_message_chunk = event[\"data\"][\"chunk\"]\n",
    "        print(f\"{ai_message_chunk.content}|\", end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7da5dd1b-634c-4dd7-8235-69adec21d195",
   "metadata": {},
   "source": [
    "Here we recover, token-by-token, the query that is passed into the retriever given our question \"What are some common ways of doing it?\"\n",
    "\n",
    "If we wanted to get our retrieved docs, we could filter on name \"Retriever\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "987ef6be-8c4e-4257-828a-a3b4fb4ccc99",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'event': 'on_retriever_start', 'name': 'Retriever', 'run_id': '6834097c-07fe-42f5-a566-a4780af4d1d0', 'tags': ['seq:step:4', 'Chroma', 'OpenAIEmbeddings'], 'metadata': {}, 'data': {'input': {'query': 'What are some typical methods used for task decomposition?'}}}\n",
      "\n",
      "{'event': 'on_retriever_end', 'name': 'Retriever', 'run_id': '6834097c-07fe-42f5-a566-a4780af4d1d0', 'tags': ['seq:step:4', 'Chroma', 'OpenAIEmbeddings'], 'metadata': {}, 'data': {'input': {'query': 'What are some typical methods used for task decomposition?'}, 'output': {'documents': [Document(page_content='Tree of Thoughts (Yao et al. 2023) extends CoT by exploring multiple reasoning possibilities at each step. It first decomposes the problem into multiple thought steps and generates multiple thoughts per step, creating a tree structure. The search process can be BFS (breadth-first search) or DFS (depth-first search) with each state evaluated by a classifier (via a prompt) or majority vote.\\nTask decomposition can be done (1) by LLM with simple prompting like \"Steps for XYZ.\\\\n1.\", \"What are the subgoals for achieving XYZ?\", (2) by using task-specific instructions; e.g. \"Write a story outline.\" for writing a novel, or (3) with human inputs.', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}), Document(page_content='Fig. 1. Overview of a LLM-powered autonomous agent system.\\nComponent One: Planning#\\nA complicated task usually involves many steps. An agent needs to know what they are and plan ahead.\\nTask Decomposition#\\nChain of thought (CoT; Wei et al. 2022) has become a standard prompting technique for enhancing model performance on complex tasks. The model is instructed to “think step by step” to utilize more test-time computation to decompose hard tasks into smaller and simpler steps. CoT transforms big tasks into multiple manageable tasks and shed lights into an interpretation of the model’s thinking process.', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}), Document(page_content='Resources:\\n1. Internet access for searches and information gathering.\\n2. Long Term memory management.\\n3. GPT-3.5 powered Agents for delegation of simple tasks.\\n4. File output.\\n\\nPerformance Evaluation:\\n1. Continuously review and analyze your actions to ensure you are performing to the best of your abilities.\\n2. Constructively self-criticize your big-picture behavior constantly.\\n3. Reflect on past decisions and strategies to refine your approach.\\n4. Every command has a cost, so be smart and efficient. Aim to complete tasks in the least number of steps.', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'}), Document(page_content='Fig. 9. Comparison of MIPS algorithms, measured in recall@10. (Image source: Google Blog, 2020)\\nCheck more MIPS algorithms and performance comparison in ann-benchmarks.com.\\nComponent Three: Tool Use#\\nTool use is a remarkable and distinguishing characteristic of human beings. We create, modify and utilize external objects to do things that go beyond our physical and cognitive limits. Equipping LLMs with external tools can significantly extend the model capabilities.', metadata={'source': 'https://lilianweng.github.io/posts/2023-06-23-agent/'})]}}}\n",
      "\n"
     ]
    }
   ],
   "source": [
    "async for event in rag_chain.astream_events(\n",
    "    {\n",
    "        \"input\": follow_up_question,\n",
    "        \"chat_history\": chat_history,\n",
    "    },\n",
    "    version=\"v1\",\n",
    "):\n",
    "    if event[\"name\"] == \"Retriever\":\n",
    "        print(event)\n",
    "        print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5470a79-258a-4108-8ceb-dfe8180160ca",
   "metadata": {},
   "source": [
    "For more on how to stream intermediate steps check out the [streaming guide](/docs/how_to/streaming)."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}