OpenAI, ChatGPT, DALL·E 2 API

This post is describe how to use openai API.

How to use OpenAI API

First off all read the preview post

Awesome ChatGPT prompts

1curl https://api.openai.com/v1/completions \
2-H 'Content-Type: application/json' \
3-H "Authorization: Bearer $OPENAI_API_KEY" \
4-d '{
5"model": "text-davinci-003",
6"prompt": "Generate Python script for downloading from official websites and combining all logos for Android, iOS, Windows, macOS, Linux, WEB frontend and backend",
7"max_tokens": 3000,
8"temperature": 0
9}'
 1import os
 2import openai
 3openai.api_key = os.getenv("OPENAI_API_KEY")
 4
 5def raw_query(query: str):
 6    return " ".join(query.replace("\n", " ").split())
 7
 8
 9SQL = raw_query("""SELECT * FROM users""")
10
11completion = openai.Completion.create(
12    engine="text-davinci-003",
13    prompt=f"Optimize this SQL (PostgreSQL) code {SQL}",
14    max_tokens=3096
15)
16print(completion.choices[0]['text'])

DALL·E 2 API

1openai api image.create -p "Image with Android logo, ios logo, macos logo, linux logo, windows logo at the center" -n 4


Have fun, Ego!