ChatGPT Integrated Development

ChatGPT Integration Development

As artificial intelligence technology matures, its application areas are becoming increasingly widespread. Taking the recently popular ChatGPT as an example, this issue presents a case of system integration.

Introduction to ChatGPT

ChatGPT is a super dialogue model. Currently, ChatGPT is in the testing phase (chat.apps.openai.com), and anyone with an OpenAI account can use it for free (with a free quota of $18). It can help you write code, solve math problems, create recipes, learn English, translate foreign languages, write articles…

The GPT-3 model can understand and generate natural language. It offers four main models with different power levels, suitable for various tasks. Da Vinci is the most capable model, while Ada is the fastest.

Latest Model Description Training Data
Text-DaVinci-003 The most powerful GPT-3 model. Can complete any task that other models can, usually with higher quality, longer output, and better instruction following. Also supports inserting completions in text. As of 2021<> months
Text-Curie-001 Very capable, but faster and cheaper than Da Vinci. As of 2019<> months
Text-Babbage-001 Can complete simple tasks, very fast, and cheaper. As of 2019<> months
Text-Ada-001 Can complete very simple tasks, usually the fastest model in the GPT-3 series, and the cheapest. As of 2019<> months

Although Da Vinci is usually the most capable, other models can perform certain tasks exceptionally well, with significant speed or cost advantages. For example, Curie can perform many of the same tasks as Da Vinci but is faster and costs only 1/10 of Da Vinci.

ChatGPT Account Registration

ChatGPT does not support use in domestic environments, so during registration, you need to use tools like VPN to direct your IP address abroad, and you must have an overseas phone number to register. For a detailed guide, you can open the following link in the Edge browser (Complete ChatGPT Registration Guide, How to Create an Account – SMS-Activate).

Registering a Phone Number

  • Open the website, purchase a virtual phone number, using a VPN tool can speed up website access
image-20230224162724232
  • Choose a phone number from a country, then purchase and activate it. A one-time phone number can receive SMS within 20 minutes
image-20230224163354725

Registering an Open AI Account

  • Open the Open AI official website ChatGPT | OpenAI, and register using an email (both domestic and foreign emails are acceptable, Google email is recommended)
  • Enter the phone number and verification code. The phone number is the virtual phone number purchased earlier, and the verification code needs to be checked on the sms-activate website to see if it has been received
  • After the verification code is passed, you can log in to the Open AI homepage

ChatGPT Chat Dialogue

Now you can start chatting with ChatGPT. Note that chatting is charged based on the number of characters in the response, and different dialogue models have different charging standards. For the most expensive Da Vinci model, the cost is $0.02 per thousand characters, and each account has an $18 free quota.

Chat example:

Python Call to ChatGPT

Calling ChatGPT with Python is simple and quick, just install the openai package to use it. You need to obtain an APIKey first, create an APIKey on the OpenAI API website

image-20230224172740615
  • Write Python code to make a simple call to answer text questions
import openai

# Enter your api_key
openai.api_key = "sk-UVkILazQh4BMKZqeIKR3T3BlbkFJxAhV8Bl7r8JrBVgrucy"

# Use the Da Vinci model to answer questions
def question(inputStr):
    response = openai.Completion.create(
        model="text-davinci-003",
        prompt=inputStr,
        temperature=0,
        max_tokens=1024,
        top_p=1,
        frequency_penalty=0,
        presence_penalty=0
    )
    choices = response.get("choices")

    for text in choices:
        print(text.text)

# Loop to input questions
def ask():
    while True:
        inputStr = str(input())
        print(inputStr)
        question(inputStr)

if __name__ == '__main__':
    ask()
  • Write Python code to generate images
import requests

# Fill in your api_key
endpoint = "https://api.openai.com/v1/images/generations"
api_key = "sk-UVkILazQh4BMKZqeIKR3T3BlbkFJxAhV8Bl7r8JrBVgrucy"

# Set model and prompt
model = "image-alpha-001"

prompt = "A white Garfield cat with black spots standing up"

# Set the width and height of the generated image
width = 1024
height = 1024

# Set response mode
response_format = "url"

# Set the number of images
num_images = 1

# Set HTTP request headers
headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {api_key}"
}

# Set the request body
payload = {
    "model": model,
    "prompt": prompt,
    "num_images": num_images,
    "size": f"{width}x{height}",
    "response_format": response_format
}

# Send request to call API
response = requests.post(endpoint, headers=headers, json=payload)

# Check if the response code indicates a successful call
if response.status_code == 200:
    # Get the returned image URL
    image_url = response.json()["data"][0]["url"]

    # Download and convert the image URL to a file
    image_data = requests.get(image_url).content
    with open("img.jpg", "wb") as f:
        f.write(image_data)

else:
    # Print the message of the failed request
    print(f"Failed to generate image: {response.json()['message']}")

AI-generated image

By Tim

Leave a Reply

Your email address will not be published. Required fields are marked *