Skip to content

Instantly share code, notes, and snippets.

@drakedevel
Created April 16, 2024 16:50
Show Gist options
  • Save drakedevel/9a57e35e199ce8849cbdfd5a08ae02b9 to your computer and use it in GitHub Desktop.
Save drakedevel/9a57e35e199ce8849cbdfd5a08ae02b9 to your computer and use it in GitHub Desktop.
Extract prompts/responses from ChatGPT shared conversation pages
import json
import sys
from bs4 import BeautifulSoup
with open(sys.argv[1]) as in_f:
soup = BeautifulSoup(in_f.read(), 'html.parser')
data = json.loads(soup.find('script', {'id': '__NEXT_DATA__'}).text)
for entry in data['props']['pageProps']['serverResponse']['data']['linear_conversation']:
if msg := entry.get('message'):
role = msg['author']['role']
if msg['content']['content_type'] != 'text':
print(f"{role}: [non-text content]")
continue
for part in msg['content']['parts']:
if part:
print(f"{role}: {part!r}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment