0
# ChatGPT Python API
1
2
A Python library for TLS-based ChatGPT API interaction that automatically handles authentication and token management. This library enables developers to programmatically chat with ChatGPT by providing email/password authentication, automatic access token retrieval and refresh, conversation tracking and resumption, proxy support for network routing, and conversation logging capabilities.
3
4
## Package Information
5
6
- **Package Name**: chatgptpy
7
- **Package Type**: pypi
8
- **Language**: Python
9
- **Installation**: `pip install chatgptpy`
10
11
## Core Imports
12
13
```python
14
from pychatgpt import Chat, Options
15
```
16
17
Authentication utilities:
18
19
```python
20
from pychatgpt import OpenAI
21
```
22
23
## Basic Usage
24
25
```python
26
from pychatgpt import Chat, Options
27
28
# Basic chat session
29
chat = Chat(email="your-email@example.com", password="your-password")
30
response, previous_id, conversation_id = chat.ask("Hello, how are you?")
31
print(response)
32
33
# Chat with options for proxy and tracking
34
options = Options()
35
options.proxies = "http://proxy.example.com:8080"
36
options.track = True # Enable conversation tracking
37
options.log = True # Enable console logging
38
39
chat = Chat(
40
email="your-email@example.com",
41
password="your-password",
42
options=options
43
)
44
45
# Interactive CLI session
46
chat.cli_chat()
47
```
48
49
## Architecture
50
51
The library is built around three main components:
52
53
- **Chat Class**: Main interface for ChatGPT conversations with session management, authentication handling, and conversation tracking
54
- **Options Class**: Configuration container for customizing chat behavior including proxies, logging, conversation persistence, and moderation settings
55
- **OpenAI Module**: Authentication and token management utilities that handle the complex TLS-based login flow, token creation, and automatic refresh
56
57
The authentication flow automatically handles OpenAI's multi-step login process including CSRF tokens, state management, captcha solving when required, and access token caching to avoid repeated logins.
58
59
## Capabilities
60
61
### Chat Interface
62
63
Core conversation functionality including message sending, response handling, conversation context management, and interactive CLI sessions.
64
65
```python { .api }
66
class Chat:
67
def __init__(
68
self,
69
email: str,
70
password: str,
71
options: Options or None = None,
72
conversation_id: str or None = None,
73
previous_convo_id: str or None = None
74
): ...
75
76
def ask(
77
self,
78
prompt: str,
79
previous_convo_id: str or None = None,
80
conversation_id: str or None = None,
81
rep_queue: Queue or None = None
82
) -> Tuple[str or None, str or None, str or None] or None: ...
83
84
def cli_chat(self, rep_queue: Queue or None = None) -> None: ...
85
```
86
87
[Chat Interface](./chat-interface.md)
88
89
### Configuration Options
90
91
Session configuration including proxy settings, conversation tracking, logging control, and moderation bypass options.
92
93
```python { .api }
94
class Options:
95
def __init__(self): ...
96
97
log: bool
98
proxies: str or dict or None
99
track: bool or None
100
verify: bool
101
pass_moderation: bool
102
chat_log: str or None
103
id_log: str or None
104
```
105
106
[Configuration](./configuration.md)
107
108
### Authentication Management
109
110
Token management utilities for handling OpenAI authentication, including manual token operations and session validation.
111
112
```python { .api }
113
def token_expired() -> bool: ...
114
def get_access_token() -> Tuple[str or None, str or None]: ...
115
116
class Auth:
117
def __init__(
118
self,
119
email_address: str,
120
password: str,
121
proxy: str = None
122
): ...
123
124
def create_token(self): ...
125
```
126
127
[Authentication](./authentication.md)
128
129
## Types
130
131
```python { .api }
132
from queue import Queue
133
from typing import Tuple
134
135
# Exception classes
136
class PyChatGPTException(Exception):
137
def __init__(self, message: str): ...
138
139
class Auth0Exception(PyChatGPTException):
140
def __init__(self, message: str): ...
141
142
class IPAddressRateLimitException(PyChatGPTException):
143
def __init__(self, message: str): ...
144
```