Skip to content
This repository was archived by the owner on Jan 25, 2023. It is now read-only.

Commit 96232c1

Browse files
committed
First commit
1 parent 4d6228f commit 96232c1

File tree

10 files changed

+150
-29
lines changed

10 files changed

+150
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Custom
22
.dccache
3+
config.json
34

45
# Byte-compiled / optimized / DLL files
56
__pycache__/
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
from .package_name import *
4+
from .chatgpt import *
55

66
__version__ = '0.0.1b0'

chatgpt/chatgpt.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from uuid import uuid4 as uuid
5+
import json
6+
import requests
7+
from .errors import CallError
8+
9+
10+
class Conversation:
11+
DEFAULT_MODEL_NAME = 'text-davinci-002-render'
12+
13+
def __init__(
14+
self,
15+
model_name: str = None,
16+
conversation_id: str = None,
17+
parent_message_id: str = None,
18+
access_token: str = None,
19+
):
20+
self.load_config()
21+
22+
self._model_name = model_name or self.DEFAULT_MODEL_NAME
23+
24+
if self._access_token is None:
25+
self._access_token = access_token
26+
27+
if self._conversation_id is None:
28+
self._conversation_id = conversation_id
29+
30+
if self._first_parent_message_id is None:
31+
self._first_parent_message_id = parent_message_id
32+
self._message_id = parent_message_id
33+
34+
def load_config(self, config_path: str = None):
35+
if config_path is None:
36+
config_path = 'config.json'
37+
38+
with open(config_path, 'r') as f:
39+
config = json.load(f)
40+
41+
self._access_token = config['access_token']
42+
self._conversation_id = config['conversation_id']
43+
self._first_parent_message_id = config['parent_message_id']
44+
45+
def chat(self, message: str):
46+
self._parent_message_id = self._message_id
47+
self._message_id = str(uuid())
48+
49+
url = "https://chat.openai.com/backend-api/conversation"
50+
51+
payload = json.dumps(
52+
{
53+
"action": "next",
54+
"messages": [
55+
{
56+
"id": self._message_id,
57+
"role": "user",
58+
"content": {
59+
"content_type": "text",
60+
"parts": [message]
61+
}
62+
}
63+
],
64+
"conversation_id": self._conversation_id,
65+
"parent_message_id": self._parent_message_id,
66+
"model": self._model_name
67+
}
68+
)
69+
headers = {
70+
'Authorization': f'Bearer {self._access_token}',
71+
}
72+
73+
response = requests.request("POST", url, headers=headers, data=payload)
74+
payload = response.text
75+
76+
try:
77+
last_item = payload.split(('data:'))[-2]
78+
text_items = json.loads(last_item)['message']['content']['parts']
79+
text = '\n'.join(text_items)
80+
postprocessed_text = text.replace(r'\n+', '\n')
81+
return postprocessed_text
82+
except IndexError:
83+
error_message = json.loads(payload)['detail']
84+
raise CallError(error_message)
85+
86+
def reset(self):
87+
self._message_id = self._first_parent_message_id

chatgpt/errors.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class CallError(Exception):
2+
3+
def __init__(self, message):
4+
super().__init__(message)

chatgpt/interceptor.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
const textareas = document.getElementsByTagName('textarea');
2+
const lastTextarea = textareas[textareas.length - 1];
3+
4+
let buttons = document.getElementsByTagName('button');
5+
buttons = Array.prototype.slice.call(buttons);
6+
const lastButton = buttons[buttons.length - 1];
7+
8+
lastTextarea.value = 'Hello'
9+
lastButton.click();
10+
11+
var interval = setInterval(function() {
12+
if (lastButton.disabled === false) {
13+
clearInterval(interval);
14+
lastTextarea.value = "Let's start"
15+
lastButton.click();
16+
}
17+
}, 100);
18+
19+
function fetchInterceptor(fetch) {
20+
return function(...args) {
21+
const body = JSON.parse(args[1]?.body);
22+
const headers = args[1]?.headers;
23+
24+
const accessToken = headers?.Authorization.split('Bearer')[1].trim()
25+
const conversationId = body?.conversation_id
26+
const parentMessageId = body?.parent_message_id
27+
28+
if (accessToken && conversationId && parentMessageId) {
29+
const config = {
30+
access_token: accessToken,
31+
conversation_id: body.conversation_id,
32+
parent_message_id: body.parent_message_id,
33+
};
34+
console.log(JSON.stringify(config));
35+
}
36+
37+
return fetch(...args);
38+
};
39+
}
40+
window.fetch = fetchInterceptor(window.fetch);

chatgpt/package_name/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
from .chatgpt import *
5+
6+
__version__ = '0.0.1b0'
File renamed without changes.

playground/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from chatgpt import Conversation
2+
3+
conversation = Conversation()
4+
print(conversation.chat("Hola."))

rename.sh

Lines changed: 0 additions & 21 deletions
This file was deleted.

setup.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33

44
from setuptools import setup
55
from setuptools import find_packages
6-
import package_name
6+
import chatgpt
77

88
setup(
9-
name='package_name',
10-
version=package_name.__version__,
11-
description='description_value',
12-
url='url_value',
13-
author='author_value',
14-
author_email='email_value',
9+
name='chatgpt',
10+
version=chatgpt.__version__,
11+
description='',
12+
url='https://github.com/brunneis/chatgpt-python',
13+
author='Rodrigo Martínez Castaño',
14+
author_email='rodrigo@martinez.gal',
1515
license='GNU General Public License v3 (GPLv3)',
1616
packages=find_packages(),
1717
zip_safe=False,

0 commit comments

Comments
 (0)