summaryrefslogtreecommitdiffstats
path: root/g4f/Provider
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--g4f/Provider/ChatgptX.py49
-rw-r--r--g4f/Provider/Cromicle.py30
-rw-r--r--g4f/Provider/GptForLove.py5
-rw-r--r--g4f/Provider/Yqcloud.py3
-rw-r--r--g4f/Provider/__init__.py2
5 files changed, 62 insertions, 27 deletions
diff --git a/g4f/Provider/ChatgptX.py b/g4f/Provider/ChatgptX.py
index 2d932af2..5621613f 100644
--- a/g4f/Provider/ChatgptX.py
+++ b/g4f/Provider/ChatgptX.py
@@ -1,25 +1,26 @@
from __future__ import annotations
import re
+import json
from aiohttp import ClientSession
-
-from .base_provider import AsyncProvider
+from typing import AsyncGenerator, Dict, List
+from .base_provider import AsyncGeneratorProvider
from .helper import format_prompt
-class ChatgptX(AsyncProvider):
+class ChatgptX(AsyncGeneratorProvider):
url = "https://chatgptx.de"
supports_gpt_35_turbo = True
working = True
@classmethod
- async def create_async(
+ async def create_async_generator(
cls,
model: str,
- messages: list[dict[str, str]],
+ messages: List[Dict[str, str]],
**kwargs
- ) -> str:
+ ) -> AsyncGenerator[str, None]:
headers = {
'accept-language': 'de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US',
'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
@@ -63,8 +64,34 @@ class ChatgptX(AsyncProvider):
}
async with session.post(cls.url + '/sendchat', data=data, headers=headers) as response:
response.raise_for_status()
- data = await response.json()
- if "message" in data:
- return data["message"]
- elif "messages" in data:
- raise RuntimeError(f'Response: {data["messages"]}') \ No newline at end of file
+ chat = await response.json()
+ if "response" not in chat or not chat["response"]:
+ raise RuntimeError(f'Response: {data}')
+ headers = {
+ 'authority': 'chatgptx.de',
+ 'accept': 'text/event-stream',
+ 'referer': f'{cls.url}/',
+ 'x-csrf-token': csrf_token,
+ 'x-requested-with': 'XMLHttpRequest'
+ }
+ data = {
+ "user_id": user_id,
+ "chats_id": chat_id,
+ "prompt": format_prompt(messages),
+ "current_model": "gpt3",
+ "conversions_id": chat["conversions_id"],
+ "ass_conversions_id": chat["ass_conversions_id"],
+ }
+ async with session.get(f'{cls.url}/chats_stream', params=data, headers=headers) as response:
+ response.raise_for_status()
+ async for line in response.content:
+ if line.startswith(b"data: "):
+ row = line[6:-1]
+ if row == b"[DONE]":
+ break
+ try:
+ content = json.loads(row)["choices"][0]["delta"].get("content")
+ except:
+ raise RuntimeError(f"Broken line: {line.decode()}")
+ if content:
+ yield content \ No newline at end of file
diff --git a/g4f/Provider/Cromicle.py b/g4f/Provider/Cromicle.py
index bd29544d..5f521b3e 100644
--- a/g4f/Provider/Cromicle.py
+++ b/g4f/Provider/Cromicle.py
@@ -2,32 +2,32 @@ from __future__ import annotations
from aiohttp import ClientSession
from hashlib import sha256
+from typing import AsyncGenerator, Dict, List
-from ..typing import AsyncGenerator
from .base_provider import AsyncGeneratorProvider
+from .helper import format_prompt
class Cromicle(AsyncGeneratorProvider):
- url = 'https://cromicle.top'
- working = True
- supports_gpt_35_turbo = True
+ url: str = 'https://cromicle.top'
+ working: bool = True
+ supports_gpt_35_turbo: bool = True
@classmethod
async def create_async_generator(
cls,
model: str,
- messages: list[dict[str, str]],
+ messages: List[Dict[str, str]],
proxy: str = None,
**kwargs
- ) -> AsyncGenerator:
- message = messages[-1]["content"]
+ ) -> AsyncGenerator[str, None]:
async with ClientSession(
headers=_create_header()
) as session:
async with session.post(
- cls.url + '/chat',
+ f'{cls.url}/chat',
proxy=proxy,
- json=_create_payload(message, **kwargs)
+ json=_create_payload(format_prompt(messages))
) as response:
response.raise_for_status()
async for stream in response.content.iter_any():
@@ -35,16 +35,16 @@ class Cromicle(AsyncGeneratorProvider):
yield stream.decode()
-def _create_header():
+def _create_header() -> Dict[str, str]:
return {
'accept': '*/*',
'content-type': 'application/json',
}
-def _create_payload(message: str):
+def _create_payload(message: str) -> Dict[str, str]:
return {
- 'message' : message,
- 'token' : 'abc',
- 'hash' : sha256('abc'.encode() + message.encode()).hexdigest()
- }
+ 'message': message,
+ 'token': 'abc',
+ 'hash': sha256('abc'.encode() + message.encode()).hexdigest()
+ } \ No newline at end of file
diff --git a/g4f/Provider/GptForLove.py b/g4f/Provider/GptForLove.py
index 18a42e85..53c403e1 100644
--- a/g4f/Provider/GptForLove.py
+++ b/g4f/Provider/GptForLove.py
@@ -50,7 +50,10 @@ class GptForLove(AsyncGeneratorProvider):
async with session.post("https://api.gptplus.one/chat-process", json=data) as response:
response.raise_for_status()
async for line in response.content:
- line = json.loads(line)
+ try:
+ line = json.loads(line)
+ except:
+ raise RuntimeError(f"Broken line: {line}")
if "detail" in line:
content = line["detail"]["choices"][0]["delta"].get("content")
if content:
diff --git a/g4f/Provider/Yqcloud.py b/g4f/Provider/Yqcloud.py
index ac93315c..74c998e2 100644
--- a/g4f/Provider/Yqcloud.py
+++ b/g4f/Provider/Yqcloud.py
@@ -26,6 +26,9 @@ class Yqcloud(AsyncGeneratorProvider):
response.raise_for_status()
async for stream in response.content.iter_any():
if stream:
+ stream = stream.decode()
+ if "sorry, 您的ip已由于触发防滥用检测而被封禁" in stream:
+ raise RuntimeError("IP address is blocked by abuse detection.")
yield stream.decode()
diff --git a/g4f/Provider/__init__.py b/g4f/Provider/__init__.py
index c7513c35..5b0ec33b 100644
--- a/g4f/Provider/__init__.py
+++ b/g4f/Provider/__init__.py
@@ -15,6 +15,7 @@ from .ChatgptAi import ChatgptAi
from .ChatgptDemo import ChatgptDemo
from .ChatgptDuo import ChatgptDuo
from .ChatgptX import ChatgptX
+from .Cromicle import Cromicle
from .DeepAi import DeepAi
from .FreeGpt import FreeGpt
from .GPTalk import GPTalk
@@ -62,6 +63,7 @@ __all__ = [
'ChatgptDuo',
'ChatgptLogin',
'ChatgptX',
+ 'Cromicle',
'CodeLinkAva',
'DeepAi',
'DfeHub',