chatgpt
AI
python
访问量:114
2023-04-12 18:08:01.0
无极低码 :https://wheart.cn

以下是使用Python的示例代码,用于调用OpenAI的API:

import openai
from dotenv import load_dotenv
import os

load_dotenv()

openai.api_key = os.getenv('OPENAI_API_KEY')
model_engine = "text-davinci-002"
prompt = "What is the meaning of life?"

# Call the OpenAI API
response = openai.Completion.create(
  engine=model_engine,
  prompt=prompt,
  max_tokens=60,
  n=1,
  stop=None,
  temperature=0.5,
)

# Print the API response
print(response.choices[0].text)

在此代码中,我们使用Python的OpenAI SDK调用API。我们首先使用dotenv模块从环境变量中提供API金钥。接下来,我们设置引擎模型和提示字符串,然后使用openai.Completion.create函数调用API,并传递所需的参数。最后,我们将API响应中的文本字段打印到控制台。请注意,此代码使用了异步调用,但是我们只检索了异步响应的第一个文本输出。你可以根据自己的需求来调整这个代码的细节。


无极低码 :https://wheart.cn
最新文章