Add a dialog turn to the conversation history. Args: user_query: The user's query assistant_response: The assistant's response Returns: bool: True if successful, False otherwise
(self, user_query: str, assistant_response: str)
| 89 | return all_dialog_turns |
| 90 | |
| 91 | def add_dialog_turn(self, user_query: str, assistant_response: str) -> bool: |
| 92 | """ |
| 93 | Add a dialog turn to the conversation history. |
| 94 | |
| 95 | Args: |
| 96 | user_query: The user's query |
| 97 | assistant_response: The assistant's response |
| 98 | |
| 99 | Returns: |
| 100 | bool: True if successful, False otherwise |
| 101 | """ |
| 102 | try: |
| 103 | # Create a new dialog turn using our custom implementation |
| 104 | dialog_turn = DialogTurn( |
| 105 | id=str(uuid4()), |
| 106 | user_query=UserQuery(query_str=user_query), |
| 107 | assistant_response=AssistantResponse(response_str=assistant_response), |
| 108 | ) |
| 109 | |
| 110 | # Make sure the current_conversation has the append_dialog_turn method |
| 111 | if not hasattr(self.current_conversation, 'append_dialog_turn'): |
| 112 | logger.warning("current_conversation does not have append_dialog_turn method, creating new one") |
| 113 | # Initialize a new conversation if needed |
| 114 | self.current_conversation = CustomConversation() |
| 115 | |
| 116 | # Ensure dialog_turns exists |
| 117 | if not hasattr(self.current_conversation, 'dialog_turns'): |
| 118 | logger.warning("dialog_turns not found, initializing empty list") |
| 119 | self.current_conversation.dialog_turns = [] |
| 120 | |
| 121 | # Safely append the dialog turn |
| 122 | self.current_conversation.dialog_turns.append(dialog_turn) |
| 123 | logger.info(f"Successfully added dialog turn, now have {len(self.current_conversation.dialog_turns)} turns") |
| 124 | return True |
| 125 | |
| 126 | except Exception as e: |
| 127 | logger.error(f"Error adding dialog turn: {str(e)}") |
| 128 | # Try to recover by creating a new conversation |
| 129 | try: |
| 130 | self.current_conversation = CustomConversation() |
| 131 | dialog_turn = DialogTurn( |
| 132 | id=str(uuid4()), |
| 133 | user_query=UserQuery(query_str=user_query), |
| 134 | assistant_response=AssistantResponse(response_str=assistant_response), |
| 135 | ) |
| 136 | self.current_conversation.dialog_turns.append(dialog_turn) |
| 137 | logger.info("Recovered from error by creating new conversation") |
| 138 | return True |
| 139 | except Exception as e2: |
| 140 | logger.error(f"Failed to recover from error: {str(e2)}") |
| 141 | return False |
| 142 | |
| 143 | |
| 144 | from dataclasses import dataclass, field |
no test coverage detected