0
# Chat Interface
1
2
Core conversation functionality for interacting with ChatGPT, including message sending, response handling, conversation context management, and interactive CLI sessions.
3
4
## Capabilities
5
6
### Chat Class
7
8
Main conversation handler that manages ChatGPT sessions with authentication, conversation tracking, and session persistence.
9
10
```python { .api }
11
class Chat:
12
"""
13
Main interface for ChatGPT conversations with authentication, session management, and conversation tracking.
14
15
Args:
16
email (str): User's OpenAI account email address
17
password (str): User's OpenAI account password
18
options (Options or None): Configuration options for the chat session
19
conversation_id (str or None): ID to resume an existing conversation
20
previous_convo_id (str or None): Previous conversation context ID
21
"""
22
def __init__(
23
self,
24
email: str,
25
password: str,
26
options: Options or None = None,
27
conversation_id: str or None = None,
28
previous_convo_id: str or None = None
29
): ...
30
```
31
32
### Message Sending
33
34
Send messages to ChatGPT and receive responses with conversation context tracking.
35
36
```python { .api }
37
def ask(
38
self,
39
prompt: str,
40
previous_convo_id: str or None = None,
41
conversation_id: str or None = None,
42
rep_queue: Queue or None = None
43
) -> Tuple[str or None, str or None, str or None] or None:
44
"""
45
Send a message to ChatGPT and get response.
46
47
Args:
48
prompt (str): The message to send to ChatGPT
49
previous_convo_id (str or None): Override previous conversation ID
50
conversation_id (str or None): Override conversation ID
51
rep_queue (Queue or None): Queue to put response for threading
52
53
Returns:
54
Tuple[str or None, str or None, str or None] or None:
55
(response_text, previous_conversation_id, conversation_id) or None if request fails
56
57
Raises:
58
PyChatGPTException: If prompt is invalid or authentication fails
59
"""
60
```
61
62
#### Usage Example
63
64
```python
65
from pychatgpt import Chat
66
67
chat = Chat(email="user@example.com", password="password")
68
69
# Simple question
70
response, prev_id, conv_id = chat.ask("What is Python?")
71
print(f"ChatGPT: {response}")
72
73
# Continue conversation with context
74
response, prev_id, conv_id = chat.ask(
75
"Can you give me an example?",
76
previous_convo_id=prev_id,
77
conversation_id=conv_id
78
)
79
print(f"ChatGPT: {response}")
80
```
81
82
### Interactive CLI Session
83
84
Start an interactive command-line conversation session with ChatGPT.
85
86
```python { .api }
87
def cli_chat(self, rep_queue: Queue or None = None) -> None:
88
"""
89
Start an interactive CLI chat session.
90
91
Args:
92
rep_queue (Queue or None): Queue to put responses for threading
93
94
Returns:
95
None: Method returns None when session ends or if authentication fails
96
97
Note:
98
Type 'exit' to end the session. Conversation will be automatically saved
99
if tracking is enabled in options.
100
101
Raises:
102
PyChatGPTException: If authentication fails or queue is invalid
103
"""
104
```
105
106
#### Usage Example
107
108
```python
109
from pychatgpt import Chat, Options
110
111
options = Options()
112
options.track = True # Save conversation history
113
114
chat = Chat(
115
email="user@example.com",
116
password="password",
117
options=options
118
)
119
120
# Start interactive session
121
chat.cli_chat()
122
123
# User can now type messages and receive responses
124
# Type 'exit' to quit
125
```
126
127
### Data Persistence
128
129
Save conversation data to files when tracking is enabled.
130
131
```python { .api }
132
def save_data(self):
133
"""
134
Save conversation history and IDs to configured log files.
135
136
Only functions when options.track is True. Saves conversation history
137
to chat_log file and conversation IDs to id_log file.
138
139
Raises:
140
PyChatGPTException: If file operations fail
141
"""
142
```
143
144
### Logging
145
146
Control console output for debugging and monitoring.
147
148
```python { .api }
149
def log(self, inout):
150
"""
151
Log message to stderr if logging is enabled.
152
153
Args:
154
inout: Message to log (any type)
155
156
Note:
157
Only outputs if options.log is True
158
"""
159
```
160
161
## Threading Support
162
163
The chat interface supports multi-threaded usage through response queues:
164
165
```python
166
import threading
167
from queue import Queue
168
from pychatgpt import Chat
169
170
def chat_worker(chat, prompt, response_queue):
171
response, prev_id, conv_id = chat.ask(prompt, rep_queue=response_queue)
172
return response, prev_id, conv_id
173
174
# Create chat instance
175
chat = Chat(email="user@example.com", password="password")
176
177
# Use with threading
178
response_queue = Queue()
179
thread = threading.Thread(
180
target=chat_worker,
181
args=(chat, "Hello!", response_queue)
182
)
183
thread.start()
184
185
# Get response from queue
186
prompt, response = response_queue.get()
187
print(f"You: {prompt}")
188
print(f"ChatGPT: {response}")
189
```