three notebooks updated SDK

pull/892/head
jhills20 10 months ago
parent 59019bd21e
commit 6b7062b848

File diff suppressed because one or more lines are too long

@ -27,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "80e71f33",
"metadata": {
"pycharm": {
@ -39,93 +39,94 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: scipy in /opt/homebrew/lib/python3.11/site-packages (1.10.1)\n",
"Requirement already satisfied: numpy<1.27.0,>=1.19.5 in /opt/homebrew/lib/python3.11/site-packages (from scipy) (1.24.3)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tenacity in /opt/homebrew/lib/python3.11/site-packages (8.2.2)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tiktoken in /opt/homebrew/lib/python3.11/site-packages (0.4.0)\n",
"Requirement already satisfied: regex>=2022.1.18 in /opt/homebrew/lib/python3.11/site-packages (from tiktoken) (2023.6.3)\n",
"Requirement already satisfied: requests>=2.26.0 in /opt/homebrew/lib/python3.11/site-packages (from tiktoken) (2.30.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (3.1.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (1.25.11)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.26.0->tiktoken) (2023.5.7)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: termcolor in /opt/homebrew/lib/python3.11/site-packages (2.3.0)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: openai in /opt/homebrew/lib/python3.11/site-packages (0.27.6)\n",
"Requirement already satisfied: requests>=2.20 in /opt/homebrew/lib/python3.11/site-packages (from openai) (2.30.0)\n",
"Requirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (from openai) (4.65.0)\n",
"Requirement already satisfied: aiohttp in /opt/homebrew/lib/python3.11/site-packages (from openai) (3.8.4)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.20->openai) (3.1.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.20->openai) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.20->openai) (1.25.11)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests>=2.20->openai) (2023.5.7)\n",
"Requirement already satisfied: attrs>=17.3.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (23.1.0)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (6.0.4)\n",
"Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (4.0.2)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (1.9.2)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (1.3.3)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in /opt/homebrew/lib/python3.11/site-packages (from aiohttp->openai) (1.3.1)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: requests in /opt/homebrew/lib/python3.11/site-packages (2.30.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/homebrew/lib/python3.11/site-packages (from requests) (3.1.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /opt/homebrew/lib/python3.11/site-packages (from requests) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/homebrew/lib/python3.11/site-packages (from requests) (1.25.11)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /opt/homebrew/lib/python3.11/site-packages (from requests) (2023.5.7)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: arxiv in /opt/homebrew/lib/python3.11/site-packages (1.4.7)\n",
"Requirement already satisfied: feedparser in /opt/homebrew/lib/python3.11/site-packages (from arxiv) (6.0.10)\n",
"Requirement already satisfied: sgmllib3k in /opt/homebrew/lib/python3.11/site-packages (from feedparser->arxiv) (1.0.0)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[0mRequirement already satisfied: pandas in /opt/homebrew/lib/python3.11/site-packages (2.0.1)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /opt/homebrew/lib/python3.11/site-packages (from pandas) (2.8.2)\n",
"Requirement already satisfied: pytz>=2020.1 in /opt/homebrew/lib/python3.11/site-packages (from pandas) (2023.3)\n",
"Requirement already satisfied: tzdata>=2022.1 in /opt/homebrew/lib/python3.11/site-packages (from pandas) (2023.3)\n",
"Requirement already satisfied: numpy>=1.21.0 in /opt/homebrew/lib/python3.11/site-packages (from pandas) (1.24.3)\n",
"Requirement already satisfied: six>=1.5 in /opt/homebrew/lib/python3.11/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: PyPDF2 in /opt/homebrew/lib/python3.11/site-packages (3.0.1)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tqdm in /opt/homebrew/lib/python3.11/site-packages (4.65.0)\n",
"\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"\u001b[0m\u001b[33mWARNING: Skipping /opt/homebrew/lib/python3.11/site-packages/PyYAML-6.0-py3.11.egg-info due to invalid metadata entry 'name'\u001b[0m\u001b[33m\n",
"Requirement already satisfied: scipy in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (1.11.3)\n",
"Requirement already satisfied: numpy<1.28.0,>=1.21.6 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from scipy) (1.26.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tenacity in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (8.2.3)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mCollecting tiktoken==0.3.3\n",
" Downloading tiktoken-0.3.3-cp39-cp39-macosx_11_0_arm64.whl (706 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m706.8/706.8 kB\u001b[0m \u001b[31m14.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: regex>=2022.1.18 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from tiktoken==0.3.3) (2023.8.8)\n",
"Requirement already satisfied: requests>=2.26.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from tiktoken==0.3.3) (2.31.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken==0.3.3) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken==0.3.3) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken==0.3.3) (1.26.16)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken==0.3.3) (2023.7.22)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mInstalling collected packages: tiktoken\n",
" Attempting uninstall: tiktoken\n",
" Found existing installation: tiktoken 0.5.1\n",
" Uninstalling tiktoken-0.5.1:\n",
" Successfully uninstalled tiktoken-0.5.1\n",
"Successfully installed tiktoken-0.3.3\n",
"Collecting termcolor\n",
" Downloading termcolor-2.3.0-py3-none-any.whl (6.9 kB)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mInstalling collected packages: termcolor\n",
"Successfully installed termcolor-2.3.0\n",
"Requirement already satisfied: openai in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (1.3.5)\n",
"Requirement already satisfied: anyio<4,>=3.5.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (3.7.1)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (1.8.0)\n",
"Requirement already satisfied: httpx<1,>=0.23.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (0.25.1)\n",
"Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (2.4.2)\n",
"Requirement already satisfied: tqdm>4 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (4.66.1)\n",
"Requirement already satisfied: typing-extensions<5,>=4.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (4.8.0)\n",
"Requirement already satisfied: idna>=2.8 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (3.4)\n",
"Requirement already satisfied: sniffio>=1.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (1.3.0)\n",
"Requirement already satisfied: exceptiongroup in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (1.1.3)\n",
"Requirement already satisfied: certifi in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpx<1,>=0.23.0->openai) (2023.7.22)\n",
"Requirement already satisfied: httpcore in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpx<1,>=0.23.0->openai) (1.0.1)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pydantic<3,>=1.9.0->openai) (0.5.0)\n",
"Requirement already satisfied: pydantic-core==2.10.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pydantic<3,>=1.9.0->openai) (2.10.1)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpcore->httpx<1,>=0.23.0->openai) (0.14.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: requests in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (2.31.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (1.26.16)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (2023.7.22)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mCollecting arxiv\n",
" Downloading arxiv-2.0.0-py3-none-any.whl.metadata (8.4 kB)\n",
"Collecting feedparser==6.0.10 (from arxiv)\n",
" Downloading feedparser-6.0.10-py3-none-any.whl (81 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m81.1/81.1 kB\u001b[0m \u001b[31m5.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: requests==2.31.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from arxiv) (2.31.0)\n",
"Collecting sgmllib3k (from feedparser==6.0.10->arxiv)\n",
" Downloading sgmllib3k-1.0.0.tar.gz (5.8 kB)\n",
" Installing build dependencies ... \u001b[?25ldone\n",
"\u001b[?25h Getting requirements to build wheel ... \u001b[?25ldone\n",
"\u001b[?25h Preparing metadata (pyproject.toml) ... \u001b[?25ldone\n",
"\u001b[?25hRequirement already satisfied: charset-normalizer<4,>=2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests==2.31.0->arxiv) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests==2.31.0->arxiv) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests==2.31.0->arxiv) (1.26.16)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests==2.31.0->arxiv) (2023.7.22)\n",
"Downloading arxiv-2.0.0-py3-none-any.whl (11 kB)\n",
"Building wheels for collected packages: sgmllib3k\n",
" Building wheel for sgmllib3k (pyproject.toml) ... \u001b[?25ldone\n",
"\u001b[?25h Created wheel for sgmllib3k: filename=sgmllib3k-1.0.0-py3-none-any.whl size=6048 sha256=34bcfd8eb3301d2f3ec55a2773accc60e641c93b32b2ca131dae0ddcd60c7bc9\n",
" Stored in directory: /Users/james.hills/Library/Caches/pip/wheels/65/7a/a7/78c287f64e401255dff4c13fdbc672fed5efbfd21c530114e1\n",
"Successfully built sgmllib3k\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mInstalling collected packages: sgmllib3k, feedparser, arxiv\n",
"Successfully installed arxiv-2.0.0 feedparser-6.0.10 sgmllib3k-1.0.0\n",
"Requirement already satisfied: pandas in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (2.1.1)\n",
"Requirement already satisfied: numpy>=1.22.4 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pandas) (1.26.0)\n",
"Requirement already satisfied: python-dateutil>=2.8.2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pandas) (2.8.2)\n",
"Requirement already satisfied: pytz>=2020.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pandas) (2023.3.post1)\n",
"Requirement already satisfied: tzdata>=2022.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pandas) (2023.3)\n",
"Requirement already satisfied: six>=1.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from python-dateutil>=2.8.2->pandas) (1.12.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mCollecting PyPDF2\n",
" Downloading pypdf2-3.0.1-py3-none-any.whl (232 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m232.6/232.6 kB\u001b[0m \u001b[31m8.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: typing_extensions>=3.10.0.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from PyPDF2) (4.8.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mInstalling collected packages: PyPDF2\n",
"Successfully installed PyPDF2-3.0.1\n",
"Requirement already satisfied: tqdm in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (4.66.1)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0m"
]
}
@ -134,18 +135,18 @@
"!pip install scipy\n",
"!pip install tenacity\n",
"!pip install tiktoken==0.3.3\n",
"!pip install termcolor \n",
"!pip install termcolor\n",
"!pip install openai\n",
"!pip install requests\n",
"!pip install arxiv\n",
"!pip install pandas\n",
"!pip install PyPDF2\n",
"!pip install tqdm"
"!pip install tqdm\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"id": "dab872c5",
"metadata": {},
"outputs": [],
@ -156,20 +157,20 @@
"import ast\n",
"import concurrent\n",
"from csv import writer\n",
"from IPython.display import display, Markdown, Latex\n",
"from IPython.display import display, Markdown\n",
"import json\n",
"import openai\n",
"from openai import OpenAI\n",
"import os\n",
"import pandas as pd\n",
"from PyPDF2 import PdfReader\n",
"import requests\n",
"from scipy import spatial\n",
"from tenacity import retry, wait_random_exponential, stop_after_attempt\n",
"import tiktoken\n",
"from tqdm import tqdm\n",
"from termcolor import colored\n",
"\n",
"GPT_MODEL = \"gpt-3.5-turbo-0613\"\n",
"client = OpenAI()\n",
"GPT_MODEL = \"gpt-3.5-turbo-1106\"\n",
"EMBEDDING_MODEL = \"text-embedding-ada-002\"\n"
]
},
@ -188,10 +189,18 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 7,
"id": "2de5d32d",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Directory './data/papers' already exists.\n"
]
}
],
"source": [
"directory = './data/papers'\n",
"\n",
@ -202,12 +211,12 @@
" print(f\"Directory '{directory}' created successfully.\")\n",
"else:\n",
" # If the directory already exists, print a message indicating it\n",
" print(f\"Directory '{directory}' already exists.\")"
" print(f\"Directory '{directory}' already exists.\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "ae5cb7a1",
"metadata": {},
"outputs": [],
@ -218,19 +227,18 @@
"\n",
"# Generate a blank dataframe where we can store downloaded files\n",
"df = pd.DataFrame(list())\n",
"df.to_csv(paper_dir_filepath)"
"df.to_csv(paper_dir_filepath)\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 11,
"id": "57217b9d",
"metadata": {},
"outputs": [],
"source": [
"@retry(wait=wait_random_exponential(min=1, max=40), stop=stop_after_attempt(3))\n",
"def embedding_request(text):\n",
" response = openai.Embedding.create(input=text, model=EMBEDDING_MODEL)\n",
" response = client.embeddings.create(input=text, model=EMBEDDING_MODEL)\n",
" return response\n",
"\n",
"\n",
@ -257,7 +265,7 @@
" file_reference = [\n",
" result.title,\n",
" result.download_pdf(data_dir),\n",
" response[\"data\"][0][\"embedding\"],\n",
" response.data[0].embedding,\n",
" ]\n",
"\n",
" # Write to file\n",
@ -270,10 +278,18 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 12,
"id": "dda02bdb",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/1g/fk6xzzvj0c1ggf_6nwknlfy00000gq/T/ipykernel_84898/3977585067.py:14: DeprecationWarning: The '(Search).results' method is deprecated, use 'Client.results' instead\n",
" for result in search.results():\n"
]
},
{
"data": {
"text/plain": [
@ -283,7 +299,7 @@
" 'pdf_url': 'http://arxiv.org/pdf/1808.07982v1'}"
]
},
"execution_count": 5,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@ -296,7 +312,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 13,
"id": "11675627",
"metadata": {},
"outputs": [],
@ -362,10 +378,10 @@
"def extract_chunk(content, template_prompt):\n",
" \"\"\"This function applies a prompt to some input content. In this case it returns a summarized chunk of text\"\"\"\n",
" prompt = template_prompt + content\n",
" response = openai.ChatCompletion.create(\n",
" response = client.chat.completions.create(\n",
" model=GPT_MODEL, messages=[{\"role\": \"user\", \"content\": prompt}], temperature=0\n",
" )\n",
" return response[\"choices\"][0][\"message\"][\"content\"]\n",
" return response.choices[0].message.content\n",
"\n",
"\n",
"def summarize_text(query):\n",
@ -569,7 +585,7 @@
" f\"{message['role']}: {message['content']}\\n\\n\",\n",
" role_to_color[message[\"role\"]],\n",
" )\n",
" )"
" )\n"
]
},
{
@ -847,9 +863,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "tua_test",
"display_name": "openai-cookbook",
"language": "python",
"name": "tua_test"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
@ -861,7 +877,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.11"
"version": "3.9.16"
}
},
"nbformat": 4,

