Skip to content
This repository has been archived by the owner on Mar 30, 2024. It is now read-only.

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
diezo committed Feb 11, 2024
1 parent a7ed841 commit 8986e08
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 216 deletions.
114 changes: 0 additions & 114 deletions ProGPT/Authentication.py

This file was deleted.

49 changes: 5 additions & 44 deletions ProGPT/Conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,26 @@ class Conversation:

session: Session

session_token: str
access_token: str
conversation_id: str = None
history_and_training_enabled: bool
bearer_token: str
logging: bool

def __init__(
self,
session_token: str,
access_token: str,
history_and_training_enabled: bool = False,
logging: bool = False
) -> None:

self.logging = logging
self.session_token = session_token
self.access_token = access_token
self.history_and_training_enabled = history_and_training_enabled
self.session: Session = requests.Session()

self.session.headers["user-agent"] = "node"

self.refresh_bearer_token()

def refresh_bearer_token(self) -> None:

response: Response = requests.get(
url="https://chat.openai.com/api/auth/session",
headers={
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"accept": "*/*",
"accept-language": "en-US",
"content-type": "application/json",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"cookie": f"__Secure-next-auth.session-token={self.session_token}",
"Referer": "https://chat.openai.com",
"Referrer-Policy": "strict-origin-when-cross-origin"
}
)

try:
response_json: dict = response.json()

if "accessToken" not in response_json:
raise Exception(
"Key 'accessToken' not in response JSON. Most probably you're not "
"logged in. Please check your credentials and try deleting the session file."
)

self.bearer_token = response_json["accessToken"]

except JSONDecodeError:
raise Exception(
"HTTP Response is not a valid JSON. "
"Please check your network connection or switch to another network."
)

def send(self, text: str) -> str:
def prompt(self, text: str) -> str:

body: dict = {
"action": "next",
Expand Down Expand Up @@ -99,7 +60,7 @@ def send(self, text: str) -> str:
headers={
"accept": "text/event-stream",
"accept-language": "en-US",
"authorization": f"Bearer {self.bearer_token}",
"authorization": f"Bearer {self.access_token}",
"content-type": "application/json",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
Expand Down
51 changes: 5 additions & 46 deletions ProGPT/Generative.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,24 @@ class Generative:

session: Session

email: str
password: str

session_token: str
bearer_token: str
access_token: str
history_and_training_enabled: bool
logging: bool

def __init__(
self,
session_token: str,
access_token: str,
history_and_training_enabled: bool = False,
logging: bool = False
) -> None:

self.logging = logging
self.session_token = session_token
self.access_token = access_token
self.history_and_training_enabled = history_and_training_enabled
self.session: Session = requests.Session()

self.session.headers["user-agent"] = "node"

self.refresh_bearer_token()

def refresh_bearer_token(self) -> None:

response: Response = requests.get(
url="https://chat.openai.com/api/auth/session",
headers={
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
"accept": "*/*",
"accept-language": "en-US",
"content-type": "application/json",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"cookie": f"__Secure-next-auth.session-token={self.session_token}",
"Referer": "https://chat.openai.com",
"Referrer-Policy": "strict-origin-when-cross-origin"
}
)

try:
response_json: dict = response.json()

if "accessToken" not in response_json:
raise Exception(
"Key 'accessToken' not in response JSON. Most probably you're not "
"logged in. Please check your credentials and try deleting the session file."
)

self.bearer_token = response_json["accessToken"]

except JSONDecodeError:
raise Exception(
"HTTP Response is not a valid JSON. "
"Please check your network connection or switch to another network."
)

def prompt(self, text: str) -> str:

body: dict = {
Expand Down Expand Up @@ -98,7 +57,7 @@ def prompt(self, text: str) -> str:
headers={
"accept": "text/event-stream",
"accept-language": "en-US",
"authorization": f"Bearer {self.bearer_token}",
"authorization": f"Bearer {self.access_token}",
"content-type": "application/json",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
Expand Down
1 change: 0 additions & 1 deletion ProGPT/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from .Generative import Generative
from .Conversation import Conversation
from .Authentication import Authentication
20 changes: 9 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ Both **Generative** & **Conversation** modes are supported.
$ pip install progpt
```

### How to get *session_token*?
On your computer:
1. Open [**chat.openai.com**](https://chat.openai.com) and login
2. Open DevTools by pressing **F12**
3. Open **Application** tab
4. Under **Cookies**, tap **https://chat.openai.com**
5. From the list of cookies, copy value of **__Secure-next-auth.session-token**
### How to get *access_token*?
In your browser:
1. Login to [**chat.openai.com**](https://chat.openai.com)
2. Open [**this page**](https://chat.openai.com/api/auth/session), and you'll see **JSON** data
3. Copy value of **accessToken**


### Generative Mode
Expand All @@ -32,7 +30,7 @@ Answers individual prompts, doesn't remember past messages.
```python
from ProGPT import Generative

bot = Generative(session_token)
bot = Generative(access_token)

print(bot.prompt("who invented electricity?"))
```
Expand All @@ -43,10 +41,10 @@ Creates a conversation thread and remembers your chat history.
```python
from ProGPT import Conversation

bot = Conversation(session_token)
bot = Conversation(access_token)

print(bot.send("hello"))
print(bot.send("how are you?"))
print(bot.prompt("hello"))
print(bot.prompt("how are you?"))
```

## Rate Limits
Expand Down

0 comments on commit 8986e08

Please sign in to comment.