avatarCassius

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3334

Abstract

</span>

prompt = <span class="hljs-string">"Here is where you enter your requests!"</span>

completion = openai.Completion.create( engine=model_engine, prompt=prompt, max_tokens=<span class="hljs-number">1024</span>, n=<span class="hljs-number">1</span>, stop=<span class="hljs-literal">None</span>, temperature=<span class="hljs-number">0.5</span>, top_p=<span class="hljs-number">1</span>, frequency_penalty=<span class="hljs-number">0</span>, presence_penalty=<span class="hljs-number">0</span> )

<span class="hljs-built_in">print</span>(completion.choices[<span class="hljs-number">0</span>].text)</pre></div><p id="e3cf">The API client provides several methods for interacting with ChatGPT, including the ability to set the context of the conversation and specify the maximum length of the response (using max tokens). It also allows users to send and receive messages and view the full conversation history.</p><p id="5521">Below, we briefly explain the tuning parameters:</p><h2 id="506a">n parameter</h2><p id="f7b3">The number of completions to be generated for each prompt. If you want the GPT model to generate more results, then n should be greater than 1.</p><figure id="9bce"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*p3J3ydW8RkkwBwO9qH7V3A.png"><figcaption></figcaption></figure><h2 id="a3c1">Temperature parameter</h2><p id="62bb">This controls randomness. Low temperature is less random or more deterministic, whilst high temperature is more random. In other words, low temperature makes the model more confident in its top choices.</p><figure id="4e44"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*kTqLXDgzR_6xloH6EfYGDQ.png"><figcaption></figcaption></figure><h2 id="533f">Max Token parameter</h2><p id="f0a0">This specifies the upper bound of the in the maximum number of tokens to generate in the completion. It does not necessary guarantee the response will be of the same length.</p><figure id="e346"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*YAsGcDuvfHHd89TprpI2yA.png"><figcaption></figcaption></figure><h2 id="2f1b">Top_p parameter</h2><p id="6baf">This is alternative to sampling with the temperature parameter.</p><p id="bc75"><i>Top P is “An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered” (OpenAI, 2022).</i></p><div id="8b4a" class="link-block"> <a href="https://community.openai.com/t/a-better-explanation-of-top-p/2426/2"> <div> <div> <h2>A better explanation of "Top P"?</h2> <div><h3>The tooltip for "Top P" in playground doesn't really tell me what it does, qualitatively. I get that higher numbers get…</h3></div> <div><p>community.openai.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*eLjOV0dty10ebxSS)"></div> </div> </div> </a> </div><h2 id="e9a4">Frequency Penalty parameter</h2><p id="c0b0">The frequency penalty is a number between -2.0 and 2.0. Positive values penalize new tokens

Options

based on their existing frequency in the accumulated text so far, decreasing the model’s likelihood to repeat the same line verbatim.</p><h2 id="afcc">Presence Penalty parameter</h2><p id="b8ac">This checks whether the tokens have already been present in the response or not</p><figure id="e5ae"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DeaYp0Qjwy7lqzxBVRLCJw.png"><figcaption></figcaption></figure><p id="627e">For more details, you can check out the Open AI API reference manual.</p><div id="5800" class="link-block"> <a href="https://platform.openai.com/docs/api-reference/completions/create"> <div> <div> <h2>OpenAI API</h2> <div><h3>An API for accessing new AI models developed by OpenAI</h3></div> <div><p>platform.openai.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/)"></div> </div> </div> </a> </div><h1 id="0bc2">Some applications</h1><p id="83cf">I hope the above has provided you with some basics on using ChatGPT or GPT models in Python. Below are some examples to use in practice.</p><h2 id="818d">Summarizing email content…</h2><div id="7e13" class="link-block"> <a href="https://readmedium.com/how-to-use-chatgpt-to-summarize-your-email-content-8541af08a89f"> <div> <div> <h2>How to use ChatGPT to summarize your email content</h2> <div><h3>Let’s talk about how to use OpenAI’s API to allow ChatGPT to summarize your email content. A step by step introduction.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*p0UaJgfN0FjWaKiD)"></div> </div> </div> </a> </div><h2 id="ac8e">Learning Python with ChatGPT…</h2><div id="ed55" class="link-block"> <a href="https://readmedium.com/using-chatgpt-from-python-28e8c8a0f8ff"> <div> <div> <h2>Using ChatGPT from Python</h2> <div><h3>How to use ChatGPT directly from Python.</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*INlnZRdcta6hPjnT)"></div> </div> </div> </a> </div><h2 id="94b2">Join Medium to get more content on ChatGPT…</h2><div id="ab10" class="link-block"> <a href="https://cassiusdio.medium.com/membership"> <div> <div> <h2>Join Medium with my referral link - Cassius</h2> <div><h3>Read every story from Cassius (and thousands of other writers on Medium). Your membership fee directly supports Cassius…</h3></div> <div><p>cassiusdio.medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*U9PbUO0waDwg8bd9)"></div> </div> </div> </a> </div></article></body>

