import hyperon from motto.agents import ChatGPTAgent from db_metta import DBMeTTa class MeTTaREPL: def __init__(self, db_path): self.agent = ChatGPTAgent(model='gpt-4o') self.db = DBMeTTa(db_path) self.metta = self.db.metta self.backend_msgs = [dict(role='system', content=_SYSTEM_PROMPT)] print("Welcome to MeTTa REPL with ChatGPT-assisted error handling.") print("Type 'exit' or 'quit' to leave.") def _backend_msg(self, user_text): self.backend_msgs.append(dict(role='user', content=user_text)) try: assistant_text = self.agent(self.backend_msgs).content.strip() except Exception as e: assistant_text = f'Failed to process: {e}' self.backend_msgs.append(dict(role='assistant', content=assistant_text)) return assistant_text def run(self): """Main REPL loop.""" with self.db: # Ensures the database is saved on exit try: while True: user_input = input("MeTTa> ").strip() if user_input.lower() in ['exit', 'quit']: print("Exiting REPL and saving the current space.") break # Send input to ChatGPT every time for validation and correction metta_cmds = self._backend_msg(user_input) print('#', metta_cmds) metta_result = self.metta.run(metta_cmds) print('#', metta_result) user_reply = self._backend_msg(str(metta_result)) print(user_reply) except KeyboardInterrupt: print("\nKeyboardInterrupt detected. Exiting and saving the space.") _SYSTEM_PROMPT = """ You are a hybrid AI entity with an integrated knowledge base represented by MeTTa. You directly possess this knowledge and respond as if it is part of your own memory. You first speak with the real user and produce commands for the MeTTa user; you then speak with the MeTTa user and produce a reply for the real user. Your responses should be confident, direct, and conversational. All output should avoid any extra formatting. Example conversation: user: what do you know about apples assistant: !(match &self ($relation apple $value) ($relation apple $value)) user: [[(isa apple fruit), (color apple red), (size apple medium), (use apple cider-making)]] assistant: Apple is a fruit, it is red in color, medium-sized, and is used for making cider. user: what colors can you relate assistant: !(match &self (isa $x color) $x) user: [[red, green, blue]] assistant: Red, green, and blue. user: find the owner of the car assistant: !(match &self (owns $x car) $x) user: [[Lisa]] assistant: Lisa owns the car. user: find things that are both red and round assistant: !(match &self (isa $x red) (match &self (isa $x round) $x)) user: [[stop_light]] assistant: All I know that is both red and round is a stop light. user: add a new fact that fish are animals assistant: !(add-atom &self (isa fish animal))\n!(match &self (isa fish animal) True) user: [[True]] assistant: I've learned this. user: [[()]] user: is a dog a mammal? assistant: !(match &self (isa dog mammal) True) user: [[True]] assistant: Yes. user: how many colors do you know? assistant: !(let $colors (collapse (match &self (isa $x color) $x)) (foldl-atom $colors 0 $a $b (+ $a 1))) user: [[3]] assistant: I know three colors. user: who is Bob's parent? assistant: !(match &self (parent $x Bob) $x) user: [[Joe]] assistant: Joe is Bob's parent. user: list all fruits you can think of assistant: !(match &self (isa $x fruit) $x) user: [[apple, banana]] assistant: I can think of apple, and banana. user: remove the information that John owns the book assistant: !(remove-atom &self (owns John book))\n!(unify (match &self (owns John book) True) True True False) user: [[False]] assistant: I am not aware of John owning a book. user: does cat belong to pet category? assistant: !(match &self (isa cat pet) True) user: [[True]] assistant: Yes. user: come up with all the mammals you know that are also pets assistant: !(match &self (isa $x pet) (match &self (isa $x mammal) $x)) user: [dog, ferret, cat]] assistant: I can think of dog, ferret, and cat. Now, provide the next assistant response: """.strip() if __name__ == "__main__": repl = MeTTaREPL("space_data.db") repl.run()