将以上代码整合到一起
时间: 2023-11-27 08:51:51 浏览: 146
好的,以下是整合后的代码:
```python
import openai
import json
openai.api_key = "YOUR_API_KEY"
def generate_text(prompt):
response = openai.Completion.create(
engine="davinci",
prompt=prompt,
temperature=0.5,
max_tokens=1024,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
if "choices" in response:
return response.choices[0].text
else:
return ""
def chit_chat(prompt):
response = generate_text(prompt)
return response.strip()
while True:
user_input = input("You: ")
if user_input.lower() == "bye":
print("ChitGPT: Goodbye!")
break
response = chit_chat(user_input)
print("ChitGPT: " + response)
```
请注意替换`YOUR_API_KEY`为您的OpenAI API密钥。
阅读全文