Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] OpenAI API – or How I Made My Python Code Intelligent

#1
OpenAI API – or How I Made My Python Code Intelligent

<div>
<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;}'>
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 142.5px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend" style="font-size: 19.2px;"> 5/5 – (1 vote) </div>
</p></div>
<p>In this quick tutorial, I’ll show you how I integrated ChatGPT intelligence into an app I’m currently working on. It’s really simple, so let’s get started!</p>
<h2>Step 1: Create a Paid Account with OpenAI</h2>
<p>I’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’ll need in order to connect ChatGPT with your code.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f449.png" alt="?" 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>
<p>I use it a lot and pay only a couple of cents per day so it’s really inexpensive for now.</p>
<h2>Step 2: Get Your API Key</h2>
<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>
<div class="wp-block-image">
<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/uplo...00x211.png 300w, https://blog.finxter.com/wp-content/uplo...68x541.png 768w, https://blog.finxter.com/wp-content/uplo...6x1082.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-301.png 1651w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Now, click the <code>+ Create new secret key</code> button to create a new API key:</p>
<div class="wp-block-image">
<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/uplo...00x167.png 300w, https://blog.finxter.com/wp-content/uplo...68x427.png 768w, https://blog.finxter.com/wp-content/uplo...36x854.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-302.png 1667w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<p>Now copy the API key to your clipboard:</p>
<div class="wp-block-image">
<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/uplo...00x158.png 300w, https://blog.finxter.com/wp-content/uplo...68x404.png 768w, https://blog.finxter.com/wp-content/uplo...36x808.png 1536w, https://blog.finxter.com/wp-content/uplo...ge-303.png 1664w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<h2>Step 3: Pip Install OpenAI</h2>
<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>
<ul>
<li><code>pip install openai</code></li>
<li><code>pip3 install openai</code></li>
<li><code>pip3.11 install openai</code></li>
</ul>
<p>As I’ve installed Python 3.9 at the point of writing, I used <code>pip3.9 install openai</code>:</p>
<div class="wp-block-image">
<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/uplo...00x237.png 300w, https://blog.finxter.com/wp-content/uplo...68x608.png 768w, https://blog.finxter.com/wp-content/uplo...ge-304.png 1097w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
</div>
<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>
<h2>Step 4: Python Code to Access OpenAI</h2>
<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>
<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
import 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
) print(response)</pre>
<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>
<pre class="wp-block-preformatted"><code>{ "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null,
<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 }
}</code></pre>
<p>Unfortunately, it couldn’t figure out the answer 42. <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f609.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p></p>
</div>


https://www.sickgaming.net/blog/2023/01/...telligent/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016