1、关于ChatGPT 和 API 获取
2、调用API 实现 ChatGPT 对话
import openaiopenai.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].textreturn messagewhile(True):prompt = input("问题:")print(completion(prompt))
3、调用API 实现AI 图片生成
import openai
import requests
import osopenai.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)
备注:
需要开启VPN openai 是国外的