到ChatGPT官网 登录
保存这个key
然后去google搜索colab
pip install openai
import openaiAPI_KEY = '你的OpenAIkey'
openai.api_key = API_KEY
model_id = 'gpt-3.5-turbo'def ChatGPT_conversation(conversation):response = openai.ChatCompletion.create(model=model_id,messages=conversation)# api_usage = response['usage']# print('Total token consumed: {0}'.format(api_usage['total_tokens']))# stop means complete# print(response['choices'][0].finish_reason)# print(response['choices'][0].index)conversation.append({'role': response.choices[0].message.role, 'content': response.choices[0].message.content})return conversationconversation = []
conversation.append({'role': 'system', 'content': 'How may I help you?'})
conversation = ChatGPT_conversation(conversation)
print('{0}: {1}\n'.format(conversation[-1]['role'].strip(), conversation[-1]['content'].strip()))while True:prompt = input('User:')conversation.append({'role': 'user', 'content': prompt})conversation = ChatGPT_conversation(conversation)print('{0}: {1}\n'.format(conversation[-1]['role'].strip(), conversation[-1]['content'].strip()))
直接使用chatBox
gitHub-chatBox