ChatGPT with Python

A simple step-by-step tutorial on how to use ChatGPT in Python

Photo by ilgmyzin on Unsplash

Recently, OpenAI has made their text-davinci-003 model ‘free’ along with their Python API. This tutorial allows users to access this GPT model from Python using a few simple steps.

  1. Create a free account with OpenAI
  2. Create an API Key
  3. Some basic Python code to parse through your prompt, and return the response

The text-davinci-003 GPT-3 API enables developers to generate natural language text from just a few examples. It is designed to be able to generate high quality text for various tasks, such as the ability to summarize text, translation, question answering and text editing. The API also provides a library of pre-trained models, allowing developers to quickly start using the model without having to build it from scratch. Overall, a very powerful tool!

Step 1. Create an OpenAI account

Create a user account from here:

This should be free. New users are provided with $18 credit which is last approximately 1,800 API calls.

Photo by Choong Deng Xiang on Unsplash

Step 2. Create an API key

You will need an OpenAI API secret key, which you can obtain from the OpenAI website above, after you have created an account. Once you have an API key, you can use it to access the various OpenAI APIs, including the GPT-3 API, which powers ChatGPT.

An example of an OpenAI secret key

Keep your secret key safe!

Step 3. Basic Python Code

Next you will need to install openai on your Python distribution. This can be done using the following command prompt:

pip install openai
import openai

openai.api_key = "your secret key here"
model_engine = "text-davinci-003"

prompt = "Here is where you enter your requests!"

completion = openai.Completion.create(
  engine=model_engine,
  prompt=prompt,
  max_tokens=1024,
  n=1,
  stop=None, 
  temperature=0.5, 
  top_p=1, 
  frequency_penalty=0,
  presence_penalty=0
)

print(completion.choices[0].text)

The API client provides several methods for interacting with ChatGPT, including the ability to set the context of the conversation and specify the maximum length of the response (using max tokens). It also allows users to send and receive messages and view the full conversation history.

Below, we briefly explain the tuning parameters:

n parameter

The number of completions to be generated for each prompt. If you want the GPT model to generate more results, then n should be greater than 1.

Temperature parameter

This controls randomness. Low temperature is less random or more deterministic, whilst high temperature is more random. In other words, low temperature makes the model more confident in its top choices.

Max Token parameter

This specifies the upper bound of the in the maximum number of tokens to generate in the completion. It does not necessary guarantee the response will be of the same length.

Top_p parameter

This is alternative to sampling with the temperature parameter.

Top P is “An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered” (OpenAI, 2022).

Frequency Penalty parameter

The frequency penalty is a number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the accumulated text so far, decreasing the model’s likelihood to repeat the same line verbatim.

Presence Penalty parameter

This checks whether the tokens have already been present in the response or not

For more details, you can check out the Open AI API reference manual.

Some applications

I hope the above has provided you with some basics on using ChatGPT or GPT models in Python. Below are some examples to use in practice.

Summarizing email content…

Learning Python with ChatGPT…

Join Medium to get more content on ChatGPT…

ChatGPT
Python
AI
Programming
Productivity
Recommended from ReadMedium