{ "cells": [ { "cell_type": "markdown", "id": "0fbd8c48", "metadata": {}, "source": [ "\n", "Pip install is the command you use to install Python packages with the help of a tool called Pip package manager.\n", "

Installing LangChain package\n", "
" ] }, { "cell_type": "code", "execution_count": 1, "id": "d4e032ba", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: langchain in c:\\users\\user\\anaconda3\\lib\\site-packages (0.0.181)\n", "Requirement already satisfied: aiohttp<4.0.0,>=3.8.3 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (3.8.4)\n", "Requirement already satisfied: numpy<2,>=1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.23.5)\n", "Requirement already satisfied: requests<3,>=2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (2.28.1)\n", "Requirement already satisfied: openapi-schema-pydantic<2.0,>=1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.2.4)\n", "Requirement already satisfied: SQLAlchemy<3,>=1.4 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.4.39)\n", "Requirement already satisfied: pydantic<2,>=1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (1.10.8)\n", "Requirement already satisfied: PyYAML>=5.4.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (6.0)\n", "Requirement already satisfied: dataclasses-json<0.6.0,>=0.5.7 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (0.5.7)\n", "Requirement already satisfied: async-timeout<5.0.0,>=4.0.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (4.0.2)\n", "Requirement already satisfied: tenacity<9.0.0,>=8.1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (8.2.2)\n", "Requirement already satisfied: numexpr<3.0.0,>=2.8.4 in c:\\users\\user\\anaconda3\\lib\\site-packages (from langchain) (2.8.4)\n", "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.1)\n", "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.3.3)\n", "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (22.1.0)\n", "Requirement already satisfied: charset-normalizer<4.0,>=2.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (2.0.4)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (6.0.4)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp<4.0.0,>=3.8.3->langchain) (1.9.2)\n", "Requirement already satisfied: marshmallow<4.0.0,>=3.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (3.19.0)\n", "Requirement already satisfied: typing-inspect>=0.4.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (0.9.0)\n", "Requirement already satisfied: marshmallow-enum<2.0.0,>=1.5.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from dataclasses-json<0.6.0,>=0.5.7->langchain) (1.5.1)\n", "Requirement already satisfied: typing-extensions>=4.2.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from pydantic<2,>=1->langchain) (4.4.0)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (1.26.14)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (2022.12.7)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests<3,>=2->langchain) (3.4)\n", "Requirement already satisfied: greenlet!=0.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from SQLAlchemy<3,>=1.4->langchain) (2.0.1)\n", "Requirement already satisfied: packaging>=17.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from marshmallow<4.0.0,>=3.3.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (22.0)\n", "Requirement already satisfied: mypy-extensions>=0.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from typing-inspect>=0.4.0->dataclasses-json<0.6.0,>=0.5.7->langchain) (0.4.3)\n" ] } ], "source": [ "!pip install langchain" ] }, { "cell_type": "markdown", "id": "45626414", "metadata": {}, "source": [ "## Let's use OpenAI" ] }, { "cell_type": "markdown", "id": "91756516", "metadata": {}, "source": [ "\n", "Installing Openai package, which includes the classes that we can use to communicate with Openai services\n", "" ] }, { "cell_type": "code", "execution_count": 2, "id": "f3471ee1", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: Openai in c:\\users\\user\\anaconda3\\lib\\site-packages (0.27.7)\n", "Requirement already satisfied: requests>=2.20 in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (2.28.1)\n", "Requirement already satisfied: tqdm in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (4.64.1)\n", "Requirement already satisfied: aiohttp in c:\\users\\user\\anaconda3\\lib\\site-packages (from Openai) (3.8.4)\n", "Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (1.26.14)\n", "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (3.4)\n", "Requirement already satisfied: charset-normalizer<3,>=2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (2.0.4)\n", "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\user\\anaconda3\\lib\\site-packages (from requests>=2.20->Openai) (2022.12.7)\n", "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (22.1.0)\n", "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (6.0.4)\n", "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (4.0.2)\n", "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.3.1)\n", "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.3.3)\n", "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\user\\anaconda3\\lib\\site-packages (from aiohttp->Openai) (1.9.2)\n", "Requirement already satisfied: colorama in c:\\users\\user\\anaconda3\\lib\\site-packages (from tqdm->Openai) (0.4.6)\n" ] } ], "source": [ "!pip install Openai" ] }, { "cell_type": "markdown", "id": "cb45820c", "metadata": {}, "source": [ "\n", "Imports the Python built-in module called \"os.\"\n", "
This module provides a way to interact with the operating system, such as accessing environment variables, working with files and directories, executing shell commands, etc\n", "

\n", "The environ attribute is a dictionary-like object that contains the environment variables of the current operating system session\n", "

\n", "By accessing os.environ, you can retrieve and manipulate environment variables within your Python program. For example, you can retrieve the value of a specific environment variable using the syntax os.environ['VARIABLE_NAME'], where \"VARIABLE_NAME\" is the name of the environment variable you want to access.\n", "" ] }, { "cell_type": "code", "execution_count": 3, "id": "9cfce38a", "metadata": {}, "outputs": [], "source": [ "import os\n", "os.environ[\"OPENAI_API_KEY\"] = \"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\"" ] }, { "cell_type": "markdown", "id": "ad0d8d98", "metadata": {}, "source": [ "\n", "LangChain has built a Wrapper around OpenAI APIs, using which we can get access to all the services OpenAI provides.\n", "
\n", "The code snippet below imports a specific class called 'ChatOpenAI'(Wrapper around OpenAI large language models) from the 'chat_models' module of the 'langchain' library.\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 4, "id": "c89fb843", "metadata": {}, "outputs": [], "source": [ "from langchain.chat_models import ChatOpenAI" ] }, { "cell_type": "markdown", "id": "6982a5b3", "metadata": {}, "source": [ "\n", "The code snippet below imports HumanMessage,SystemMessage and AIMessage from the 'schema' module of the 'langchain' library.\n", "\n", "" ] }, { "cell_type": "code", "execution_count": 5, "id": "30c96ae5", "metadata": {}, "outputs": [], "source": [ "from langchain.schema import HumanMessage, SystemMessage, AIMessage" ] }, { "cell_type": "markdown", "id": "a005e36f", "metadata": {}, "source": [ "\n", "Initialize the ChatOpenAI object and \n", "
\n", "We'll set temperature=.7 to maximise randomness and make outputs creative.\n", "
\n", "The parameter model_name is provided with the value \"gpt-3.5-turbo\" which is a specific version or variant of a language model for chat\n", "" ] }, { "cell_type": "code", "execution_count": 6, "id": "2d2146ed", "metadata": {}, "outputs": [], "source": [ "chat = ChatOpenAI(temperature=.7, model='gpt-3.5-turbo')" ] }, { "cell_type": "markdown", "id": "85d1c2a6", "metadata": {}, "source": [ "\n", "Chats with the Chat-GPT model 'gpt-3.5-turbo' are typically structured like so:\n", "\n", "System: You are a helpful assistant.\n", "\n", "User: Hi AI, how are you today?\n", "\n", "Assistant: I'm great thank you. How can I help you?\n", "\n", "User: I'd like to understand string theory.\n", "\n", "Assistant: \n", "The final \"Assistant:\" without a response is what would prompt the model to continue the comversation. In the official \n", "" ] }, { "cell_type": "code", "execution_count": 8, "id": "8c0971b8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "AIMessage(content='Well, assuming you have access to a car, start by finding a driving school or experienced friend to teach you the basics. Then, practice, practice, practice (but not in rush hour traffic).', additional_kwargs={}, example=False)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chat(\n", " [\n", " SystemMessage(content=\"You are a sarcastic AI assistant\"),\n", " HumanMessage(content=\"Please answer in 30 words: How can I learn driving a car\")\n", " ]\n", ")" ] }, { "cell_type": "markdown", "id": "742b2a7c", "metadata": {}, "source": [ "\n", "In the below scenario\n", "

\n", "We are asking the model to behave in a specific way\n", "
And passing our question\n", "
And also passing on more context so that it can elaborate more on that specific topic
\n", "
\n", "
This model gives us a better way to have conversation kind of opportunity with the model, which can be used to build chat bots.\n", "" ] }, { "cell_type": "code", "execution_count": 11, "id": "b37153b0", "metadata": {}, "outputs": [], "source": [ "ourConversation=chat(\n", " [\n", " SystemMessage(content=\"You are a 3 years old girl who answers very cutely and in a funny way\"),\n", " HumanMessage(content=\"How can I learn driving a car\"),\n", " AIMessage(content=\"I can't drive yet! But I have a driver, my dad...\"),\n", " HumanMessage(content=\"Can you teach me driving?\")\n", " ]\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "id": "a927ebb3", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sure, I can teach you how to drive a toy car! We can start with that and then move on to a real car when you're old enough. But for now, let's buckle up and hit the imaginary road!\n" ] } ], "source": [ "print(ourConversation.content)" ] }, { "cell_type": "code", "execution_count": null, "id": "58503a16", "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.10.9" } }, "nbformat": 4, "nbformat_minor": 5 }