@ -33,37 +33,83 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"id": "80e71f33",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: scipy in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (1.11.3)\n",
"Requirement already satisfied: numpy<1.28.0,>=1.21.6 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from scipy) (1.26.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tenacity in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (8.2.3)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: tiktoken in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (0.3.3)\n",
"Requirement already satisfied: regex>=2022.1.18 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from tiktoken) (2023.8.8)\n",
"Requirement already satisfied: requests>=2.26.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from tiktoken) (2.31.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken) (1.26.16)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests>=2.26.0->tiktoken) (2023.7.22)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: termcolor in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (2.3.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: openai in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (1.3.5)\n",
"Requirement already satisfied: anyio<4,>=3.5.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (3.7.1)\n",
"Requirement already satisfied: distro<2,>=1.7.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (1.8.0)\n",
"Requirement already satisfied: httpx<1,>=0.23.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (0.25.1)\n",
"Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (2.4.2)\n",
"Requirement already satisfied: tqdm>4 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (4.66.1)\n",
"Requirement already satisfied: typing-extensions<5,>=4.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from openai) (4.8.0)\n",
"Requirement already satisfied: idna>=2.8 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (3.4)\n",
"Requirement already satisfied: sniffio>=1.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (1.3.0)\n",
"Requirement already satisfied: exceptiongroup in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from anyio<4,>=3.5.0->openai) (1.1.3)\n",
"Requirement already satisfied: certifi in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpx<1,>=0.23.0->openai) (2023.7.22)\n",
"Requirement already satisfied: httpcore in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpx<1,>=0.23.0->openai) (1.0.1)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pydantic<3,>=1.9.0->openai) (0.5.0)\n",
"Requirement already satisfied: pydantic-core==2.10.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from pydantic<3,>=1.9.0->openai) (2.10.1)\n",
"Requirement already satisfied: h11<0.15,>=0.13 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from httpcore->httpx<1,>=0.23.0->openai) (0.14.0)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0mRequirement already satisfied: requests in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (2.31.0)\n",
"Requirement already satisfied: charset-normalizer<4,>=2 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (3.2.0)\n",
"Requirement already satisfied: idna<4,>=2.5 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (3.4)\n",
"Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (1.26.16)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/james.hills/.pyenv/versions/3.9.16/envs/openai-cookbook/lib/python3.9/site-packages (from requests) (2023.7.22)\n",
"\u001b[33mDEPRECATION: textract 1.6.5 has a non-standard dependency specifier extract-msg<=0.29.*. pip 24.0 will enforce this behaviour change. A possible replacement is to upgrade to a newer version of textract or contact the author to suggest that they release a version with a conforming dependency specifiers. Discussion can be found at https://github.com/pypa/pip/issues/12063\u001b[0m\u001b[33m\n",
"\u001b[0m"
]
}
],
"source": [
"!pip install scipy\n",
"!pip install tenacity\n",
"!pip install tiktoken\n",
"!pip install termcolor \n",
"!pip install termcolor\n",
"!pip install openai\n",
"!pip install requests"
"!pip install requests\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"id": "dab872c5",
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import openai\n",
"from openai import OpenAI\n",
"import requests\n",
"from tenacity import retry, wait_random_exponential, stop_after_attempt\n",
"from termcolor import colored\n",
"\n",
"GPT_MODEL = \"gpt-3.5-turbo-0613\""
"client = OpenAI()\n",
"GPT_MODEL = \"gpt-3.5-turbo-0613\"\n"
]
},
{
@ -79,16 +125,15 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"id": "745ceec5",
"metadata": {},
"outputs": [],
"source": [
"@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))\n",
"def chat_completion_request(messages, tools=None, tool_choice=None, model=GPT_MODEL):\n",
" headers = {\n",
" \"Content-Type\": \"application/json\",\n",
" \"Authorization\": \"Bearer \" + openai.api_key,\n",
" \"Authorization\": \"Bearer \" + client.api_key,\n",
" }\n",
" json_data = {\"model\": model, \"messages\": messages}\n",
" if tools is not None:\n",
@ -110,7 +155,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"id": "c4d1c99f",
"metadata": {},
"outputs": [],
@ -122,7 +167,7 @@
" \"assistant\": \"blue\",\n",
" \"tool\": \"magenta\",\n",
" }\n",
" \n",
"\n",
" for message in messages:\n",
" if message[\"role\"] == \"system\":\n",
" print(colored(f\"system: {message['content']}\\n\", role_to_color[message[\"role\"]]))\n",
@ -149,7 +194,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"id": "d2e25069",
"metadata": {},
"outputs": [],
@ -203,7 +248,7 @@
" },\n",
" }\n",
" },\n",
"]"
"]\n"
]
},
{
@ -217,7 +262,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"id": "518d6827",
"metadata": {},
"outputs": [
@ -225,10 +270,10 @@
"data": {
"text/plain": [
"{'role': 'assistant',\n",
" 'content': 'Sure, I can help you with that. Could you please tell me the city and state you are in or the location you want to know the weather for?'}"
" 'content': 'Sure, can you please provide me with the location?'}"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@ -240,7 +285,7 @@
"chat_response = chat_completion_request(\n",
" messages, tools=tools\n",
")\n",
"assistant_message = chat_response.json()[\"choices\"][0][\"message\"]\n",
"assistant_message = chat_response.json()['choices'][0]['message']\n",
"messages.append(assistant_message)\n",
"assistant_message\n"
]
@ -256,7 +301,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"id": "23c42a6e",
"metadata": {},
"outputs": [
@ -265,13 +310,13 @@
"text/plain": [
"{'role': 'assistant',\n",
" 'content': None,\n",
" 'tool_calls': [{'id': 'call_o7uyztQLeVIoRdjcDkDJY3ni',\n",
" 'tool_calls': [{'id': 'call_X4MzS6ItfRQoS1iMejHlSGor',\n",
" 'type': 'function',\n",
" 'function': {'name': 'get_current_weather',\n",
" 'arguments': '{\\n \"location\": \"Glasgow, Scotland\",\\n \"format\": \"celsius\"\\n}'}}]}"
" 'arguments': '{\\n \"location\": \"Glasgow, Scotland\",\\n \"format\": \"celsius\"\\n} '}}]}"
]
},
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
@ -297,7 +342,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"id": "fa232e54",
"metadata": {},
"outputs": [
@ -305,10 +350,10 @@
"data": {
"text/plain": [
"{'role': 'assistant',\n",
" 'content': 'Sure, I can help you with that. Please let me know the value for x.'}"
" 'content': 'Sure, I can help you with that. Could you please specify the number of days you would like the weather forecast for?'}"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@ -336,7 +381,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"id": "c7d8a543",
"metadata": {},
"outputs": [
@ -346,14 +391,14 @@
"{'index': 0,\n",
" 'message': {'role': 'assistant',\n",
" 'content': None,\n",
" 'tool_calls': [{'id': 'call_drz2YpGPWEMVySzYgsWYY249',\n",
" 'tool_calls': [{'id': 'call_VXFknH4CuI42JRa8DtXD54aY',\n",
" 'type': 'function',\n",
" 'function': {'name': 'get_n_day_weather_forecast',\n",
" 'arguments': '{\\n \"location\": \"Glasgow, Scotland\",\\n \"format\": \"celsius\",\\n \"num_days\": 5\\n}'}}]},\n",
" 'finish_reason': 'tool_calls'}"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@ -386,7 +431,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"id": "559371b7",
"metadata": {},
"outputs": [
@ -395,13 +440,13 @@
"text/plain": [
"{'role': 'assistant',\n",
" 'content': None,\n",
" 'tool_calls': [{'id': 'call_jdmoJQ4lqsu4mBWcVBYtt5cU',\n",
" 'tool_calls': [{'id': 'call_dgFM39omP3Em2lqb0dxL6m4g',\n",
" 'type': 'function',\n",
" 'function': {'name': 'get_n_day_weather_forecast',\n",
" 'arguments': '{\\n \"location\": \"Toronto, Canada\",\\n \"format\": \"celsius\",\\n \"num_days\": 1\\n}'}}]}"
]
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@ -419,7 +464,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"id": "a7ab0f58",
"metadata": {},
"outputs": [
@ -428,13 +473,13 @@
"text/plain": [
"{'role': 'assistant',\n",
" 'content': None,\n",
" 'tool_calls': [{'id': 'call_RYXaDjxpUCfWmpXU7BZEYVqS',\n",
" 'tool_calls': [{'id': 'call_ZcinUqVnLZZFIOiON4PuJj1c',\n",
" 'type': 'function',\n",
" 'function': {'name': 'get_current_weather',\n",
" 'arguments': '{\\n \"location\": \"Toronto, Canada\",\\n \"format\": \"celsius\"\\n}'}}]}"
]
},
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@ -461,7 +506,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"id": "acfe54e6",
"metadata": {},
"outputs": [
@ -469,10 +514,10 @@
"data": {
"text/plain": [
"{'role': 'assistant',\n",
" 'content': '{ \"location\": \"Toronto, Canada\", \"format\": \"celsius\" }'}"
" 'content': '{\\n \"location\": \"Toronto, Canada\",\\n \"format\": \"celsius\"\\n}'}"
]
},
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@ -498,23 +543,23 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'id': 'call_fLsKR5vGllhbWxvpqsDT3jBj',\n",
"[{'id': 'call_1jJtnIwH5NU2PAZK8ZtOkzzy',\n",
" 'type': 'function',\n",
" 'function': {'name': 'get_n_day_weather_forecast',\n",
" 'arguments': '{\"location\": \"San Francisco, CA\", \"format\": \"celsius\", \"num_days\": 4}'}},\n",
" {'id': 'call_CchlsGE8OE03QmeyFbg7pkDz',\n",
" {'id': 'call_8IhvpuseAJuYPsS8Nw0x4doB',\n",
" 'type': 'function',\n",
" 'function': {'name': 'get_n_day_weather_forecast',\n",
" 'arguments': '{\"location\": \"Glasgow\", \"format\": \"celsius\", \"num_days\": 4}'}}]"
]
},
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@ -529,7 +574,7 @@
"\n",
"chat_response.json()\n",
"assistant_message = chat_response.json()[\"choices\"][0][\"message\"]['tool_calls']\n",
"assistant_message"
"assistant_message\n"
]
},
{
@ -558,7 +603,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"id": "30f6b60e",
"metadata": {},
"outputs": [
@ -574,12 +619,12 @@
"import sqlite3\n",
"\n",
"conn = sqlite3.connect(\"data/Chinook.db\")\n",
"print(\"Opened database successfully\")"
"print(\"Opened database successfully\")\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"id": "abec0214",
"metadata": {},
"outputs": [],
@ -622,7 +667,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"id": "0c0104cd",
"metadata": {},
"outputs": [],
@ -633,7 +678,7 @@
" f\"Table: {table['table_name']}\\nColumns: {', '.join(table['column_names'])}\"\n",
" for table in database_schema_dict\n",
" ]\n",
")"
")\n"
]
},
{
@ -647,7 +692,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"id": "0258813a",
"metadata": {},
"outputs": [],
@ -675,7 +720,7 @@
" },\n",
" }\n",
" }\n",
"]"
"]\n"
]
},
{
@ -691,7 +736,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"id": "65585e74",
"metadata": {},
"outputs": [],
@ -710,12 +755,12 @@
" results = ask_database(conn, query)\n",
" else:\n",
" results = f\"Error: function {message['tool_calls'][0]['function']['name']} does not exist\"\n",
" return results"
" return results\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"id": "38c55083",
"metadata": {},
"outputs": [
@ -727,7 +772,7 @@
"\u001b[0m\n",
"\u001b[32muser: Hi, who are the top 5 artists by number of tracks?\n",
"\u001b[0m\n",
"\u001b[34massistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT Artist.Name, COUNT(Track.TrackId) AS TrackCount FROM Artist JOIN Album ON Artist.ArtistId = Album.ArtistId JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Artist.Name ORDER BY TrackCount DESC LIMIT 5\"\\n}'}\n",
"\u001b[34massistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT ar.Name, COUNT(tr.TrackId) AS NumTracks FROM Artist ar JOIN Album al ON ar.ArtistId = al.ArtistId JOIN Track tr ON al.AlbumId = tr.AlbumId GROUP BY ar.ArtistId ORDER BY NumTracks DESC LIMIT 5;\"\\n}'}\n",
"\u001b[0m\n",
"\u001b[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n",
"\u001b[0m\n"
@ -745,12 +790,12 @@
"if assistant_message.get(\"tool_calls\"):\n",
" results = execute_function_call(assistant_message)\n",
" messages.append({\"role\": \"tool\", \"tool_call_id\": assistant_message[\"tool_calls\"][0]['id'], \"name\": assistant_message[\"tool_calls\"][0][\"function\"][\"name\"], \"content\": results})\n",
"pretty_print_conversation(messages)"
"pretty_print_conversation(messages)\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"id": "710481dc",
"metadata": {
"scrolled": true
@ -764,13 +809,13 @@
"\u001b[0m\n",
"\u001b[32muser: Hi, who are the top 5 artists by number of tracks?\n",
"\u001b[0m\n",
"\u001b[34massistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT Artist.Name, COUNT(Track.TrackId) AS TrackCount FROM Artist JOIN Album ON Artist.ArtistId = Album.ArtistId JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Artist.Name ORDER BY TrackCount DESC LIMIT 5\"\\n}'}\n",
"\u001b[34massistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT ar.Name, COUNT(tr.TrackId) AS NumTracks FROM Artist ar JOIN Album al ON ar.ArtistId = al.ArtistId JOIN Track tr ON al.AlbumId = tr.AlbumId GROUP BY ar.ArtistId ORDER BY NumTracks DESC LIMIT 5;\"\\n}'}\n",
"\u001b[0m\n",
"\u001b[35mfunction (ask_database): [('Iron Maiden', 213), ('U2', 135), ('Led Zeppelin', 114), ('Metallica', 112), ('Lost', 92)]\n",
"\u001b[0m\n",
"\u001b[32muser: What is the name of the album with the most tracks?\n",
"\u001b[0m\n",
"\u001b[34massistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT Album.Title, COUNT(Track.TrackId) AS TrackCount FROM Album JOIN Track ON Album.AlbumId = Track.AlbumId GROUP BY Album.Title ORDER BY TrackCount DESC LIMIT 1\"\\n}'}\n",
"\u001b[34massistant: {'name': 'ask_database', 'arguments': '{\\n \"query\": \"SELECT al.Title, COUNT(tr.TrackId) AS NumTracks FROM Album al JOIN Track tr ON al.AlbumId = tr.AlbumId GROUP BY al.AlbumId ORDER BY NumTracks DESC LIMIT 1;\"\\n}'}\n",
"\u001b[0m\n",
"\u001b[35mfunction (ask_database): [('Greatest Hits', 57)]\n",
"\u001b[0m\n"
@ -786,7 +831,7 @@
"if assistant_message.get(\"tool_calls\"):\n",
" results = execute_function_call(assistant_message)\n",
" messages.append({\"role\": \"tool\", \"tool_call_id\": assistant_message[\"tool_calls\"][0]['id'], \"name\": assistant_message[\"tool_calls\"][0][\"function\"][\"name\"], \"content\": results})\n",
"pretty_print_conversation(messages)"
"pretty_print_conversation(messages)\n"
]
},
{
@ -817,7 +862,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.9.16"
}
},
"nbformat": 4,

Loading…
Cancel
Save