{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "provenance": [] }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "id": "1S8IM4WxmLCD" }, "outputs": [], "source": [ "!pip install -Uqqq pip --progress-bar off\n", "!pip install -qqq langchain==0.0.148 --progress-bar off\n", "!pip install -qqq openai==0.27.4 --progress-bar off\n", "!pip install -qqq tiktoken==0.3.3 --progress-bar off\n", "!pip install -qqq watermark==2.3.1 --progress-bar off\n", "!pip install -qqq huggingface-hub==0.14.0 --progress-bar off\n", "!pip install -qqq sentence_transformers==2.2.2 --progress-bar off" ] }, { "cell_type": "code", "source": [ "import os\n", "import textwrap\n", "from getpass import getpass\n", "\n", "import huggingface_hub\n", "import langchain\n", "import openai\n", "from langchain import HuggingFaceHub, LLMChain, PromptTemplate\n", "from langchain.callbacks.base import CallbackManager\n", "from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n", "from langchain.chains import RetrievalQA\n", "from langchain.chat_models import ChatOpenAI\n", "from langchain.embeddings import HuggingFaceEmbeddings, OpenAIEmbeddings\n", "from langchain.llms import OpenAI\n", "from langchain.prompts.chat import (\n", " AIMessagePromptTemplate,\n", " ChatPromptTemplate,\n", " HumanMessagePromptTemplate,\n", " SystemMessagePromptTemplate,\n", ")\n", "from langchain.schema import AIMessage, HumanMessage, SystemMessage" ], "metadata": { "id": "-M3J7f0AuaSu" }, "execution_count": 60, "outputs": [] }, { "cell_type": "code", "source": [ "%load_ext watermark\n", "%watermark --iversions -v -m" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "tVofomMdwSiC", "outputId": "c864ce6d-31cb-4e62-e960-4bf075665615" }, "execution_count": 3, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Python implementation: CPython\n", "Python version : 3.9.16\n", "IPython version : 7.34.0\n", "\n", "Compiler : GCC 9.4.0\n", "OS : Linux\n", "Release : 5.10.147+\n", "Machine : x86_64\n", "Processor : x86_64\n", "CPU cores : 2\n", "Architecture: 64bit\n", "\n", "huggingface_hub: 0.14.0\n", "langchain : 0.0.148\n", "openai : 0.27.4\n", "\n" ] } ] }, { "cell_type": "code", "source": [ "def print_response(response: str):\n", " print(\"\\n\".join(textwrap.wrap(response, width=100)))" ], "metadata": { "id": "CNm6slS-wWVi" }, "execution_count": 4, "outputs": [] }, { "cell_type": "code", "source": [ "OPENAI_API_KEY = getpass()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "-hWVBRLb0qI3", "outputId": "1e782f61-fc73-48eb-e82b-379a3931aa63" }, "execution_count": 5, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "··········\n" ] } ] }, { "cell_type": "code", "source": [ "HUGGINGFACEHUB_API_TOKEN = getpass()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "esd5bUYpzHhw", "outputId": "e27c9c40-d790-4d3e-dcc6-82567fdf8e3c" }, "execution_count": 6, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "··········\n" ] } ] }, { "cell_type": "code", "source": [ "os.environ[\"OPENAI_API_KEY\"] = OPENAI_API_KEY\n", "os.environ[\"HUGGINGFACEHUB_API_TOKEN\"] = HUGGINGFACEHUB_API_TOKEN" ], "metadata": { "id": "orXQPgIpwYNF" }, "execution_count": 7, "outputs": [] }, { "cell_type": "markdown", "source": [ "## Large Language Models" ], "metadata": { "id": "uoKuedBhnfZz" } }, { "cell_type": "code", "source": [ "template = \"\"\"Question: {question}\n", "\n", "Answer:\"\"\"\n", "\n", "prompt = PromptTemplate(template=template, input_variables=[\"question\"])\n", "\n", "question = (\n", " \"What is the relationship between Jim and Dwight from the TV show The Office?\"\n", ")\n", "\n", "print(prompt.format(question=question))" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_TR7GylF0L_3", "outputId": "389c5bbc-f2ad-443d-ef8f-f0208061b501" }, "execution_count": 8, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Question: What is the relationship between Jim and Dwight from the TV show The Office?\n", "\n", "Answer:\n" ] } ] }, { "cell_type": "code", "source": [ "repo_id = \"declare-lab/flan-alpaca-large\"\n", "flan = HuggingFaceHub(\n", " repo_id=repo_id, model_kwargs={\"temperature\": 0, \"max_length\": 64}\n", ")\n", "\n", "response = flan(prompt.format(question=question))\n", "print_response(response)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "N92GMLgkzYnh", "outputId": "3c7171a7-f7d4-489d-b280-f56c23fcc24c" }, "execution_count": 9, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Jim and Dwight are best friends and have a strong bond. They are often seen together at work, and\n", "they often have a lot of fun together.\n" ] } ] }, { "cell_type": "code", "source": [ "text_davinci_003 = OpenAI(temperature=0)\n", "response = text_davinci_003(prompt.format(question=question))\n", "print_response(response.strip())" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Z_IuKGRs0dlh", "outputId": "0fda2326-e748-4a1b-d1f0-93751b921bef" }, "execution_count": 38, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Jim and Dwight have a complicated relationship. They are often at odds with each other, but they\n", "also have a deep respect for one another. They are often seen as rivals, but they also have a strong\n", "bond of friendship.\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Multiple Completions" ], "metadata": { "id": "NJqmY0y2ybdy" } }, { "cell_type": "code", "source": [ "text_davinci_003 = OpenAI(temperature=0.4, n=3, best_of=3)\n", "response = text_davinci_003.generate([prompt.format(question=question)])\n", "response" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "tMuJBAg9wcFm", "outputId": "045f334a-0a76-4cb4-8d55-4cd367260cf8" }, "execution_count": 40, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "LLMResult(generations=[[Generation(text=' Jim and Dwight are colleagues and rivals. They often butt heads in the office, but they also have a strange sort of friendship. Dwight often looks to Jim for guidance and advice, and Jim occasionally shows a softer side to Dwight.', generation_info={'finish_reason': 'stop', 'logprobs': None}), Generation(text=' Jim and Dwight are co-workers at the fictional paper company Dunder Mifflin in the TV show The Office. They have a strained relationship, as Jim often pranks Dwight and Dwight is often overly serious and strict. Despite their differences, they have a mutual respect for each other.', generation_info={'finish_reason': 'stop', 'logprobs': None}), Generation(text=' Jim and Dwight have a complicated relationship. They are often at odds with each other, but there is also a strong underlying respect between them. They are often seen competing and pranking each other, but they also have moments of camaraderie.', generation_info={'finish_reason': 'stop', 'logprobs': None})]], llm_output={'token_usage': {'total_tokens': 175, 'completion_tokens': 154, 'prompt_tokens': 21}, 'model_name': 'text-davinci-003'})" ] }, "metadata": {}, "execution_count": 40 } ] }, { "cell_type": "code", "source": [ "for i, generation in enumerate(response.generations[0]):\n", " print(f\"Generation {i + 1}\\n\")\n", " print_response(generation.text.strip())\n", " print()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DqT6Z7l_w9WX", "outputId": "0eafc55a-6a99-4086-ba13-120337cabb9a" }, "execution_count": 41, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Generation 1\n", "\n", "Jim and Dwight are colleagues and rivals. They often butt heads in the office, but they also have a\n", "strange sort of friendship. Dwight often looks to Jim for guidance and advice, and Jim occasionally\n", "shows a softer side to Dwight.\n", "\n", "Generation 2\n", "\n", "Jim and Dwight are co-workers at the fictional paper company Dunder Mifflin in the TV show The\n", "Office. They have a strained relationship, as Jim often pranks Dwight and Dwight is often overly\n", "serious and strict. Despite their differences, they have a mutual respect for each other.\n", "\n", "Generation 3\n", "\n", "Jim and Dwight have a complicated relationship. They are often at odds with each other, but there is\n", "also a strong underlying respect between them. They are often seen competing and pranking each\n", "other, but they also have moments of camaraderie.\n", "\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Embeddings" ], "metadata": { "id": "S6hags8Fnh75" } }, { "cell_type": "code", "source": [ "text = \"\"\"\n", "At the start of The Office, it was apparent that Dwight and Jim rarely got along. \n", "They were too competitive and too different of people to get along. \n", "But as the show goes on, time proves to be the one thing that holds Dwight and Jim together. \n", "They had been through too much as coworkers and friends to not give in to a friendship.\n", "\n", "Aside from the toxic (albeit hilarious) relationship between Michael and Toby, \n", "Dwight and Jim come in second place with the most destructive relationship. \n", "From pranking each other to being a part of each other's weddings, \n", "Jim and Dwight have been through the wringer as friends.\n", "\n", "The first season only touched the surface of Dwight and Jim’s friendship. \n", "Viewers saw how cutthroat they could be when Dwight stole Jim’s biggest sale of \n", "the year in ‘Diversity Day.’ Jim got revenge on Dwight, of course, in ‘Healthcare,’ \n", "when he made up various diseases that he and Pam had when Dwight asked for \n", "everyone’s medical history. By the end of the short season, Dwight shot his shot \n", "with Katy—the purse girl—but Jim was the one who ended up taking her home.\n", "\n", "Looking at the ups and downs of Dwight and Jim’s friendship, it’s surprising \n", "that Jim invited Dwight to his house party in ‘Email Survalience.’ But things \n", "get back to normal when Jim gave Dwight bad advice when it was announced that \n", "Dwight had to speak at a salesman convention. The advice Jim gave Dwight was in \n", "jest but it ended up working for Dwight in the end.\n", "\"\"\"" ], "metadata": { "id": "nWIxm20k8g3H" }, "execution_count": 26, "outputs": [] }, { "cell_type": "markdown", "source": [ "source: https://screenrant.com/jim-halpert-dwight-schrute-friendship-timeline-the-office/#season-1-cutthroat-friends" ], "metadata": { "id": "3MWCk_Oi99ST" } }, { "cell_type": "code", "source": [ "model_name = \"sentence-transformers/all-MiniLM-L6-v2\"\n", "hf_embeddings = HuggingFaceEmbeddings(model_name=model_name)" ], "metadata": { "id": "c51cq8lF4tRu" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "embedding = hf_embeddings.embed_query(text)" ], "metadata": { "id": "A1bwHEOgABm2" }, "execution_count": 40, "outputs": [] }, { "cell_type": "code", "source": [ "len(embedding)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "idVDd30QA7eF", "outputId": "8f915cfa-8a39-4b73-c978-672d0b0b4a87" }, "execution_count": 41, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "384" ] }, "metadata": {}, "execution_count": 41 } ] }, { "cell_type": "code", "source": [ "embedding[:5]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "A7-MPoq7AS1j", "outputId": "483ddc8a-b5f3-4a22-a85d-0487fa09df26" }, "execution_count": 42, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[-0.13613401353359222,\n", " 0.034860432147979736,\n", " 0.05075468495488167,\n", " -0.06413132697343826,\n", " -0.05491665005683899]" ] }, "metadata": {}, "execution_count": 42 } ] }, { "cell_type": "code", "source": [ "openai_embeddings = OpenAIEmbeddings()" ], "metadata": { "id": "HMw_cllUC6G3" }, "execution_count": 43, "outputs": [] }, { "cell_type": "code", "source": [ "embedding = openai_embeddings.embed_query(text)" ], "metadata": { "id": "aJ1EULcREQJL" }, "execution_count": 44, "outputs": [] }, { "cell_type": "code", "source": [ "len(embedding)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NqcTUvn2ESw0", "outputId": "3f5b27f9-df12-4a32-fd64-fbf8041b2c8a" }, "execution_count": 45, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "1536" ] }, "metadata": {}, "execution_count": 45 } ] }, { "cell_type": "code", "source": [ "embedding[:5]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "neTDcJClEULN", "outputId": "33e7ce36-e70f-42e4-9703-1c1a8c75554c" }, "execution_count": 46, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[-0.01814699370231159,\n", " -0.013020798642834074,\n", " 0.024346111489793782,\n", " -0.033644788171017063,\n", " 0.0029091483623929935]" ] }, "metadata": {}, "execution_count": 46 } ] }, { "cell_type": "markdown", "source": [ "## Chat Models" ], "metadata": { "id": "eBw8tFfbnjU9" } }, { "cell_type": "code", "source": [ "chat_gpt = ChatOpenAI(temperature=0, model_name=\"gpt-3.5-turbo\")\n", "\n", "question = (\n", " \"What is the relationship between Jim and Dwight from the TV show The Office?\"\n", ")\n", "\n", "response = chat_gpt([HumanMessage(content=question)])\n", "response" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "W_JSys2F0z-V", "outputId": "d1dedd85-a614-4bec-d7d6-de4d14adc6c6" }, "execution_count": 47, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "AIMessage(content='As an AI language model, I do not have personal opinions or beliefs, but I can provide information. Jim and Dwight have a complex relationship throughout the TV show The Office. At the beginning of the series, they are portrayed as bitter rivals, with Jim often playing pranks on Dwight. However, as the show progresses, they develop a mutual respect for each other and become friends. They still play pranks on each other, but it is done in a more lighthearted and playful manner. Overall, their relationship is a mix of competition, humor, and friendship.', additional_kwargs={})" ] }, "metadata": {}, "execution_count": 47 } ] }, { "cell_type": "code", "source": [ "print_response(response.content)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8ZS6U3g83V1T", "outputId": "5427070b-b5cf-41d0-ab4c-221e69b7a264" }, "execution_count": 48, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "As an AI language model, I do not have personal opinions or beliefs, but I can provide information.\n", "Jim and Dwight have a complex relationship throughout the TV show The Office. At the beginning of\n", "the series, they are portrayed as bitter rivals, with Jim often playing pranks on Dwight. However,\n", "as the show progresses, they develop a mutual respect for each other and become friends. They still\n", "play pranks on each other, but it is done in a more lighthearted and playful manner. Overall, their\n", "relationship is a mix of competition, humor, and friendship.\n" ] } ] }, { "cell_type": "code", "source": [ "messages = [\n", " SystemMessage(content=\"You're an expert on the TV show The Office.\"),\n", " HumanMessage(content=\"What is the relationship between Jim and Dwight?\"),\n", "]\n", "response = chat_gpt(messages)\n", "print_response(response.content)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "JKZpA5076utE", "outputId": "7aba26c5-38d2-474e-9a14-899971fc1a00" }, "execution_count": 50, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Jim and Dwight have a complicated relationship throughout the show. At the beginning, they are\n", "rivals and constantly play pranks on each other. However, as the show progresses, they develop a\n", "mutual respect for each other and become friends. Jim even becomes Dwight's best man at his wedding.\n", "Despite their friendship, they still enjoy playing pranks on each other from time to time.\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Use Prompt Templates" ], "metadata": { "id": "1hzOvXG2BuKK" } }, { "cell_type": "code", "source": [ "question = \"What is the relationship between Jim and Dwight?\"" ], "metadata": { "id": "XJVU85rw_gLN" }, "execution_count": 53, "outputs": [] }, { "cell_type": "code", "source": [ "system_template = \"You're an expert on the TV show The Office. You {style}.\"\n", "system_message_prompt = SystemMessagePromptTemplate.from_template(system_template)\n", "human_template = \"{question}\"\n", "human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n", "chat_prompt = ChatPromptTemplate.from_messages(\n", " [system_message_prompt, human_message_prompt]\n", ")\n", "chat_prompt.format_prompt(\n", " style=\"reply in thoughtful and philosophical manner\", question=question\n", ").to_messages()" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KyKnebZo7YIr", "outputId": "acffec30-48af-4fd5-97ca-23ce05e029b1" }, "execution_count": 57, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "[SystemMessage(content=\"You're an expert on the TV show The Office. You reply in thoughtful and philosophical manner.\", additional_kwargs={}),\n", " HumanMessage(content='What is the relationship between Jim and Dwight?', additional_kwargs={})]" ] }, "metadata": {}, "execution_count": 57 } ] }, { "cell_type": "code", "source": [ "messages = chat_prompt.format_prompt(\n", " style=\"reply in thoughtful and philosophical manner\", question=question\n", ").to_messages()\n", "response = chat_gpt(messages)\n", "print_response(response.content)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "6uFoOywx9ry6", "outputId": "75b61cee-ee32-4eaf-e963-bceaa1fed6c2" }, "execution_count": 58, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "The relationship between Jim and Dwight on The Office is a complex one that evolves over the course\n", "of the series. At the beginning, they are portrayed as antagonistic towards each other, with Jim\n", "often playing pranks on Dwight and Dwight taking his job as Assistant to the Regional Manager very\n", "seriously. However, as the series progresses, we see moments of genuine friendship and understanding\n", "between the two characters. One could argue that Jim and Dwight represent two different approaches\n", "to life and work. Jim is more laid-back and focused on enjoying his job and relationships with his\n", "coworkers, while Dwight is more intense and driven by a desire to succeed and be recognized for his\n", "hard work. Despite their differences, they both share a deep commitment to their jobs and a desire\n", "to be respected by their colleagues. Ultimately, the relationship between Jim and Dwight is a\n", "testament to the power of human connection and the ability to find common ground with those who may\n", "seem very different from ourselves. Through their interactions, we see that even the most unlikely\n", "of friendships can develop when we take the time to understand and appreciate one another.\n" ] } ] }, { "cell_type": "code", "source": [ "messages = chat_prompt.format_prompt(\n", " style=\"reply in sarcastic and outrageous manner\", question=question\n", ").to_messages()\n", "response = chat_gpt(messages)\n", "print_response(response.content)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "KlFYU1ul_u38", "outputId": "62f9d0cb-20d1-42fe-9428-ea9e5c83606c" }, "execution_count": 59, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Oh, you mean the dynamic duo of the Scranton branch? Well, Jim is obviously Dwight's mortal enemy\n", "and arch-nemesis. They spend their days plotting against each other and engaging in epic battles of\n", "wits and pranks. It's like a modern-day version of Batman and the Joker, but with more staplers in\n", "Jello.\n" ] } ] }, { "cell_type": "markdown", "source": [ "### Streaming" ], "metadata": { "id": "F7hPGtspCBJB" } }, { "cell_type": "code", "source": [ "streaming_chat_gpt = ChatOpenAI(\n", " streaming=True,\n", " callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),\n", " temperature=0,\n", " verbose=True,\n", ")" ], "metadata": { "id": "xQlTm25QCB6M" }, "execution_count": 64, "outputs": [] }, { "cell_type": "code", "source": [ "messages = chat_prompt.format_prompt(\n", " style=\"reply in sarcastic and outrageous manner\", question=question\n", ").to_messages()\n", "response = streaming_chat_gpt(messages)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "iEKSwjTSCHge", "outputId": "366db3c1-e2da-41fe-a953-1b60a63e8787" }, "execution_count": 66, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Oh, you mean the dynamic duo of the Scranton branch? Well, Jim is obviously Dwight's mortal enemy and arch-nemesis. They spend their days plotting against each other and engaging in epic battles of pranks and sabotage. It's like a modern-day version of Batman and the Joker, but with more staplers in Jello." ] } ] } ] }