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

XFEstudio/gpt4free

Add Messages and AsyncResult typing Add system_message in Yqcloud

6401084f
Heiner Lohaus <heiner@lohaus.eu>
提交于

代码差异

6 个文件 +45 -32
Modified g4f/Provider/ChatgptX.py +4 -4
@@ -4,7 +4,7 @@ import re
4 4 import json
5 5
6 6 from aiohttp import ClientSession
7 from typing import AsyncGenerator, Dict, List
7 from ..typing import AsyncResult, Messages
8 8 from .base_provider import AsyncGeneratorProvider
9 9 from .helper import format_prompt
10 10
@@ -18,9 +18,9 @@ class ChatgptX(AsyncGeneratorProvider):
18 18 async def create_async_generator(
19 19 cls,
20 20 model: str,
21 messages: List[Dict[str, str]],
21 messages: Messages,
22 22 **kwargs
23 ) -> AsyncGenerator[str, None]:
23 ) -> AsyncResult:
24 24 headers = {
25 25 'accept-language': 'de-DE,de;q=0.9,en-DE;q=0.8,en;q=0.7,en-US',
26 26 'sec-ch-ua': '"Google Chrome";v="117", "Not;A=Brand";v="8", "Chromium";v="117"',
@@ -66,7 +66,7 @@ class ChatgptX(AsyncGeneratorProvider):
66 66 response.raise_for_status()
67 67 chat = await response.json()
68 68 if "response" not in chat or not chat["response"]:
69 raise RuntimeError(f'Response: {data}')
69 raise RuntimeError(f'Response: {chat}')
70 70 headers = {
71 71 'authority': 'chatgptx.de',
72 72 'accept': 'text/event-stream',
Modified g4f/Provider/Vitalentum.py +4 -3
@@ -4,7 +4,7 @@ import json
4 4 from aiohttp import ClientSession
5 5
6 6 from .base_provider import AsyncGeneratorProvider
7 from ..typing import AsyncGenerator
7 from ..typing import AsyncResult, Messages
8 8
9 9 class Vitalentum(AsyncGeneratorProvider):
10 10 url = "https://app.vitalentum.io"
@@ -16,10 +16,10 @@ class Vitalentum(AsyncGeneratorProvider):
16 16 async def create_async_generator(
17 17 cls,
18 18 model: str,
19 messages: list[dict[str, str]],
19 messages: Messages,
20 20 proxy: str = None,
21 21 **kwargs
22 ) -> AsyncGenerator:
22 ) -> AsyncResult:
23 23 headers = {
24 24 "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",
25 25 "Accept" : "text/event-stream",
@@ -62,6 +62,7 @@ class Vitalentum(AsyncGeneratorProvider):
62 62 ("model", "str"),
63 63 ("messages", "list[dict[str, str]]"),
64 64 ("stream", "bool"),
65 ("proxy", "str"),
65 66 ("temperature", "float"),
66 67 ]
67 68 param = ", ".join([": ".join(p) for p in params])
Modified g4f/Provider/Ylokh.py +7 -6
@@ -4,7 +4,7 @@ import json
4 4
5 5 from ..requests import StreamSession
6 6 from .base_provider import AsyncGeneratorProvider
7 from ..typing import AsyncGenerator
7 from ..typing import AsyncResult, Messages
8 8
9 9 class Ylokh(AsyncGeneratorProvider):
10 10 url = "https://chat.ylokh.xyz"
@@ -16,16 +16,16 @@ class Ylokh(AsyncGeneratorProvider):
16 16 async def create_async_generator(
17 17 cls,
18 18 model: str,
19 messages: list[dict[str, str]],
19 messages: Messages,
20 20 stream: bool = True,
21 21 proxy: str = None,
22 timeout: int = 30,
22 timeout: int = 120,
23 23 **kwargs
24 ) -> AsyncGenerator:
24 ) -> AsyncResult:
25 25 model = model if model else "gpt-3.5-turbo"
26 26 headers = {
27 "Origin" : cls.url,
28 "Referer" : cls.url + "/",
27 "Origin" : cls.url,
28 "Referer": cls.url + "/",
29 29 }
30 30 data = {
31 31 "messages": messages,
@@ -69,6 +69,7 @@ class Ylokh(AsyncGeneratorProvider):
69 69 ("messages", "list[dict[str, str]]"),
70 70 ("stream", "bool"),
71 71 ("proxy", "str"),
72 ("timeout", "int"),
72 73 ("temperature", "float"),
73 74 ("top_p", "float"),
74 75 ]
Modified g4f/Provider/You.py +7 -6
@@ -3,7 +3,7 @@ from __future__ import annotations
3 3 import json
4 4
5 5 from ..requests import StreamSession
6 from ..typing import AsyncGenerator
6 from ..typing import AsyncGenerator, Messages
7 7 from .base_provider import AsyncGeneratorProvider, format_prompt
8 8
9 9
@@ -17,19 +17,20 @@ class You(AsyncGeneratorProvider):
17 17 async def create_async_generator(
18 18 cls,
19 19 model: str,
20 messages: list[dict[str, str]],
20 messages: Messages,
21 21 proxy: str = None,
22 timeout: int = 30,
22 timeout: int = 120,
23 23 **kwargs,
24 24 ) -> AsyncGenerator:
25 25 async with StreamSession(proxies={"https": proxy}, impersonate="chrome107", timeout=timeout) as session:
26 26 headers = {
27 27 "Accept": "text/event-stream",
28 "Referer": "https://you.com/search?fromSearchBar=true&tbm=youchat",
28 "Referer": f"{cls.url}/search?fromSearchBar=true&tbm=youchat",
29 29 }
30 data = {"q": format_prompt(messages), "domain": "youchat", "chat": ""}
30 31 async with session.get(
31 "https://you.com/api/streamingSearch",
32 params={"q": format_prompt(messages), "domain": "youchat", "chat": ""},
32 f"{cls.url}/api/streamingSearch",
33 params=data,
33 34 headers=headers
34 35 ) as response:
35 36 response.raise_for_status()
Modified g4f/Provider/Yqcloud.py +20 -12
@@ -1,8 +1,9 @@
1 1 from __future__ import annotations
2 2
3 import random
3 4 from aiohttp import ClientSession
4 5
5 from ..typing import AsyncGenerator
6 from ..typing import AsyncResult, Messages
6 7 from .base_provider import AsyncGeneratorProvider, format_prompt
7 8
8 9
@@ -14,22 +15,22 @@ class Yqcloud(AsyncGeneratorProvider):
14 15 @staticmethod
15 16 async def create_async_generator(
16 17 model: str,
17 messages: list[dict[str, str]],
18 messages: Messages,
18 19 proxy: str = None,
19 20 **kwargs,
20 ) -> AsyncGenerator:
21 ) -> AsyncResult:
21 22 async with ClientSession(
22 23 headers=_create_header()
23 24 ) as session:
24 payload = _create_payload(messages)
25 payload = _create_payload(messages, **kwargs)
25 26 async with session.post("https://api.aichatos.cloud/api/generateStream", proxy=proxy, json=payload) as response:
26 27 response.raise_for_status()
27 async for stream in response.content.iter_any():
28 if stream:
29 stream = stream.decode()
30 if "sorry, 您的ip已由于触发防滥用检测而被封禁" in stream:
28 async for chunk in response.content.iter_any():
29 if chunk:
30 chunk = chunk.decode()
31 if "sorry, 您的ip已由于触发防滥用检测而被封禁" in chunk:
31 32 raise RuntimeError("IP address is blocked by abuse detection.")
32 yield stream.decode()
33 yield chunk
33 34
34 35
35 36 def _create_header():
@@ -40,12 +41,19 @@ def _create_header():
40 41 }
41 42
42 43
43 def _create_payload(messages: list[dict[str, str]]):
44 def _create_payload(
45 messages: Messages,
46 system_message: str = "",
47 user_id: int = None,
48 **kwargs
49 ):
50 if not user_id:
51 user_id = random.randint(1690000544336, 2093025544336)
44 52 return {
45 53 "prompt": format_prompt(messages),
46 54 "network": True,
47 "system": "",
55 "system": system_message,
48 56 "withoutContext": False,
49 57 "stream": True,
50 "userId": "#/chat/1693025544336"
58 "userId": f"#/chat/{user_id}"
51 59 }
Modified g4f/typing.py +3 -1
@@ -1,5 +1,5 @@
1 1 import sys
2 from typing import Any, AsyncGenerator, Generator, NewType, Tuple, Union
2 from typing import Any, AsyncGenerator, Generator, NewType, Tuple, Union, List, Dict
3 3
4 4 if sys.version_info >= (3, 8):
5 5 from typing import TypedDict
@@ -8,6 +8,8 @@ else:
8 8
9 9 SHA256 = NewType('sha_256_hash', str)
10 10 CreateResult = Generator[str, None, None]
11 AsyncResult = AsyncGenerator[str]
12 Messages = List[Dict[str, str]]
11 13
12 14 __all__ = [
13 15 'Any',