~/Library/Messages/chat.db
(at least for macOS Ventura 13.4).
The IMessageChatLoader
loads from this database file.
- Create the
IMessageChatLoader
with the file path pointed tochat.db
database you’d like to process. - Call
loader.load()
(orloader.lazy_load()
) to perform the conversion. Optionally usemerge_chat_runs
to combine message from the same sender in sequence, and/ormap_ai_messages
to convert messages from the specified sender to the “AIMessage” class.
1. Access Chat DB
It’s likely that your terminal is denied access to~/Library/Messages
. To use this class, you can copy the DB to an accessible directory (e.g., Documents) and load from there. Alternatively (and not recommended), you can grant full disk access for your terminal emulator in System Settings > Security and Privacy > Full Disk Access.
We have created an example database you can use at this linked drive file.
2. Create the Chat Loader
Provide the loader with the file path to the zip directory. You can optionally specify the user id that maps to an ai message as well as configure whether to merge message runs.3. Load messages
Theload()
(or lazy_load
) methods return a list of “ChatSessions” that currently just contain a list of messages per loaded conversation. All messages are mapped to “HumanMessage” objects to start.
You can optionally choose to merge message “runs” (consecutive messages from the same sender) and select a sender to represent the “AI”. The fine-tuned LLM will learn to generate these AI messages.
3. Prepare for fine-tuning
Now it’s time to convert our chat messages to OpenAI dictionaries. We can use theconvert_messages_for_finetuning
utility to do so.
4. Fine-tune the model
It’s time to fine-tune the model. Make sure you haveopenai
installed
and have set your OPENAI_API_KEY
appropriately
5. Use in LangChain
You can use the resulting model ID directly theChatOpenAI
model class.