{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "90cd3ded",
   "metadata": {},
   "source": [
    "# Cassandra Chat Message History\n",
    "\n",
    ">[Apache Cassandra®](https://cassandra.apache.org) is a NoSQL, row-oriented, highly scalable and highly available database, well suited for storing large amounts of data.\n",
    "\n",
    "Cassandra is a good choice for storing chat message history because it is easy to scale and can handle a large number of writes.\n",
    "\n",
    "This notebook goes over how to use Cassandra to store chat message history.\n",
    "\n",
    "To run this notebook you need either a running Cassandra cluster or a DataStax Astra DB instance running in the cloud (you can get one for free at [datastax.com](https://astra.datastax.com)). Check [cassio.org](https://cassio.org/start_here/) for more information."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7092199",
   "metadata": {},
   "outputs": [],
   "source": [
    "!pip install \"cassio>=0.0.7\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e3d97b65",
   "metadata": {},
   "source": [
    "### Please provide database connection parameters and secrets:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "163d97f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import getpass\n",
    "\n",
    "database_mode = (input('\\n(C)assandra or (A)stra DB? ')).upper()\n",
    "\n",
    "keyspace_name = input('\\nKeyspace name? ')\n",
    "\n",
    "if database_mode == 'A':\n",
    "    ASTRA_DB_APPLICATION_TOKEN = getpass.getpass('\\nAstra DB Token (\"AstraCS:...\") ')\n",
    "    #\n",
    "    ASTRA_DB_SECURE_BUNDLE_PATH = input('Full path to your Secure Connect Bundle? ')\n",
    "elif database_mode == 'C':\n",
    "    CASSANDRA_CONTACT_POINTS = input('Contact points? (comma-separated, empty for localhost) ').strip()"
   ]
  },
  {
   "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": "code",
   "execution_count": null,
   "id": "8dff2798",
   "metadata": {},
   "outputs": [],
   "source": [
    "from cassandra.cluster import Cluster\n",
    "from cassandra.auth import PlainTextAuthProvider\n",
    "\n",
    "if database_mode == 'C':\n",
    "    if CASSANDRA_CONTACT_POINTS:\n",
    "        cluster = Cluster([\n",
    "            cp.strip()\n",
    "            for cp in CASSANDRA_CONTACT_POINTS.split(',')\n",
    "            if cp.strip()\n",
    "        ])\n",
    "    else:\n",
    "        cluster = Cluster()\n",
    "    session = cluster.connect()\n",
    "elif database_mode == 'A':\n",
    "    ASTRA_DB_CLIENT_ID = \"token\"\n",
    "    cluster = Cluster(\n",
    "        cloud={\n",
    "            \"secure_connect_bundle\": ASTRA_DB_SECURE_BUNDLE_PATH,\n",
    "        },\n",
    "        auth_provider=PlainTextAuthProvider(\n",
    "            ASTRA_DB_CLIENT_ID,\n",
    "            ASTRA_DB_APPLICATION_TOKEN,\n",
    "        ),\n",
    "    )\n",
    "    session = cluster.connect()\n",
    "else:\n",
    "    raise NotImplementedError"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36c163e8",
   "metadata": {},
   "source": [
    "### Creation and usage of the Chat Message History"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d15e3302",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.memory import CassandraChatMessageHistory\n",
    "\n",
    "message_history = CassandraChatMessageHistory(\n",
    "    session_id=\"test-session\",\n",
    "    session=session,\n",
    "    keyspace=keyspace_name,\n",
    ")\n",
    "\n",
    "message_history.add_user_message(\"hi!\")\n",
    "\n",
    "message_history.add_ai_message(\"whats up?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "64fc465e",
   "metadata": {},
   "outputs": [],
   "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.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
