给出以上代码修改后的完整代码
时间: 2023-07-10 19:04:44 浏览: 63
以下是修改后的完整代码:
```python
import openai
import re
# replace with your own OpenAI API key
openai.api_key = "YOUR_API_KEY_HERE"
# define regex pattern to match prompt endings
prompt_end_pattern = r"[.?!][\s\"\']*$"
# define function to generate response from GPT
def generate_response(prompt):
# remove any trailing punctuation or whitespace from prompt
prompt = re.sub(prompt_end_pattern, "", prompt)
# set GPT parameters
model_engine = "text-davinci-002"
max_tokens = 50
temperature = 0.7
# generate response from GPT
response = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=max_tokens,
temperature=temperature,
n=1,
stop=None,
frequency_penalty=0,
presence_penalty=0
)
# return GPT's response text
return response.choices[0].text.strip()
# define function to start conversation with user
def start_conversation():
# initialize empty prompt and response
prompt = ""
response = ""
# continue conversation until user ends it
while True:
# get user input
user_input = input(prompt + response).strip()
# end conversation if user inputs "bye" or "goodbye"
if user_input.lower() in ["bye", "goodbye"]:
print("Goodbye!")
break
# add user input to prompt
prompt += user_input
# generate response from GPT
response = generate_response(prompt)
# add GPT's response to prompt
prompt += response
# print GPT's response
print(response)
# start conversation with user
start_conversation()
```
请注意,你需要将 `YOUR_API_KEY_HERE` 替换为你自己的 OpenAI API 密钥。此外,你也可以更改默认的 GPT 参数以调整生成的响应的质量和风格。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)