如何调用openai的接口? 请用python给出demo,在已经有api key的情况下。并且能使用代理。
时间: 2023-03-26 07:00:58 浏览: 96
你可以使用以下代码调用 OpenAI 的接口:
```
import openai
import requests
# 设置 API 密钥
openai.api_key = "YOUR_API_KEY"
# 设置代理
proxies = {
"http": "http://YOUR_PROXY_ADDRESS",
"https": "https://YOUR_PROXY_ADDRESS",
}
# 调用 OpenAI 的接口
def generate_text(prompt):
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=.5,
)
text = response.choices[].text
return text.strip()
# 测试调用接口
prompt = "Hello, my name is"
text = generate_text(prompt)
print(text)
```
请将 `YOUR_API_KEY` 替换为你的 OpenAI API 密钥,将 `YOUR_PROXY_ADDRESS` 替换为你的代理地址。然后,你可以调用 `generate_text` 函数来生成文本。