5. OpenAI API
YOU CAN USE THE OPENAI API TO USE THE MODELS IN YOUR CODE FOR
EXAMPLE COMPLETE CODE OR GENERATE IMAGES ETC.
https://beta.openai.com/docs/api-reference/introduction
Here you will find a link to API key page where you can download the key
(copy to clipboard) and later used in code.
6. Here you will find APIS for code completion, correction ,image generation
etc.
Different URLS are to be passed for different purposes. HTTP response post
method is used to post the request in python with json data and headers and
shown in next slide.
Further before doing any thing you have to install openai using pip.
7. The code for image generation
!pip install openai
#Image generation using OpenAI api
#Singup and get the api key from https://beta.openai.com/account/api-keys
# Todo for you.
# response.content is the image save it and display it.
import os
import openai
import requests
import requests
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_API_KEY',
}
8. json_data = {
'prompt': 'A mountain with clouds',
'n': 2,
'size': '1024x1024',
}
response = requests.post('https://api.openai.com/v1/images/generations', he
aders=headers, json=json_data)
#save the content into binary and display it
print(response.content)
9. Link to my github code
https://github.com/faizulhz/datascience1/blob/master/Codex_image_gener
ations_ex2.ipynb
For code generation use the URL
https://api.openai.com/v1/completions
Use the parameters and URLs from API page
https://beta.openai.com/docs/api-reference/introduction
Also you can use the command line CURL tool to run it as a .bat file progam.