{ "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\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "|" ] }, { "name": "stdout", "output_type": "stream", "text": [ "hub://zuppif/disney-lyrics loaded successfully.\n", "\n", "Deep Lake Dataset in hub://zuppif/disney-lyrics already exists, loading from the storage\n", "Dataset(path='hub://zuppif/disney-lyrics', 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": 80, "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": 81, "id": "ebca722d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Melancholy, Coziness, Nostalgia, Calmness.'" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "emotions = chain.run(content=\"It's rainy\")\n", "emotions" ] }, { "cell_type": "code", "execution_count": 84, "id": "9598a36c", "metadata": { "scrolled": false }, "outputs": [ { "data": { "text/plain": [ "'https://open.spotify.com/embed/track/5EeQQ8BVJTRkp1AIKJILGY?utm_source=generator'" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doc, score = db.similarity_search_with_score(emotions, distance_metric=\"cos\")[0]\n", "doc.metadata[\"embed_url\"]" ] }, { "cell_type": "code", "execution_count": 83, "id": "d6214e40", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "doc.metadata[\"embed_url\"]\n", "\n", "from IPython.display import IFrame\n", "IFrame(doc.metadata[\"embed_url\"], width=700, height=350)" ] }, { "cell_type": "code", "execution_count": 4, "id": "28ae2c63", "metadata": { "scrolled": true }, "outputs": [ { "data": { "text/plain": [ "Dataset(path='hub://zuppif/disney-lyrics', read_only=True, index=Index([()]), tensors=['embedding', 'ids', 'metadata', 'text'])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "db.ds.query(\"select * where contains(\\\"text\\\", 'Did they') limit 2\")" ] }, { "cell_type": "code", "execution_count": null, "id": "1780552c", "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 }