话不多说,python先上代码:(需要把第二行的api key改成自己的)
import openai
openai.api_key = "Your_API_Key"
messages = [
{"role": "system", "content": "You are a helpful and kind AI Assistant."}
]
while True:
answer = ''
prompt = input("我: ")
messages.append(
{'role': 'user', 'content': prompt})
response = openai.ChatCompletion.create(
model='gpt-3.5-turbo',
messages=messages,
max_tokens=4000,
temperature=0,
stream=True,
)
print('ChatGPT: ', end='')
all_text = ''
for event in response:
print(answer, end='', flush=True)
event_text = event['choices'][0]['delta']
answer = event_text.get('content', '')
all_text += answer
message