|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [ |
| 8 | + { |
| 9 | + "ename": "NameError", |
| 10 | + "evalue": "name 'openai' is not defined", |
| 11 | + "output_type": "error", |
| 12 | + "traceback": [ |
| 13 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 14 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 15 | + "Cell \u001b[1;32mIn[1], line 18\u001b[0m\n\u001b[0;32m 3\u001b[0m URL \u001b[39m=\u001b[39m \u001b[39m\"\u001b[39m\u001b[39mhttps://api.openai.com/v1/chat/completions\u001b[39m\u001b[39m\"\u001b[39m\n\u001b[0;32m 5\u001b[0m payload \u001b[39m=\u001b[39m {\n\u001b[0;32m 6\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mmodel\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mgpt-3.5-turbo\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[0;32m 7\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mmessages\u001b[39m\u001b[39m\"\u001b[39m: [{\u001b[39m\"\u001b[39m\u001b[39mrole\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39muser\u001b[39m\u001b[39m\"\u001b[39m, \u001b[39m\"\u001b[39m\u001b[39mcontent\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mWhat is the first computer in the world?\u001b[39m\u001b[39m\"\u001b[39m}],\n\u001b[1;32m (...)\u001b[0m\n\u001b[0;32m 13\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mfrequency_penalty\u001b[39m\u001b[39m\"\u001b[39m:\u001b[39m0\u001b[39m,\n\u001b[0;32m 14\u001b[0m }\n\u001b[0;32m 16\u001b[0m headers \u001b[39m=\u001b[39m {\n\u001b[0;32m 17\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mContent-Type\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mapplication/json\u001b[39m\u001b[39m\"\u001b[39m,\n\u001b[1;32m---> 18\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mAuthorization\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39mf\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mBearer \u001b[39m\u001b[39m{\u001b[39;00mopenai\u001b[39m.\u001b[39mapi_key\u001b[39m}\u001b[39;00m\u001b[39m\"\u001b[39m\n\u001b[0;32m 19\u001b[0m }\n\u001b[0;32m 21\u001b[0m response \u001b[39m=\u001b[39m requests\u001b[39m.\u001b[39mpost(URL, headers\u001b[39m=\u001b[39mheaders, json\u001b[39m=\u001b[39mpayload, stream\u001b[39m=\u001b[39m\u001b[39mFalse\u001b[39;00m)\n", |
| 16 | + "\u001b[1;31mNameError\u001b[0m: name 'openai' is not defined" |
| 17 | + ] |
| 18 | + } |
| 19 | + ], |
| 20 | + "source": [ |
| 21 | + "import requests\n", |
| 22 | + "\n", |
| 23 | + "URL = \"https://api.openai.com/v1/chat/completions\"\n", |
| 24 | + "\n", |
| 25 | + "payload = {\n", |
| 26 | + "\"model\": \"gpt-3.5-turbo\",\n", |
| 27 | + "\"messages\": [{\"role\": \"user\", \"content\": f\"What is the first computer in the world?\"}],\n", |
| 28 | + "\"temperature\" : 1.0,\n", |
| 29 | + "\"top_p\":1.0,\n", |
| 30 | + "\"n\" : 1,\n", |
| 31 | + "\"stream\": False,\n", |
| 32 | + "\"presence_penalty\":0,\n", |
| 33 | + "\"frequency_penalty\":0,\n", |
| 34 | + "}\n", |
| 35 | + "\n", |
| 36 | + "headers = {\n", |
| 37 | + "\"Content-Type\": \"application/json\",\n", |
| 38 | + "\"Authorization\": f\"Bearer {openai.api_key}\"\n", |
| 39 | + "}\n", |
| 40 | + "\n", |
| 41 | + "response = requests.post(URL, headers=headers, json=payload, stream=False)" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "code", |
| 46 | + "execution_count": 2, |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [ |
| 49 | + { |
| 50 | + "ename": "NameError", |
| 51 | + "evalue": "name 'response' is not defined", |
| 52 | + "output_type": "error", |
| 53 | + "traceback": [ |
| 54 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 55 | + "\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)", |
| 56 | + "Cell \u001b[1;32mIn[2], line 1\u001b[0m\n\u001b[1;32m----> 1\u001b[0m response\u001b[39m.\u001b[39mcontent\n", |
| 57 | + "\u001b[1;31mNameError\u001b[0m: name 'response' is not defined" |
| 58 | + ] |
| 59 | + } |
| 60 | + ], |
| 61 | + "source": [ |
| 62 | + "\n", |
| 63 | + "response.content" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": null, |
| 69 | + "metadata": {}, |
| 70 | + "outputs": [], |
| 71 | + "source": [] |
| 72 | + } |
| 73 | + ], |
| 74 | + "metadata": { |
| 75 | + "kernelspec": { |
| 76 | + "display_name": "Python 3", |
| 77 | + "language": "python", |
| 78 | + "name": "python3" |
| 79 | + }, |
| 80 | + "language_info": { |
| 81 | + "codemirror_mode": { |
| 82 | + "name": "ipython", |
| 83 | + "version": 3 |
| 84 | + }, |
| 85 | + "file_extension": ".py", |
| 86 | + "mimetype": "text/x-python", |
| 87 | + "name": "python", |
| 88 | + "nbconvert_exporter": "python", |
| 89 | + "pygments_lexer": "ipython3", |
| 90 | + "version": "3.11.4" |
| 91 | + }, |
| 92 | + "orig_nbformat": 4 |
| 93 | + }, |
| 94 | + "nbformat": 4, |
| 95 | + "nbformat_minor": 2 |
| 96 | +} |
0 commit comments