{ "cells": [ { "cell_type": "code", "execution_count": 2, "id": "21b3b31c-c951-47eb-98d3-0d41f066c449", "metadata": {}, "outputs": [], "source": [ "# imports\n", "import openai # for OpenAI API calls\n", "import time # for measuring time duration of API calls" ] }, { "cell_type": "code", "execution_count": 11, "id": "63724402-590a-49e6-bf77-942835432b03", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['APIError',\n", " 'Audio',\n", " 'ChatCompletion',\n", " 'Completion',\n", " 'ContextVar',\n", " 'Customer',\n", " 'Deployment',\n", " 'Edit',\n", " 'Embedding',\n", " 'Engine',\n", " 'ErrorObject',\n", " 'File',\n", " 'FineTune',\n", " 'Image',\n", " 'InvalidRequestError',\n", " 'Model',\n", " 'Moderation',\n", " 'OpenAIError',\n", " 'Optional',\n", " 'TYPE_CHECKING',\n", " '__all__',\n", " '__annotations__',\n", " '__builtins__',\n", " '__cached__',\n", " '__doc__',\n", " '__file__',\n", " '__loader__',\n", " '__name__',\n", " '__package__',\n", " '__path__',\n", " '__spec__',\n", " 'aiosession',\n", " 'api_base',\n", " 'api_key',\n", " 'api_key_path',\n", " 'api_requestor',\n", " 'api_resources',\n", " 'api_type',\n", " 'api_version',\n", " 'app_info',\n", " 'ca_bundle_path',\n", " 'datalib',\n", " 'debug',\n", " 'enable_telemetry',\n", " 'error',\n", " 'log',\n", " 'openai_object',\n", " 'openai_response',\n", " 'organization',\n", " 'os',\n", " 'proxy',\n", " 'util',\n", " 'verify_ssl_certs',\n", " 'version']" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "dir(openai)" ] }, { "cell_type": "code", "execution_count": null, "id": "ad154406-c78a-4e34-9ba7-e00ded084f25", "metadata": {}, "outputs": [], "source": [ "# record the time before the request is sent\n", "start_time = time.time()\n", "\n", "# send a ChatCompletion request to count to 100\n", "response = openai.ChatCompletion.create(\n", " model='gpt-3.5-turbo',\n", " messages=[\n", " {'role': 'user', 'content': 'Count to 100, with a comma between each number and no newlines. E.g., 1, 2, 3, ...'}\n", " ],\n", " temperature=0,\n", ")\n", "\n", "# calculate the time it took to receive the response\n", "response_time = time.time() - start_time\n", "\n", "# print the time delay and text received\n", "print(f\"Full response received {response_time:.2f} seconds after request\")\n", "print(f\"Full response received:\\n{response}\")\n", "reply = response['choices'][0]['message']\n", "print(f\"Extracted reply: \\n{reply}\")\n", "\n", "reply_content = response['choices'][0]['message']['content']\n", "print(f\"Extracted content: \\n{reply_content}\")" ] }, { "cell_type": "code", "execution_count": null, "id": "b42e1746-f2a7-49b1-baf9-148d72e22ce7", "metadata": {}, "outputs": [], "source": [ "# Example of an OpenAI ChatCompletion request with stream=True\n", "# https://platform.openai.com/docs/guides/chat\n", "\n", "# a ChatCompletion request\n", "response = openai.ChatCompletion.create(\n", " model='gpt-3.5-turbo',\n", " messages=[\n", " {'role': 'user', 'content': \"What's 1+1? Answer in one word.\"}\n", " ],\n", " temperature=0,\n", " stream=True # this time, we set stream=True\n", ")\n", "\n", "for chunk in response:\n", " print(chunk)" ] } ], "metadata": { "kernelspec": { "display_name": "openai", "language": "python", "name": "openai" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.16" } }, "nbformat": 4, "nbformat_minor": 5 }