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

XFEstudio/gpt4free

AItianhuSpace Provider with GPT 4 added Reduced chunksize to better text completion

72c3ff7a
Heiner Lohaus <heiner@lohaus.eu>
提交于

代码差异

3 个文件 +50 -18
Modified g4f/Provider/Vercel.py +17 -13
@@ -18,7 +18,13 @@ class Vercel(BaseProvider):
18 18 def create_completion(
19 19 model: str,
20 20 messages: list[dict[str, str]],
21 stream: bool, **kwargs ) -> CreateResult:
21 stream: bool,
22 **kwargs
23 ) -> CreateResult:
24 if not model:
25 model = "gpt-3.5-turbo"
26 elif model not in model_info:
27 raise ValueError(f"Model are not supported: {model}")
22 28
23 29 headers = {
24 30 'authority' : 'sdk.vercel.ai',
@@ -26,7 +32,7 @@ class Vercel(BaseProvider):
26 32 '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',
27 33 'cache-control' : 'no-cache',
28 34 'content-type' : 'application/json',
29 'custom-encoding' : AntiBotToken(),
35 'custom-encoding' : get_anti_bot_token(),
30 36 'origin' : 'https://sdk.vercel.ai',
31 37 'pragma' : 'no-cache',
32 38 'referer' : 'https://sdk.vercel.ai/',
@@ -48,22 +54,20 @@ class Vercel(BaseProvider):
48 54 'playgroundId': str(uuid.uuid4()),
49 55 'chatIndex' : 0} | model_info[model]['default_params']
50 56
51 server_error = True
52 retries = 0
53 57 max_retries = kwargs.get('max_retries', 20)
54
55 while server_error and not retries > max_retries:
58 for i in range(max_retries):
56 59 response = requests.post('https://sdk.vercel.ai/api/generate',
57 60 headers=headers, json=json_data, stream=True)
61 try:
62 response.raise_for_status()
63 except:
64 continue
65 for token in response.iter_content(chunk_size=8):
66 yield token.decode()
67 break
58 68
59 for token in response.iter_content(chunk_size=2046):
60 if token != b'Internal Server Error':
61 server_error = False
62 yield (token.decode())
63
64 retries += 1
65 69
66 def AntiBotToken() -> str:
70 def get_anti_bot_token() -> str:
67 71 headers = {
68 72 'authority' : 'sdk.vercel.ai',
69 73 'accept' : '*/*',
Modified g4f/Provider/base_provider.py +29 -2
@@ -1,7 +1,9 @@
1 1 from __future__ import annotations
2 2
3 3 import asyncio
4 from asyncio import SelectorEventLoop
4 import functools
5 from asyncio import SelectorEventLoop, AbstractEventLoop
6 from concurrent.futures import ThreadPoolExecutor
5 7 from abc import ABC, abstractmethod
6 8
7 9 import browser_cookie3
@@ -27,6 +29,31 @@ class BaseProvider(ABC):
27 29 ) -> CreateResult:
28 30 raise NotImplementedError()
29 31
32 @classmethod
33 async def create_async(
34 cls,
35 model: str,
36 messages: list[dict[str, str]],
37 *,
38 loop: AbstractEventLoop = None,
39 executor: ThreadPoolExecutor = None,
40 **kwargs
41 ) -> str:
42 if not loop:
43 loop = asyncio.get_event_loop()
44
45 partial_func = functools.partial(
46 cls.create_completion,
47 model,
48 messages,
49 False,
50 **kwargs
51 )
52 response = await loop.run_in_executor(
53 executor,
54 partial_func
55 )
56 return "".join(response)
30 57
31 58 @classmethod
32 59 @property
@@ -127,7 +154,7 @@ def create_event_loop() -> SelectorEventLoop:
127 154 except RuntimeError:
128 155 return SelectorEventLoop()
129 156 raise RuntimeError(
130 'Use "create_async" instead of "create" function in a async loop.')
157 'Use "create_async" instead of "create" function in a running event loop.')
131 158
132 159
133 160 _cookies = {}
Modified g4f/models.py +4 -3
@@ -17,6 +17,7 @@ from .Provider import (
17 17 Wewordle,
18 18 Yqcloud,
19 19 AItianhu,
20 AItianhuSpace,
20 21 Aichat,
21 22 Myshell,
22 23 )
@@ -38,7 +39,7 @@ default = Model(
38 39 Wewordle, # Responds with markdown
39 40 Yqcloud, # Answers short questions in chinese
40 41 ChatBase, # Don't want to answer creatively
41 DeepAi, ChatgptLogin, ChatgptAi, Aivvm, GptGo, AItianhu, Aichat, Myshell,
42 DeepAi, ChatgptLogin, ChatgptAi, Aivvm, GptGo, AItianhu, AItianhuSpace, Aichat, Myshell,
42 43 ])
43 44 )
44 45
@@ -47,7 +48,7 @@ gpt_35_turbo = Model(
47 48 name = 'gpt-3.5-turbo',
48 49 base_provider = 'openai',
49 50 best_provider = RetryProvider([
50 DeepAi, ChatgptLogin, ChatgptAi, Aivvm, GptGo, AItianhu, Aichat, Myshell,
51 DeepAi, ChatgptLogin, ChatgptAi, Aivvm, GptGo, AItianhu, Aichat, AItianhuSpace, Myshell,
51 52 ])
52 53 )
53 54
@@ -55,7 +56,7 @@ gpt_4 = Model(
55 56 name = 'gpt-4',
56 57 base_provider = 'openai',
57 58 best_provider = RetryProvider([
58 Aivvm, Myshell
59 Aivvm, Myshell, AItianhuSpace,
59 60 ])
60 61 )
61 62