{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "26b62e0c", "metadata": {}, "outputs": [], "source": [ "%load_ext autoreload\n", "%autoreload " ] }, { "cell_type": "code", "execution_count": 2, "id": "b1a6a020", "metadata": { "scrolled": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/home/zuppif/miniconda3/envs/activeloop/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.4.3) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.\n", " warnings.warn(\n", "-" ] }, { "name": "stdout", "output_type": "stream", "text": [ "This dataset can be visualized in Jupyter Notebook by ds.visualize() or at https://app.activeloop.ai/zuppif/disney-lyrics-emotions\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\\" ] }, { "name": "stdout", "output_type": "stream", "text": [ "hub://zuppif/disney-lyrics-emotions loaded successfully.\n", "\n", "Deep Lake Dataset in hub://zuppif/disney-lyrics-emotions already exists, loading from the storage\n", "Dataset(path='hub://zuppif/disney-lyrics-emotions', read_only=True, tensors=['embedding', 'ids', 'metadata', 'text'])\n", "\n", " tensor htype shape dtype compression\n", " ------- ------- ------- ------- ------- \n", " embedding generic (85, 1536) float32 None \n", " ids text (85, 1) str None \n", " metadata json (85, 1) str None \n", " text text (85, 1) str None \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "\r", " \r", "\r", " \r" ] } ], "source": [ "from dotenv import load_dotenv\n", "load_dotenv() \n", "from names import DATASET_ID, MODEL_ID\n", "from data import load_db\n", "import os\n", "from langchain.chains import RetrievalQA, ConversationalRetrievalChain\n", "from langchain.vectorstores import DeepLake\n", "from langchain.llms import OpenAI\n", "from langchain.embeddings.openai import OpenAIEmbeddings\n", "from langchain.chat_models import ChatOpenAI\n", "\n", "embeddings = OpenAIEmbeddings(model=MODEL_ID)\n", "dataset_path = f\"hub://{os.environ['ACTIVELOOP_ORG_ID']}/{DATASET_ID}\"\n", "\n", "db = load_db(dataset_path, embedding_function=embeddings, token=os.environ['ACTIVELOOP_TOKEN'], org_id=os.environ[\"ACTIVELOOP_ORG_ID\"], read_only=True)" ] }, { "cell_type": "markdown", "id": "97c3370c", "metadata": {}, "source": [ "## Using similarity search" ] }, { "cell_type": "code", "execution_count": 75, "id": "07d8a381", "metadata": {}, "outputs": [], "source": [ "from langchain.chains import LLMChain\n", "from langchain.prompts import PromptTemplate\n", "from pathlib import Path\n", "\n", "prompt = PromptTemplate(\n", " input_variables=[\"content\"],\n", " template=Path(\"prompts/bot.prompt\").read_text(),\n", ")\n", "\n", "llm = ChatOpenAI(temperature=0.7)\n", "\n", "chain = LLMChain(llm=llm, prompt=prompt)" ] }, { "cell_type": "code", "execution_count": 76, "id": "ebca722d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Exhaustion, Fatigue, Sleepiness, Drained.'" ] }, "execution_count": 76, "metadata": {}, "output_type": "execute_result" } ], "source": [ "emotions = chain.run(content=\"Damn I am feeling so tired\")\n", "emotions" ] }, { "cell_type": "code", "execution_count": 77, "id": "9598a36c", "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[(Document(page_content='Hopeful, determined, inspired, optimistic, longing, driven, passionate, adventurous.', metadata={'movie': 'Hercules', 'name': 'Go the Distance', 'embed_url': 'https://open.spotify.com/embed/track/0D1OY0M5A0qD5HGBvFmFid?utm_source=generator'}), 0.8135085701942444), (Document(page_content='upset, mad, regret, sad, fine, longing, hopeful, impatient', metadata={'movie': 'Encanto', 'name': 'Waiting on a Miracle', 'embed_url': 'https://open.spotify.com/embed/track/3oRW9ZGPRbLRMneQ5lwflt?utm_source=generator'}), 0.8108540177345276), (Document(page_content='nasty, repentant, magic, sad, lonely, bored, withdrawn, busy', metadata={'movie': 'The Little Mermaid', 'name': 'Poor Unfortunate Souls', 'embed_url': 'https://open.spotify.com/embed/track/7zsw78LtXUD7JfEwH64HK2?utm_source=generator'}), 0.8080281615257263), (Document(page_content='hopeful, optimistic, dreamy, inspired, happy, content, fulfilled, grateful', metadata={'movie': 'Pinocchio', 'name': 'When You Wish Upon a Star', 'embed_url': 'https://open.spotify.com/embed/track/1WrPa4lrIddctGWAIYYfP9?utm_source=generator'}), 0.8055723309516907)]\n", "https://open.spotify.com/embed/track/0D1OY0M5A0qD5HGBvFmFid?utm_source=generator\n", "page_content='Hopeful, determined, inspired, optimistic, longing, driven, passionate, adventurous.' metadata={'movie': 'Hercules', 'name': 'Go the Distance', 'embed_url': 'https://open.spotify.com/embed/track/0D1OY0M5A0qD5HGBvFmFid?utm_source=generator'}\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matches = db.similarity_search_with_score(emotions, distance_metric=\"cos\")\n", "print(matches)\n", "doc, score = matches[0]\n", "print(doc.metadata[\"embed_url\"])\n", "print(doc)\n", "\n", "from IPython.display import IFrame\n", "IFrame(doc.metadata[\"embed_url\"], width=700, height=350)" ] }, { "cell_type": "markdown", "id": "8a474a1c", "metadata": {}, "source": [ "## Using all the songs emotions in the prommpt" ] }, { "cell_type": "code", "execution_count": 23, "id": "c3cb2f3d", "metadata": {}, "outputs": [], "source": [ "import json\n", "from langchain.chains import LLMChain\n", "from langchain.prompts import PromptTemplate\n", "from pathlib import Path\n", "\n", "prompt = PromptTemplate(\n", " input_variables=[\"songs\", \"user_input\"],\n", " template=Path(\"prompts/bot_with_summary.prompt\").read_text(),\n", ")\n", "\n", "llm = ChatOpenAI(temperature=0.7)\n", "\n", "chain = LLMChain(llm=llm, prompt=prompt)" ] }, { "cell_type": "markdown", "id": "b1ca9c9c", "metadata": {}, "source": [ "Let's create the songs string" ] }, { "cell_type": "code", "execution_count": 24, "id": "00416443", "metadata": {}, "outputs": [], "source": [ "with open(\"data/emotions_with_spotify_url.json\", \"r\") as f:\n", " data = json.load(f)\n", " \n", "movies_and_names_to_songs = {}" ] }, { "cell_type": "code", "execution_count": 25, "id": "e4bf60d4", "metadata": { "scrolled": true }, "outputs": [], "source": [ "songs_str = \"\"\n", "\n", "for movie, songs in data.items():\n", " for song in songs:\n", " movie_and_name = f\"{movie};{song['name']}\".lower()\n", " songs_str += f\"{movie_and_name}:{song['text']}\\n\"\n", " movies_and_names_to_songs[movie_and_name] = song" ] }, { "cell_type": "code", "execution_count": 26, "id": "32cd1a47", "metadata": {}, "outputs": [], "source": [ "# prompt.format(songs=songs_str, user_input=\"I am feeling great today\")" ] }, { "cell_type": "code", "execution_count": 30, "id": "a056e5e9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'[\"coco;remember me (dĂșo)\", \"mulan;reflection\", \"frozen;do you want to build a snowman?\"]'" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "res = chain.run(songs=songs_str, user_input=\"I am sad\")\n", "res" ] }, { "cell_type": "code", "execution_count": 31, "id": "e84eeeaa", "metadata": {}, "outputs": [], "source": [ "import random\n", "\n", "res = random.choice(eval(res))" ] }, { "cell_type": "code", "execution_count": 32, "id": "e24ed65f", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "frozen;do you want to build a snowman?\n" ] }, { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "print(res)\n", "doc = movies_and_names_to_songs[res]\n", "\n", "from IPython.display import IFrame\n", "IFrame(doc[\"embed_url\"], width=700, height=350)" ] }, { "cell_type": "code", "execution_count": null, "id": "03de1b93", "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.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }