返回提交历史
Modified
etc/tool/create_provider.py
+2
-0
Added
g4f/Provider/Berlin.py
+78
-0
Added
g4f/Provider/Koala.py
+71
-0
Modified
g4f/Provider/__init__.py
+6
-0
Modified
g4f/models.py
+5
-5
XFEstudio/gpt4free
Add Berlin and Koala Provider
2fb93222
代码差异
5 个文件
+162
-5
@@ -7,6 +7,8 @@ sys.path.append(str(Path(__file__).parent.parent.parent))
7
7
8
8
import g4f
9
9
10
g4f.debug.logging = True
11
10
12
def read_code(text):
11
13
if match := re.search(r"```(python|py|)\n(?P<code>[\S\s]+?)\n```", text):
12
14
return match.group("code")
@@ -0,0 +1,78 @@
1
from __future__ import annotations
2
3
import secrets
4
import uuid
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 Berlin(AsyncGeneratorProvider):
14
url = "https://ai.berlin4h.top"
15
working = True
16
supports_gpt_35_turbo = True
17
_token = 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
if not model:
28
model = "gpt-3.5-turbo"
29
headers = {
30
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0",
31
"Accept": "*/*",
32
"Accept-Language": "de,en-US;q=0.7,en;q=0.3",
33
"Accept-Encoding": "gzip, deflate, br",
34
"Referer": f"{cls.url}/",
35
"Content-Type": "application/json",
36
"Origin": cls.url,
37
"Alt-Used": "ai.berlin4h.top",
38
"Connection": "keep-alive",
39
"Sec-Fetch-Dest": "empty",
40
"Sec-Fetch-Mode": "cors",
41
"Sec-Fetch-Site": "same-origin",
42
"Pragma": "no-cache",
43
"Cache-Control": "no-cache",
44
"TE": "trailers",
45
}
46
async with ClientSession(headers=headers) as session:
47
if not cls._token:
48
data = {
49
"account": '免费使用GPT3.5模型@163.com',
50
"password": '659e945c2d004686bad1a75b708c962f'
51
}
52
async with session.post(f"{cls.url}/api/login", json=data, proxy=proxy) as response:
53
response.raise_for_status()
54
cls._token = (await response.json())["data"]["token"]
55
headers = {
56
"token": cls._token
57
}
58
prompt = format_prompt(messages)
59
data = {
60
"prompt": prompt,
61
"parentMessageId": str(uuid.uuid4()),
62
"options": {
63
"model": model,
64
"temperature": 0,
65
"presence_penalty": 0,
66
"frequency_penalty": 0,
67
"max_tokens": 1888,
68
**kwargs
69
},
70
}
71
async with session.post(f"{cls.url}/api/chat/completions", json=data, proxy=proxy, headers=headers) as response:
72
response.raise_for_status()
73
async for chunk in response.content:
74
if chunk.strip():
75
try:
76
yield json.loads(chunk)["content"]
77
except:
78
raise RuntimeError(f"Response: {chunk.decode()}")
@@ -0,0 +1,71 @@
1
from __future__ import annotations
2
3
import random
4
import string
5
import json
6
from aiohttp import ClientSession
7
8
from ..typing import AsyncResult, Messages
9
from .base_provider import AsyncGeneratorProvider
10
11
class Koala(AsyncGeneratorProvider):
12
url = "https://koala.sh"
13
supports_gpt_35_turbo = True
14
supports_message_history = True
15
working = True
16
17
@classmethod
18
async def create_async_generator(
19
cls,
20
model: str,
21
messages: Messages,
22
proxy: str = None,
23
**kwargs
24
) -> AsyncResult:
25
if not model:
26
model = "gpt-3.5-turbo"
27
headers = {
28
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0",
29
"Accept": "text/event-stream",
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
"Flag-Real-Time-Data": "false",
35
"Visitor-ID": random_string(),
36
"Origin": cls.url,
37
"Alt-Used": "koala.sh",
38
"Connection": "keep-alive",
39
"Sec-Fetch-Dest": "empty",
40
"Sec-Fetch-Mode": "cors",
41
"Sec-Fetch-Site": "same-origin",
42
"Pragma": "no-cache",
43
"Cache-Control": "no-cache",
44
"TE": "trailers",
45
}
46
async with ClientSession(headers=headers) as session:
47
data = {
48
"input": messages[-1]["content"],
49
"inputHistory": [
50
message["content"]
51
for message in messages
52
if message["role"] == "user"
53
],
54
"outputHistory": [
55
message["content"]
56
for message in messages
57
if message["role"] == "assistant"
58
],
59
"model": model,
60
}
61
async with session.post(f"{cls.url}/api/gpt/", json=data, proxy=proxy) as response:
62
response.raise_for_status()
63
async for chunk in response.content:
64
if chunk.startswith(b"data: "):
65
yield json.loads(chunk[6:])
66
67
68
def random_string(length: int = 20):
69
return ''.join(random.choice(
70
string.ascii_letters + string.digits
71
) for _ in range(length))
@@ -6,6 +6,7 @@ from .Aichat import Aichat
6
6
from .Ails import Ails
7
7
from .AItianhu import AItianhu
8
8
from .AItianhuSpace import AItianhuSpace
9
from .Berlin import Berlin
9
10
from .Bing import Bing
10
11
from .ChatBase import ChatBase
11
12
from .ChatForAi import ChatForAi
@@ -26,6 +27,7 @@ from .GptForLove import GptForLove
26
27
from .GptGo import GptGo
27
28
from .GptGod import GptGod
28
29
from .Hashnode import Hashnode
30
from .Koala import Koala
29
31
from .Liaobots import Liaobots
30
32
from .Llama2 import Llama2
31
33
from .MyShell import MyShell
@@ -59,6 +61,7 @@ class ProviderUtils:
59
61
'AsyncProvider': AsyncProvider,
60
62
'Bard': Bard,
61
63
'BaseProvider': BaseProvider,
64
'Berlin': Berlin,
62
65
'Bing': Bing,
63
66
'ChatBase': ChatBase,
64
67
'ChatForAi': ChatForAi,
@@ -89,6 +92,7 @@ class ProviderUtils:
89
92
'H2o': H2o,
90
93
'HuggingChat': HuggingChat,
91
94
'Komo': Komo,
95
'Koala': Koala,
92
96
'Liaobots': Liaobots,
93
97
'Llama2': Llama2,
94
98
'Lockchat': Lockchat,
@@ -135,6 +139,7 @@ __all__ = [
135
139
'AItianhuSpace',
136
140
'Aivvm',
137
141
'Bard',
142
'Berlin',
138
143
'Bing',
139
144
'ChatBase',
140
145
'ChatForAi',
@@ -162,6 +167,7 @@ __all__ = [
162
167
'Hashnode',
163
168
'H2o',
164
169
'HuggingChat',
170
'Koala',
165
171
'Liaobots',
166
172
'Llama2',
167
173
'Lockchat',
@@ -5,7 +5,6 @@ from .Provider import BaseProvider, RetryProvider
5
5
from .Provider import (
6
6
GptForLove,
7
7
ChatgptAi,
8
GptChatly,
9
8
DeepInfra,
10
9
ChatgptX,
11
10
ChatBase,
@@ -13,10 +12,12 @@ from .Provider import (
13
12
FakeGpt,
14
13
FreeGpt,
15
14
NoowAi,
15
Berlin,
16
16
Llama2,
17
17
Vercel,
18
18
Aichat,
19
19
GPTalk,
20
Koala,
20
21
AiAsk,
21
22
GptGo,
22
23
Phind,
@@ -51,10 +52,9 @@ gpt_35_long = Model(
51
52
name = 'gpt-3.5-turbo',
52
53
base_provider = 'openai',
53
54
best_provider = RetryProvider([
54
AiAsk, Aichat, FreeGpt, You,
55
GptChatly, GptForLove,
56
NoowAi, GeekGpt, Phind,
57
FakeGpt
55
FreeGpt, You,
56
GeekGpt, FakeGpt,
57
Berlin, Koala
58
58
])
59
59
)
60
60