网络问题:openai-chatgpt的api调用异常处理
官方手册:https://platform.openai.com/docs/api-reference
visgpt
gitlab代码
https://github.com/microsoft/visual-chatgpt
visual_chatgpt.py运行前添加密匙
os.environ['openai_api_key']=""
更改参数为cpu
parser.add_argument('--load', type=str, default="imagecaptioning_cpu,text2image_cpu")
非常非常慢7min,而且根据控制台查看图片,发现已经生成,而ui上没有生成
chatgpt
import openai
# apply the api key
openai.api_key = "xxx"
# define the text prompt
prompt = "to be or not to be "
# generate completions using the api
completions = openai.completion.create(
engine="text-davinci-002",
prompt=prompt,
max_tokens=100,
n=1,
stop=none,
temperature=0.5,
)
# extract the message from the api response
message = completions.choices[0].text
print(message)
结果
dalle2
import requests
import openai
from pil import image
openai.api_key = "xxx"
response=openai.image.create(
prompt="the little prince and the fox in the story, they sit together",
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
r = requests.get(image_url)
with open('test.png', 'wb') as f:
f.write(r.content)
img = image.open('test.png')
img.show()
结果:氛围有了,但是细节不够
发表评论