把这个示例代码融合到你修改的函数中去
时间: 2024-05-16 08:18:25 浏览: 14
好的,以下是将示例代码融合到我的函数中的修改后代码:
```python
import openai
import re
import requests
def generate_response(prompt):
# Define the OpenAI API key
openai.api_key = "YOUR_API_KEY"
# Define the GPT-3 model ID
model_engine = "text-davinci-002"
# Define the prompt for the model
prompt = f"{prompt}\nAI:"
# Define the parameters for the model
completion = openai.Completion.create(
engine=model_engine,
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# Extract the generated response from the model's output
response = completion.choices[0].text
response = re.sub('[^0-9a-zA-Z\n\.\?,!]+', ' ', response).strip()
return response
def chatbot():
print("AI: Hi, I'm a chatbot trained on various topics. How can I assist you today?")
while True:
user_input = input("You: ")
if user_input.lower() == "bye":
print("AI: Goodbye!")
break
prompt = f"User: {user_input}\nAI:"
response = generate_response(prompt)
print(f"AI: {response}")
```
这个修改后的 `chatbot` 函数现在使用 OpenAI 的 GPT-3 模型进行回答生成,而不是之前使用的简单字符替换方法。