From 6dc99646012591839de3bb9d69478c1a22d36235 Mon Sep 17 00:00:00 2001 From: willcyber Date: Sun, 4 Jun 2023 21:19:34 -0700 Subject: [PATCH] more blog --- _notebooks/2023-06-04-chatgptapi.ipynb | 142 +++++++++++++++++++++++++ 1 file changed, 142 insertions(+) diff --git a/_notebooks/2023-06-04-chatgptapi.ipynb b/_notebooks/2023-06-04-chatgptapi.ipynb index 035d8a9..4048a35 100644 --- a/_notebooks/2023-06-04-chatgptapi.ipynb +++ b/_notebooks/2023-06-04-chatgptapi.ipynb @@ -108,6 +108,148 @@ "\n", "" ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## In Depth" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "
\n", + " \n", + " \n", + " \n", + "
" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This piece of html takes in and stores the user input" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "
" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is the area where the api will output the response" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "// Encode the API key using Base64\n", + "const encodedApiKey = \"encoded API Key\";\n", + "const decodedApiKey = atob(encodedApiKey);\n", + "const authorizationHeader = 'Bearer ' + decodedApiKey;" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Base64 the key so github won't complain lol" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "try {\n", + " const response = await axios.post('https://api.openai.com/v1/chat/completions', {\n", + " messages: [\n", + " { role: 'system', content: 'You are a user requesting music recommendations.' },\n", + " { role: 'user', content: userInput }\n", + " ],\n", + " model: 'gpt-3.5-turbo' \n", + " }, {\n", + " headers: {\n", + " 'Content-Type': 'application/json',\n", + " 'Authorization': authorizationHeader\n", + " }\n", + " });\n", + "\n", + " const assistantReply = response.data.choices[0].message.content;\n", + " displayAssistantReply(assistantReply);" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "This is the most important part of the code. These are the options that I had alluded to in the beginning of this blog" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "catch (error) {\n", + " console.error('Error calling ChatGPT API:', error);\n", + " console.error('Error response:', error.response);\n", + " console.error('Error message:', error.message);\n", + " displayAssistantReply('Sorry, an error occurred while processing your request.');\n", + " }\n", + "});" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Catches the errors. Outputs it onto html and in the console" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "function displayAssistantReply(reply) {\n", + " const replyElement = document.createElement('p');\n", + " replyElement.textContent = 'Assistant: ' + reply;\n", + " outputContainer.appendChild(replyElement);\n", + " }" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "If a response successfully is sent, it will be shown on the html" + ] } ], "metadata": {