From 4111a96493b2601f8c72ec6710e37d7386235068 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Wed, 19 Jul 2023 14:19:45 -0700 Subject: [PATCH 01/11] adding functions demo --- Basic_Samples/Functions/README.md | 0 Basic_Samples/Functions/config.json | 5 + Basic_Samples/Functions/functions_demo.ipynb | 773 +++++++++++++++++++ Basic_Samples/Functions/requirements.txt | 10 + Basic_Samples/Functions/stock_data.csv | 9 + 5 files changed, 797 insertions(+) create mode 100644 Basic_Samples/Functions/README.md create mode 100644 Basic_Samples/Functions/config.json create mode 100644 Basic_Samples/Functions/functions_demo.ipynb create mode 100644 Basic_Samples/Functions/requirements.txt create mode 100644 Basic_Samples/Functions/stock_data.csv diff --git a/Basic_Samples/Functions/README.md b/Basic_Samples/Functions/README.md new file mode 100644 index 00000000..e69de29b diff --git a/Basic_Samples/Functions/config.json b/Basic_Samples/Functions/config.json new file mode 100644 index 00000000..70da44d3 --- /dev/null +++ b/Basic_Samples/Functions/config.json @@ -0,0 +1,5 @@ +{ + "DEPLOYMENT_ID":"", + "OPENAI_API_BASE":"https://.openai.azure.com", + "OPENAI_API_VERSION":"" +} \ No newline at end of file diff --git a/Basic_Samples/Functions/functions_demo.ipynb b/Basic_Samples/Functions/functions_demo.ipynb new file mode 100644 index 00000000..eb755e8a --- /dev/null +++ b/Basic_Samples/Functions/functions_demo.ipynb @@ -0,0 +1,773 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Working with functions in Azure OpenAI\n", + "This notebook shows how to use the Chat Completions API in combination with functions to extend the current capabilities of GPT models. GPT models, do not inherently support real-time interaction with external systems, databases, or files. However, functions can be used to do so.\n", + "\n", + "Overview:
\n", + "`functions` is an optional parameter in the Chat Completion API which can be used to provide function specifications. This allows models to generate function arguments for the specifications provided by the user. \n", + "\n", + "Note: The API will not execute any function calls. Executing function calls using the outputed argments must be done by developers. " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setup" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Requirement already satisfied: openai in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (0.27.8)\n", + "Requirement already satisfied: requests>=2.20 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from openai) (2.31.0)\n", + "Requirement already satisfied: tqdm in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from openai) (4.65.0)\n", + "Requirement already satisfied: aiohttp in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from openai) (3.8.4)\n", + "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from requests>=2.20->openai) (3.1.0)\n", + "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from requests>=2.20->openai) (3.4)\n", + "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from requests>=2.20->openai) (2.0.3)\n", + "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from requests>=2.20->openai) (2023.5.7)\n", + "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (23.1.0)\n", + "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (6.0.4)\n", + "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (4.0.2)\n", + "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (1.9.2)\n", + "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (1.3.3)\n", + "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (1.3.1)\n", + "Requirement already satisfied: colorama in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from tqdm->openai) (0.4.6)\n", + "Note: you may need to restart the kernel to use updated packages.\n" + ] + } + ], + "source": [ + "# if needed, install and/or upgrade to the latest version of the OpenAI Python library\n", + "%pip install --upgrade openai" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [], + "source": [ + "import openai\n", + "import json\n", + "import os\n", + "\n", + "# Load config values\n", + "with open(r'config.json') as config_file:\n", + " config_details = json.load(config_file)\n", + " \n", + "# Setting up the deployment name\n", + "deployment_id = config_details['DEPLOYMENT_ID']\n", + "\n", + "# This is set to `azure`\n", + "openai.api_type = \"azure\"\n", + "\n", + "# The API key for your Azure OpenAI resource.\n", + "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "\n", + "# The base URL for your Azure OpenAI resource. e.g. \"https://.openai.azure.com\"\n", + "openai.api_base = config_details['OPENAI_API_BASE']\n", + "\n", + "# Currently Chat Completion API have the following versions available: 2023-07-01-preview\n", + "openai.api_version = config_details['OPENAI_API_VERSION']" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1.0 Test functions\n", + "\n", + "This code calls the model with the user query and the set of functions defined in the functions parameter. The model then can choose if it calls a function. If a function is called, the content will be in a strigified JSON object. The function call that should be made and arguments are location in: response[`choices`][0][`function_call`]." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "def get_function_call(messages, function_call = \"auto\"):\n", + " # Define the functions to use\n", + " functions = [\n", + " {\n", + " \"name\": \"get_current_weather\",\n", + " \"description\": \"Get the current weather in a given location\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"location\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"The city and state, e.g. San Francisco, CA\",\n", + " },\n", + " \"unit\": {\"type\": \"string\", \"enum\": [\"celsius\", \"fahrenheit\"]},\n", + " },\n", + " \"required\": [\"location\"],\n", + " },\n", + " },\n", + " ]\n", + "\n", + " # Call the model with the user query (messages) and the functions defined in the functions parameter\n", + " response = openai.ChatCompletion.create(\n", + " deployment_id = deployment_id,\n", + " messages=messages,\n", + " functions=functions,\n", + " function_call=function_call, \n", + " )\n", + "\n", + " return response" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Forcing the use of a specific function or no function\n", + "By changing the value of the `functions` parameter you can allow the model to decide what function to use, force the model to use a specific function, or force the model to use no function." + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Let the model decide what function to call:\n" + ] + }, + { + "ename": "AuthenticationError", + "evalue": "No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mAuthenticationError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[22], line 4\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[39m# 'auto' : Let the model decide what function to call\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mLet the model decide what function to call:\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m----> 4\u001b[0m \u001b[39mprint\u001b[39m (get_function_call(first_message, \u001b[39m\"\u001b[39;49m\u001b[39mauto\u001b[39;49m\u001b[39m\"\u001b[39;49m))\n\u001b[0;32m 7\u001b[0m \u001b[39m# 'none' : Don't call any function \u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mDon\u001b[39m\u001b[39m'\u001b[39m\u001b[39mt call any function:\u001b[39m\u001b[39m\"\u001b[39m)\n", + "Cell \u001b[1;32mIn[21], line 22\u001b[0m, in \u001b[0;36mget_function_call\u001b[1;34m(messages, function_call)\u001b[0m\n\u001b[0;32m 3\u001b[0m functions \u001b[39m=\u001b[39m [\n\u001b[0;32m 4\u001b[0m {\n\u001b[0;32m 5\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mget_current_weather\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 18\u001b[0m },\n\u001b[0;32m 19\u001b[0m ]\n\u001b[0;32m 21\u001b[0m \u001b[39m# Call the model with the user query (messages) and the functions defined in the functions parameter\u001b[39;00m\n\u001b[1;32m---> 22\u001b[0m response \u001b[39m=\u001b[39m openai\u001b[39m.\u001b[39;49mChatCompletion\u001b[39m.\u001b[39;49mcreate(\n\u001b[0;32m 23\u001b[0m deployment_id \u001b[39m=\u001b[39;49m deployment_id,\n\u001b[0;32m 24\u001b[0m messages\u001b[39m=\u001b[39;49mmessages,\n\u001b[0;32m 25\u001b[0m functions\u001b[39m=\u001b[39;49mfunctions,\n\u001b[0;32m 26\u001b[0m function_call\u001b[39m=\u001b[39;49mfunction_call, \n\u001b[0;32m 27\u001b[0m )\n\u001b[0;32m 29\u001b[0m \u001b[39mreturn\u001b[39;00m response\n", + "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\api_resources\\chat_completion.py:25\u001b[0m, in \u001b[0;36mChatCompletion.create\u001b[1;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[0;32m 23\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[0;32m 24\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m---> 25\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39msuper\u001b[39;49m()\u001b[39m.\u001b[39;49mcreate(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[0;32m 26\u001b[0m \u001b[39mexcept\u001b[39;00m TryAgain \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 27\u001b[0m \u001b[39mif\u001b[39;00m timeout \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m time\u001b[39m.\u001b[39mtime() \u001b[39m>\u001b[39m start \u001b[39m+\u001b[39m timeout:\n", + "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\api_resources\\abstract\\engine_api_resource.py:149\u001b[0m, in \u001b[0;36mEngineAPIResource.create\u001b[1;34m(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)\u001b[0m\n\u001b[0;32m 127\u001b[0m \u001b[39m@classmethod\u001b[39m\n\u001b[0;32m 128\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcreate\u001b[39m(\n\u001b[0;32m 129\u001b[0m \u001b[39mcls\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 136\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mparams,\n\u001b[0;32m 137\u001b[0m ):\n\u001b[0;32m 138\u001b[0m (\n\u001b[0;32m 139\u001b[0m deployment_id,\n\u001b[0;32m 140\u001b[0m engine,\n\u001b[0;32m 141\u001b[0m timeout,\n\u001b[0;32m 142\u001b[0m stream,\n\u001b[0;32m 143\u001b[0m headers,\n\u001b[0;32m 144\u001b[0m request_timeout,\n\u001b[0;32m 145\u001b[0m typed_api_type,\n\u001b[0;32m 146\u001b[0m requestor,\n\u001b[0;32m 147\u001b[0m url,\n\u001b[0;32m 148\u001b[0m params,\n\u001b[1;32m--> 149\u001b[0m ) \u001b[39m=\u001b[39m \u001b[39mcls\u001b[39;49m\u001b[39m.\u001b[39;49m__prepare_create_request(\n\u001b[0;32m 150\u001b[0m api_key, api_base, api_type, api_version, organization, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mparams\n\u001b[0;32m 151\u001b[0m )\n\u001b[0;32m 153\u001b[0m response, _, api_key \u001b[39m=\u001b[39m requestor\u001b[39m.\u001b[39mrequest(\n\u001b[0;32m 154\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mpost\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m 155\u001b[0m url,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 160\u001b[0m request_timeout\u001b[39m=\u001b[39mrequest_timeout,\n\u001b[0;32m 161\u001b[0m )\n\u001b[0;32m 163\u001b[0m \u001b[39mif\u001b[39;00m stream:\n\u001b[0;32m 164\u001b[0m \u001b[39m# must be an iterator\u001b[39;00m\n", + "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\api_resources\\abstract\\engine_api_resource.py:106\u001b[0m, in \u001b[0;36mEngineAPIResource.__prepare_create_request\u001b[1;34m(cls, api_key, api_base, api_type, api_version, organization, **params)\u001b[0m\n\u001b[0;32m 103\u001b[0m \u001b[39melif\u001b[39;00m timeout \u001b[39m==\u001b[39m \u001b[39m0\u001b[39m:\n\u001b[0;32m 104\u001b[0m params[\u001b[39m\"\u001b[39m\u001b[39mtimeout\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m MAX_TIMEOUT\n\u001b[1;32m--> 106\u001b[0m requestor \u001b[39m=\u001b[39m api_requestor\u001b[39m.\u001b[39;49mAPIRequestor(\n\u001b[0;32m 107\u001b[0m api_key,\n\u001b[0;32m 108\u001b[0m api_base\u001b[39m=\u001b[39;49mapi_base,\n\u001b[0;32m 109\u001b[0m api_type\u001b[39m=\u001b[39;49mapi_type,\n\u001b[0;32m 110\u001b[0m api_version\u001b[39m=\u001b[39;49mapi_version,\n\u001b[0;32m 111\u001b[0m organization\u001b[39m=\u001b[39;49morganization,\n\u001b[0;32m 112\u001b[0m )\n\u001b[0;32m 113\u001b[0m url \u001b[39m=\u001b[39m \u001b[39mcls\u001b[39m\u001b[39m.\u001b[39mclass_url(engine, api_type, api_version)\n\u001b[0;32m 114\u001b[0m \u001b[39mreturn\u001b[39;00m (\n\u001b[0;32m 115\u001b[0m deployment_id,\n\u001b[0;32m 116\u001b[0m engine,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 124\u001b[0m params,\n\u001b[0;32m 125\u001b[0m )\n", + "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\api_requestor.py:138\u001b[0m, in \u001b[0;36mAPIRequestor.__init__\u001b[1;34m(self, key, api_base, api_type, api_version, organization)\u001b[0m\n\u001b[0;32m 129\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m__init__\u001b[39m(\n\u001b[0;32m 130\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 131\u001b[0m key\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 135\u001b[0m organization\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m,\n\u001b[0;32m 136\u001b[0m ):\n\u001b[0;32m 137\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_base \u001b[39m=\u001b[39m api_base \u001b[39mor\u001b[39;00m openai\u001b[39m.\u001b[39mapi_base\n\u001b[1;32m--> 138\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_key \u001b[39m=\u001b[39m key \u001b[39mor\u001b[39;00m util\u001b[39m.\u001b[39;49mdefault_api_key()\n\u001b[0;32m 139\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_type \u001b[39m=\u001b[39m (\n\u001b[0;32m 140\u001b[0m ApiType\u001b[39m.\u001b[39mfrom_str(api_type)\n\u001b[0;32m 141\u001b[0m \u001b[39mif\u001b[39;00m api_type\n\u001b[0;32m 142\u001b[0m \u001b[39melse\u001b[39;00m ApiType\u001b[39m.\u001b[39mfrom_str(openai\u001b[39m.\u001b[39mapi_type)\n\u001b[0;32m 143\u001b[0m )\n\u001b[0;32m 144\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_version \u001b[39m=\u001b[39m api_version \u001b[39mor\u001b[39;00m openai\u001b[39m.\u001b[39mapi_version\n", + "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\util.py:186\u001b[0m, in \u001b[0;36mdefault_api_key\u001b[1;34m()\u001b[0m\n\u001b[0;32m 184\u001b[0m \u001b[39mreturn\u001b[39;00m openai\u001b[39m.\u001b[39mapi_key\n\u001b[0;32m 185\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m--> 186\u001b[0m \u001b[39mraise\u001b[39;00m openai\u001b[39m.\u001b[39merror\u001b[39m.\u001b[39mAuthenticationError(\n\u001b[0;32m 187\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mNo API key provided. You can set your API key in code using \u001b[39m\u001b[39m'\u001b[39m\u001b[39mopenai.api_key = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with \u001b[39m\u001b[39m'\u001b[39m\u001b[39mopenai.api_key_path = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 188\u001b[0m )\n", + "\u001b[1;31mAuthenticationError\u001b[0m: No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details." + ] + } + ], + "source": [ + "first_message = [{\"role\": \"user\", \"content\": \"What's the weather like in San Francisco?\"}]\n", + "# 'auto' : Let the model decide what function to call\n", + "print(\"Let the model decide what function to call:\")\n", + "print (get_function_call(first_message, \"auto\"))\n", + "\n", + "\n", + "# 'none' : Don't call any function \n", + "print(\"Don't call any function:\")\n", + "print (get_function_call(first_message, \"none\"))\n", + "\n", + "# force a specific function call\n", + "print(\"Force a specific function call:\")\n", + "print (get_function_call(first_message, function_call={\"name\": \"get_current_weather\"}))" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2.0 Defining functions\n", + "Now that we know how to work with functions, let's define some functions in code so that we can walk through the process of using functions end to end." + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Function #1: Get current time" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pytz\n", + "from datetime import datetime\n", + "\n", + "def get_current_time(location):\n", + " try:\n", + " # Get the timezone for the city\n", + " timezone = pytz.timezone(location)\n", + "\n", + " # Get the current time in the timezone\n", + " now = datetime.now(timezone)\n", + " current_time = now.strftime(\"%I:%M:%S %p\")\n", + "\n", + " return current_time\n", + " except:\n", + " return \"Sorry, I couldn't find the timezone for that location.\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'03:21:55 PM'" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "get_current_time(\"America/New_York\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Function #2: Get stock market data\n", + "For simplicity, we're just hard coding some stock market data but you could easily edit the code to call out to an API to retrieve real-time data." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "\n", + "# Define your data\n", + "data = [\n", + " {'Index': 'S&P 500', 'Date': '2023-07-12', 'Open': 4300.25, 'High': 4350.32, 'Low': 4200.20, 'Close': 4325.74, 'Volume': 3500000},\n", + " {'Index': 'S&P 500', 'Date': '2023-07-13', 'Open': 4325.55, 'High': 4350.00, 'Low': 4300.98, 'Close': 4310.33, 'Volume': 3600000},\n", + " {'Index': 'NASDAQ Composite', 'Date': '2023-07-12', 'Open': 14000.65, 'High': 14200.06, 'Low': 13800.08, 'Close': 14100.44, 'Volume': 4000000},\n", + " {'Index': 'NASDAQ Composite', 'Date': '2023-07-13', 'Open': 14100.11, 'High': 14250.00, 'Low': 14000.67, 'Close': 14050.81, 'Volume': 4200000},\n", + " {'Index': 'Dow Jones Industrial Average', 'Date': '2023-07-12', 'Open': 34000.87, 'High': 34500.22, 'Low': 33000.11, 'Close': 34200.90, 'Volume': 3000000},\n", + " {'Index': 'Dow Jones Industrial Average', 'Date': '2023-07-13', 'Open': 34200.73, 'High': 34500.73, 'Low': 34000.40, 'Close': 34100.64, 'Volume': 3200000},\n", + " {'Index': 'Financial Times Stock Exchange 100 Index', 'Date': '2023-07-12', 'Open': 7000.55, 'High': 7050.61, 'Low': 6900.76, 'Close': 7025.22, 'Volume': 2000000},\n", + " {'Index': 'Financial Times Stock Exchange 100 Index', 'Date': '2023-07-13', 'Open': 7025.39, 'High': 7050.90, 'Low': 7000.56, 'Close': 7010.24, 'Volume': 2100000}\n", + "]\n", + "\n", + "# Create a DataFrame\n", + "df = pd.DataFrame(data)\n", + "\n", + "# Write the DataFrame to a CSV file\n", + "df.to_csv('stock_data.csv', index=False)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pandas as pd\n", + "import json\n", + "\n", + "def get_stock_market_data(index):\n", + " available_indices = [\"S&P 500\", \"NASDAQ Composite\", \"Dow Jones Industrial Average\", \"Financial Times Stock Exchange 100 Index\"]\n", + "\n", + " if index not in available_indices:\n", + " return \"Invalid index. Please choose from 'S&P 500', 'NASDAQ Composite', 'Dow Jones Industrial Average', 'Financial Times Stock Exchange 100 Index'.\"\n", + "\n", + " # Read the CSV file\n", + " data = pd.read_csv('stock_data.csv')\n", + "\n", + " # Filter data for the given index\n", + " data_filtered = data[data['Index'] == index]\n", + "\n", + " # Remove 'Index' column\n", + " data_filtered = data_filtered.drop(columns=['Index'])\n", + "\n", + " # Convert the DataFrame into a dictionary\n", + " hist_dict = data_filtered.to_dict()\n", + "\n", + " for key, value_dict in hist_dict.items():\n", + " hist_dict[key] = {k: v for k, v in value_dict.items()}\n", + "\n", + " return json.dumps(hist_dict)\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{\"Date\": {\"2\": \"2023-07-12\", \"3\": \"2023-07-13\"}, \"Open\": {\"2\": 14000.65, \"3\": 14100.11}, \"High\": {\"2\": 14200.06, \"3\": 14250.0}, \"Low\": {\"2\": 13800.08, \"3\": 14000.67}, \"Close\": {\"2\": 14100.44, \"3\": 14050.81}, \"Volume\": {\"2\": 4000000, \"3\": 4200000}}\n" + ] + } + ], + "source": [ + "print(get_stock_market_data(\"NASDAQ Composite\"))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Function #3: Calculator " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import math\n", + "\n", + "def calculator(num1, num2, operator):\n", + " if operator == '+':\n", + " return str(num1 + num2)\n", + " elif operator == '-':\n", + " return str(num1 - num2)\n", + " elif operator == '*':\n", + " return str(num1 * num2)\n", + " elif operator == '/':\n", + " return str(num1 / num2)\n", + " elif operator == '**':\n", + " return str(num1 ** num2)\n", + " elif operator == 'sqrt':\n", + " return str(math.sqrt(num1))\n", + " else:\n", + " return \"Invalid operator\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "print(calculator(5, 5, '+'))" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3.0 Calling a Function using GPT\n", + "\n", + "Steps for Function Calling: \n", + "\n", + "1. Call the model with the user query and a set of functions defined in the functions parameter.\n", + "2. The model can choose to call a function; if so, the content will be a stringified JSON object adhering to your custom schema (note: the model may generate invalid JSON or hallucinate parameters).\n", + "3. Parse the string into JSON in your code, and call your function with the provided arguments if they exist.\n", + "4. Call the model again by appending the function response as a new message, and let the model summarize the results back to the user.\n", + "\n", + "### 3.1 Describe the functions so that the model knows how to call them" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "functions = [\n", + " {\n", + " \"name\": \"get_current_time\",\n", + " \"description\": \"Get the current time in a given location\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"location\": {\n", + " \"type\": \"string\",\n", + " \"description\": \"The location name. The pytz is used to get the timezone for that location. Location names should be in a format like America/New_York, Asia/Bangkok, Europe/London\",\n", + " }\n", + " },\n", + " \"required\": [\"location\"],\n", + " },\n", + " },\n", + " {\n", + " \"name\": \"get_stock_market_data\",\n", + " \"description\": \"Get the stock market data for a given index\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"index\": {\n", + " \"type\": \"string\",\n", + " \"enum\": [\"S&P 500\", \"NASDAQ Composite\", \"Dow Jones Industrial Average\", \"Financial Times Stock Exchange 100 Index\"]},\n", + " },\n", + " \"required\": [\"index\"],\n", + " }, \n", + " },\n", + " {\n", + " \"name\": \"calculator\",\n", + " \"description\": \"A simple calculator used to perform basic arithmetic operations\",\n", + " \"parameters\": {\n", + " \"type\": \"object\",\n", + " \"properties\": {\n", + " \"num1\": {\"type\": \"number\"},\n", + " \"num2\": {\"type\": \"number\"},\n", + " \"operator\": {\"type\": \"string\", \"enum\": [\"+\", \"-\", \"*\", \"/\", \"**\", \"sqrt\"]},\n", + " },\n", + " \"required\": [\"num1\", \"num2\", \"operator\"],\n", + " },\n", + " }\n", + " ]\n", + "\n", + "available_functions = {\n", + " \"get_current_time\": get_current_time,\n", + " \"get_stock_market_data\": get_stock_market_data,\n", + " \"calculator\": calculator,\n", + " } " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 3.2 Define a helper function to validate the function call\n", + "It's possible that the models could generate incorrect function calls so it's important to validate the calls. Here we define a simple helper function to validate the function call although you could apply more complex validation for your use case." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import inspect\n", + "\n", + "# helper method used to check if the correct arguments are provided to a function\n", + "def check_args(function, args):\n", + " sig = inspect.signature(function)\n", + " params = sig.parameters\n", + "\n", + " # Check if there are extra arguments\n", + " for name in args:\n", + " if name not in params:\n", + " return False\n", + " # Check if the required arguments are provided \n", + " for name, param in params.items():\n", + " if param.default is param.empty and name not in args:\n", + " return False\n", + "\n", + " return True" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def run_conversation(messages, functions, available_functions, deployment_id):\n", + " # Step 1: send the conversation and available functions to GPT\n", + "\n", + " response = openai.ChatCompletion.create(\n", + " deployment_id=deployment_id,\n", + " messages=messages,\n", + " functions=functions,\n", + " function_call=\"auto\", \n", + " )\n", + " response_message = response[\"choices\"][0][\"message\"]\n", + "\n", + "\n", + " # Step 2: check if GPT wanted to call a function\n", + " if response_message.get(\"function_call\"):\n", + " print(\"Recommended Function call:\")\n", + " print(response_message.get(\"function_call\"))\n", + " print()\n", + " \n", + " # Step 3: call the function\n", + " # Note: the JSON response may not always be valid; be sure to handle errors\n", + " \n", + " function_name = response_message[\"function_call\"][\"name\"]\n", + " \n", + " # verify function exists\n", + " if function_name not in available_functions:\n", + " return \"Function \" + function_name + \" does not exist\"\n", + " fuction_to_call = available_functions[function_name] \n", + " \n", + " # verify function has correct number of arguments\n", + " function_args = json.loads(response_message[\"function_call\"][\"arguments\"])\n", + " if check_args(fuction_to_call, function_args) is False:\n", + " return \"Invalid number of arguments for function: \" + function_name\n", + " function_response = fuction_to_call(**function_args)\n", + " \n", + " print(\"Output of function call:\")\n", + " print(function_response)\n", + " print()\n", + " \n", + " # Step 4: send the info on the function call and function response to GPT\n", + " \n", + " # adding assistant response to messages\n", + " messages.append(\n", + " {\n", + " \"role\": response_message[\"role\"],\n", + " \"name\": response_message[\"function_call\"][\"name\"],\n", + " \"content\": response_message[\"function_call\"][\"arguments\"],\n", + " }\n", + " )\n", + "\n", + " # adding function response to messages\n", + " messages.append(\n", + " {\n", + " \"role\": \"function\",\n", + " \"name\": function_name,\n", + " \"content\": function_response,\n", + " }\n", + " ) # extend conversation with function response\n", + "\n", + " print(\"Messages in second request:\")\n", + " for message in messages:\n", + " print(message)\n", + " print()\n", + "\n", + " second_response = openai.ChatCompletion.create(\n", + " messages=messages,\n", + " deployment_id=deployment_id\n", + " ) # get a new response from GPT where it can see the function response\n", + "\n", + " return second_response" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Recommended Function call:\n", + "{\n", + " \"name\": \"get_current_time\",\n", + " \"arguments\": \"{\\n \\\"location\\\": \\\"America/New_York\\\"\\n}\"\n", + "}\n", + "\n", + "Output of function call:\n", + "03:22:04 PM\n", + "\n", + "Messages in second request:\n", + "{'role': 'user', 'content': 'What time is it in New York?'}\n", + "{'role': 'assistant', 'name': 'get_current_time', 'content': '{\\n \"location\": \"America/New_York\"\\n}'}\n", + "{'role': 'function', 'name': 'get_current_time', 'content': '03:22:04 PM'}\n", + "\n", + "{\n", + " \"role\": \"assistant\",\n", + " \"content\": \"The current time in New York is 03:22 PM.\"\n", + "}\n" + ] + } + ], + "source": [ + "messages = [{\"role\": \"user\", \"content\": \"What time is it in New York?\"}]\n", + "assistant_response = run_conversation(messages, functions, available_functions, deployment_id)\n", + "print(assistant_response['choices'][0]['message'])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### 4.0 Calling multiple functions together\n", + "In some cases, you may want to string together multiple function calls to get the desired result. We modified the `run_conversation()` function above to allow multiple function calls to be made." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def run_multiturn_conversation(messages, functions, available_functions, deployment_id):\n", + " # Step 1: send the conversation and available functions to GPT\n", + "\n", + " response = openai.ChatCompletion.create(\n", + " deployment_id=deployment_id,\n", + " messages=messages,\n", + " functions=functions,\n", + " function_call=\"auto\", \n", + " temperature=0\n", + " )\n", + "\n", + " # Step 2: check if GPT wanted to call a function\n", + " while response[\"choices\"][0][\"finish_reason\"] == 'function_call':\n", + " response_message = response[\"choices\"][0][\"message\"]\n", + " print(\"Recommended Function call:\")\n", + " print(response_message.get(\"function_call\"))\n", + " print()\n", + " \n", + " # Step 3: call the function\n", + " # Note: the JSON response may not always be valid; be sure to handle errors\n", + " \n", + " function_name = response_message[\"function_call\"][\"name\"]\n", + " \n", + " # verify function exists\n", + " if function_name not in available_functions:\n", + " return \"Function \" + function_name + \" does not exist\"\n", + " fuction_to_call = available_functions[function_name] \n", + " \n", + " # verify function has correct number of arguments\n", + " function_args = json.loads(response_message[\"function_call\"][\"arguments\"])\n", + " if check_args(fuction_to_call, function_args) is False:\n", + " return \"Invalid number of arguments for function: \" + function_name\n", + " function_response = fuction_to_call(**function_args)\n", + " \n", + " print(\"Output of function call:\")\n", + " print(function_response)\n", + " print()\n", + " \n", + " # Step 4: send the info on the function call and function response to GPT\n", + " \n", + " # adding assistant response to messages\n", + " messages.append(\n", + " {\n", + " \"role\": response_message[\"role\"],\n", + " \"name\": response_message[\"function_call\"][\"name\"],\n", + " \"content\": response_message[\"function_call\"][\"arguments\"],\n", + " }\n", + " )\n", + "\n", + " # adding function response to messages\n", + " messages.append(\n", + " {\n", + " \"role\": \"function\",\n", + " \"name\": function_name,\n", + " \"content\": function_response,\n", + " }\n", + " ) # extend conversation with function response\n", + "\n", + " print(\"Messages in next request:\")\n", + " for message in messages:\n", + " print(message)\n", + " print()\n", + "\n", + " response = openai.ChatCompletion.create(\n", + " messages=messages,\n", + " deployment_id=deployment_id,\n", + " function_call=\"auto\",\n", + " functions=functions,\n", + " temperature=0\n", + " ) # get a new response from GPT where it can see the function response\n", + "\n", + " return response" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Recommended Function call:\n", + "{\n", + " \"name\": \"get_stock_market_data\",\n", + " \"arguments\": \"{\\n \\\"index\\\": \\\"S&P 500\\\"\\n}\"\n", + "}\n", + "\n", + "Output of function call:\n", + "{\"Date\": {\"0\": \"2023-07-12\", \"1\": \"2023-07-13\"}, \"Open\": {\"0\": 4300.25, \"1\": 4325.55}, \"High\": {\"0\": 4350.32, \"1\": 4350.0}, \"Low\": {\"0\": 4200.2, \"1\": 4300.98}, \"Close\": {\"0\": 4325.74, \"1\": 4310.33}, \"Volume\": {\"0\": 3500000, \"1\": 3600000}}\n", + "\n", + "Messages in next request:\n", + "{'role': 'system', 'content': 'Assistant is a helpful assistant that helps users get answers to questions. Assistant has access to several tools and sometimes you may need to call multiple tools in sequence to get answers for your users.'}\n", + "{'role': 'user', 'content': 'How much did S&P 500 change between July 12 and July 13? Use the calculator.'}\n", + "{'role': 'assistant', 'name': 'get_stock_market_data', 'content': '{\\n \"index\": \"S&P 500\"\\n}'}\n", + "{'role': 'function', 'name': 'get_stock_market_data', 'content': '{\"Date\": {\"0\": \"2023-07-12\", \"1\": \"2023-07-13\"}, \"Open\": {\"0\": 4300.25, \"1\": 4325.55}, \"High\": {\"0\": 4350.32, \"1\": 4350.0}, \"Low\": {\"0\": 4200.2, \"1\": 4300.98}, \"Close\": {\"0\": 4325.74, \"1\": 4310.33}, \"Volume\": {\"0\": 3500000, \"1\": 3600000}}'}\n", + "\n", + "Recommended Function call:\n", + "{\n", + " \"name\": \"calculator\",\n", + " \"arguments\": \"{\\n \\\"num1\\\": 4310.33,\\n \\\"num2\\\": 4325.74,\\n \\\"operator\\\": \\\"-\\\"\\n}\"\n", + "}\n", + "\n", + "Output of function call:\n", + "-15.409999999999854\n", + "\n", + "Messages in next request:\n", + "{'role': 'system', 'content': 'Assistant is a helpful assistant that helps users get answers to questions. Assistant has access to several tools and sometimes you may need to call multiple tools in sequence to get answers for your users.'}\n", + "{'role': 'user', 'content': 'How much did S&P 500 change between July 12 and July 13? Use the calculator.'}\n", + "{'role': 'assistant', 'name': 'get_stock_market_data', 'content': '{\\n \"index\": \"S&P 500\"\\n}'}\n", + "{'role': 'function', 'name': 'get_stock_market_data', 'content': '{\"Date\": {\"0\": \"2023-07-12\", \"1\": \"2023-07-13\"}, \"Open\": {\"0\": 4300.25, \"1\": 4325.55}, \"High\": {\"0\": 4350.32, \"1\": 4350.0}, \"Low\": {\"0\": 4200.2, \"1\": 4300.98}, \"Close\": {\"0\": 4325.74, \"1\": 4310.33}, \"Volume\": {\"0\": 3500000, \"1\": 3600000}}'}\n", + "{'role': 'assistant', 'name': 'calculator', 'content': '{\\n \"num1\": 4310.33,\\n \"num2\": 4325.74,\\n \"operator\": \"-\"\\n}'}\n", + "{'role': 'function', 'name': 'calculator', 'content': '-15.409999999999854'}\n", + "\n", + "Final Response:\n", + "{\n", + " \"role\": \"assistant\",\n", + " \"content\": \"The S&P 500 index changed by approximately -15.41 points between July 12 and July 13.\"\n", + "}\n", + "Conversation complete!\n" + ] + } + ], + "source": [ + "# Can add system prompting to guide the model to call functions and perform in specific ways\n", + "next_messages = [{\"role\": \"system\", \"content\": \"Assistant is a helpful assistant that helps users get answers to questions. Assistant has access to several tools and sometimes you may need to call multiple tools in sequence to get answers for your users.\"}]\n", + "next_messages.append({\"role\": \"user\", \"content\": \"How much did S&P 500 change between July 12 and July 13? Use the calculator.\"})\n", + "\n", + "assistant_response = run_multiturn_conversation(next_messages, functions, available_functions, deployment_id)\n", + "print(\"Final Response:\")\n", + "print(assistant_response[\"choices\"][0][\"message\"])\n", + "print(\"Conversation complete!\") " + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "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.11.4" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/Basic_Samples/Functions/requirements.txt b/Basic_Samples/Functions/requirements.txt new file mode 100644 index 00000000..917edd20 --- /dev/null +++ b/Basic_Samples/Functions/requirements.txt @@ -0,0 +1,10 @@ +# Packages needed to call AOAI API with the OpenAI Python API +openai + +# Packages needed to run the notebook samples +jupyter + +# Other packages needed to run the notebook samples +json +pytz +pandas \ No newline at end of file diff --git a/Basic_Samples/Functions/stock_data.csv b/Basic_Samples/Functions/stock_data.csv new file mode 100644 index 00000000..c44e7c25 --- /dev/null +++ b/Basic_Samples/Functions/stock_data.csv @@ -0,0 +1,9 @@ +Index,Date,Open,High,Low,Close,Volume +S&P 500,2023-07-12,4300.25,4350.32,4200.2,4325.74,3500000 +S&P 500,2023-07-13,4325.55,4350.0,4300.98,4310.33,3600000 +NASDAQ Composite,2023-07-12,14000.65,14200.06,13800.08,14100.44,4000000 +NASDAQ Composite,2023-07-13,14100.11,14250.0,14000.67,14050.81,4200000 +Dow Jones Industrial Average,2023-07-12,34000.87,34500.22,33000.11,34200.9,3000000 +Dow Jones Industrial Average,2023-07-13,34200.73,34500.73,34000.4,34100.64,3200000 +Financial Times Stock Exchange 100 Index,2023-07-12,7000.55,7050.61,6900.76,7025.22,2000000 +Financial Times Stock Exchange 100 Index,2023-07-13,7025.39,7050.9,7000.56,7010.24,2100000 From a4600616ebd2c6de2aef25d2040dd5d28ca692c4 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Wed, 19 Jul 2023 14:24:16 -0700 Subject: [PATCH 02/11] adding README File --- Basic_Samples/Functions/README.md | 53 +++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/Basic_Samples/Functions/README.md b/Basic_Samples/Functions/README.md index e69de29b..77f715f5 100644 --- a/Basic_Samples/Functions/README.md +++ b/Basic_Samples/Functions/README.md @@ -0,0 +1,53 @@ +# Introduction +This repository contains samples demonstrating how to use functions to extend the current capabilities of GPT Models + +## Installation +Install all Python modules and packages listed in the requirements.txt file using the below command. + +```python +pip install -r requirements.txt +``` + +### Microsoft Azure Endpoints +In order to use the Open AI library or REST API with Microsoft Azure endpoints, you need to set DEPLOYMENT_NAME, OPENAI_API_BASE & OPENAI_API_VERSION in _config.json_ file. + +```js +{ + "DEPLOYMENT_ID":"", + "OPENAI_API_BASE":"https://.openai.azure.com", + "OPENAI_API_VERSION":"" +} +``` + +### For getting started: +- Add "OPENAI_API_KEY" as variable name and \ as variable value in the environment variables. +
+One can get the OPENAI_API_KEY value from the Azure Portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values. +
+ + WINDOWS Users: + setx OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" + + MACOS/LINUX Users: + export OPENAI_API_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" + +- To find your "DEPLOYMENT_ID" go to the deployments page of the Azure AI Studio. Create a deployment if one does not already exist. +One can start with using your model name as "gpt-35-turbo-0613" or "gpt-4." + +- To find your "OPENAI_API_BASE" go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value. +- Current OpenAI api version is "2022-12-01". Check out versions [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference). + + +## Requirements +Python 3.8+
+Jupyter Notebook 6.5.2 + +
+ +## Trademarks + +This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft +trademarks or logos is subject to and must follow +[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). +Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. +Any use of third-party trademarks or logos are subject to those third-party's policies. \ No newline at end of file From aa4f0baa2758d53990b02f5781126a2df6d7fbbb Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Wed, 19 Jul 2023 15:19:52 -0700 Subject: [PATCH 03/11] resolving comments --- Basic_Samples/Functions/README.md | 2 +- ...emo.ipynb => working_with_functions.ipynb} | 275 ++++++++++++------ 2 files changed, 185 insertions(+), 92 deletions(-) rename Basic_Samples/Functions/{functions_demo.ipynb => working_with_functions.ipynb} (61%) diff --git a/Basic_Samples/Functions/README.md b/Basic_Samples/Functions/README.md index 77f715f5..4c909285 100644 --- a/Basic_Samples/Functions/README.md +++ b/Basic_Samples/Functions/README.md @@ -35,7 +35,7 @@ One can get the OPENAI_API_KEY value from the Azure Portal. Go to https://portal One can start with using your model name as "gpt-35-turbo-0613" or "gpt-4." - To find your "OPENAI_API_BASE" go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value. -- Current OpenAI api version is "2022-12-01". Check out versions [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference). +- Current, function calling can only be used with the "2023-07-01-preview" API version. Check out versions [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference). ## Requirements diff --git a/Basic_Samples/Functions/functions_demo.ipynb b/Basic_Samples/Functions/working_with_functions.ipynb similarity index 61% rename from Basic_Samples/Functions/functions_demo.ipynb rename to Basic_Samples/Functions/working_with_functions.ipynb index eb755e8a..b04017b1 100644 --- a/Basic_Samples/Functions/functions_demo.ipynb +++ b/Basic_Samples/Functions/working_with_functions.ipynb @@ -23,40 +23,17 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 99, "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: openai in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (0.27.8)\n", - "Requirement already satisfied: requests>=2.20 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from openai) (2.31.0)\n", - "Requirement already satisfied: tqdm in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from openai) (4.65.0)\n", - "Requirement already satisfied: aiohttp in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from openai) (3.8.4)\n", - "Requirement already satisfied: charset-normalizer<4,>=2 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from requests>=2.20->openai) (3.1.0)\n", - "Requirement already satisfied: idna<4,>=2.5 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from requests>=2.20->openai) (3.4)\n", - "Requirement already satisfied: urllib3<3,>=1.21.1 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from requests>=2.20->openai) (2.0.3)\n", - "Requirement already satisfied: certifi>=2017.4.17 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from requests>=2.20->openai) (2023.5.7)\n", - "Requirement already satisfied: attrs>=17.3.0 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (23.1.0)\n", - "Requirement already satisfied: multidict<7.0,>=4.5 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (6.0.4)\n", - "Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (4.0.2)\n", - "Requirement already satisfied: yarl<2.0,>=1.0 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (1.9.2)\n", - "Requirement already satisfied: frozenlist>=1.1.1 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (1.3.3)\n", - "Requirement already satisfied: aiosignal>=1.1.2 in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from aiohttp->openai) (1.3.1)\n", - "Requirement already satisfied: colorama in c:\\users\\t-amfoster\\appdata\\local\\packages\\pythonsoftwarefoundation.python.3.11_qbz5n2kfra8p0\\localcache\\local-packages\\python311\\site-packages (from tqdm->openai) (0.4.6)\n", - "Note: you may need to restart the kernel to use updated packages.\n" - ] - } - ], + "outputs": [], "source": [ "# if needed, install and/or upgrade to the latest version of the OpenAI Python library\n", - "%pip install --upgrade openai" + "#%pip install --upgrade openai" ] }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 100, "metadata": {}, "outputs": [], "source": [ @@ -96,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 101, "metadata": {}, "outputs": [], "source": [ @@ -141,31 +118,174 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Let the model decide what function to call:\n" - ] - }, - { - "ename": "AuthenticationError", - "evalue": "No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mAuthenticationError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[22], line 4\u001b[0m\n\u001b[0;32m 2\u001b[0m \u001b[39m# 'auto' : Let the model decide what function to call\u001b[39;00m\n\u001b[0;32m 3\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mLet the model decide what function to call:\u001b[39m\u001b[39m\"\u001b[39m)\n\u001b[1;32m----> 4\u001b[0m \u001b[39mprint\u001b[39m (get_function_call(first_message, \u001b[39m\"\u001b[39;49m\u001b[39mauto\u001b[39;49m\u001b[39m\"\u001b[39;49m))\n\u001b[0;32m 7\u001b[0m \u001b[39m# 'none' : Don't call any function \u001b[39;00m\n\u001b[0;32m 8\u001b[0m \u001b[39mprint\u001b[39m(\u001b[39m\"\u001b[39m\u001b[39mDon\u001b[39m\u001b[39m'\u001b[39m\u001b[39mt call any function:\u001b[39m\u001b[39m\"\u001b[39m)\n", - "Cell \u001b[1;32mIn[21], line 22\u001b[0m, in \u001b[0;36mget_function_call\u001b[1;34m(messages, function_call)\u001b[0m\n\u001b[0;32m 3\u001b[0m functions \u001b[39m=\u001b[39m [\n\u001b[0;32m 4\u001b[0m {\n\u001b[0;32m 5\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mname\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mget_current_weather\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 18\u001b[0m },\n\u001b[0;32m 19\u001b[0m ]\n\u001b[0;32m 21\u001b[0m \u001b[39m# Call the model with the user query (messages) and the functions defined in the functions parameter\u001b[39;00m\n\u001b[1;32m---> 22\u001b[0m response \u001b[39m=\u001b[39m openai\u001b[39m.\u001b[39;49mChatCompletion\u001b[39m.\u001b[39;49mcreate(\n\u001b[0;32m 23\u001b[0m deployment_id \u001b[39m=\u001b[39;49m deployment_id,\n\u001b[0;32m 24\u001b[0m messages\u001b[39m=\u001b[39;49mmessages,\n\u001b[0;32m 25\u001b[0m functions\u001b[39m=\u001b[39;49mfunctions,\n\u001b[0;32m 26\u001b[0m function_call\u001b[39m=\u001b[39;49mfunction_call, \n\u001b[0;32m 27\u001b[0m )\n\u001b[0;32m 29\u001b[0m \u001b[39mreturn\u001b[39;00m response\n", - "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\api_resources\\chat_completion.py:25\u001b[0m, in \u001b[0;36mChatCompletion.create\u001b[1;34m(cls, *args, **kwargs)\u001b[0m\n\u001b[0;32m 23\u001b[0m \u001b[39mwhile\u001b[39;00m \u001b[39mTrue\u001b[39;00m:\n\u001b[0;32m 24\u001b[0m \u001b[39mtry\u001b[39;00m:\n\u001b[1;32m---> 25\u001b[0m \u001b[39mreturn\u001b[39;00m \u001b[39msuper\u001b[39;49m()\u001b[39m.\u001b[39;49mcreate(\u001b[39m*\u001b[39;49margs, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mkwargs)\n\u001b[0;32m 26\u001b[0m \u001b[39mexcept\u001b[39;00m TryAgain \u001b[39mas\u001b[39;00m e:\n\u001b[0;32m 27\u001b[0m \u001b[39mif\u001b[39;00m timeout \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m \u001b[39mand\u001b[39;00m time\u001b[39m.\u001b[39mtime() \u001b[39m>\u001b[39m start \u001b[39m+\u001b[39m timeout:\n", - "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\api_resources\\abstract\\engine_api_resource.py:149\u001b[0m, in \u001b[0;36mEngineAPIResource.create\u001b[1;34m(cls, api_key, api_base, api_type, request_id, api_version, organization, **params)\u001b[0m\n\u001b[0;32m 127\u001b[0m \u001b[39m@classmethod\u001b[39m\n\u001b[0;32m 128\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcreate\u001b[39m(\n\u001b[0;32m 129\u001b[0m \u001b[39mcls\u001b[39m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 136\u001b[0m \u001b[39m*\u001b[39m\u001b[39m*\u001b[39mparams,\n\u001b[0;32m 137\u001b[0m ):\n\u001b[0;32m 138\u001b[0m (\n\u001b[0;32m 139\u001b[0m deployment_id,\n\u001b[0;32m 140\u001b[0m engine,\n\u001b[0;32m 141\u001b[0m timeout,\n\u001b[0;32m 142\u001b[0m stream,\n\u001b[0;32m 143\u001b[0m headers,\n\u001b[0;32m 144\u001b[0m request_timeout,\n\u001b[0;32m 145\u001b[0m typed_api_type,\n\u001b[0;32m 146\u001b[0m requestor,\n\u001b[0;32m 147\u001b[0m url,\n\u001b[0;32m 148\u001b[0m params,\n\u001b[1;32m--> 149\u001b[0m ) \u001b[39m=\u001b[39m \u001b[39mcls\u001b[39;49m\u001b[39m.\u001b[39;49m__prepare_create_request(\n\u001b[0;32m 150\u001b[0m api_key, api_base, api_type, api_version, organization, \u001b[39m*\u001b[39;49m\u001b[39m*\u001b[39;49mparams\n\u001b[0;32m 151\u001b[0m )\n\u001b[0;32m 153\u001b[0m response, _, api_key \u001b[39m=\u001b[39m requestor\u001b[39m.\u001b[39mrequest(\n\u001b[0;32m 154\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mpost\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m 155\u001b[0m url,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 160\u001b[0m request_timeout\u001b[39m=\u001b[39mrequest_timeout,\n\u001b[0;32m 161\u001b[0m )\n\u001b[0;32m 163\u001b[0m \u001b[39mif\u001b[39;00m stream:\n\u001b[0;32m 164\u001b[0m \u001b[39m# must be an iterator\u001b[39;00m\n", - "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\api_resources\\abstract\\engine_api_resource.py:106\u001b[0m, in \u001b[0;36mEngineAPIResource.__prepare_create_request\u001b[1;34m(cls, api_key, api_base, api_type, api_version, organization, **params)\u001b[0m\n\u001b[0;32m 103\u001b[0m \u001b[39melif\u001b[39;00m timeout \u001b[39m==\u001b[39m \u001b[39m0\u001b[39m:\n\u001b[0;32m 104\u001b[0m params[\u001b[39m\"\u001b[39m\u001b[39mtimeout\u001b[39m\u001b[39m\"\u001b[39m] \u001b[39m=\u001b[39m MAX_TIMEOUT\n\u001b[1;32m--> 106\u001b[0m requestor \u001b[39m=\u001b[39m api_requestor\u001b[39m.\u001b[39;49mAPIRequestor(\n\u001b[0;32m 107\u001b[0m api_key,\n\u001b[0;32m 108\u001b[0m api_base\u001b[39m=\u001b[39;49mapi_base,\n\u001b[0;32m 109\u001b[0m api_type\u001b[39m=\u001b[39;49mapi_type,\n\u001b[0;32m 110\u001b[0m api_version\u001b[39m=\u001b[39;49mapi_version,\n\u001b[0;32m 111\u001b[0m organization\u001b[39m=\u001b[39;49morganization,\n\u001b[0;32m 112\u001b[0m )\n\u001b[0;32m 113\u001b[0m url \u001b[39m=\u001b[39m \u001b[39mcls\u001b[39m\u001b[39m.\u001b[39mclass_url(engine, api_type, api_version)\n\u001b[0;32m 114\u001b[0m \u001b[39mreturn\u001b[39;00m (\n\u001b[0;32m 115\u001b[0m deployment_id,\n\u001b[0;32m 116\u001b[0m engine,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 124\u001b[0m params,\n\u001b[0;32m 125\u001b[0m )\n", - "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\api_requestor.py:138\u001b[0m, in \u001b[0;36mAPIRequestor.__init__\u001b[1;34m(self, key, api_base, api_type, api_version, organization)\u001b[0m\n\u001b[0;32m 129\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39m__init__\u001b[39m(\n\u001b[0;32m 130\u001b[0m \u001b[39mself\u001b[39m,\n\u001b[0;32m 131\u001b[0m key\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m,\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 135\u001b[0m organization\u001b[39m=\u001b[39m\u001b[39mNone\u001b[39;00m,\n\u001b[0;32m 136\u001b[0m ):\n\u001b[0;32m 137\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_base \u001b[39m=\u001b[39m api_base \u001b[39mor\u001b[39;00m openai\u001b[39m.\u001b[39mapi_base\n\u001b[1;32m--> 138\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_key \u001b[39m=\u001b[39m key \u001b[39mor\u001b[39;00m util\u001b[39m.\u001b[39;49mdefault_api_key()\n\u001b[0;32m 139\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_type \u001b[39m=\u001b[39m (\n\u001b[0;32m 140\u001b[0m ApiType\u001b[39m.\u001b[39mfrom_str(api_type)\n\u001b[0;32m 141\u001b[0m \u001b[39mif\u001b[39;00m api_type\n\u001b[0;32m 142\u001b[0m \u001b[39melse\u001b[39;00m ApiType\u001b[39m.\u001b[39mfrom_str(openai\u001b[39m.\u001b[39mapi_type)\n\u001b[0;32m 143\u001b[0m )\n\u001b[0;32m 144\u001b[0m \u001b[39mself\u001b[39m\u001b[39m.\u001b[39mapi_version \u001b[39m=\u001b[39m api_version \u001b[39mor\u001b[39;00m openai\u001b[39m.\u001b[39mapi_version\n", - "File \u001b[1;32m~\\AppData\\Local\\Packages\\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\\LocalCache\\local-packages\\Python311\\site-packages\\openai\\util.py:186\u001b[0m, in \u001b[0;36mdefault_api_key\u001b[1;34m()\u001b[0m\n\u001b[0;32m 184\u001b[0m \u001b[39mreturn\u001b[39;00m openai\u001b[39m.\u001b[39mapi_key\n\u001b[0;32m 185\u001b[0m \u001b[39melse\u001b[39;00m:\n\u001b[1;32m--> 186\u001b[0m \u001b[39mraise\u001b[39;00m openai\u001b[39m.\u001b[39merror\u001b[39m.\u001b[39mAuthenticationError(\n\u001b[0;32m 187\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mNo API key provided. You can set your API key in code using \u001b[39m\u001b[39m'\u001b[39m\u001b[39mopenai.api_key = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m, or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with \u001b[39m\u001b[39m'\u001b[39m\u001b[39mopenai.api_key_path = \u001b[39m\u001b[39m'\u001b[39m\u001b[39m. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details.\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 188\u001b[0m )\n", - "\u001b[1;31mAuthenticationError\u001b[0m: No API key provided. You can set your API key in code using 'openai.api_key = ', or you can set the environment variable OPENAI_API_KEY=). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = '. You can generate API keys in the OpenAI web interface. See https://platform.openai.com/account/api-keys for details." + "Let the model decide what function to call:\n", + "{\n", + " \"id\": \"chatcmpl-7e9QCkcvCtXbyiZwm3eKVHHQ2waXE\",\n", + " \"object\": \"chat.completion\",\n", + " \"created\": 1689803692,\n", + " \"model\": \"gpt-4\",\n", + " \"prompt_annotations\": [\n", + " {\n", + " \"prompt_index\": 0,\n", + " \"content_filter_results\": {\n", + " \"hate\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"self_harm\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"sexual\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"violence\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " }\n", + " }\n", + " }\n", + " ],\n", + " \"choices\": [\n", + " {\n", + " \"index\": 0,\n", + " \"finish_reason\": \"function_call\",\n", + " \"message\": {\n", + " \"role\": \"assistant\",\n", + " \"function_call\": {\n", + " \"name\": \"get_current_weather\",\n", + " \"arguments\": \"{\\n \\\"location\\\": \\\"San Francisco\\\"\\n}\"\n", + " }\n", + " },\n", + " \"content_filter_results\": {}\n", + " }\n", + " ],\n", + " \"usage\": {\n", + " \"completion_tokens\": 17,\n", + " \"prompt_tokens\": 83,\n", + " \"total_tokens\": 100\n", + " }\n", + "}\n", + "Don't call any function:\n", + "{\n", + " \"id\": \"chatcmpl-7e9QDkseJ33uGMacx6X4MEmS12BuT\",\n", + " \"object\": \"chat.completion\",\n", + " \"created\": 1689803693,\n", + " \"model\": \"gpt-4\",\n", + " \"prompt_annotations\": [\n", + " {\n", + " \"prompt_index\": 0,\n", + " \"content_filter_results\": {\n", + " \"hate\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"self_harm\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"sexual\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"violence\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " }\n", + " }\n", + " }\n", + " ],\n", + " \"choices\": [\n", + " {\n", + " \"index\": 0,\n", + " \"finish_reason\": \"stop\",\n", + " \"message\": {\n", + " \"role\": \"assistant\",\n", + " \"content\": \"Sure, let me just find that information for you.\"\n", + " },\n", + " \"content_filter_results\": {\n", + " \"hate\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"self_harm\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"sexual\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"violence\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " }\n", + " }\n", + " }\n", + " ],\n", + " \"usage\": {\n", + " \"completion_tokens\": 11,\n", + " \"prompt_tokens\": 84,\n", + " \"total_tokens\": 95\n", + " }\n", + "}\n", + "Force a specific function call:\n", + "{\n", + " \"id\": \"chatcmpl-7e9QEsAAhn44pwg7ty9osRltgwXPb\",\n", + " \"object\": \"chat.completion\",\n", + " \"created\": 1689803694,\n", + " \"model\": \"gpt-4\",\n", + " \"prompt_annotations\": [\n", + " {\n", + " \"prompt_index\": 0,\n", + " \"content_filter_results\": {\n", + " \"hate\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"self_harm\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"sexual\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " },\n", + " \"violence\": {\n", + " \"filtered\": false,\n", + " \"severity\": \"safe\"\n", + " }\n", + " }\n", + " }\n", + " ],\n", + " \"choices\": [\n", + " {\n", + " \"index\": 0,\n", + " \"finish_reason\": \"stop\",\n", + " \"message\": {\n", + " \"role\": \"assistant\",\n", + " \"function_call\": {\n", + " \"name\": \"get_current_weather\",\n", + " \"arguments\": \"{\\n \\\"location\\\": \\\"San Francisco\\\"\\n}\"\n", + " }\n", + " },\n", + " \"content_filter_results\": {}\n", + " }\n", + " ],\n", + " \"usage\": {\n", + " \"completion_tokens\": 10,\n", + " \"prompt_tokens\": 90,\n", + " \"total_tokens\": 100\n", + " }\n", + "}\n" ] } ], @@ -204,7 +324,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 103, "metadata": {}, "outputs": [], "source": [ @@ -227,16 +347,16 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'03:21:55 PM'" + "'05:54:54 PM'" ] }, - "execution_count": 6, + "execution_count": 104, "metadata": {}, "output_type": "execute_result" } @@ -255,34 +375,7 @@ }, { "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "import pandas as pd\n", - "\n", - "# Define your data\n", - "data = [\n", - " {'Index': 'S&P 500', 'Date': '2023-07-12', 'Open': 4300.25, 'High': 4350.32, 'Low': 4200.20, 'Close': 4325.74, 'Volume': 3500000},\n", - " {'Index': 'S&P 500', 'Date': '2023-07-13', 'Open': 4325.55, 'High': 4350.00, 'Low': 4300.98, 'Close': 4310.33, 'Volume': 3600000},\n", - " {'Index': 'NASDAQ Composite', 'Date': '2023-07-12', 'Open': 14000.65, 'High': 14200.06, 'Low': 13800.08, 'Close': 14100.44, 'Volume': 4000000},\n", - " {'Index': 'NASDAQ Composite', 'Date': '2023-07-13', 'Open': 14100.11, 'High': 14250.00, 'Low': 14000.67, 'Close': 14050.81, 'Volume': 4200000},\n", - " {'Index': 'Dow Jones Industrial Average', 'Date': '2023-07-12', 'Open': 34000.87, 'High': 34500.22, 'Low': 33000.11, 'Close': 34200.90, 'Volume': 3000000},\n", - " {'Index': 'Dow Jones Industrial Average', 'Date': '2023-07-13', 'Open': 34200.73, 'High': 34500.73, 'Low': 34000.40, 'Close': 34100.64, 'Volume': 3200000},\n", - " {'Index': 'Financial Times Stock Exchange 100 Index', 'Date': '2023-07-12', 'Open': 7000.55, 'High': 7050.61, 'Low': 6900.76, 'Close': 7025.22, 'Volume': 2000000},\n", - " {'Index': 'Financial Times Stock Exchange 100 Index', 'Date': '2023-07-13', 'Open': 7025.39, 'High': 7050.90, 'Low': 7000.56, 'Close': 7010.24, 'Volume': 2100000}\n", - "]\n", - "\n", - "# Create a DataFrame\n", - "df = pd.DataFrame(data)\n", - "\n", - "# Write the DataFrame to a CSV file\n", - "df.to_csv('stock_data.csv', index=False)\n" - ] - }, - { - "cell_type": "code", - "execution_count": null, + "execution_count": 105, "metadata": {}, "outputs": [], "source": [ @@ -315,7 +408,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 106, "metadata": {}, "outputs": [ { @@ -339,7 +432,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 107, "metadata": {}, "outputs": [], "source": [ @@ -364,7 +457,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 115, "metadata": {}, "outputs": [ { @@ -398,7 +491,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 116, "metadata": {}, "outputs": [], "source": [ @@ -462,7 +555,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 117, "metadata": {}, "outputs": [], "source": [ @@ -487,7 +580,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 118, "metadata": {}, "outputs": [], "source": [ @@ -564,7 +657,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 119, "metadata": {}, "outputs": [ { @@ -578,16 +671,16 @@ "}\n", "\n", "Output of function call:\n", - "03:22:04 PM\n", + "05:55:08 PM\n", "\n", "Messages in second request:\n", "{'role': 'user', 'content': 'What time is it in New York?'}\n", "{'role': 'assistant', 'name': 'get_current_time', 'content': '{\\n \"location\": \"America/New_York\"\\n}'}\n", - "{'role': 'function', 'name': 'get_current_time', 'content': '03:22:04 PM'}\n", + "{'role': 'function', 'name': 'get_current_time', 'content': '05:55:08 PM'}\n", "\n", "{\n", " \"role\": \"assistant\",\n", - " \"content\": \"The current time in New York is 03:22 PM.\"\n", + " \"content\": \"The current time in New York is 05:55:08 PM.\"\n", "}\n" ] } @@ -608,7 +701,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 120, "metadata": {}, "outputs": [], "source": [ @@ -688,7 +781,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 121, "metadata": {}, "outputs": [ { From e225c1ad5375a1e4a373e224fd918b58a367f4d6 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Thu, 20 Jul 2023 11:37:05 -0700 Subject: [PATCH 04/11] minor clean-up changes --- Basic_Samples/Functions/README.md | 4 +- .../Functions/working_with_functions.ipynb | 69 +++++++++---------- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/Basic_Samples/Functions/README.md b/Basic_Samples/Functions/README.md index 4c909285..fe155856 100644 --- a/Basic_Samples/Functions/README.md +++ b/Basic_Samples/Functions/README.md @@ -1,5 +1,5 @@ # Introduction -This repository contains samples demonstrating how to use functions to extend the current capabilities of GPT Models +This repository contains samples demonstrating how to use functions to extend the current capabilities of GPT Models. ## Installation Install all Python modules and packages listed in the requirements.txt file using the below command. @@ -9,7 +9,7 @@ pip install -r requirements.txt ``` ### Microsoft Azure Endpoints -In order to use the Open AI library or REST API with Microsoft Azure endpoints, you need to set DEPLOYMENT_NAME, OPENAI_API_BASE & OPENAI_API_VERSION in _config.json_ file. +In order to use the Open AI library or REST API with Microsoft Azure endpoints, you need to set DEPLOYMENT_ID, OPENAI_API_BASE & OPENAI_API_VERSION in _config.json_ file. ```js { diff --git a/Basic_Samples/Functions/working_with_functions.ipynb b/Basic_Samples/Functions/working_with_functions.ipynb index b04017b1..f1f155cf 100644 --- a/Basic_Samples/Functions/working_with_functions.ipynb +++ b/Basic_Samples/Functions/working_with_functions.ipynb @@ -23,7 +23,7 @@ }, { "cell_type": "code", - "execution_count": 99, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -33,7 +33,7 @@ }, { "cell_type": "code", - "execution_count": 100, + "execution_count": 17, "metadata": {}, "outputs": [], "source": [ @@ -73,7 +73,7 @@ }, { "cell_type": "code", - "execution_count": 101, + "execution_count": 18, "metadata": {}, "outputs": [], "source": [ @@ -118,7 +118,7 @@ }, { "cell_type": "code", - "execution_count": 102, + "execution_count": 19, "metadata": {}, "outputs": [ { @@ -127,9 +127,9 @@ "text": [ "Let the model decide what function to call:\n", "{\n", - " \"id\": \"chatcmpl-7e9QCkcvCtXbyiZwm3eKVHHQ2waXE\",\n", + " \"id\": \"chatcmpl-7e9d5AFNbVuiil0l0IWCKSX0luNPV\",\n", " \"object\": \"chat.completion\",\n", - " \"created\": 1689803692,\n", + " \"created\": 1689804491,\n", " \"model\": \"gpt-4\",\n", " \"prompt_annotations\": [\n", " {\n", @@ -176,9 +176,9 @@ "}\n", "Don't call any function:\n", "{\n", - " \"id\": \"chatcmpl-7e9QDkseJ33uGMacx6X4MEmS12BuT\",\n", + " \"id\": \"chatcmpl-7e9d6AQAqykfQ8cnGlrkJM5zEZGSl\",\n", " \"object\": \"chat.completion\",\n", - " \"created\": 1689803693,\n", + " \"created\": 1689804492,\n", " \"model\": \"gpt-4\",\n", " \"prompt_annotations\": [\n", " {\n", @@ -209,7 +209,7 @@ " \"finish_reason\": \"stop\",\n", " \"message\": {\n", " \"role\": \"assistant\",\n", - " \"content\": \"Sure, let me just find that information for you.\"\n", + " \"content\": \"Sure, let me check that for you.\\n\\nI am using the function `get_current_weather` for this purpose.\"\n", " },\n", " \"content_filter_results\": {\n", " \"hate\": {\n", @@ -232,16 +232,16 @@ " }\n", " ],\n", " \"usage\": {\n", - " \"completion_tokens\": 11,\n", + " \"completion_tokens\": 23,\n", " \"prompt_tokens\": 84,\n", - " \"total_tokens\": 95\n", + " \"total_tokens\": 107\n", " }\n", "}\n", "Force a specific function call:\n", "{\n", - " \"id\": \"chatcmpl-7e9QEsAAhn44pwg7ty9osRltgwXPb\",\n", + " \"id\": \"chatcmpl-7e9d7bOcTyEe3mA9rBhAPflFEqYX3\",\n", " \"object\": \"chat.completion\",\n", - " \"created\": 1689803694,\n", + " \"created\": 1689804493,\n", " \"model\": \"gpt-4\",\n", " \"prompt_annotations\": [\n", " {\n", @@ -274,16 +274,16 @@ " \"role\": \"assistant\",\n", " \"function_call\": {\n", " \"name\": \"get_current_weather\",\n", - " \"arguments\": \"{\\n \\\"location\\\": \\\"San Francisco\\\"\\n}\"\n", + " \"arguments\": \"{\\n \\\"location\\\": \\\"San Francisco, CA\\\"\\n}\"\n", " }\n", " },\n", " \"content_filter_results\": {}\n", " }\n", " ],\n", " \"usage\": {\n", - " \"completion_tokens\": 10,\n", + " \"completion_tokens\": 12,\n", " \"prompt_tokens\": 90,\n", - " \"total_tokens\": 100\n", + " \"total_tokens\": 102\n", " }\n", "}\n" ] @@ -295,7 +295,6 @@ "print(\"Let the model decide what function to call:\")\n", "print (get_function_call(first_message, \"auto\"))\n", "\n", - "\n", "# 'none' : Don't call any function \n", "print(\"Don't call any function:\")\n", "print (get_function_call(first_message, \"none\"))\n", @@ -324,7 +323,7 @@ }, { "cell_type": "code", - "execution_count": 103, + "execution_count": 20, "metadata": {}, "outputs": [], "source": [ @@ -347,16 +346,16 @@ }, { "cell_type": "code", - "execution_count": 104, + "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'05:54:54 PM'" + "'06:07:59 PM'" ] }, - "execution_count": 104, + "execution_count": 6, "metadata": {}, "output_type": "execute_result" } @@ -375,7 +374,7 @@ }, { "cell_type": "code", - "execution_count": 105, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -408,7 +407,7 @@ }, { "cell_type": "code", - "execution_count": 106, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -432,7 +431,7 @@ }, { "cell_type": "code", - "execution_count": 107, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -457,7 +456,7 @@ }, { "cell_type": "code", - "execution_count": 115, + "execution_count": 10, "metadata": {}, "outputs": [ { @@ -477,7 +476,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## 3.0 Calling a Function using GPT\n", + "## 3.0 Calling a function using GPT\n", "\n", "Steps for Function Calling: \n", "\n", @@ -491,7 +490,7 @@ }, { "cell_type": "code", - "execution_count": 116, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -555,7 +554,7 @@ }, { "cell_type": "code", - "execution_count": 117, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -580,7 +579,7 @@ }, { "cell_type": "code", - "execution_count": 118, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -657,7 +656,7 @@ }, { "cell_type": "code", - "execution_count": 119, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -671,16 +670,16 @@ "}\n", "\n", "Output of function call:\n", - "05:55:08 PM\n", + "06:08:07 PM\n", "\n", "Messages in second request:\n", "{'role': 'user', 'content': 'What time is it in New York?'}\n", "{'role': 'assistant', 'name': 'get_current_time', 'content': '{\\n \"location\": \"America/New_York\"\\n}'}\n", - "{'role': 'function', 'name': 'get_current_time', 'content': '05:55:08 PM'}\n", + "{'role': 'function', 'name': 'get_current_time', 'content': '06:08:07 PM'}\n", "\n", "{\n", " \"role\": \"assistant\",\n", - " \"content\": \"The current time in New York is 05:55:08 PM.\"\n", + " \"content\": \"The current time in New York is 06:08 PM.\"\n", "}\n" ] } @@ -701,7 +700,7 @@ }, { "cell_type": "code", - "execution_count": 120, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -781,7 +780,7 @@ }, { "cell_type": "code", - "execution_count": 121, + "execution_count": 16, "metadata": {}, "outputs": [ { From c42ef46d8b6ceb1292f00eaed13d35b67a60fe6f Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Thu, 20 Jul 2023 11:55:43 -0700 Subject: [PATCH 05/11] updating output --- .../Functions/working_with_functions.ipynb | 203 ++++-------------- 1 file changed, 40 insertions(+), 163 deletions(-) diff --git a/Basic_Samples/Functions/working_with_functions.ipynb b/Basic_Samples/Functions/working_with_functions.ipynb index 89183dca..eb4a7296 100644 --- a/Basic_Samples/Functions/working_with_functions.ipynb +++ b/Basic_Samples/Functions/working_with_functions.ipynb @@ -23,11 +23,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 1, -======= - "execution_count": 99, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 21, "metadata": {}, "outputs": [], "source": [ @@ -37,11 +33,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 17, -======= - "execution_count": 100, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 22, "metadata": {}, "outputs": [], "source": [ @@ -81,11 +73,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 18, -======= - "execution_count": 101, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 23, "metadata": {}, "outputs": [], "source": [ @@ -130,28 +118,24 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 19, -======= - "execution_count": 102, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 24, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Let the model decide what function to call:\n", + "Let the model decide what function to call:\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ "{\n", -<<<<<<< HEAD - " \"id\": \"chatcmpl-7e9d5AFNbVuiil0l0IWCKSX0luNPV\",\n", + " \"id\": \"chatcmpl-7eT5XAUt5gGx6o3oEcdbrOHYD4HDp\",\n", " \"object\": \"chat.completion\",\n", - " \"created\": 1689804491,\n", -======= - " \"id\": \"chatcmpl-7e9QCkcvCtXbyiZwm3eKVHHQ2waXE\",\n", - " \"object\": \"chat.completion\",\n", - " \"created\": 1689803692,\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + " \"created\": 1689879291,\n", " \"model\": \"gpt-4\",\n", " \"prompt_annotations\": [\n", " {\n", @@ -184,29 +168,23 @@ " \"role\": \"assistant\",\n", " \"function_call\": {\n", " \"name\": \"get_current_weather\",\n", - " \"arguments\": \"{\\n \\\"location\\\": \\\"San Francisco\\\"\\n}\"\n", + " \"arguments\": \"{\\n \\\"location\\\": \\\"San Francisco, CA\\\"\\n}\"\n", " }\n", " },\n", " \"content_filter_results\": {}\n", " }\n", " ],\n", " \"usage\": {\n", - " \"completion_tokens\": 17,\n", + " \"completion_tokens\": 19,\n", " \"prompt_tokens\": 83,\n", - " \"total_tokens\": 100\n", + " \"total_tokens\": 102\n", " }\n", "}\n", "Don't call any function:\n", "{\n", -<<<<<<< HEAD - " \"id\": \"chatcmpl-7e9d6AQAqykfQ8cnGlrkJM5zEZGSl\",\n", - " \"object\": \"chat.completion\",\n", - " \"created\": 1689804492,\n", -======= - " \"id\": \"chatcmpl-7e9QDkseJ33uGMacx6X4MEmS12BuT\",\n", + " \"id\": \"chatcmpl-7eT5YoCPrCTz18wr8HyXruy7M9EPt\",\n", " \"object\": \"chat.completion\",\n", - " \"created\": 1689803693,\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + " \"created\": 1689879292,\n", " \"model\": \"gpt-4\",\n", " \"prompt_annotations\": [\n", " {\n", @@ -237,11 +215,7 @@ " \"finish_reason\": \"stop\",\n", " \"message\": {\n", " \"role\": \"assistant\",\n", -<<<<<<< HEAD - " \"content\": \"Sure, let me check that for you.\\n\\nI am using the function `get_current_weather` for this purpose.\"\n", -======= - " \"content\": \"Sure, let me just find that information for you.\"\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + " \"content\": \"Just one moment please, let me check the current weather in San Francisco for you.\"\n", " },\n", " \"content_filter_results\": {\n", " \"hate\": {\n", @@ -264,28 +238,16 @@ " }\n", " ],\n", " \"usage\": {\n", -<<<<<<< HEAD - " \"completion_tokens\": 23,\n", - " \"prompt_tokens\": 84,\n", - " \"total_tokens\": 107\n", -======= - " \"completion_tokens\": 11,\n", + " \"completion_tokens\": 17,\n", " \"prompt_tokens\": 84,\n", - " \"total_tokens\": 95\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + " \"total_tokens\": 101\n", " }\n", "}\n", "Force a specific function call:\n", "{\n", -<<<<<<< HEAD - " \"id\": \"chatcmpl-7e9d7bOcTyEe3mA9rBhAPflFEqYX3\",\n", - " \"object\": \"chat.completion\",\n", - " \"created\": 1689804493,\n", -======= - " \"id\": \"chatcmpl-7e9QEsAAhn44pwg7ty9osRltgwXPb\",\n", + " \"id\": \"chatcmpl-7eT5ZBn6q623O6nN2EOwjJTjtDWY3\",\n", " \"object\": \"chat.completion\",\n", - " \"created\": 1689803694,\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + " \"created\": 1689879293,\n", " \"model\": \"gpt-4\",\n", " \"prompt_annotations\": [\n", " {\n", @@ -318,26 +280,16 @@ " \"role\": \"assistant\",\n", " \"function_call\": {\n", " \"name\": \"get_current_weather\",\n", -<<<<<<< HEAD " \"arguments\": \"{\\n \\\"location\\\": \\\"San Francisco, CA\\\"\\n}\"\n", -======= - " \"arguments\": \"{\\n \\\"location\\\": \\\"San Francisco\\\"\\n}\"\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b " }\n", " },\n", " \"content_filter_results\": {}\n", " }\n", " ],\n", " \"usage\": {\n", -<<<<<<< HEAD " \"completion_tokens\": 12,\n", " \"prompt_tokens\": 90,\n", " \"total_tokens\": 102\n", -======= - " \"completion_tokens\": 10,\n", - " \"prompt_tokens\": 90,\n", - " \"total_tokens\": 100\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b " }\n", "}\n" ] @@ -349,10 +301,6 @@ "print(\"Let the model decide what function to call:\")\n", "print (get_function_call(first_message, \"auto\"))\n", "\n", -<<<<<<< HEAD -======= - "\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b "# 'none' : Don't call any function \n", "print(\"Don't call any function:\")\n", "print (get_function_call(first_message, \"none\"))\n", @@ -381,11 +329,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 20, -======= - "execution_count": 103, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 25, "metadata": {}, "outputs": [], "source": [ @@ -408,27 +352,16 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 6, -======= - "execution_count": 104, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ -<<<<<<< HEAD - "'06:07:59 PM'" + "'02:54:52 PM'" ] }, - "execution_count": 6, -======= - "'05:54:54 PM'" - ] - }, - "execution_count": 104, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 26, "metadata": {}, "output_type": "execute_result" } @@ -447,11 +380,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 7, -======= - "execution_count": 105, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 27, "metadata": {}, "outputs": [], "source": [ @@ -484,11 +413,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 8, -======= - "execution_count": 106, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 28, "metadata": {}, "outputs": [ { @@ -512,11 +437,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 9, -======= - "execution_count": 107, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 29, "metadata": {}, "outputs": [], "source": [ @@ -541,11 +462,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 10, -======= - "execution_count": 115, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 30, "metadata": {}, "outputs": [ { @@ -565,11 +482,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ -<<<<<<< HEAD "## 3.0 Calling a function using GPT\n", -======= - "## 3.0 Calling a Function using GPT\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b "\n", "Steps for Function Calling: \n", "\n", @@ -583,11 +496,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 11, -======= - "execution_count": 116, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 31, "metadata": {}, "outputs": [], "source": [ @@ -651,11 +560,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 12, -======= - "execution_count": 117, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 32, "metadata": {}, "outputs": [], "source": [ @@ -680,11 +585,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 13, -======= - "execution_count": 118, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 33, "metadata": {}, "outputs": [], "source": [ @@ -761,11 +662,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 14, -======= - "execution_count": 119, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 34, "metadata": {}, "outputs": [ { @@ -779,28 +676,16 @@ "}\n", "\n", "Output of function call:\n", -<<<<<<< HEAD - "06:08:07 PM\n", -======= - "05:55:08 PM\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "02:54:53 PM\n", "\n", "Messages in second request:\n", "{'role': 'user', 'content': 'What time is it in New York?'}\n", "{'role': 'assistant', 'name': 'get_current_time', 'content': '{\\n \"location\": \"America/New_York\"\\n}'}\n", -<<<<<<< HEAD - "{'role': 'function', 'name': 'get_current_time', 'content': '06:08:07 PM'}\n", - "\n", - "{\n", - " \"role\": \"assistant\",\n", - " \"content\": \"The current time in New York is 06:08 PM.\"\n", -======= - "{'role': 'function', 'name': 'get_current_time', 'content': '05:55:08 PM'}\n", + "{'role': 'function', 'name': 'get_current_time', 'content': '02:54:53 PM'}\n", "\n", "{\n", " \"role\": \"assistant\",\n", - " \"content\": \"The current time in New York is 05:55:08 PM.\"\n", ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + " \"content\": \"It's 2:54 PM in New York.\"\n", "}\n" ] } @@ -821,11 +706,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 15, -======= - "execution_count": 120, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 35, "metadata": {}, "outputs": [], "source": [ @@ -905,11 +786,7 @@ }, { "cell_type": "code", -<<<<<<< HEAD - "execution_count": 16, -======= - "execution_count": 121, ->>>>>>> 79eed5c26849030e91a4d89e4a9fa61c2878229b + "execution_count": 36, "metadata": {}, "outputs": [ { From 7c25d3885aaa949f6f10ab921eff2e87d908e6e1 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Fri, 4 Aug 2023 14:18:52 -0700 Subject: [PATCH 06/11] adding LangChain samples --- .../Azure OpenAI and Langchain.ipynb | 304 ++++++++++++++++++ Basic_Samples/LangChain/README.md | 54 ++++ Basic_Samples/LangChain/config.json | 5 + Basic_Samples/LangChain/requirements.txt | 9 + 4 files changed, 372 insertions(+) create mode 100644 Basic_Samples/LangChain/Azure OpenAI and Langchain.ipynb create mode 100644 Basic_Samples/LangChain/README.md create mode 100644 Basic_Samples/LangChain/config.json create mode 100644 Basic_Samples/LangChain/requirements.txt diff --git a/Basic_Samples/LangChain/Azure OpenAI and Langchain.ipynb b/Basic_Samples/LangChain/Azure OpenAI and Langchain.ipynb new file mode 100644 index 00000000..7ef68f94 --- /dev/null +++ b/Basic_Samples/LangChain/Azure OpenAI and Langchain.ipynb @@ -0,0 +1,304 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6d251ced", + "metadata": {}, + "source": [ + "# How to use LangChain and Azure OpenAI with Python\n", + "\n", + "\n", + "Langchain is an open source framework for developing applications using large language models (LLM).
\n", + "\n", + "This guide will demonstrate how to setup and use Azure OpenAI models' API with LangChain.\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "9d0ee335", + "metadata": {}, + "source": [ + "## Set Up\n", + "The following libraries must be installed to use LangChain with Azure OpenAI.
" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "35289cea", + "metadata": {}, + "outputs": [], + "source": [ + "#%pip install --upgrade openai\n", + "#%pip install langchain" + ] + }, + { + "cell_type": "markdown", + "id": "ba880453", + "metadata": {}, + "source": [ + "## API Configuation\n", + "\n", + "After installing the necessary libraies, the API must be configured. The code below shows how to configure the API directly in your Python environment. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "a9752fda", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "from langchain.llms import AzureOpenAI \n", + "import openai\n", + "import json\n", + "import os\n", + "\n", + "# Load config values\n", + "with open(r'config.json') as config_file:\n", + " config_details = json.load(config_file)\n", + "\n", + "# Setting up the deployment name\n", + "deployment_name = config_details['DEPLOYMENT_NAME']\n", + "\n", + "# This is set to `azure`\n", + "openai.api_type = \"azure\"\n", + "\n", + "# The API key for your Azure OpenAI resource.\n", + "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "\n", + "# The base URL for your Azure OpenAI resource. e.g. \"https://.openai.azure.com\"\n", + "openai.api_base = config_details['OPENAI_API_BASE']\n", + "\n", + "# Currently Chat Completion API have the following versions available: 2023-07-01-preview\n", + "openai.api_version = config_details['OPENAI_API_VERSION']" + ] + }, + { + "cell_type": "markdown", + "id": "ab4779e5", + "metadata": {}, + "source": [ + "## Deployed Model Setup\n", + "\n", + "Azure OpenAI allows users to create and manage their own model deployments. To call the API, you must specify the deployment you want to use by passing in the deployment name and model name. " + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "6ea3e99f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"\\n\\n\\n\\n\\nThe world is a beautiful place,\\nThe colors are so bright and true,\\nAnd I feel so free and free,\\nWhen I'm away from here.\\n\\nThe sky is so blue,\\nAnd the sun is so warm,\\nAnd I feel so free and free,\\nWhen I'm away from here.\"" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create an instance of Azure OpenAI\n", + "\n", + "# Replace the deployment name and model name with your own\n", + "llm = AzureOpenAI(\n", + " deployment_name= deployment_name,\n", + " model_name=\"text-davinci-002\", \n", + ")\n", + "\n", + "# Run the LLM\n", + "llm(\"Write me a poem\")" + ] + }, + { + "cell_type": "markdown", + "id": "dc7ea2d4", + "metadata": {}, + "source": [ + "## PromptTemplates\n", + "\n", + "Langchain provides a built in PromptsTemplate module to simplify the construction of prompts to get more specific answers." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "927d4bac", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "There are a number of good face washes that can help with acne prone skin. A few popular options include the Neutrogena Oil-Free Acne Wash, the Cetaphil Dermatological Gentle Skin Cleanser, and the La Roche-Posay Effaclar Medicated Gel Cleanser.\n" + ] + } + ], + "source": [ + "from langchain import PromptTemplate\n", + "\n", + "template = \"\"\"\n", + "You are a skin care consulant that recommends products based on customer\n", + "needs and preferences.\n", + "\n", + "What is a good {product_type} to help with {customer_request}?\n", + "\"\"\"\n", + "\n", + "prompt = PromptTemplate(\n", + "input_variables=[\"product_type\", \"customer_request\"],\n", + "template=template,\n", + ")\n", + "\n", + "print(llm(\n", + " prompt.format(\n", + " product_type=\"face wash\",\n", + " customer_request = \"acne prone skin\"\n", + " )\n", + "))" + ] + }, + { + "cell_type": "markdown", + "id": "7b6723eb", + "metadata": {}, + "source": [ + "## Chains\n", + "There are many applications of chains that allow you to combine numerous LLM calls and actions.
\n", + "\n", + "### Simple Sequential Chains
\n", + "Allow you to feed the output of one LLM Chain as input for another." + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "af7c236f", + "metadata": {}, + "outputs": [], + "source": [ + "#from langchain.llms import AzureOpenAI\n", + "from langchain.chains import LLMChain\n", + "# from langchain.prompts import PromptTemplate\n", + "from langchain.chains import SimpleSequentialChain" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "2a4a32f0", + "metadata": {}, + "outputs": [], + "source": [ + "template = \"\"\"Your job is to come up with a fun DIY project for the specified gender, age, and description of a kid.\n", + "% CHILD_DESCRIPTION\n", + "{child_description}\n", + "\n", + "YOUR RESPONSE:\n", + "\"\"\"\n", + "prompt_template = PromptTemplate(input_variables=[\"child_description\"], template=template)\n", + "\n", + "# Holds my 'location' chain\n", + "description_chain = LLMChain(llm=llm, prompt=prompt_template)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "6eec47ff", + "metadata": {}, + "outputs": [], + "source": [ + "template = \"\"\"Given a DIY project, give a short and simple recipe step-by-step guide on how to complete the project and a materials list.\n", + "% DIY_PROJECT\n", + "{diy_project}\n", + "\n", + "YOUR RESPONSE:\n", + "\"\"\"\n", + "prompt_template = PromptTemplate(input_variables=[\"diy_project\"], template=template)\n", + "\n", + "# Holds my 'meal' chain\n", + "diy_chain = LLMChain(llm=llm, prompt=prompt_template)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "84a15aea", + "metadata": {}, + "outputs": [], + "source": [ + "overall_chain = SimpleSequentialChain(chains=[description_chain, diy_chain], verbose=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "15928f72", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[36;1m\u001b[1;3m\n", + "A simple and fun DIY project for a 5-year-old girl is to make a paper doll. All you need is some paper, scissors, and crayons or markers. First, cut out a paper doll shape from the paper. Then, decorate the paper doll with clothes, hair, and facial features. You can also cut out paper furniture and accessories to create a paper doll scene.\u001b[0m\n", + "\u001b[33;1m\u001b[1;3m\n", + "1. Cut out a paper doll shape from the paper.\n", + "2. Decorate the paper doll with clothes, hair, and facial features.\n", + "3. Cut out paper furniture and accessories to create a paper doll scene.\n", + "\n", + "Materials needed:\n", + "\n", + "-Paper\n", + "-Scissors\n", + "-Crayons or markers\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + } + ], + "source": [ + "review = overall_chain.run(\"5-year-old girl\")" + ] + } + ], + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/Basic_Samples/LangChain/README.md b/Basic_Samples/LangChain/README.md new file mode 100644 index 00000000..ef121f83 --- /dev/null +++ b/Basic_Samples/LangChain/README.md @@ -0,0 +1,54 @@ +# Introduction +This repository shows how to setup and use Azure OpenAI models' API with LangChain. + +## Installation +Install all Python modules and packages listed in the requirements.txt file using the below command. + +```python +pip install -r requirements.txt +``` + +### Microsoft Azure Endpoints +In order to use the Open AI library or REST API with Microsoft Azure endpoints, you need to set DEPLOYMENT_NAME, OPENAI_API_BASE & OPENAI_API_VERSION in _config.json_ file. + +```js +{ + "DEPLOYMENT_NAME":"", + "OPENAI_API_BASE":"https://.openai.azure.com", + "OPENAI_API_VERSION":"" +} +``` + +### For getting started: +- Add "OPENAI_API_KEY" as variable name and \ as variable value in the environment variables. +
+One can get the OPENAI_API_KEY value from the Azure Portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values. +
+ + WINDOWS Users: + setx OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" + + MACOS/LINUX Users: + export OPENAI_API_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" + +- To find your "DEPLOYMENT_NAME" go to the deployments page of the Azure AI Studio. Create a deployment if one does not already exist. +One can start with using your model name as "gpt-35-turbo-0613" or "gpt-4." + +- To find your "OPENAI_API_BASE" go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value. + +- Check out versions [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference). + + +## Requirements +Python 3.8+
+Jupyter Notebook 6.5.2 + +
+ +## Trademarks + +This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft +trademarks or logos is subject to and must follow +[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). +Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. +Any use of third-party trademarks or logos are subject to those third-party's policies. \ No newline at end of file diff --git a/Basic_Samples/LangChain/config.json b/Basic_Samples/LangChain/config.json new file mode 100644 index 00000000..70da44d3 --- /dev/null +++ b/Basic_Samples/LangChain/config.json @@ -0,0 +1,5 @@ +{ + "DEPLOYMENT_ID":"", + "OPENAI_API_BASE":"https://.openai.azure.com", + "OPENAI_API_VERSION":"" +} \ No newline at end of file diff --git a/Basic_Samples/LangChain/requirements.txt b/Basic_Samples/LangChain/requirements.txt new file mode 100644 index 00000000..521ef18a --- /dev/null +++ b/Basic_Samples/LangChain/requirements.txt @@ -0,0 +1,9 @@ +# Packages needed to call AOAI API with the OpenAI Python API +openai + +# Packages needed to run the notebook samples +jupyter + +# Other packages needed to run the notebook samples +langchain +json \ No newline at end of file From b5ec5e0c4f79b89c2c6935e70724bd9d7bf4e8e1 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Fri, 4 Aug 2023 14:21:24 -0700 Subject: [PATCH 07/11] renaming file --- .../LangChain/working_with_langchain.ipynb | 304 ++++++++++++++++++ 1 file changed, 304 insertions(+) create mode 100644 Basic_Samples/LangChain/working_with_langchain.ipynb diff --git a/Basic_Samples/LangChain/working_with_langchain.ipynb b/Basic_Samples/LangChain/working_with_langchain.ipynb new file mode 100644 index 00000000..7ef68f94 --- /dev/null +++ b/Basic_Samples/LangChain/working_with_langchain.ipynb @@ -0,0 +1,304 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6d251ced", + "metadata": {}, + "source": [ + "# How to use LangChain and Azure OpenAI with Python\n", + "\n", + "\n", + "Langchain is an open source framework for developing applications using large language models (LLM).
\n", + "\n", + "This guide will demonstrate how to setup and use Azure OpenAI models' API with LangChain.\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "9d0ee335", + "metadata": {}, + "source": [ + "## Set Up\n", + "The following libraries must be installed to use LangChain with Azure OpenAI.
" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "35289cea", + "metadata": {}, + "outputs": [], + "source": [ + "#%pip install --upgrade openai\n", + "#%pip install langchain" + ] + }, + { + "cell_type": "markdown", + "id": "ba880453", + "metadata": {}, + "source": [ + "## API Configuation\n", + "\n", + "After installing the necessary libraies, the API must be configured. The code below shows how to configure the API directly in your Python environment. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "a9752fda", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "from langchain.llms import AzureOpenAI \n", + "import openai\n", + "import json\n", + "import os\n", + "\n", + "# Load config values\n", + "with open(r'config.json') as config_file:\n", + " config_details = json.load(config_file)\n", + "\n", + "# Setting up the deployment name\n", + "deployment_name = config_details['DEPLOYMENT_NAME']\n", + "\n", + "# This is set to `azure`\n", + "openai.api_type = \"azure\"\n", + "\n", + "# The API key for your Azure OpenAI resource.\n", + "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "\n", + "# The base URL for your Azure OpenAI resource. e.g. \"https://.openai.azure.com\"\n", + "openai.api_base = config_details['OPENAI_API_BASE']\n", + "\n", + "# Currently Chat Completion API have the following versions available: 2023-07-01-preview\n", + "openai.api_version = config_details['OPENAI_API_VERSION']" + ] + }, + { + "cell_type": "markdown", + "id": "ab4779e5", + "metadata": {}, + "source": [ + "## Deployed Model Setup\n", + "\n", + "Azure OpenAI allows users to create and manage their own model deployments. To call the API, you must specify the deployment you want to use by passing in the deployment name and model name. " + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "6ea3e99f", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "\"\\n\\n\\n\\n\\nThe world is a beautiful place,\\nThe colors are so bright and true,\\nAnd I feel so free and free,\\nWhen I'm away from here.\\n\\nThe sky is so blue,\\nAnd the sun is so warm,\\nAnd I feel so free and free,\\nWhen I'm away from here.\"" + ] + }, + "execution_count": 33, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create an instance of Azure OpenAI\n", + "\n", + "# Replace the deployment name and model name with your own\n", + "llm = AzureOpenAI(\n", + " deployment_name= deployment_name,\n", + " model_name=\"text-davinci-002\", \n", + ")\n", + "\n", + "# Run the LLM\n", + "llm(\"Write me a poem\")" + ] + }, + { + "cell_type": "markdown", + "id": "dc7ea2d4", + "metadata": {}, + "source": [ + "## PromptTemplates\n", + "\n", + "Langchain provides a built in PromptsTemplate module to simplify the construction of prompts to get more specific answers." + ] + }, + { + "cell_type": "code", + "execution_count": 40, + "id": "927d4bac", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "There are a number of good face washes that can help with acne prone skin. A few popular options include the Neutrogena Oil-Free Acne Wash, the Cetaphil Dermatological Gentle Skin Cleanser, and the La Roche-Posay Effaclar Medicated Gel Cleanser.\n" + ] + } + ], + "source": [ + "from langchain import PromptTemplate\n", + "\n", + "template = \"\"\"\n", + "You are a skin care consulant that recommends products based on customer\n", + "needs and preferences.\n", + "\n", + "What is a good {product_type} to help with {customer_request}?\n", + "\"\"\"\n", + "\n", + "prompt = PromptTemplate(\n", + "input_variables=[\"product_type\", \"customer_request\"],\n", + "template=template,\n", + ")\n", + "\n", + "print(llm(\n", + " prompt.format(\n", + " product_type=\"face wash\",\n", + " customer_request = \"acne prone skin\"\n", + " )\n", + "))" + ] + }, + { + "cell_type": "markdown", + "id": "7b6723eb", + "metadata": {}, + "source": [ + "## Chains\n", + "There are many applications of chains that allow you to combine numerous LLM calls and actions.
\n", + "\n", + "### Simple Sequential Chains
\n", + "Allow you to feed the output of one LLM Chain as input for another." + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "af7c236f", + "metadata": {}, + "outputs": [], + "source": [ + "#from langchain.llms import AzureOpenAI\n", + "from langchain.chains import LLMChain\n", + "# from langchain.prompts import PromptTemplate\n", + "from langchain.chains import SimpleSequentialChain" + ] + }, + { + "cell_type": "code", + "execution_count": 51, + "id": "2a4a32f0", + "metadata": {}, + "outputs": [], + "source": [ + "template = \"\"\"Your job is to come up with a fun DIY project for the specified gender, age, and description of a kid.\n", + "% CHILD_DESCRIPTION\n", + "{child_description}\n", + "\n", + "YOUR RESPONSE:\n", + "\"\"\"\n", + "prompt_template = PromptTemplate(input_variables=[\"child_description\"], template=template)\n", + "\n", + "# Holds my 'location' chain\n", + "description_chain = LLMChain(llm=llm, prompt=prompt_template)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "id": "6eec47ff", + "metadata": {}, + "outputs": [], + "source": [ + "template = \"\"\"Given a DIY project, give a short and simple recipe step-by-step guide on how to complete the project and a materials list.\n", + "% DIY_PROJECT\n", + "{diy_project}\n", + "\n", + "YOUR RESPONSE:\n", + "\"\"\"\n", + "prompt_template = PromptTemplate(input_variables=[\"diy_project\"], template=template)\n", + "\n", + "# Holds my 'meal' chain\n", + "diy_chain = LLMChain(llm=llm, prompt=prompt_template)" + ] + }, + { + "cell_type": "code", + "execution_count": 56, + "id": "84a15aea", + "metadata": {}, + "outputs": [], + "source": [ + "overall_chain = SimpleSequentialChain(chains=[description_chain, diy_chain], verbose=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 57, + "id": "15928f72", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[36;1m\u001b[1;3m\n", + "A simple and fun DIY project for a 5-year-old girl is to make a paper doll. All you need is some paper, scissors, and crayons or markers. First, cut out a paper doll shape from the paper. Then, decorate the paper doll with clothes, hair, and facial features. You can also cut out paper furniture and accessories to create a paper doll scene.\u001b[0m\n", + "\u001b[33;1m\u001b[1;3m\n", + "1. Cut out a paper doll shape from the paper.\n", + "2. Decorate the paper doll with clothes, hair, and facial features.\n", + "3. Cut out paper furniture and accessories to create a paper doll scene.\n", + "\n", + "Materials needed:\n", + "\n", + "-Paper\n", + "-Scissors\n", + "-Crayons or markers\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + } + ], + "source": [ + "review = overall_chain.run(\"5-year-old girl\")" + ] + } + ], + "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.11.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From d88cca765272d35c4f2c9224c81134fd67ebc6a7 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Fri, 4 Aug 2023 14:27:50 -0700 Subject: [PATCH 08/11] removing file duplicate --- .../Azure OpenAI and Langchain.ipynb | 304 ------------------ 1 file changed, 304 deletions(-) delete mode 100644 Basic_Samples/LangChain/Azure OpenAI and Langchain.ipynb diff --git a/Basic_Samples/LangChain/Azure OpenAI and Langchain.ipynb b/Basic_Samples/LangChain/Azure OpenAI and Langchain.ipynb deleted file mode 100644 index 7ef68f94..00000000 --- a/Basic_Samples/LangChain/Azure OpenAI and Langchain.ipynb +++ /dev/null @@ -1,304 +0,0 @@ -{ - "cells": [ - { - "cell_type": "markdown", - "id": "6d251ced", - "metadata": {}, - "source": [ - "# How to use LangChain and Azure OpenAI with Python\n", - "\n", - "\n", - "Langchain is an open source framework for developing applications using large language models (LLM).
\n", - "\n", - "This guide will demonstrate how to setup and use Azure OpenAI models' API with LangChain.\n", - " " - ] - }, - { - "cell_type": "markdown", - "id": "9d0ee335", - "metadata": {}, - "source": [ - "## Set Up\n", - "The following libraries must be installed to use LangChain with Azure OpenAI.
" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "id": "35289cea", - "metadata": {}, - "outputs": [], - "source": [ - "#%pip install --upgrade openai\n", - "#%pip install langchain" - ] - }, - { - "cell_type": "markdown", - "id": "ba880453", - "metadata": {}, - "source": [ - "## API Configuation\n", - "\n", - "After installing the necessary libraies, the API must be configured. The code below shows how to configure the API directly in your Python environment. \n" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "id": "a9752fda", - "metadata": { - "scrolled": true - }, - "outputs": [], - "source": [ - "from langchain.llms import AzureOpenAI \n", - "import openai\n", - "import json\n", - "import os\n", - "\n", - "# Load config values\n", - "with open(r'config.json') as config_file:\n", - " config_details = json.load(config_file)\n", - "\n", - "# Setting up the deployment name\n", - "deployment_name = config_details['DEPLOYMENT_NAME']\n", - "\n", - "# This is set to `azure`\n", - "openai.api_type = \"azure\"\n", - "\n", - "# The API key for your Azure OpenAI resource.\n", - "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", - "\n", - "# The base URL for your Azure OpenAI resource. e.g. \"https://.openai.azure.com\"\n", - "openai.api_base = config_details['OPENAI_API_BASE']\n", - "\n", - "# Currently Chat Completion API have the following versions available: 2023-07-01-preview\n", - "openai.api_version = config_details['OPENAI_API_VERSION']" - ] - }, - { - "cell_type": "markdown", - "id": "ab4779e5", - "metadata": {}, - "source": [ - "## Deployed Model Setup\n", - "\n", - "Azure OpenAI allows users to create and manage their own model deployments. To call the API, you must specify the deployment you want to use by passing in the deployment name and model name. " - ] - }, - { - "cell_type": "code", - "execution_count": 33, - "id": "6ea3e99f", - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "\"\\n\\n\\n\\n\\nThe world is a beautiful place,\\nThe colors are so bright and true,\\nAnd I feel so free and free,\\nWhen I'm away from here.\\n\\nThe sky is so blue,\\nAnd the sun is so warm,\\nAnd I feel so free and free,\\nWhen I'm away from here.\"" - ] - }, - "execution_count": 33, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "# Create an instance of Azure OpenAI\n", - "\n", - "# Replace the deployment name and model name with your own\n", - "llm = AzureOpenAI(\n", - " deployment_name= deployment_name,\n", - " model_name=\"text-davinci-002\", \n", - ")\n", - "\n", - "# Run the LLM\n", - "llm(\"Write me a poem\")" - ] - }, - { - "cell_type": "markdown", - "id": "dc7ea2d4", - "metadata": {}, - "source": [ - "## PromptTemplates\n", - "\n", - "Langchain provides a built in PromptsTemplate module to simplify the construction of prompts to get more specific answers." - ] - }, - { - "cell_type": "code", - "execution_count": 40, - "id": "927d4bac", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "There are a number of good face washes that can help with acne prone skin. A few popular options include the Neutrogena Oil-Free Acne Wash, the Cetaphil Dermatological Gentle Skin Cleanser, and the La Roche-Posay Effaclar Medicated Gel Cleanser.\n" - ] - } - ], - "source": [ - "from langchain import PromptTemplate\n", - "\n", - "template = \"\"\"\n", - "You are a skin care consulant that recommends products based on customer\n", - "needs and preferences.\n", - "\n", - "What is a good {product_type} to help with {customer_request}?\n", - "\"\"\"\n", - "\n", - "prompt = PromptTemplate(\n", - "input_variables=[\"product_type\", \"customer_request\"],\n", - "template=template,\n", - ")\n", - "\n", - "print(llm(\n", - " prompt.format(\n", - " product_type=\"face wash\",\n", - " customer_request = \"acne prone skin\"\n", - " )\n", - "))" - ] - }, - { - "cell_type": "markdown", - "id": "7b6723eb", - "metadata": {}, - "source": [ - "## Chains\n", - "There are many applications of chains that allow you to combine numerous LLM calls and actions.
\n", - "\n", - "### Simple Sequential Chains
\n", - "Allow you to feed the output of one LLM Chain as input for another." - ] - }, - { - "cell_type": "code", - "execution_count": 46, - "id": "af7c236f", - "metadata": {}, - "outputs": [], - "source": [ - "#from langchain.llms import AzureOpenAI\n", - "from langchain.chains import LLMChain\n", - "# from langchain.prompts import PromptTemplate\n", - "from langchain.chains import SimpleSequentialChain" - ] - }, - { - "cell_type": "code", - "execution_count": 51, - "id": "2a4a32f0", - "metadata": {}, - "outputs": [], - "source": [ - "template = \"\"\"Your job is to come up with a fun DIY project for the specified gender, age, and description of a kid.\n", - "% CHILD_DESCRIPTION\n", - "{child_description}\n", - "\n", - "YOUR RESPONSE:\n", - "\"\"\"\n", - "prompt_template = PromptTemplate(input_variables=[\"child_description\"], template=template)\n", - "\n", - "# Holds my 'location' chain\n", - "description_chain = LLMChain(llm=llm, prompt=prompt_template)" - ] - }, - { - "cell_type": "code", - "execution_count": 55, - "id": "6eec47ff", - "metadata": {}, - "outputs": [], - "source": [ - "template = \"\"\"Given a DIY project, give a short and simple recipe step-by-step guide on how to complete the project and a materials list.\n", - "% DIY_PROJECT\n", - "{diy_project}\n", - "\n", - "YOUR RESPONSE:\n", - "\"\"\"\n", - "prompt_template = PromptTemplate(input_variables=[\"diy_project\"], template=template)\n", - "\n", - "# Holds my 'meal' chain\n", - "diy_chain = LLMChain(llm=llm, prompt=prompt_template)" - ] - }, - { - "cell_type": "code", - "execution_count": 56, - "id": "84a15aea", - "metadata": {}, - "outputs": [], - "source": [ - "overall_chain = SimpleSequentialChain(chains=[description_chain, diy_chain], verbose=True)" - ] - }, - { - "cell_type": "code", - "execution_count": 57, - "id": "15928f72", - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "\n", - "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[36;1m\u001b[1;3m\n", - "A simple and fun DIY project for a 5-year-old girl is to make a paper doll. All you need is some paper, scissors, and crayons or markers. First, cut out a paper doll shape from the paper. Then, decorate the paper doll with clothes, hair, and facial features. You can also cut out paper furniture and accessories to create a paper doll scene.\u001b[0m\n", - "\u001b[33;1m\u001b[1;3m\n", - "1. Cut out a paper doll shape from the paper.\n", - "2. Decorate the paper doll with clothes, hair, and facial features.\n", - "3. Cut out paper furniture and accessories to create a paper doll scene.\n", - "\n", - "Materials needed:\n", - "\n", - "-Paper\n", - "-Scissors\n", - "-Crayons or markers\u001b[0m\n", - "\n", - "\u001b[1m> Finished chain.\u001b[0m\n" - ] - } - ], - "source": [ - "review = overall_chain.run(\"5-year-old girl\")" - ] - } - ], - "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.11.4" - } - }, - "nbformat": 4, - "nbformat_minor": 5 -} From 5380863a9ae4b9fcdc47cff2c8d16be4d8b3a245 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Tue, 15 Aug 2023 11:01:58 -0700 Subject: [PATCH 09/11] updating langchain --- .../LangChain/working_with_langchain.ipynb | 175 ++++++++++++------ 1 file changed, 115 insertions(+), 60 deletions(-) diff --git a/Basic_Samples/LangChain/working_with_langchain.ipynb b/Basic_Samples/LangChain/working_with_langchain.ipynb index 7ef68f94..20807dec 100644 --- a/Basic_Samples/LangChain/working_with_langchain.ipynb +++ b/Basic_Samples/LangChain/working_with_langchain.ipynb @@ -25,7 +25,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 20, "id": "35289cea", "metadata": {}, "outputs": [], @@ -39,83 +39,84 @@ "id": "ba880453", "metadata": {}, "source": [ - "## API Configuation\n", + "## API Configuation and Deployed Model Setup\n", "\n", "After installing the necessary libraies, the API must be configured. The code below shows how to configure the API directly in your Python environment. \n" ] }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 21, "id": "a9752fda", "metadata": { "scrolled": true }, "outputs": [], "source": [ - "from langchain.llms import AzureOpenAI \n", "import openai\n", "import json\n", "import os\n", + "from langchain.chat_models import AzureChatOpenAI\n", + "from langchain.schema import HumanMessage\n", + "from langchain import LLMChain\n", + "\n", "\n", "# Load config values\n", "with open(r'config.json') as config_file:\n", " config_details = json.load(config_file)\n", "\n", - "# Setting up the deployment name\n", - "deployment_name = config_details['DEPLOYMENT_NAME']\n", + "# The base URL for your Azure OpenAI resource. e.g. \"https://.openai.azure.com\"\n", + "openai_api_base=config_details['OPENAI_API_BASE']\n", + " \n", + "# API version e.g. \"2023-07-01-preview\"\n", + "openai_api_version=config_details['OPENAI_API_VERSION']\n", "\n", - "# This is set to `azure`\n", - "openai.api_type = \"azure\"\n", + "# The name of your Azure OpenAI deployment chat model. e.g. \"gpt-3-turbo-0613\"\n", + "deployment_name=config_details['DEPLOYMENT_NAME']\n", "\n", "# The API key for your Azure OpenAI resource.\n", - "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n", "\n", - "# The base URL for your Azure OpenAI resource. e.g. \"https://.openai.azure.com\"\n", - "openai.api_base = config_details['OPENAI_API_BASE']\n", - "\n", - "# Currently Chat Completion API have the following versions available: 2023-07-01-preview\n", - "openai.api_version = config_details['OPENAI_API_VERSION']" + "# This is set to `azure`\n", + "openai_api_type=\"azure\"" ] }, { "cell_type": "markdown", - "id": "ab4779e5", + "id": "52da840e", "metadata": {}, "source": [ - "## Deployed Model Setup\n", - "\n", - "Azure OpenAI allows users to create and manage their own model deployments. To call the API, you must specify the deployment you want to use by passing in the deployment name and model name. " + "## Deployed Model Setup" ] }, { "cell_type": "code", - "execution_count": 33, - "id": "6ea3e99f", + "execution_count": 22, + "id": "041f0a56", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\"\\n\\n\\n\\n\\nThe world is a beautiful place,\\nThe colors are so bright and true,\\nAnd I feel so free and free,\\nWhen I'm away from here.\\n\\nThe sky is so blue,\\nAnd the sun is so warm,\\nAnd I feel so free and free,\\nWhen I'm away from here.\"" + "AIMessage(content=\"In a world of dreams, where words take flight,\\nI'll weave a poem, to bring you delight.\\nWith ink and rhythm, I'll paint a scene,\\nWhere emotions dance in colors unseen.\\n\\nIn twilight's embrace, the stars ignite,\\nAs moonbeams whisper, a poet's plight.\\nEach syllable, a gentle caress,\\nUnveiling secrets, in poetic finesse.\\n\\nThrough nature's canvas, I'll take you there,\\nWhere sunsets melt, in hues so rare.\\nA symphony of birds, their melodies blend,\\nAs whispers of wind, through tall trees ascend.\\n\\nIn the depths of silence, where echoes reside,\\nI'll find the stories that time can't hide.\\nUnearth forgotten tales, from ancient lore,\\nAnd breathe life into them, like never before.\\n\\nWith passion's fire, my verses will ignite,\\nEmbracing love's joy, and sorrow's might.\\nI'll capture the essence, of a tender touch,\\nAnd the ache of longing, that hurts so much.\\n\\nThrough valleys of hope, and mountains of despair,\\nI'll guide you gently, with tender care.\\nWith each word penned, a journey will start,\\nTo touch your soul, and mend a broken heart.\\n\\nSo let these lines, like whispered breeze,\\nWrap around your heart, with gentle ease.\\nFor in this poem, I'll pour my soul,\\nAnd hope its essence makes you whole.\", additional_kwargs={}, example=False)" ] }, - "execution_count": 33, + "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# Create an instance of Azure OpenAI\n", - "\n", - "# Replace the deployment name and model name with your own\n", - "llm = AzureOpenAI(\n", - " deployment_name= deployment_name,\n", - " model_name=\"text-davinci-002\", \n", + "# Create an instance of chat llm\n", + "llm = AzureChatOpenAI(\n", + " openai_api_base=openai_api_base,\n", + " openai_api_version=openai_api_version,\n", + " deployment_name=deployment_name,\n", + " openai_api_key=openai_api_key,\n", + " openai_api_type=openai_api_type,\n", ")\n", "\n", - "# Run the LLM\n", - "llm(\"Write me a poem\")" + "llm([HumanMessage(content=\"Write me a poem\")])" ] }, { @@ -130,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 40, + "execution_count": 23, "id": "927d4bac", "metadata": {}, "outputs": [ @@ -138,14 +139,38 @@ "name": "stdout", "output_type": "stream", "text": [ + "Example #1:\n", + "content=\"A good face wash for acne-prone skin is one that contains ingredients like salicylic acid or benzoyl peroxide. These ingredients are effective in combating acne by unclogging pores, reducing inflammation, and killing acne-causing bacteria. Here are a few recommendations:\\n\\n1. Neutrogena Oil-Free Acne Wash: This face wash contains salicylic acid, which helps to clear breakouts and prevent new ones. It is gentle on the skin and doesn't leave it feeling dry or tight.\\n\\n2. La Roche-Posay Effaclar Medicated Gel Cleanser: This cleanser is formulated with 2% salicylic acid and helps to remove excess oil and impurities from the skin. It is suitable for sensitive skin and helps to reduce acne breakouts.\\n\\n3. CeraVe Acne Foaming Cream Cleanser: This face wash is formulated with benzoyl peroxide to combat acne and promote a clear complexion. It also contains ceramides and hyaluronic acid to moisturize and protect the skin barrier.\\n\\nRemember, it's important to choose a face wash that suits your specific skin type and concerns. If you have severe acne or any underlying skin conditions, it's best to consult a dermatologist for personalized recommendations.\" additional_kwargs={} example=False\n", + "\n", + "\n", + "Example #2:\n", + "If you are looking for warm weather and beautiful beaches in December, here are some top destinations to consider:\n", + "\n", + "1. Maldives: Known for its crystal-clear waters and luxurious resorts, the Maldives is a perfect winter getaway. With average temperatures of around 27°C (81°F), you can enjoy sunbathing, snorkeling, and diving in the stunning coral reefs.\n", + "\n", + "2. Thailand: Thailand offers a variety of beach destinations such as Phuket, Krabi, and Koh Samui. With temperatures ranging from 28-32°C (82-89°F), you can relax on the white sandy beaches, explore vibrant marine life, and indulge in delicious Thai cuisine.\n", + "\n", + "3. Bali, Indonesia: Bali is a tropical paradise with warm temperatures around 30°C (86°F) in December. It offers a mix of stunning beaches, lush landscapes, and vibrant culture. You can enjoy surfing, snorkeling, or simply unwind in luxury beach resorts.\n", + "\n", + "4. Seychelles: Located in the Indian Ocean, Seychelles is known for its pristine white sand beaches and turquoise waters. With temperatures averaging around 29°C (84°F), you can enjoy swimming, snorkeling, and even spot rare wildlife in the national parks.\n", + "\n", + "5. Caribbean Islands: The Caribbean is a popular choice for warm weather and beach vacations in December. Destinations like Barbados, Jamaica, and the Dominican Republic offer beautiful beaches, water sports, and a vibrant island atmosphere.\n", "\n", - "There are a number of good face washes that can help with acne prone skin. A few popular options include the Neutrogena Oil-Free Acne Wash, the Cetaphil Dermatological Gentle Skin Cleanser, and the La Roche-Posay Effaclar Medicated Gel Cleanser.\n" + "Remember to check travel advisories and local COVID-19 guidelines before planning your trip.\n" ] } ], "source": [ "from langchain import PromptTemplate\n", "\n", + "from langchain.prompts.chat import (\n", + " ChatPromptTemplate,\n", + " SystemMessagePromptTemplate,\n", + " HumanMessagePromptTemplate,\n", + ")\n", + "\n", + "\n", + "# First Example\n", "template = \"\"\"\n", "You are a skin care consulant that recommends products based on customer\n", "needs and preferences.\n", @@ -158,12 +183,25 @@ "template=template,\n", ")\n", "\n", - "print(llm(\n", - " prompt.format(\n", + "print(\"Example #1:\")\n", + "print(llm([HumanMessage(content=prompt.format(\n", " product_type=\"face wash\",\n", " customer_request = \"acne prone skin\"\n", - " )\n", - "))" + " ))]\n", + "))\n", + "print(\"\\n\")\n", + "\n", + "# Second Example\n", + "system_message = \"You are an AI assistant travel assistant that provides vacation recommendations.\"\n", + "\n", + "system_message_prompt = SystemMessagePromptTemplate.from_template(system_message)\n", + "human_template=\"{text}\"\n", + "human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n", + "chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])\n", + "chain = LLMChain(llm=llm, prompt=chat_prompt)\n", + "result = chain.run(f\"Where should I go on vaction in Decemember for warm weather and beaches?\")\n", + "print(\"Example #2:\")\n", + "print(result)" ] }, { @@ -180,20 +218,17 @@ }, { "cell_type": "code", - "execution_count": 46, + "execution_count": 24, "id": "af7c236f", "metadata": {}, "outputs": [], "source": [ - "#from langchain.llms import AzureOpenAI\n", - "from langchain.chains import LLMChain\n", - "# from langchain.prompts import PromptTemplate\n", "from langchain.chains import SimpleSequentialChain" ] }, { "cell_type": "code", - "execution_count": 51, + "execution_count": 25, "id": "2a4a32f0", "metadata": {}, "outputs": [], @@ -212,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 55, + "execution_count": 26, "id": "6eec47ff", "metadata": {}, "outputs": [], @@ -231,7 +266,7 @@ }, { "cell_type": "code", - "execution_count": 56, + "execution_count": 27, "id": "84a15aea", "metadata": {}, "outputs": [], @@ -241,7 +276,7 @@ }, { "cell_type": "code", - "execution_count": 57, + "execution_count": 28, "id": "15928f72", "metadata": {}, "outputs": [ @@ -251,25 +286,45 @@ "text": [ "\n", "\n", - "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\u001b[36;1m\u001b[1;3m\n", - "A simple and fun DIY project for a 5-year-old girl is to make a paper doll. All you need is some paper, scissors, and crayons or markers. First, cut out a paper doll shape from the paper. Then, decorate the paper doll with clothes, hair, and facial features. You can also cut out paper furniture and accessories to create a paper doll scene.\u001b[0m\n", - "\u001b[33;1m\u001b[1;3m\n", - "1. Cut out a paper doll shape from the paper.\n", - "2. Decorate the paper doll with clothes, hair, and facial features.\n", - "3. Cut out paper furniture and accessories to create a paper doll scene.\n", + "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", + "\u001b[36;1m\u001b[1;3mDIY Princess Crown Craft Kit: \n", + "\n", + "Materials needed:\n", + "- Cardboard or foam sheet\n", + "- Scissors\n", + "- Glue\n", + "- Decorative items (such as glitter, stickers, feathers, beads, sequins)\n", + "- Elastic string or ribbon\n", + "\n", + "Instructions:\n", + "1. Cut a strip of cardboard or foam sheet that is long enough to fit around the child's head.\n", + "2. Measure the strip to the child's head size and cut to adjust if needed.\n", + "3. Let the child decorate the strip with glue and decorative items of their choice. They can use glitter, stickers, feathers, beads, sequins, or anything else they find appealing.\n", + "4. Once the decorations are complete, let the glue dry completely.\n", + "5. Attach elastic string or ribbon to both ends of the strip to create a crown that can be worn on the head.\n", + "6. Help the child tie the elastic string or ribbon securely, making sure it is not too tight or too loose.\n", + "7. The princess crown is now ready to be worn and enjoyed! The child can wear it during imaginative play, dress-up sessions, or even for a princess-themed birthday party.\n", + "\n", + "Note: Adult supervision may be required for cutting and tying the elastic string/ribbon.\u001b[0m\n", + "\u001b[33;1m\u001b[1;3mDIY Princess Crown Craft Kit:\n", "\n", "Materials needed:\n", + "- Cardboard or foam sheet\n", + "- Scissors\n", + "- Glue\n", + "- Decorative items (such as glitter, stickers, feathers, beads, sequins)\n", + "- Elastic string or ribbon\n", + "\n", + "Instructions:\n", + "1. Cut a strip of cardboard or foam sheet that is long enough to fit around the child's head.\n", + "2. Measure the strip to the child's head size and cut to adjust if needed.\n", + "3. Let the child decorate the strip with glue and decorative items of their choice. They can use glitter, stickers, feathers, beads, sequins, or anything else they find appealing.\n", + "4. Once the decorations are complete, let the glue dry completely.\n", + "5. Attach elastic string or ribbon to both ends of the strip to create a crown that can be worn on the head.\n", + "6. Help the child tie the elastic string or ribbon securely, making sure it is not too tight or too loose.\n", + "7. The princess crown is now ready to be worn and enjoyed! The child can wear it during imaginative play, dress-up sessions, or even for a princess-themed birthday party.\n", "\n", - "-Paper\n", - "-Scissors\n", - "-Crayons or markers\u001b[0m\n", + "Note: Adult supervision may be required for cutting and tying the elastic string/ribbon.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ] From 4457a68f95b29e2816cc1a6657fccef256f6af27 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Tue, 15 Aug 2023 11:26:55 -0700 Subject: [PATCH 10/11] updating config file --- Basic_Samples/LangChain/config.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Basic_Samples/LangChain/config.json b/Basic_Samples/LangChain/config.json index 70da44d3..b7a3a182 100644 --- a/Basic_Samples/LangChain/config.json +++ b/Basic_Samples/LangChain/config.json @@ -1,5 +1,5 @@ { - "DEPLOYMENT_ID":"", + "DEPLOYMENT_NAME":"", "OPENAI_API_BASE":"https://.openai.azure.com", "OPENAI_API_VERSION":"" } \ No newline at end of file From 8dd46e894ad14a15ad152bd618a9eca3fbf78c61 Mon Sep 17 00:00:00 2001 From: Amanda Foster Date: Tue, 15 Aug 2023 12:39:49 -0700 Subject: [PATCH 11/11] minor style fixes --- .../LangChain/working_with_langchain.ipynb | 110 +++++++++--------- 1 file changed, 56 insertions(+), 54 deletions(-) diff --git a/Basic_Samples/LangChain/working_with_langchain.ipynb b/Basic_Samples/LangChain/working_with_langchain.ipynb index 20807dec..606dedfc 100644 --- a/Basic_Samples/LangChain/working_with_langchain.ipynb +++ b/Basic_Samples/LangChain/working_with_langchain.ipynb @@ -25,7 +25,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 36, "id": "35289cea", "metadata": {}, "outputs": [], @@ -46,7 +46,7 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": 42, "id": "a9752fda", "metadata": { "scrolled": true @@ -71,7 +71,7 @@ "# API version e.g. \"2023-07-01-preview\"\n", "openai_api_version=config_details['OPENAI_API_VERSION']\n", "\n", - "# The name of your Azure OpenAI deployment chat model. e.g. \"gpt-3-turbo-0613\"\n", + "# The name of your Azure OpenAI deployment chat model. e.g. \"gpt-35-turbo-0613\"\n", "deployment_name=config_details['DEPLOYMENT_NAME']\n", "\n", "# The API key for your Azure OpenAI resource.\n", @@ -91,17 +91,17 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 43, "id": "041f0a56", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "AIMessage(content=\"In a world of dreams, where words take flight,\\nI'll weave a poem, to bring you delight.\\nWith ink and rhythm, I'll paint a scene,\\nWhere emotions dance in colors unseen.\\n\\nIn twilight's embrace, the stars ignite,\\nAs moonbeams whisper, a poet's plight.\\nEach syllable, a gentle caress,\\nUnveiling secrets, in poetic finesse.\\n\\nThrough nature's canvas, I'll take you there,\\nWhere sunsets melt, in hues so rare.\\nA symphony of birds, their melodies blend,\\nAs whispers of wind, through tall trees ascend.\\n\\nIn the depths of silence, where echoes reside,\\nI'll find the stories that time can't hide.\\nUnearth forgotten tales, from ancient lore,\\nAnd breathe life into them, like never before.\\n\\nWith passion's fire, my verses will ignite,\\nEmbracing love's joy, and sorrow's might.\\nI'll capture the essence, of a tender touch,\\nAnd the ache of longing, that hurts so much.\\n\\nThrough valleys of hope, and mountains of despair,\\nI'll guide you gently, with tender care.\\nWith each word penned, a journey will start,\\nTo touch your soul, and mend a broken heart.\\n\\nSo let these lines, like whispered breeze,\\nWrap around your heart, with gentle ease.\\nFor in this poem, I'll pour my soul,\\nAnd hope its essence makes you whole.\", additional_kwargs={}, example=False)" + "AIMessage(content=\"In the realm of dreams, where thoughts take flight,\\nA tapestry of words, I shall now write.\\nWith ink and quill, I'll weave a tale,\\nOf love and hope, where hearts prevail.\\n\\nIn meadows adorned with flowers so fair,\\nA gentle breeze whispers secrets in the air.\\nThe sun shines bright, painting the sky,\\nA canvas of colors, where dreams never die.\\n\\nBeneath a canopy of stars, we shall dance,\\nLost in a moment, in a lover's trance.\\nOur hearts entwined, beats synchronized,\\nA symphony of love, never compromised.\\n\\nThrough valleys of sorrow, we shall tread,\\nWith courage and strength, our fears we'll shed.\\nFor love, a beacon, shall guide our way,\\nThrough darkest nights, to a brighter day.\\n\\nIn the depths of silence, a whispered prayer,\\nFor peace and harmony, beyond compare.\\nMay kindness bloom, like flowers in spring,\\nAnd compassion's song, forever sing.\\n\\nOh, let this poem be a gentle reminder,\\nThat within us all, love is a powerful binder.\\nFor in these words, a message so true,\\nThat love's embrace can heal and renew.\\n\\nSo let us cherish, this gift we possess,\\nThe power of words, to heal and impress.\\nThrough poetry's grace, may hearts be moved,\\nAnd in its beauty, we shall be proved.\", additional_kwargs={}, example=False)" ] }, - "execution_count": 22, + "execution_count": 43, "metadata": {}, "output_type": "execute_result" } @@ -131,7 +131,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 44, "id": "927d4bac", "metadata": {}, "outputs": [ @@ -140,23 +140,23 @@ "output_type": "stream", "text": [ "Example #1:\n", - "content=\"A good face wash for acne-prone skin is one that contains ingredients like salicylic acid or benzoyl peroxide. These ingredients are effective in combating acne by unclogging pores, reducing inflammation, and killing acne-causing bacteria. Here are a few recommendations:\\n\\n1. Neutrogena Oil-Free Acne Wash: This face wash contains salicylic acid, which helps to clear breakouts and prevent new ones. It is gentle on the skin and doesn't leave it feeling dry or tight.\\n\\n2. La Roche-Posay Effaclar Medicated Gel Cleanser: This cleanser is formulated with 2% salicylic acid and helps to remove excess oil and impurities from the skin. It is suitable for sensitive skin and helps to reduce acne breakouts.\\n\\n3. CeraVe Acne Foaming Cream Cleanser: This face wash is formulated with benzoyl peroxide to combat acne and promote a clear complexion. It also contains ceramides and hyaluronic acid to moisturize and protect the skin barrier.\\n\\nRemember, it's important to choose a face wash that suits your specific skin type and concerns. If you have severe acne or any underlying skin conditions, it's best to consult a dermatologist for personalized recommendations.\" additional_kwargs={} example=False\n", + "content='A highly recommended face wash for acne-prone skin is the \"Neutrogena Oil-Free Acne Wash.\" This product contains salicylic acid, which helps to treat and prevent acne by unclogging pores and reducing inflammation. It is oil-free, non-comedogenic, and gentle enough for daily use. Additionally, it effectively removes dirt, excess oil, and makeup without over-drying the skin.' additional_kwargs={} example=False\n", "\n", "\n", "Example #2:\n", - "If you are looking for warm weather and beautiful beaches in December, here are some top destinations to consider:\n", + "If you're looking for warm weather and beautiful beaches in December, here are a few destinations you might consider:\n", "\n", - "1. Maldives: Known for its crystal-clear waters and luxurious resorts, the Maldives is a perfect winter getaway. With average temperatures of around 27°C (81°F), you can enjoy sunbathing, snorkeling, and diving in the stunning coral reefs.\n", + "1. Maldives: This tropical paradise offers pristine beaches, crystal-clear waters, and luxurious resorts. December is a great time to visit, with temperatures averaging around 28°C (82°F).\n", "\n", - "2. Thailand: Thailand offers a variety of beach destinations such as Phuket, Krabi, and Koh Samui. With temperatures ranging from 28-32°C (82-89°F), you can relax on the white sandy beaches, explore vibrant marine life, and indulge in delicious Thai cuisine.\n", + "2. Thailand: Thailand's southern islands, such as Phuket, Krabi, and Koh Samui, offer warm weather and stunning beaches in December. You can relax on the white sands, go snorkeling or diving, and explore the vibrant local culture.\n", "\n", - "3. Bali, Indonesia: Bali is a tropical paradise with warm temperatures around 30°C (86°F) in December. It offers a mix of stunning beaches, lush landscapes, and vibrant culture. You can enjoy surfing, snorkeling, or simply unwind in luxury beach resorts.\n", + "3. Bali, Indonesia: Bali is a popular destination known for its stunning beaches, lush landscapes, and vibrant culture. In December, you can enjoy warm temperatures and take part in water sports or simply unwind by the beach.\n", "\n", - "4. Seychelles: Located in the Indian Ocean, Seychelles is known for its pristine white sand beaches and turquoise waters. With temperatures averaging around 29°C (84°F), you can enjoy swimming, snorkeling, and even spot rare wildlife in the national parks.\n", + "4. Cancun, Mexico: Cancun is a favorite destination for beach lovers, with its turquoise waters and soft white sands. December is a great time to visit, with temperatures around 27°C (81°F), and you can also explore the nearby Mayan ruins.\n", "\n", - "5. Caribbean Islands: The Caribbean is a popular choice for warm weather and beach vacations in December. Destinations like Barbados, Jamaica, and the Dominican Republic offer beautiful beaches, water sports, and a vibrant island atmosphere.\n", + "5. Seychelles: This archipelago in the Indian Ocean boasts some of the world's most beautiful beaches. December is an excellent time to visit, as the weather is warm and you can enjoy activities like snorkeling, diving, and island hopping.\n", "\n", - "Remember to check travel advisories and local COVID-19 guidelines before planning your trip.\n" + "Remember to check travel restrictions and safety guidelines before planning your trip, as they may vary due to the ongoing COVID-19 pandemic.\n" ] } ], @@ -218,7 +218,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 45, "id": "af7c236f", "metadata": {}, "outputs": [], @@ -228,45 +228,43 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 46, "id": "2a4a32f0", "metadata": {}, "outputs": [], "source": [ - "template = \"\"\"Your job is to come up with a fun DIY project for the specified gender, age, and description of a kid.\n", + "description_template = \"\"\"Your job is to come up with a fun DIY project for the specified gender, age, and description of a kid.\n", "% CHILD_DESCRIPTION\n", "{child_description}\n", "\n", "YOUR RESPONSE:\n", "\"\"\"\n", - "prompt_template = PromptTemplate(input_variables=[\"child_description\"], template=template)\n", + "description_prompt_template = PromptTemplate(input_variables=[\"child_description\"], template=description_template)\n", "\n", - "# Holds my 'location' chain\n", - "description_chain = LLMChain(llm=llm, prompt=prompt_template)" + "description_chain = LLMChain(llm=llm, prompt=description_prompt_template)" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 47, "id": "6eec47ff", "metadata": {}, "outputs": [], "source": [ - "template = \"\"\"Given a DIY project, give a short and simple recipe step-by-step guide on how to complete the project and a materials list.\n", + "diy_description_template = \"\"\"Given a DIY project, give a short and simple recipe step-by-step guide on how to complete the project and a materials list.\n", "% DIY_PROJECT\n", "{diy_project}\n", "\n", "YOUR RESPONSE:\n", "\"\"\"\n", - "prompt_template = PromptTemplate(input_variables=[\"diy_project\"], template=template)\n", + "diy_prompt_template = PromptTemplate(input_variables=[\"diy_project\"], template=diy_description_template)\n", "\n", - "# Holds my 'meal' chain\n", - "diy_chain = LLMChain(llm=llm, prompt=prompt_template)" + "diy_chain = LLMChain(llm=llm, prompt=diy_prompt_template)" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 48, "id": "84a15aea", "metadata": {}, "outputs": [], @@ -276,7 +274,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 49, "id": "15928f72", "metadata": {}, "outputs": [ @@ -287,44 +285,48 @@ "\n", "\n", "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", - "\u001b[36;1m\u001b[1;3mDIY Princess Crown Craft Kit: \n", + "\u001b[36;1m\u001b[1;3mDIY Sparkling Fairy Wand:\n", "\n", "Materials needed:\n", - "- Cardboard or foam sheet\n", + "- Wooden dowel or stick\n", + "- Glitter foam sheets in various colors\n", + "- Ribbon or tulle\n", + "- Craft glue\n", "- Scissors\n", - "- Glue\n", - "- Decorative items (such as glitter, stickers, feathers, beads, sequins)\n", - "- Elastic string or ribbon\n", + "- Decorative gems or sequins\n", + "- Glitter glue (optional)\n", "\n", "Instructions:\n", - "1. Cut a strip of cardboard or foam sheet that is long enough to fit around the child's head.\n", - "2. Measure the strip to the child's head size and cut to adjust if needed.\n", - "3. Let the child decorate the strip with glue and decorative items of their choice. They can use glitter, stickers, feathers, beads, sequins, or anything else they find appealing.\n", - "4. Once the decorations are complete, let the glue dry completely.\n", - "5. Attach elastic string or ribbon to both ends of the strip to create a crown that can be worn on the head.\n", - "6. Help the child tie the elastic string or ribbon securely, making sure it is not too tight or too loose.\n", - "7. The princess crown is now ready to be worn and enjoyed! The child can wear it during imaginative play, dress-up sessions, or even for a princess-themed birthday party.\n", + "1. Begin by cutting out a star shape from one of the glitter foam sheets. This will be the top of the wand.\n", + "2. Cut out a long strip of foam from another color and wrap it around the wooden dowel or stick, starting from the bottom. Secure it with craft glue.\n", + "3. Cut out smaller shapes like hearts, butterflies, or flowers from different colored glitter foam sheets.\n", + "4. Use craft glue to stick these shapes onto the wrapped foam strip, creating a beautiful pattern. Let them dry completely.\n", + "5. Once the foam shapes are secure, add some extra sparkle by applying glitter glue to the edges or adding decorative gems or sequins.\n", + "6. Finally, tie ribbons or tulle strands to the bottom of the wooden dowel or stick for an extra touch of magic.\n", + "7. Let the wand dry completely before giving it to the 5-year-old girl to play with.\n", "\n", - "Note: Adult supervision may be required for cutting and tying the elastic string/ribbon.\u001b[0m\n", - "\u001b[33;1m\u001b[1;3mDIY Princess Crown Craft Kit:\n", + "This DIY project will allow the 5-year-old girl to express her creativity and imagination as she creates her very own sparkling fairy wand. She can use it for pretend play, dress-up parties, or even as a room decoration.\u001b[0m\n", + "\u001b[33;1m\u001b[1;3mDIY Sparkling Fairy Wand:\n", "\n", "Materials needed:\n", - "- Cardboard or foam sheet\n", + "- Wooden dowel or stick\n", + "- Glitter foam sheets in various colors\n", + "- Ribbon or tulle\n", + "- Craft glue\n", "- Scissors\n", - "- Glue\n", - "- Decorative items (such as glitter, stickers, feathers, beads, sequins)\n", - "- Elastic string or ribbon\n", + "- Decorative gems or sequins\n", + "- Glitter glue (optional)\n", "\n", "Instructions:\n", - "1. Cut a strip of cardboard or foam sheet that is long enough to fit around the child's head.\n", - "2. Measure the strip to the child's head size and cut to adjust if needed.\n", - "3. Let the child decorate the strip with glue and decorative items of their choice. They can use glitter, stickers, feathers, beads, sequins, or anything else they find appealing.\n", - "4. Once the decorations are complete, let the glue dry completely.\n", - "5. Attach elastic string or ribbon to both ends of the strip to create a crown that can be worn on the head.\n", - "6. Help the child tie the elastic string or ribbon securely, making sure it is not too tight or too loose.\n", - "7. The princess crown is now ready to be worn and enjoyed! The child can wear it during imaginative play, dress-up sessions, or even for a princess-themed birthday party.\n", + "1. Cut out a star shape from a glitter foam sheet for the top of the wand.\n", + "2. Wrap a long strip of foam from another color around the wooden dowel or stick, securing it with craft glue.\n", + "3. Cut out smaller shapes like hearts, butterflies, or flowers from different colored glitter foam sheets.\n", + "4. Use craft glue to stick these shapes onto the wrapped foam strip to create a pattern. Let them dry completely.\n", + "5. Add extra sparkle by applying glitter glue to the edges or adding decorative gems or sequins.\n", + "6. Tie ribbons or tulle strands to the bottom of the wooden dowel or stick for an extra touch of magic.\n", + "7. Let the wand dry completely before giving it to the 5-year-old girl to play with.\n", "\n", - "Note: Adult supervision may be required for cutting and tying the elastic string/ribbon.\u001b[0m\n", + "This DIY project allows the 5-year-old girl to express her creativity and imagination. She can use the sparkling fairy wand for pretend play, dress-up parties, or even as a room decoration.\u001b[0m\n", "\n", "\u001b[1m> Finished chain.\u001b[0m\n" ]