XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet
公开
关注 0 Fork 0 Star 1
返回提交历史

XFEstudio/gpt4free

Add ChatgptLogin, ChatgptFree and GptChatly Provider

1e8bbaaa
Heiner Lohaus <heiner@lohaus.eu>
提交于

代码差异

11 个文件 +392 -80
Modified README.md +1 -0
@@ -222,6 +222,7 @@ print(response)
222 222 ##### Providers:
223 223 ```py
224 224 import g4f
225
225 226 from g4f.Provider import (
226 227 AItianhu,
227 228 Acytoo,
Added g4f/Provider/ChatgptFree.py +66 -0
@@ -0,0 +1,66 @@
1 from __future__ import annotations
2
3 import re
4 from aiohttp import ClientSession
5
6 from ..typing import Messages
7 from .base_provider import AsyncProvider
8 from .helper import format_prompt
9
10
11 class ChatgptFree(AsyncProvider):
12 url = "https://chatgptfree.ai"
13 supports_gpt_35_turbo = True
14 working = True
15 _post_id = None
16 _nonce = None
17
18 @classmethod
19 async def create_async(
20 cls,
21 model: str,
22 messages: Messages,
23 proxy: str = None,
24 **kwargs
25 ) -> str:
26 headers = {
27 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
28 "Accept": "*/*",
29 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
30 "Accept-Encoding": "gzip, deflate, br",
31 "Origin": cls.url,
32 "Alt-Used": "chatgptfree.ai",
33 "Connection": "keep-alive",
34 "Referer": f"{cls.url}/",
35 "Sec-Fetch-Dest": "empty",
36 "Sec-Fetch-Mode": "cors",
37 "Sec-Fetch-Site": "same-origin",
38 "Pragma": "no-cache",
39 "Cache-Control": "no-cache",
40 "TE": "trailers"
41 }
42 async with ClientSession(headers=headers) as session:
43 if not cls._nonce:
44 async with session.get(f"{cls.url}/", proxy=proxy) as response:
45 response.raise_for_status()
46 response = await response.text()
47 result = re.search(r'data-post-id="([0-9]+)"', response)
48 if not result:
49 raise RuntimeError("No post id found")
50 cls._post_id = result.group(1)
51 result = re.search(r'data-nonce="(.*?)"', response)
52 if not result:
53 raise RuntimeError("No nonce found")
54 cls._nonce = result.group(1)
55 prompt = format_prompt(messages)
56 data = {
57 "_wpnonce": cls._nonce,
58 "post_id": cls._post_id,
59 "url": cls.url,
60 "action": "wpaicg_chat_shortcode_message",
61 "message": prompt,
62 "bot_id": "0"
63 }
64 async with session.post(cls.url + "/wp-admin/admin-ajax.php", data=data, proxy=proxy) as response:
65 response.raise_for_status()
66 return (await response.json())["data"]
Added g4f/Provider/ChatgptLogin.py +71 -0
@@ -0,0 +1,71 @@
1 from __future__ import annotations
2
3 import re
4 import time
5 import json
6 from aiohttp import ClientSession
7
8 from ..typing import AsyncResult, Messages
9 from .base_provider import AsyncGeneratorProvider
10 from .helper import format_prompt
11
12
13 class ChatgptLogin(AsyncGeneratorProvider):
14 url = "https://chatgptlogin.ai"
15 supports_gpt_35_turbo = True
16 working = True
17 _user_id = None
18
19 @classmethod
20 async def create_async_generator(
21 cls,
22 model: str,
23 messages: Messages,
24 proxy: str = None,
25 **kwargs
26 ) -> AsyncResult:
27 headers = {
28 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
29 "Accept": "*/*",
30 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
31 "Accept-Encoding": "gzip, deflate, br",
32 "Referer": f"{cls.url}/chat/",
33 "Content-Type": "application/json",
34 "Origin": cls.url,
35 "Alt-Used": "chatgptlogin.ai",
36 "Connection": "keep-alive",
37 "Sec-Fetch-Dest": "empty",
38 "Sec-Fetch-Mode": "cors",
39 "Sec-Fetch-Site": "same-origin",
40 "Pragma": "no-cache",
41 "Cache-Control": "no-cache"
42 }
43 async with ClientSession(headers=headers) as session:
44 if not cls._user_id:
45 async with session.get(f"{cls.url}/chat/", proxy=proxy) as response:
46 response.raise_for_status()
47 response = await response.text()
48 result = re.search(r'<div id="USERID" style="display: none">(.*?)<\/div>', response)
49 if not result:
50 raise RuntimeError("No user id found")
51 cls._user_id = result.group(1)
52 async with session.post(f"{cls.url}/chat/new_chat", json={"user_id": cls._user_id}, proxy=proxy) as response:
53 response.raise_for_status()
54 chat_id = (await response.json())["id_"]
55 if not chat_id:
56 raise RuntimeError("Could not create new chat")
57 prompt = format_prompt(messages)
58 data = {
59 "question": prompt,
60 "chat_id": chat_id,
61 "timestamp": int(time.time() * 1e3),
62 }
63 async with session.post(f"{cls.url}/chat/chat_api_stream", json=data, proxy=proxy) as response:
64 response.raise_for_status()
65 async for line in response.content:
66 if line.startswith(b"data: "):
67 content = json.loads(line[6:])["choices"][0]["delta"].get("content")
68 if content:
69 yield content
70 async with session.post(f"{cls.url}/chat/delete_chat", json={"chat_id": chat_id}, proxy=proxy) as response:
71 response.raise_for_status()
Added g4f/Provider/GptChatly.py +49 -0
@@ -0,0 +1,49 @@
1 from __future__ import annotations
2
3 from aiohttp import ClientSession
4
5 from ..typing import Messages
6 from .base_provider import AsyncProvider
7
8
9 class GptChatly(AsyncProvider):
10 url = "https://gptchatly.com"
11 supports_gpt_35_turbo = True
12 supports_gpt_4 = True
13 working = True
14
15 @classmethod
16 async def create_async(
17 cls,
18 model: str,
19 messages: Messages,
20 proxy: str = None,
21 **kwargs
22 ) -> str:
23 if model.startswith("gpt-4"):
24 chat_url = f"{cls.url}/fetch-gpt4-response"
25 else:
26 chat_url = f"{cls.url}/fetch-response"
27 headers = {
28 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
29 "Accept": "*/*",
30 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
31 "Accept-Encoding": "gzip, deflate, br",
32 "Referer": f"{cls.url}/",
33 "Content-Type": "application/json",
34 "Origin": cls.url,
35 "Connection": "keep-alive",
36 "Sec-Fetch-Dest": "empty",
37 "Sec-Fetch-Mode": "cors",
38 "Sec-Fetch-Site": "same-origin",
39 "Pragma": "no-cache",
40 "Cache-Control": "no-cache",
41 "TE": "trailers",
42 }
43 async with ClientSession(headers=headers) as session:
44 data = {
45 "past_conversations": messages
46 }
47 async with session.post(chat_url, json=data, proxy=proxy) as response:
48 response.raise_for_status()
49 return (await response.json())["chatGPTResponse"]
Modified g4f/Provider/__init__.py +8 -1
@@ -14,10 +14,13 @@ from .Chatgpt4Online import Chatgpt4Online
14 14 from .ChatgptAi import ChatgptAi
15 15 from .ChatgptDemo import ChatgptDemo
16 16 from .ChatgptDuo import ChatgptDuo
17 from .ChatgptFree import ChatgptFree
18 from .ChatgptLogin import ChatgptLogin
17 19 from .ChatgptX import ChatgptX
18 20 from .Cromicle import Cromicle
19 21 from .FreeGpt import FreeGpt
20 22 from .GPTalk import GPTalk
23 from .GptChatly import GptChatly
21 24 from .GptForLove import GptForLove
22 25 from .GptGo import GptGo
23 26 from .GptGod import GptGod
@@ -59,6 +62,7 @@ class ProviderUtils:
59 62 'ChatgptAi': ChatgptAi,
60 63 'ChatgptDemo': ChatgptDemo,
61 64 'ChatgptDuo': ChatgptDuo,
65 'ChatgptFree': ChatgptFree,
62 66 'ChatgptLogin': ChatgptLogin,
63 67 'ChatgptX': ChatgptX,
64 68 'CodeLinkAva': CodeLinkAva,
@@ -70,6 +74,7 @@ class ProviderUtils:
70 74 'Forefront': Forefront,
71 75 'FreeGpt': FreeGpt,
72 76 'GPTalk': GPTalk,
77 'GptChatly': GptChatly,
73 78 'GetGpt': GetGpt,
74 79 'GptForLove': GptForLove,
75 80 'GptGo': GptGo,
@@ -121,6 +126,7 @@ __all__ = [
121 126 'ChatgptAi',
122 127 'ChatgptDemo',
123 128 'ChatgptDuo',
129 'ChatgptFree',
124 130 'ChatgptLogin',
125 131 'ChatgptX',
126 132 'Cromicle',
@@ -130,6 +136,7 @@ __all__ = [
130 136 'Forefront',
131 137 'FreeGpt',
132 138 'GPTalk',
139 'GptChatly',
133 140 'GptForLove',
134 141 'GetGpt',
135 142 'GptGo',
@@ -156,4 +163,4 @@ __all__ = [
156 163 'FastGpt',
157 164 'Wuguokai',
158 165 'V50'
159 ]
166 ]
Deleted g4f/Provider/deprecated/ChatgptLogin.py +0 -74
@@ -1,74 +0,0 @@
1 from __future__ import annotations
2
3 import os, re
4 from aiohttp import ClientSession
5
6 from ..base_provider import AsyncProvider, format_prompt
7
8
9 class ChatgptLogin(AsyncProvider):
10 url = "https://opchatgpts.net"
11 supports_gpt_35_turbo = True
12 working = True
13 _nonce = None
14
15 @classmethod
16 async def create_async(
17 cls,
18 model: str,
19 messages: list[dict[str, str]],
20 **kwargs
21 ) -> str:
22 headers = {
23 "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
24 "Accept" : "*/*",
25 "Accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
26 "Origin" : "https://opchatgpts.net",
27 "Alt-Used" : "opchatgpts.net",
28 "Referer" : "https://opchatgpts.net/chatgpt-free-use/",
29 "Sec-Fetch-Dest" : "empty",
30 "Sec-Fetch-Mode" : "cors",
31 "Sec-Fetch-Site" : "same-origin",
32 }
33 async with ClientSession(
34 headers=headers
35 ) as session:
36 if not cls._nonce:
37 async with session.get(
38 "https://opchatgpts.net/chatgpt-free-use/",
39 params={"id": os.urandom(6).hex()},
40 ) as response:
41 result = re.search(r'data-nonce="(.*?)"', await response.text())
42 if not result:
43 raise RuntimeError("No nonce value")
44 cls._nonce = result.group(1)
45 data = {
46 "_wpnonce": cls._nonce,
47 "post_id": 28,
48 "url": "https://opchatgpts.net/chatgpt-free-use",
49 "action": "wpaicg_chat_shortcode_message",
50 "message": format_prompt(messages),
51 "bot_id": 0
52 }
53 async with session.post("https://opchatgpts.net/wp-admin/admin-ajax.php", data=data) as response:
54 response.raise_for_status()
55 data = await response.json()
56 if "data" in data:
57 return data["data"]
58 elif "msg" in data:
59 raise RuntimeError(data["msg"])
60 else:
61 raise RuntimeError(f"Response: {data}")
62
63
64 @classmethod
65 @property
66 def params(cls):
67 params = [
68 ("model", "str"),
69 ("messages", "list[dict[str, str]]"),
70 ("stream", "bool"),
71 ("temperature", "float"),
72 ]
73 param = ", ".join([": ".join(p) for p in params])
74 return f"g4f.provider.{cls.__name__} supports: ({param})"
Modified g4f/Provider/deprecated/Opchatgpts.py +70 -3
@@ -1,7 +1,74 @@
1 1 from __future__ import annotations
2 2
3 from .ChatgptLogin import ChatgptLogin
3 import os, re
4 from aiohttp import ClientSession
4 5
6 from ..base_provider import AsyncProvider, format_prompt
5 7
6 class Opchatgpts(ChatgptLogin):
7 url = "https://opchatgpts.net"
8
9 class Opchatgpts(AsyncProvider):
10 url = "https://opchatgpts.net"
11 supports_gpt_35_turbo = True
12 working = True
13 _nonce = None
14
15 @classmethod
16 async def create_async(
17 cls,
18 model: str,
19 messages: list[dict[str, str]],
20 **kwargs
21 ) -> str:
22 headers = {
23 "User-Agent" : "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
24 "Accept" : "*/*",
25 "Accept-language" : "en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3",
26 "Origin" : "https://opchatgpts.net",
27 "Alt-Used" : "opchatgpts.net",
28 "Referer" : "https://opchatgpts.net/chatgpt-free-use/",
29 "Sec-Fetch-Dest" : "empty",
30 "Sec-Fetch-Mode" : "cors",
31 "Sec-Fetch-Site" : "same-origin",
32 }
33 async with ClientSession(
34 headers=headers
35 ) as session:
36 if not cls._nonce:
37 async with session.get(
38 "https://opchatgpts.net/chatgpt-free-use/",
39 params={"id": os.urandom(6).hex()},
40 ) as response:
41 result = re.search(r'data-nonce="(.*?)"', await response.text())
42 if not result:
43 raise RuntimeError("No nonce value")
44 cls._nonce = result.group(1)
45 data = {
46 "_wpnonce": cls._nonce,
47 "post_id": 28,
48 "url": "https://opchatgpts.net/chatgpt-free-use",
49 "action": "wpaicg_chat_shortcode_message",
50 "message": format_prompt(messages),
51 "bot_id": 0
52 }
53 async with session.post("https://opchatgpts.net/wp-admin/admin-ajax.php", data=data) as response:
54 response.raise_for_status()
55 data = await response.json()
56 if "data" in data:
57 return data["data"]
58 elif "msg" in data:
59 raise RuntimeError(data["msg"])
60 else:
61 raise RuntimeError(f"Response: {data}")
62
63
64 @classmethod
65 @property
66 def params(cls):
67 params = [
68 ("model", "str"),
69 ("messages", "list[dict[str, str]]"),
70 ("stream", "bool"),
71 ("temperature", "float"),
72 ]
73 param = ", ".join([": ".join(p) for p in params])
74 return f"g4f.provider.{cls.__name__} supports: ({param})"
Modified g4f/Provider/deprecated/__init__.py +0 -1
@@ -11,4 +11,3 @@ from .Equing import Equing
11 11 from .Wuguokai import Wuguokai
12 12 from .V50 import V50
13 13 from .FastGpt import FastGpt
14 from .ChatgptLogin import ChatgptLogin
Added g4f/Provider/unfinished/ChatAiGpt.py +64 -0
@@ -0,0 +1,64 @@
1 from __future__ import annotations
2
3 import re
4 from aiohttp import ClientSession
5
6 from ...typing import AsyncResult, Messages
7 from ..base_provider import AsyncGeneratorProvider
8 from ..helper import format_prompt
9
10
11 class ChatAiGpt(AsyncGeneratorProvider):
12 url = "https://chataigpt.org"
13 supports_gpt_35_turbo = True
14 working = True
15 _nonce = None
16 _post_id = None
17
18 @classmethod
19 async def create_async_generator(
20 cls,
21 model: str,
22 messages: Messages,
23 proxy: str = None,
24 **kwargs
25 ) -> AsyncResult:
26 headers = {
27 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
28 "Accept": "*/*",
29 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
30 "Accept-Encoding": "gzip, deflate, br",
31 "Origin": cls.url,
32 "Alt-Used": cls.url,
33 "Connection": "keep-alive",
34 "Referer": cls.url,
35 "Pragma": "no-cache",
36 "Cache-Control": "no-cache",
37 "TE": "trailers",
38 "Sec-Fetch-Dest": "empty",
39 "Sec-Fetch-Mode": "cors",
40 "Sec-Fetch-Site": "same-origin",
41 }
42 async with ClientSession(headers=headers) as session:
43 if not cls._nonce:
44 async with session.get(f"{cls.url}/", proxy=proxy) as response:
45 response.raise_for_status()
46 response = await response.text()
47 result = re.search(r'data-nonce=(.*?) data-post-id=([0-9]+)', response)
48 if not result:
49 raise RuntimeError("No nonce found")
50 cls._nonce, cls._post_id = result.group(1), result.group(2)
51 prompt = format_prompt(messages)
52 data = {
53 "_wpnonce": cls._nonce,
54 "post_id": cls._post_id,
55 "url": cls.url,
56 "action": "wpaicg_chat_shortcode_message",
57 "message": prompt,
58 "bot_id": 0
59 }
60 async with session.post(f"{cls.url}/wp-admin/admin-ajax.php", data=data, proxy=proxy) as response:
61 response.raise_for_status()
62 async for chunk in response.content:
63 if chunk:
64 yield chunk.decode()
Added g4f/Provider/unfinished/TalkAi.py +60 -0
@@ -0,0 +1,60 @@
1 from __future__ import annotations
2
3 from aiohttp import ClientSession
4
5 from ...typing import AsyncResult, Messages
6 from ..base_provider import AsyncGeneratorProvider
7
8
9 class TalkAi(AsyncGeneratorProvider):
10 url = "https://talkai.info"
11 supports_gpt_35_turbo = True
12 working = True
13
14 @classmethod
15 async def create_async_generator(
16 cls,
17 model: str,
18 messages: Messages,
19 proxy: str = None,
20 **kwargs
21 ) -> AsyncResult:
22 if not model:
23 model = "gpt-3.5-turbo"
24 headers = {
25 "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/118.0",
26 "Accept": "application/json",
27 "Accept-Language": "de,en-US;q=0.7,en;q=0.3",
28 "Accept-Encoding": "gzip, deflate, br",
29 "Referer": f"{cls.url}/de/chat/",
30 "content-type": "application/json",
31 "Origin": cls.url,
32 "Connection": "keep-alive",
33 "Sec-Fetch-Dest": "empty",
34 "Sec-Fetch-Mode": "cors",
35 "Sec-Fetch-Site": "same-origin",
36 "Pragma": "no-cache",
37 "Cache-Control": "no-cache"
38 }
39 async with ClientSession(headers=headers) as session:
40 history = [{
41 "content": message["content"],
42 "from": "you" if message["role"] == "user" else "chatGPT"
43 } for message in messages]
44 data = {
45 "type": "chat",
46 "message": messages[-1]["content"],
47 "messagesHistory": history,
48 "model": model,
49 "max_tokens": 256,
50 "temperature": 1,
51 "top_p": 1,
52 "presence_penalty": 0,
53 "frequency_penalty": 0,
54 **kwargs
55 }
56 async with session.post(f"{cls.url}/de/chat/send2/", json=data, proxy=proxy) as response:
57 response.raise_for_status()
58 async for chunk in response.content:
59 if chunk:
60 yield chunk.decode()
Modified g4f/Provider/unfinished/__init__.py +3 -1
@@ -1,3 +1,5 @@
1 1 from .MikuChat import MikuChat
2 2 from .PerplexityAi import PerplexityAi
3 from .Komo import Komo
3 from .Komo import Komo
4 from .TalkAi import TalkAi
5 from .ChatAiGpt import ChatAiGpt