Artificial Intelligence 14 min read

Calling Your Private GPTs from ChatGPT with 50 Lines of Python Code

This tutorial shows how, with about 50 lines of Python using Playwright and pyperclip, you can automate a persistent Firefox session to log into ChatGPT Plus, navigate to your private GPT URL, send prompts and retrieve responses via the clipboard, all without incurring extra API fees.

Tencent Cloud Developer
Tencent Cloud Developer
Tencent Cloud Developer
Calling Your Private GPTs from ChatGPT with 50 Lines of Python Code

This tutorial demonstrates how to programmatically call your private GPTs created in ChatGPT using approximately 50 lines of Python code, without additional API costs.

Prerequisites:

A ChatGPT Plus membership account

Python 3.7+ installed locally

Network access to ChatGPT

Required Libraries:

Install pyperclip and playwright:

pip install pyperclip
pip install playwright && playwright install

Key Concepts:

The solution uses Playwright (a Microsoft-developed automation framework) to control the browser and pyperclip to access clipboard data. The approach leverages persistent browser context to maintain login sessions without repeated authentication.

Code Structure:

1. Persistent Login Setup:

import time
import pyperclip
from playwright.sync_api import sync_playwright
def login():
with sync_playwright() as p:
context = p.firefox.launch_persistent_context(
'./gpts_firefox_dir',
headless=False,
slow_mo=500
)
page = context.pages[0]
page.goto('https://chat.openai.com/')
login()

2. Query GPTs Implementation:

...
def query():
with sync_playwright() as p:
browser = p.firefox.launch_persistent_context(
'./gpts_firefox_dir',
headless=False,
slow_mo=500
)
page = browser.pages[0]
# This is private, replace with your own GPTs URL
page.goto('https://chat.openai.com/g/g-OitD1zCwT-story-weaver')
time.sleep(2)
page.query_selector("#prompt-textarea").fill("Tell me, what is your knowledge base cutoff date?")
page.wait_for_selector('button[data-testid="send-button"]').click()
time.sleep(10)
copy_button_selector = 'button.text-xs.dark\:hover\:text-gray-200'
copy_buttons = page.query_selector_all(copy_button_selector)
(copy_buttons[len(copy_buttons)-1]).click()
time.sleep(1)
clipboard_content = pyperclip.paste()
# Print clipboard content
print(clipboard_content)
str = 'y'
while str == 'y':
str = input('Waiting, continue waiting? y/n.\n\nPlease input: ')  # Control sleep time
page.close()
browser.close()
query()

Important Notes:

The author recommends using Firefox instead of Chromium for web robot development

Use playwright codegen tool to generate element selectors for complex pages

Replace the GPTs URL with your own created GPTs address

This method avoids additional API calling fees by leveraging the Plus membership

PythonAutomationChatGPTweb scrapingGPTsBrowser AutomationPlaywright
Tencent Cloud Developer
Written by

Tencent Cloud Developer

Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.