{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "424a9d8d",
   "metadata": {},
   "source": [
    "## Run Template\n",
    "\n",
    "In `server.py`, set -\n",
    "```\n",
    "add_routes(app, chain_rag_timescale_conv, path=\"/rag_timescale_conversation\")\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5f521923",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langserve.client import RemoteRunnable\n",
    "\n",
    "rag_app = RemoteRunnable(\"http://0.0.0.0:8000/rag_timescale_conversation\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "563a58dd",
   "metadata": {},
   "source": [
    "First, setup the history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "14541994",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"My name is Sven Klemm\"\n",
    "answer = rag_app.invoke(\n",
    "    {\n",
    "        \"question\": question,\n",
    "        \"chat_history\": [],\n",
    "    }\n",
    ")\n",
    "chat_history = [(question, answer)]"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "63e76c4d",
   "metadata": {},
   "source": [
    "Next, use the history for a question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b2d8f735",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The person named Sven Klemm made the following commits:\\n\\n1. Commit \"a31c9b9f8cdfe8643499b710dc983e5c5d6457e4\" on \"Mon May 22 11:34:06 2023 +0200\" with the change summary \"Increase number of sqlsmith loops in nightly CI\". The change details are \"To improve coverage with sqlsmith we run it for longer in the scheduled nightly run.\"\\n\\n2. Commit \"e4ba2bcf560568ae68f3775c058f0a8d7f7c0501\" on \"Wed Nov 9 09:29:36 2022 +0100\" with the change summary \"Remove debian 9 from packages tests.\" The change details are \"Debian 9 is EOL since July 2022 so we won\\'t build packages for it anymore and can remove it from CI.\"'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "answer = rag_app.invoke(\n",
    "    {\n",
    "        \"question\": \"What commits did the person with my name make?\",\n",
    "        \"chat_history\": chat_history,\n",
    "    }\n",
    ")\n",
    "answer"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "bd62df23",
   "metadata": {},
   "source": [
    "## Filter by time\n",
    "\n",
    "You can also use timed filters. For example, the sample dataset doesn't include any commits before 2010, so this should return no matches."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "b0a598b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The context does not provide any information about any commits made by a person named Sven Klemm.'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answer = rag_app.invoke(\n",
    "    {\n",
    "        \"question\": \"What commits did the person with my name make?\",\n",
    "        \"chat_history\": chat_history,\n",
    "        \"end_date\": \"2016-01-01 00:00:00\",\n",
    "    }\n",
    ")\n",
    "answer\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "25851869",
   "metadata": {},
   "source": [
    "However, there is data from 2022, which can be used"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4aef5219",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The person named Sven Klemm made the following commits:\\n\\n1. \"e4ba2bcf560568ae68f3775c058f0a8d7f7c0501\" with the change summary \"Remove debian 9 from packages tests.\" The details of this change are that \"Debian 9 is EOL since July 2022 so we won\\'t build packages for it anymore and can remove it from CI.\"\\n\\n2. \"2f237e6e57e5ac66c126233d66969a1f674ffaa4\" with the change summary \"Add Enterprise Linux 9 packages to RPM package test\". The change details for this commit are not provided.'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answer = rag_app.invoke(\n",
    "    {\n",
    "        \"question\": \"What commits did the person with my name make?\",\n",
    "        \"chat_history\": chat_history,\n",
    "        \"start_date\": \"2020-01-01 00:00:00\",\n",
    "        \"end_date\": \"2023-01-01 00:00:00\",\n",
    "    }\n",
    ")\n",
    "answer"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "6ad86fbd",
   "metadata": {},
   "source": [
    "## Filter by metadata\n",
    "\n",
    "You can also filter by metadata using this chain"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7ac9365f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The person named Sven Klemm made a commit with the ID \"5cd2c038796fb302190b080c90e5acddbef4b8d1\". The change summary for this commit is \"Simplify windows-build-and-test-ignored.yaml\" and the change details are \"Remove code not needed for the skip workflow of the windows test.\" The commit was made on \"Sat Mar 4 10:18:34 2023 +0100\".'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "answer = rag_app.invoke(\n",
    "    {\n",
    "        \"question\": \"What commits did the person with my name make?\",\n",
    "        \"chat_history\": chat_history,\n",
    "        \"metadata_filter\": {\"commit_hash\": \" 5cd2c038796fb302190b080c90e5acddbef4b8d1\"},\n",
    "    }\n",
    ")\n",
    "answer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1cde5da5",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
