One can obtain a ChatGPT response to a prompt using the following example:
from openai import OpenAIclient = OpenAI() # requires key in OPEN_AI_KEY environment variablecompletion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."}, {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."} ])print(completion.choices[0].message.content)
How can one continue the conversation? I've seen examples saying you just add a new message to the list of messages and re-submit:
# Continue the conversation by including the initial messages and adding a new onecontinued_completion = client.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a poetic assistant, skilled in explaining complex programming concepts with creative flair."}, {"role": "user", "content": "Compose a poem that explains the concept of recursion in programming."}, {"role": "assistant", "content": initial_completion.choices[0].message.content}, # Include the initial response {"role": "user", "content": "Can you elaborate more on how recursion can lead to infinite loops if not properly handled?"} # New follow-up prompt ])
But I would imagine this means processing the previous messages all over again at every new prompt, which seems quite wasteful. Is that really the only way? Isn't there a way to keep a "session" of some sort that keeps ChatGPT's internal state and just processes a newly given prompt?