ChatGPT API 简单使用

关于 ChatGPT 和 API 获取

image

调用API 实现 ChatGPT 对话



import openai


openai.api_key = "你的api_key"

def completion(prompt):
    completions = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=1,
    )
    print(completions, "\n")

    message = completions.choices[0].text
    return message

while(True):
    prompt = input("问题:")
    print(completion(prompt))

image

调用 API实现 AI 图片生成

import openai
import requests
import os

openai.api_key = "你的api-key"


# 图片描述

image_des = "一个黄毛狗"

# AI 生成

response = openai.Image.create(
    prompt=image_des,
    n=1,
    size="1024x1024"
)

image_url = response['data'][0]['url']

# 保存
res = requests.get(image_url)
image_name = "AI_"+image_des.replace(" ", "_") + ".png"
path = "./image/"
with open(path+image_name, 'wb') as f:
    f.write(res.content)

image

posted @ 2023-02-28 13:41  HUGBOY  阅读(10862)  评论(0编辑  收藏  举报