{"id":131395,"date":"2023-01-24T17:02:53","date_gmt":"2023-01-24T17:02:53","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=1081478"},"modified":"2023-01-24T17:02:53","modified_gmt":"2023-01-24T17:02:53","slug":"openai-api-or-how-i-made-my-python-code-intelligent","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2023\/01\/24\/openai-api-or-how-i-made-my-python-code-intelligent\/","title":{"rendered":"OpenAI API \u2013 or How I Made My Python Code Intelligent"},"content":{"rendered":"\n<div class=\"kk-star-ratings kksr-auto kksr-align-left kksr-valign-top\" data-payload='{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;1081478&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;ignore&quot;:&quot;&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;class&quot;:&quot;&quot;,&quot;count&quot;:&quot;1&quot;,&quot;legendonly&quot;:&quot;&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;5&quot;,&quot;starsonly&quot;:&quot;&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;5\\\/5 - (1 vote)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;142.5&quot;,&quot;_legend&quot;:&quot;{score}\\\/{best} - ({count} {votes})&quot;,&quot;font_factor&quot;:&quot;1.25&quot;}'>\n<div class=\"kksr-stars\">\n<div class=\"kksr-stars-inactive\">\n<div class=\"kksr-star\" data-star=\"1\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"2\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"3\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"4\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" data-star=\"5\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div class=\"kksr-stars-active\" style=\"width: 142.5px;\">\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<div class=\"kksr-star\" style=\"padding-right: 5px\">\n<div class=\"kksr-icon\" style=\"width: 24px; height: 24px;\"><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/div>\n<div class=\"kksr-legend\" style=\"font-size: 19.2px;\"> 5\/5 &#8211; (1 vote) <\/div>\n<\/p><\/div>\n<p>In this quick tutorial, I&#8217;ll show you how I integrated ChatGPT intelligence into an app I&#8217;m currently working on. It&#8217;s really simple, so let&#8217;s get started!<\/p>\n<h2>Step 1: Create a Paid Account with OpenAI<\/h2>\n<p>I&#8217;m not affiliated with OpenAI in any way. However, to use it, you need to create a (paid) account to create an API key that you&#8217;ll need in order to connect ChatGPT with your code.<\/p>\n<p class=\"has-base-background-color has-background\"><img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f449.png\" alt=\"\ud83d\udc49\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/> <a href=\"https:\/\/beta.openai.com\/playground\" data-type=\"URL\" data-id=\"https:\/\/beta.openai.com\/playground\" target=\"_blank\" rel=\"noreferrer noopener\">Click here to create an account<\/a> and connect it with your credit card<\/p>\n<p>I use it a lot and pay only a couple of cents per day so it&#8217;s really inexpensive for now.<\/p>\n<h2>Step 2: Get Your API Key<\/h2>\n<p>Open the link <a href=\"https:\/\/beta.openai.com\/playground\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/beta.openai.com\/playground<\/a> and navigate to <code>Personal > View API keys<\/code>. <\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"721\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-301-1024x721.png\" alt=\"\" class=\"wp-image-1081485\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-301-1024x721.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-301-300x211.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-301-768x541.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-301-1536x1082.png 1536w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-301.png 1651w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>Now, click the <code>+ Create new secret key<\/code> button to create a new API key:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"569\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-302-1024x569.png\" alt=\"\" class=\"wp-image-1081487\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-302-1024x569.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-302-300x167.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-302-768x427.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-302-1536x854.png 1536w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-302.png 1667w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>Now copy the API key to your clipboard:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"538\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-303-1024x538.png\" alt=\"\" class=\"wp-image-1081488\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-303-1024x538.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-303-300x158.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-303-768x404.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-303-1536x808.png 1536w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-303.png 1664w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<h2>Step 3: Pip Install OpenAI<\/h2>\n<p>Use your version of pip to install the <code>openai<\/code> module by running a command similar to the following (depending on your local environment):<\/p>\n<ul>\n<li><code>pip install openai<\/code><\/li>\n<li><code>pip3 install openai<\/code><\/li>\n<li><code>pip3.11 install openai<\/code><\/li>\n<\/ul>\n<p>As I&#8217;ve installed Python 3.9 at the point of writing, I used <code>pip3.9 install openai<\/code>:<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"1024\" height=\"810\" src=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-304-1024x810.png\" alt=\"\" class=\"wp-image-1081490\" srcset=\"https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-304-1024x810.png 1024w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-304-300x237.png 300w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-304-768x608.png 768w, https:\/\/blog.finxter.com\/wp-content\/uploads\/2023\/01\/image-304.png 1097w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/div>\n<p>You can <a href=\"https:\/\/blog.finxter.com\/how-to-check-your-python-version\/\" data-type=\"post\" data-id=\"1371\" target=\"_blank\" rel=\"noreferrer noopener\">check your Python version here<\/a> and learn how to <a href=\"https:\/\/blog.finxter.com\/how-to-install-xxx-in-python\/\" data-type=\"post\" data-id=\"653128\" target=\"_blank\" rel=\"noreferrer noopener\">install a module here<\/a>.<\/p>\n<h2>Step 4: Python Code to Access OpenAI<\/h2>\n<p>Copy and paste the following code into a Python script (e.g., named <code>code.py<\/code>) and also paste your API key from <strong>Step 2<\/strong> into the highlighted line (string):<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"5,9\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport openai openai.api_key = \"&lt;copy your secret API key here>\" response = openai.Completion.create( model=\"text-davinci-003\", prompt=\"What is the answer to all questions?\", temperature=0.7, max_tokens=100, top_p=1, frequency_penalty=0, presence_penalty=0\n) print(response)<\/pre>\n<p>You can modify the other highlighted line <code>\"What is the answer to all questions?\"<\/code> to customize your input prompt. The output after a few seconds will look like this:<\/p>\n<pre class=\"wp-block-preformatted\"><code>{ \"choices\": [ { \"finish_reason\": \"stop\", \"index\": 0, \"logprobs\": null,\n<strong> \"text\": \"\\n\\nThere is no one answer to all questions as each question has its own unique answer.\"<\/strong> } ], \"created\": 1674579571, \"id\": \"cmpl-6cGvr0TM2PGsExeyG3NEx43CrNwSx\", \"model\": \"text-davinci-003\", \"object\": \"text_completion\", \"usage\": { \"completion_tokens\": 19, \"prompt_tokens\": 8, \"total_tokens\": 27 }\n}<\/code><\/pre>\n<p>Unfortunately, it couldn&#8217;t figure out the answer 42. <img decoding=\"async\" src=\"https:\/\/s.w.org\/images\/core\/emoji\/14.0.0\/72x72\/1f609.png\" alt=\"\ud83d\ude09\" class=\"wp-smiley\" style=\"height: 1em; max-height: 1em;\" \/><\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>5\/5 &#8211; (1 vote) In this quick tutorial, I&#8217;ll show you how I integrated ChatGPT intelligence into an app I&#8217;m currently working on. It&#8217;s really simple, so let&#8217;s get started! Step 1: Create a Paid Account with OpenAI I&#8217;m not affiliated with OpenAI in any way. However, to use it, you need to create a [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-131395","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131395","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=131395"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/131395\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=131395"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=131395"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=131395"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}