返回提交历史
Added
g4f/Provider/TwitterBio.py
+103
-0
Modified
g4f/Provider/__init__.py
+1
-0
Modified
g4f/models.py
+11
-10
XFEstudio/gpt4free
New TwitterBio provider with support for gpt-3.5-turbo and mixtral-8x7b models
21c94f22
代码差异
3 个文件
+115
-10
@@ -0,0 +1,103 @@
1
from __future__ import annotations
2
3
import json
4
import re
5
from aiohttp import ClientSession
6
7
from ..typing import AsyncResult, Messages
8
from .base_provider import AsyncGeneratorProvider, ProviderModelMixin
9
from .helper import format_prompt
10
11
class TwitterBio(AsyncGeneratorProvider, ProviderModelMixin):
12
url = "https://www.twitterbio.io"
13
api_endpoint_mistral = "https://www.twitterbio.io/api/mistral"
14
api_endpoint_openai = "https://www.twitterbio.io/api/openai"
15
working = True
16
supports_gpt_35_turbo = True
17
18
default_model = 'gpt-3.5-turbo'
19
models = [
20
'mistralai/Mixtral-8x7B-Instruct-v0.1',
21
'gpt-3.5-turbo',
22
]
23
24
model_aliases = {
25
"mixtral-8x7b": "mistralai/Mixtral-8x7B-Instruct-v0.1",
26
}
27
28
@classmethod
29
def get_model(cls, model: str) -> str:
30
if model in cls.models:
31
return model
32
return cls.default_model
33
34
@staticmethod
35
def format_text(text: str) -> str:
36
text = re.sub(r'\s+', ' ', text.strip())
37
text = re.sub(r'\s+([,.!?])', r'\1', text)
38
return text
39
40
@classmethod
41
async def create_async_generator(
42
cls,
43
model: str,
44
messages: Messages,
45
proxy: str = None,
46
**kwargs
47
) -> AsyncResult:
48
model = cls.get_model(model)
49
50
headers = {
51
"accept": "*/*",
52
"accept-language": "en-US,en;q=0.9",
53
"cache-control": "no-cache",
54
"content-type": "application/json",
55
"origin": cls.url,
56
"pragma": "no-cache",
57
"priority": "u=1, i",
58
"referer": f"{cls.url}/",
59
"sec-ch-ua": '"Chromium";v="127", "Not)A;Brand";v="99"',
60
"sec-ch-ua-mobile": "?0",
61
"sec-ch-ua-platform": '"Linux"',
62
"sec-fetch-dest": "empty",
63
"sec-fetch-mode": "cors",
64
"sec-fetch-site": "same-origin",
65
"user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
66
}
67
async with ClientSession(headers=headers) as session:
68
prompt = format_prompt(messages)
69
data = {
70
"prompt": f'{prompt}.'
71
}
72
73
if model == 'mistralai/Mixtral-8x7B-Instruct-v0.1':
74
api_endpoint = cls.api_endpoint_mistral
75
elif model == 'gpt-3.5-turbo':
76
api_endpoint = cls.api_endpoint_openai
77
else:
78
raise ValueError(f"Unsupported model: {model}")
79
80
async with session.post(api_endpoint, json=data, proxy=proxy) as response:
81
response.raise_for_status()
82
buffer = ""
83
async for line in response.content:
84
line = line.decode('utf-8').strip()
85
if line.startswith('data: '):
86
try:
87
json_data = json.loads(line[6:])
88
if model == 'mistralai/Mixtral-8x7B-Instruct-v0.1':
89
if 'choices' in json_data and len(json_data['choices']) > 0:
90
text = json_data['choices'][0].get('text', '')
91
if text:
92
buffer += text
93
elif model == 'gpt-3.5-turbo':
94
text = json_data.get('text', '')
95
if text:
96
buffer += text
97
except json.JSONDecodeError:
98
continue
99
elif line == 'data: [DONE]':
100
break
101
102
if buffer:
103
yield cls.format_text(buffer)
@@ -51,6 +51,7 @@ from .Replicate import Replicate
51
51
from .ReplicateHome import ReplicateHome
52
52
from .Rocks import Rocks
53
53
from .TeachAnything import TeachAnything
54
from .TwitterBio import TwitterBio
54
55
from .Upstage import Upstage
55
56
from .Vercel import Vercel
56
57
from .WhiteRabbitNeo import WhiteRabbitNeo
@@ -37,6 +37,7 @@ from .Provider import (
37
37
Replicate,
38
38
ReplicateHome,
39
39
TeachAnything,
40
TwitterBio,
40
41
Upstage,
41
42
You,
42
43
)
@@ -91,7 +92,7 @@ gpt_35_turbo = Model(
91
92
name = 'gpt-3.5-turbo',
92
93
base_provider = 'openai',
93
94
best_provider = IterListProvider([
94
Allyfy,
95
Allyfy, TwitterBio,
95
96
])
96
97
)
97
98
@@ -140,50 +141,50 @@ gigachat = Model(
140
141
### Meta ###
141
142
meta = Model(
142
143
name = "meta-ai",
143
base_provider = "meta",
144
base_provider = "Meta",
144
145
best_provider = MetaAI
145
146
)
146
147
147
148
llama_3_8b = Model(
148
149
name = "llama-3-8b",
149
base_provider = "meta",
150
base_provider = "Meta",
150
151
best_provider = IterListProvider([DeepInfra, Replicate])
151
152
)
152
153
153
154
llama_3_70b = Model(
154
155
name = "llama-3-70b",
155
base_provider = "meta",
156
base_provider = "Meta",
156
157
best_provider = IterListProvider([ReplicateHome, DeepInfra, PerplexityLabs, Replicate])
157
158
)
158
159
159
160
llama_3_1_8b = Model(
160
161
name = "llama-3.1-8b",
161
base_provider = "meta",
162
base_provider = "Meta",
162
163
best_provider = IterListProvider([Blackbox])
163
164
)
164
165
165
166
llama_3_1_70b = Model(
166
167
name = "llama-3.1-70b",
167
base_provider = "meta",
168
base_provider = "Meta",
168
169
best_provider = IterListProvider([DDG, HuggingChat, FreeGpt, Blackbox, TeachAnything, HuggingFace])
169
170
)
170
171
171
172
llama_3_1_405b = Model(
172
173
name = "llama-3.1-405b",
173
base_provider = "meta",
174
base_provider = "Meta",
174
175
best_provider = IterListProvider([HuggingChat, Blackbox, HuggingFace])
175
176
)
176
177
177
178
### Mistral ###
178
179
mixtral_8x7b = Model(
179
180
name = "mixtral-8x7b",
180
base_provider = "huggingface",
181
best_provider = IterListProvider([HuggingChat, DDG, ReplicateHome, DeepInfra, HuggingFace,])
181
base_provider = "Mistral",
182
best_provider = IterListProvider([HuggingChat, DDG, ReplicateHome, TwitterBio, DeepInfra, HuggingFace,])
182
183
)
183
184
184
185
mistral_7b = Model(
185
186
name = "mistral-7b",
186
base_provider = "huggingface",
187
base_provider = "Mistral",
187
188
best_provider = IterListProvider([HuggingChat, HuggingFace, DeepInfra])
188
189
)
189
190