生成文字对话
import openai
import json
# 设置API密钥
openai.api_key = "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"
def chat(prompt):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "user", "content":prompt}
]
)
answer = response.choices[0].message.content
return answer
if __name__=='__main__':
# prompt='人口最多的国家'
prompt=input("请输入你的问题: ")
result = chat(prompt)
print(result)
生成图片
import openai
import json
# 设置API密钥
# openai.api_key = "sk-Wljk3BVhN0VieGCwAzEXT3BlbkFJ*******"
openai.api_key = "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"
def image_genaration(prompt):
response = openai.Image.create(
prompt=prompt,
n=1,
size="1024x1024"
)
image_url = response['data'][0]['url']
return image_url
if __name__=='__main__':
prompt='a delicious dessert'
result = image_genaration(prompt)
print(result)
网友评论