I wrote up a very small python gpt-3.5-turbo chatbot for you:
- Complete record of conversation,
"chat"
, - passes 5 previous question/answers from chat to the AI so the topic is clear,
- streaming generator, so you receive words as they are created,
- tolerates no API errors
- edit in your own API key to use; type exit to leave
import openai
openai.api_key = "sk-xxxxx"
system = [{"role": "system", "content": "You are a helpful AI assistant."}]
user = [{"role": "user", "content": "Introduce yourself."}]
chat = []
while not user[0]['content'] == "exit":
response = openai.ChatCompletion.create(
messages = system + chat[-10:] + user,
model="gpt-3.5-turbo", stream=True)
reply = ""
for delta in response:
if not delta['choices'][0]['finish_reason']:
word = delta['choices'][0]['delta']['content']
reply += word
print(word, end ="")
chat += user + [{"role": "assistant", "content": reply}]
user = [{"role": "user", "content": input("\nPrompt: ")}]
(revised: the AI provides an introduction so we immediately check connection, and one less conditional.)
The openai library requires python 3.7-3.9. Install the module with
pip install --upgrade openai
The next level is to use a token counter (tiktoken), record the size of each chat message, and use some size criteria to determine how many past messages to send.