代码示例
时间: 2023-07-02 17:10:07 浏览: 35
以下是一个简单的 Python 代码示例,使用 ChitGPT 进行对话:
```python
import openai
import time
openai.api_key = "your_api_key"
def ask_chitgpt(prompt):
try:
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
max_tokens=60,
n=1,
stop=None,
temperature=0.5,
)
message = response.choices[0].text.strip()
return message
except Exception as e:
print(e)
return "Sorry, I couldn't understand you."
print("Hello, I am ChitGPT. How can I assist you today?")
while True:
user_input = input("> ")
if user_input.lower() in ["exit", "quit"]:
break
response = ask_chitgpt(user_input)
print(response)
time.sleep(1)
```
您需要将 `your_api_key` 替换为您的 OpenAI API 密钥。此示例使用 OpenAI 的 `davinci` 引擎,`max_tokens` 为 60,表示每次生成的回复最多为 60 个 token。`temperature` 为 0.5,表示生成回复时使用的随机度量。您可以根据需要更改这些参数。