{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "26b62e0c",
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"%autoreload "
]
},
{
"cell_type": "code",
"execution_count": 74,
"id": "b1a6a020",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/"
]
},
{
"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": "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": "e59a45b7",
"metadata": {},
"source": [
"## Using all the songs emotions in the prommpt"
]
},
{
"cell_type": "code",
"execution_count": 66,
"id": "7b10a5f2",
"metadata": {},
"outputs": [],
"source": [
"import json\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": "70b8c445",
"metadata": {},
"source": [
"Let's create the songs string"
]
},
{
"cell_type": "code",
"execution_count": 67,
"id": "36df1c27",
"metadata": {},
"outputs": [],
"source": [
"with open(\"data/emotions_with_spotify_url.json\", \"r\") as f:\n",
" data = json.load(f)"
]
},
{
"cell_type": "code",
"execution_count": 68,
"id": "e3e496da",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"songs_str = \"\"\n",
"\n",
"for movie, songs in data.items():\n",
" for song in songs:\n",
" songs_str += f\"{song['name']}: {song['text']}\\n\""
]
},
{
"cell_type": "code",
"execution_count": 71,
"id": "070b09d8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'Zero to Hero'"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain.run(songs=songs_str, user_input=\"Damn I am feeling so tired\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b6c6e90c",
"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
}