给我用python代码运行
时间: 2024-02-28 18:29:52 浏览: 24
抱歉,我无法在这个平台上直接运行代码。但是,你可以在本地安装Python并运行以下代码:
```python
import openai
openai.api_key = "YOUR_API_KEY" # 替换为你的OpenAI API密钥
def ask_question(prompt):
response = openai.Completion.create(
engine="davinci", # 选择使用的GPT模型
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
answer = response.choices[0].text.strip()
return answer
while True:
prompt = input("你:")
if prompt.strip().lower() == "退出":
print("ChitGPT: 再见!")
break
response = ask_question(prompt="你:" + prompt + "\nChitGPT:")
print(response)
```
在运行代码之前,确保已经安装了`openai`包,并替换`YOUR_API_KEY`为你的OpenAI API密钥。然后,你只需要在命令行中运行此脚本即可开始与ChitGPT对话。