{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/aurelio-labs/semantic-router/blob/main/docs/00-introduction.ipynb) [![Open nbviewer](https://raw.githubusercontent.com/pinecone-io/examples/master/assets/nbviewer-shield.svg)](https://nbviewer.org/github/aurelio-labs/semantic-router/blob/main/docs/00-introduction.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Considering Chat History"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Applying semantic-router to the most recent interaction in a conversation can work for many cases but it misses scenarios where information provided in the latest interaction."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/Users/jamesbriggs/opt/anaconda3/envs/decision-layer/lib/python3.11/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "100%|██████████| 83.2M/83.2M [00:09<00:00, 8.45MiB/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'split 1': ['User: Hello! Can you tell me the latest news headlines?',\n",
       "  'Bot: Hi! Sure, here are the top news headlines for today...'],\n",
       " 'split 2': [\"User: That's quite interesting. I'm also looking for some new music to listen to.\",\n",
       "  'Bot: What genre do you prefer?',\n",
       "  'User: I like pop music.',\n",
       "  'Bot: You might enjoy the latest album by Dua Lipa.',\n",
       "  \"User: I'll give it a listen. Also, I'm planning a trip and need some travel tips.\",\n",
       "  'Bot: Sure, where are you planning to go?'],\n",
       " 'split 3': [\"User: I'm thinking of visiting Italy.\",\n",
       "  'Bot: Italy is a beautiful country. Make sure to visit the Colosseum in Rome and the canals in Venice.'],\n",
       " 'split 4': ['User: Those sound like great suggestions. I also need some help with my diet.',\n",
       "  'Bot: What kind of diet are you following?',\n",
       "  \"User: I'm trying to eat more protein.\",\n",
       "  'Bot: Include lean meats, eggs, and legumes in your diet for a protein boost.',\n",
       "  \"User: Thanks for the tips! I'll talk to you later.\",\n",
       "  \"Bot: You're welcome! Don't hesitate to reach out if you need more help.\",\n",
       "  'User: I appreciate it. Goodbye!',\n",
       "  'Bot: Goodbye! Take care!']}"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from semantic_router.schema import Conversation, Message\n",
    "from semantic_router.encoders import FastEmbedEncoder\n",
    "\n",
    "\n",
    "messages = [\n",
    "    \"User: Hello! Can you tell me the latest news headlines?\",\n",
    "    \"Bot: Hi! Sure, here are the top news headlines for today...\",\n",
    "    \"User: That's quite interesting. I'm also looking for some new music to listen to.\",\n",
    "    \"Bot: What genre do you prefer?\",\n",
    "    \"User: I like pop music.\",\n",
    "    \"Bot: You might enjoy the latest album by Dua Lipa.\",\n",
    "    \"User: I'll give it a listen. Also, I'm planning a trip and need some travel tips.\",\n",
    "    \"Bot: Sure, where are you planning to go?\",\n",
    "    \"User: I'm thinking of visiting Italy.\",\n",
    "    \"Bot: Italy is a beautiful country. Make sure to visit the Colosseum in Rome and the canals in Venice.\",\n",
    "    \"User: Those sound like great suggestions. I also need some help with my diet.\",\n",
    "    \"Bot: What kind of diet are you following?\",\n",
    "    \"User: I'm trying to eat more protein.\",\n",
    "    \"Bot: Include lean meats, eggs, and legumes in your diet for a protein boost.\",\n",
    "    \"User: Thanks for the tips! I'll talk to you later.\",\n",
    "    \"Bot: You're welcome! Don't hesitate to reach out if you need more help.\",\n",
    "    \"User: I appreciate it. Goodbye!\",\n",
    "    \"Bot: Goodbye! Take care!\",\n",
    "]\n",
    "\n",
    "encoder = FastEmbedEncoder(model_name=\"sentence-transformers/all-MiniLM-L6-v2\")\n",
    "\n",
    "convo = Conversation(\n",
    "    messages=[\n",
    "        Message(role=m.split(\": \")[0], content=m.split(\": \")[1]) for m in messages\n",
    "    ]\n",
    ")\n",
    "\n",
    "convo.split_by_topic(\n",
    "    encoder=encoder, threshold=0.72, split_method=\"cumulative_similarity_drop\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ml",
   "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}