Skip to content

Repository files navigation

ChatGPT Conversation Exporter

A script that fetches conversations from ChatGPT to archive them locally.

Fetch conversations with the fetch command, and convert them to Markdown with convert. See Usage for details.

Configuration

Before running the script, you'll need to acquire the cookie and token for ChatGPT. Open ChatGPT in a browser window and open the browser DevTools.

⚠️ NB: You must be logged in

⚠️ Make sure to keep these values secret!

Script in the console

Run the following in the Console, and paste the output of the command in a file called .env.

fetch(`${new URL(location.href).origin}/api/auth/session`).then((res) => res.json().then((session) => {
    console.log(`export OAI_AUTHZ_TOKEN="${session.accessToken}"\nexport OAI_ACC_ID="${session.account.id}"\nexport OAI_COOKIE="${document.cookie}"`);
}))

Manually

You may alternatively acquire the access token, account ID, and cookie by hand. This can be done via the browser DevTools (Network tab) by inspecting the requests and locating the values for the following (request) headers:

  • authorization: Remove the Bearer prefix, store as OAI_AUTHZ_TOKEN
  • chatgpt-account-id: Store as OAI_ACC_ID
  • cookie: Store as OAI_COOKIE

You may filter by "Fetch/XHR" requests to the path /backend-api/ to find eligible requests.

Save these values in a file called .env in the following format:

export OAI_AUTHZ_TOKEN="<authorization>"
export OAI_ACC_ID="<chatgpt-account-id>"
export OAI_COOKIE="<cookie>"

Requirements

Install the dependencies

pip install -r requirements.txt

Usage

# Print the help
python main.py --help
 Usage: main.py [OPTIONS] COMMAND [ARGS]...

╭─ Options ────────────────────────────────────────────────────────────────────────╮
│ --help          Show this message and exit.                                      │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─ Commands ───────────────────────────────────────────────────────────────────────╮
│ fetch-project   Fetch conversations for a specific project from ChatGPT          │
│ fetch           Fetch conversations from ChatGPT                                 │
│ convert         Convert fetched conversations to Markdown                        │
╰──────────────────────────────────────────────────────────────────────────────────╯

Example

# Source the environment variables with the cookie and token from the Configuration step above
source .env

# Run the script to fetch the conversations
python main.py fetch

# Run the script to convert the conversations from JSON to Markdown
python main.py convert

# If you want to re-fetch or re-convert all the files, run with the `--no-skip-existing` flag
python main.py fetch --no-skip-existing
python main.py convert --no-skip-existing

# If you want to fetch conversations for a specific project, run 'fetch-project'. Note that <project_name> is case-insensitive.
python main.py fetch-project <project_name>

Output files

  • The output files are stored in the directory out/.
  • The command fetch retrieves and saves conversation files as JSON.
  • The command convert converts the retrieved JSON files to Markdown.

Conversations

  • Individual conversation files are named by the conversation's title.
  • Conversations associated with a project are stored in a subdirectory named after the project.
  • Conversations without a project are stored in a subdirectory named recent_conversations.

Meta files

  • A list of projects associated with the ChatGPT account is stored in the file projects.json.
  • A list of conversations associated with a project is stored in a file named <project_name>/conversations_g-p-<project_id>.

Disclaimer

Not affiliated with OpenAI or ChatGPT. This project is provided as-is. You are responsible for your own usage and any violation of terms, agreements or law.

About

A script that fetches conversations from ChatGPT to archive them locally as JSON and Markdown

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Used by

Contributors

Languages