Complete the code to initialize a conversation history list.
conversation_history = [1]We use an empty list [] to store conversation turns in order.
Complete the code to add a user message to the conversation history.
conversation_history.[1]({'role': 'user', 'content': user_input})
pop which removes items.extend which expects an iterable.The append method adds a new item to the end of the list, which is perfect for adding new messages.
Fix the error in the code to retrieve the last message from conversation history.
last_message = conversation_history[1]Using [-1] gets the last item in the list, which is the most recent message.
Fill both blanks to filter messages from the assistant only.
assistant_messages = [msg for msg in conversation_history if msg[1] 'role' [2] 'assistant']
!= which filters out assistant messages.We access the 'role' key with ['role'] and check if it equals 'assistant'.
Fill all three blanks to create a summary dictionary of user messages count and last assistant message.
summary = [1]( 'user_count': sum(1 for msg in conversation_history if msg['role'] [2] 'user'), 'last_assistant': next((msg['content'] for msg in conversation_history if msg['role'] [3] 'assistant'), None) )
list instead of dict for summary.!= which reverses the logic.We use dict to create a dictionary. The == operator checks roles for 'user' and 'assistant'.