from chatgpt_wrapper import ChatGPT from imessage_tools import read_messages, print_messages, send_message import time bot = ChatGPT()
def user_inputs(): #ask the user for what phone number they want to send/read messages from phone_number = input("What phone number would you like to send/read messages from? (format: +12223334444): ") #ask the user for the path of their iMessage database chat_db = input("What is the path to your iMessage database? (/Users/userName/Messages/chatDB): ") #ask the user for the name of the person they are texting person = input("What is the name of the person you are texting? (format: FIRSTNAME): ")
#default values for testing, uncomment the below 3 lines and comment out the above 3 lines to use the default values`` chat_db = "/Users/kellygold/Library/Messages/chat.db" phone_number = "+12223334444" person = "Albert"
def get_recent_messages(phone_number, chat_db, person): # Phone number or label for "you" self_number = "Me" # Number of messages to return n = 15 # Read the messages messages = read_messages(chat_db, n=n, self_number=self_number, human_readable_date=True) # Filter messages where phone_number is phone_number messages = [message for message in messages if message['phone_number'] == phone_number]
#if is_from_me is true, then set messages.sender to "ME: " and if is_from_me is false, then set messages.sender to "THEM: " for message in messages: if message['is_from_me'] == True: message['sender'] = " ME: " else: message['sender'] = person + ": " return messages
在这个例子中,我给 ChatGPT 一个带有提示的单行命令。后跟包含消息历史记录的单个字符串。我传递给 ChatGPT 的字符串看起来像ME: nah, not really you? Want to go skiing? THEM: Hey got any plans this weekend? ME:.
为我写下以下短信消息对话的回复。回复必须少于 280 个字符,因此要非常简洁。不要在你的回复中使用引号。不要使用问候语。对话发生在我(KELLY)和一个朋友(“+ person +”)之间。不要使用 KELLY 或“+person+”这样的词。来自 KELLY 的消息以“KELLY:”开头,来自“+person+”的消息以““+person+””开头:最旧的消息在最后。最新消息排在最前面。回复的语气应该是友好的、支持的和鼓励的。风格应该是休闲和非正式的。如果需要,您可以提出 1 或 2 个跟进问题。该消息应该从最近的消息继续并且是非正式的。最新消息包含相关主题。选择相关主题。写下对以下对话的回复......
这是将提示与您的消息结合起来的代码
def build_prompt(messages, person): prompt = "" promptPrefix = "Write a reply for me to the following text message message conversation. The reply must be less than 280 characters so be very concise. Do not use quotations in your resposne. Do not use a greeting. The conversation is between me (KELLY) and a friend (" + person + "). Do not use words KELLY or "+person+". Messages from KELLY begin 'KELLY: ' and messages from "+person+" begin with '"+person+"': .The oldest messages are last. The most recent messages are first. The tone in the reply should be friendly, supportive, and encouraging. The style should be casual and informal. You may ask 1 or 2 follow up quetsions if needed. The message should continue from the most recent messages and be imformal. The most recent messages contain relevant topics. Choose relevant topics. Write a reply to the following conversation....." promptSuffix = "" for message in messages: promptSuffix += message['sender'] + message['body'] prompt = promptPrefix + promptSuffix #print(prompt) return prompt
7.自动化
要让脚本无限期运行,请将除用户输入之外的所有函数包装到一个 while 循环中,并添加一些逻辑来检查时间间隔并检查最后一个发送者是否是我。我们不要在人们回复之前向他们发送垃圾邮件
from chatgpt_wrapper import ChatGPT from imessage_tools import read_messages, print_messages, send_message import time bot = ChatGPT()
def user_inputs(): #ask the user for what phone number they want to send/read messages from phone_number = input("What phone number would you like to send/read messages from? (format: +12223334444): ") #ask the user for the path of their iMessage database chat_db = input("What is the path to your iMessage database? (/Users/userName/Messages/chatDB): ") #ask the user for the name of the person they are texting person = input("What is the name of the person you are texting? (format: FIRSTNAME): ")
#default values for testing, uncomment the below 3 lines and comment out the above 3 lines to use the default values``
chat_db = "/Users/userName/Library/Messages/chat.db" phone_number = "+13034768549" person = "Albert"
while True: def get_recent_messages(phone_number, chat_db, person): # Phone number or label for "you" self_number = "Me" # Number of messages to return n = 15 # Read the messages messages = read_messages(chat_db, n=n, self_number=self_number, human_readable_date=True) # Filter messages where phone_number is phone_number messages = [message for message in messages if message['phone_number'] == phone_number]
#if is_from_me is true, then set messages.sender to "ME: " and if is_from_me is false, then set messages.sender to "THEM: " for message in messages: if message['is_from_me'] == True: message['sender'] = " ME: " else: message['sender'] = person + ": " return messages
def build_prompt(messages, person): prompt = "" promptPrefix = "Write a reply for me to the following text message message conversation. The reply must be less than 280 characters so be very concise. Do not use quotations in your resposne. Do not use a greeting. The conversation is between me (KELLY) and a friend (" + person + "). Do not use words KELLY or "+person+". Messages from KELLY begin 'KELLY: ' and messages from "+person+" begin with '"+person+"': .The oldest messages are last. The most recent messages are first. The tone in the reply should be friendly, supportive, and encouraging. The style should be casual and informal. You may ask 1 or 2 follow up quetsions if needed. The message should continue from the most recent messages and be imformal. The most recent messages contain relevant topics. Choose relevant topics. Write a reply to the following conversation....." promptSuffix = "" for message in messages: promptSuffix += message['sender'] + message['body'] prompt = promptPrefix + promptSuffix #print(prompt) return prompt
recent_messages_for_number = get_recent_messages(dynamicData[0], dynamicData[1], dynamicData[2]) if check_last_sender(recent_messages_for_number) == " ME: ": print("I am the last sender, so I will not respond to myself.") time.sleep(25) continue else: chatGPT_Prompt = build_prompt(recent_messages_for_number, dynamicData[2]) chatGPT_response = ask_chatGPT(chatGPT_Prompt) sender(dynamicData[0], chatGPT_response) time.sleep(25)