OpenAI模型的API调用与使用-测试(2)
OpenAI模型的API调用与使用-测试(2)
- 1. 参考Quick start搭建一个demo
- 1.1 安装openai包
- 1.2 demo测试1
- 1.3 demo测试2
- 参考资料
1. 参考Quick start搭建一个demo
1.1 安装openai包
注意关掉科学上网工具,下载openai包
pip install openai
安装好后,按照官网的例子【1】做一个python的小demo
1.2 demo测试1
import os
import openai
openai.organization = "org-bEQmoujGrHZIASU1KzDKIxkL"
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.Model.list()
运行结果如下,似乎这种方式不行…
openai.error.AuthenticationError: No API key provided. You can set your API key in code using 'openai.api_key = <API-KEY>', or you can set the environment variable OPENAI_API_KEY=<API-KEY>). If your API key is stored in a file, you can point the openai module at it with 'openai.api_key_path = <PATH>'. You can generate API keys in the OpenAI web interface. See https://onboard.openai.com for details, or email support@openai.com if you have any questions.
1.3 demo测试2
另一个参考的demo
import openai
openai.organization = 'tsinghua-mechanics' # OpenAI注册的账号名
openai.api_key = 'xxx' # 需要从openAI官网注册后获取def completion(prompt):response = openai.Completion.create(# text-davinci-003 是指它的模型model="text-davinci-003",prompt=prompt,temperature=0.5,max_tokens=1024,n=1,stop=None)message = response.choices[0].textreturn messageprint(completion(input("Write a python code to get the weather of Beijing")))
返回结果,仍然网络错误,可能现在调用比较困难,用的人太多了…
openai.error.APIConnectionError: Error communicating with OpenAI: HTTPSConnectionPool(host='api.openai.com', port=443):
参考资料
【1】OpenAI quickstart
【2】demo2
【3】openAI错误