summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhp256 <971748116@qq.com>2023-05-25 04:41:45 +0200
committerhp256 <971748116@qq.com>2023-05-25 04:41:45 +0200
commitb0dee1385d68bfa360a75be6003a73acd30adf38 (patch)
tree4ded94e0eacee474a9c7b347f17f512e7be465a6
parentMerge pull request #598 from enganese/patch-4 (diff)
downloadgpt4free-b0dee1385d68bfa360a75be6003a73acd30adf38.tar
gpt4free-b0dee1385d68bfa360a75be6003a73acd30adf38.tar.gz
gpt4free-b0dee1385d68bfa360a75be6003a73acd30adf38.tar.bz2
gpt4free-b0dee1385d68bfa360a75be6003a73acd30adf38.tar.lz
gpt4free-b0dee1385d68bfa360a75be6003a73acd30adf38.tar.xz
gpt4free-b0dee1385d68bfa360a75be6003a73acd30adf38.tar.zst
gpt4free-b0dee1385d68bfa360a75be6003a73acd30adf38.zip
-rw-r--r--gpt4free/hpgptai/__init__.py37
1 files changed, 28 insertions, 9 deletions
diff --git a/gpt4free/hpgptai/__init__.py b/gpt4free/hpgptai/__init__.py
index c8772a19..f5d1f0ed 100644
--- a/gpt4free/hpgptai/__init__.py
+++ b/gpt4free/hpgptai/__init__.py
@@ -5,20 +5,26 @@
@File :__init__.py.py
@IDE :PyCharm
"""
+import re
import json
-import requests
+import base64
import random
import string
+import requests
+from fake_useragent import UserAgent
+
class ChatCompletion:
@staticmethod
def create(
messages: list,
- context: str="Converse as if you were an AI assistant. Be friendly, creative.",
- restNonce:str="9d6d743bd3",
- proxy:str=None
+ context: str = "Converse as if you were an AI assistant. Be friendly, creative.",
+ restNonce: str = None,
+ proxy: str = None
):
url = "https://chatgptlogin.ac/wp-json/ai-chatbot/v1/chat"
+ if not restNonce:
+ restNonce = ChatCompletion.get_restNonce(proxy)
headers = {
"Content-Type": "application/json",
"X-Wp-Nonce": restNonce
@@ -27,7 +33,7 @@ class ChatCompletion:
data = {
"env": "chatbot",
"session": "N/A",
- "prompt": ChatCompletion.__build_prompt(context,messages),
+ "prompt": ChatCompletion.__build_prompt(context, messages),
"context": context,
"messages": messages,
"newMessage": messages[-1]["content"],
@@ -48,7 +54,6 @@ class ChatCompletion:
return res.json()
return res.text
-
@staticmethod
def randomStr():
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=34))[:11]
@@ -66,12 +71,26 @@ class ChatCompletion:
prompt += '\n' + "AI: "
return prompt
-
+ @classmethod
+ def get_restNonce(cls, proxy: str = None):
+ url = "https://chatgptlogin.ac/"
+ headers = {
+ "Referer": "https://chatgptlogin.ac/",
+ "User-Agent": UserAgent().random
+ }
+ proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
+ res = requests.get(url, headers=headers, proxies=proxies)
+ src = re.search(
+ 'class="mwai-chat mwai-chatgpt">.*<span>Send</span></button></div></div></div> <script defer src="(.*?)">',
+ res.text).group(1)
+ decoded_string = base64.b64decode(src.split(",")[-1]).decode('utf-8')
+ restNonce = re.search(r"let restNonce = '(.*?)';", decoded_string).group(1)
+ return restNonce
class Completion:
@staticmethod
- def create(prompt: str,proxy:str):
+ def create(prompt: str, proxy: str):
messages = [
{
"content": prompt,
@@ -81,4 +100,4 @@ class Completion:
"who": "User: ",
},
]
- return ChatCompletion.create(messages=messages,proxy=proxy) \ No newline at end of file
+ return ChatCompletion.create(messages=messages, proxy=proxy) \ No newline at end of file