{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90cd3ded",
   "metadata": {},
   "source": [
    "# Astra DB \n",
    "\n",
    "> DataStax [Astra DB](https://docs.datastax.com/en/astra/home/astra.html) is a serverless vector-capable database built on Cassandra and made conveniently available through an easy-to-use JSON API.\n",
    "\n",
    "This notebook goes over how to use Astra DB to store chat message history."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f507f58b-bf22-4a48-8daf-68d869bcd1ba",
   "metadata": {},
   "source": [
    "## Setting up\n",
    "\n",
    "To run this notebook you need a running Astra DB. Get the connection secrets on your Astra dashboard:\n",
    "\n",
    "- the API Endpoint looks like `https://01234567-89ab-cdef-0123-456789abcdef-us-east1.apps.astra.datastax.com`;\n",
    "- the Token looks like `AstraCS:6gBhNmsk135...`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7092199",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install --quiet \"astrapy>=0.6.2\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3d97b65",
   "metadata": {},
   "source": [
    "### Set up the database connection parameters and secrets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "163d97f0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "ASTRA_DB_API_ENDPOINT =  https://01234567-89ab-cdef-0123-456789abcdef-us-east1.apps.astra.datastax.com\n",
      "ASTRA_DB_APPLICATION_TOKEN =  ········\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "\n",
    "ASTRA_DB_API_ENDPOINT = input(\"ASTRA_DB_API_ENDPOINT = \")\n",
    "ASTRA_DB_APPLICATION_TOKEN = getpass.getpass(\"ASTRA_DB_APPLICATION_TOKEN = \")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55860b2d",
   "metadata": {},
   "source": [
    "Depending on whether local or cloud-based Astra DB, create the corresponding database connection \"Session\" object."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c163e8",
   "metadata": {},
   "source": [
    "## Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d15e3302",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.memory import AstraDBChatMessageHistory\n",
    "\n",
    "message_history = AstraDBChatMessageHistory(\n",
    "    session_id=\"test-session\",\n",
    "    api_endpoint=ASTRA_DB_API_ENDPOINT,\n",
    "    token=ASTRA_DB_APPLICATION_TOKEN,\n",
    ")\n",
    "\n",
    "message_history.add_user_message(\"hi!\")\n",
    "\n",
    "message_history.add_ai_message(\"whats up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "64fc465e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='hi!'), AIMessage(content='whats up?')]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "message_history.messages"
   ]
  }
 ],
 "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
