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

XFEstudio/gpt4free

Update Pollinations provider

68b60f13
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

1 个文件 +14 -68
Modified g4f/Provider/Pollinations.py +14 -68
@@ -46,26 +46,21 @@ class Pollinations(AsyncGeneratorProvider, ProviderModelMixin):
46 46 supports_native_tools = True
47 47
48 48 # API endpoints
49 text_api_endpoint = "https://text.pollinations.ai/openai"
50 49 image_api_endpoint = "https://image.pollinations.ai/prompt/{}"
51 50 image_models_endpoint = "https://image.pollinations.ai/models"
52 51 gen_image_api_endpoint = "https://gen.pollinations.ai/image/{}"
53 52 gen_text_api_endpoint = "https://gen.pollinations.ai/v1/chat/completions"
54 53 gen_image_models_endpoint = "https://gen.pollinations.ai/image/models"
55 text_models_endpoint = "https://gen.pollinations.ai/text/models"
56 54 quota_url = "https://g4f.space/api/pollinations/quota"
57 55 worker_api_endpoint = "https://g4f.space/api/pollinations/chat/completions"
58 56 worker_models_endpoint = "https://g4f.space/api/pollinations/models"
59 57
60 58 # Models configuration
61 default_model = "openai-fast"
62 fallback_model = "deepseek"
63 default_vision_model = default_model
64 59 default_voice = "alloy"
65 text_models = {default_model: {"id": default_model}}
60 text_models = {}
66 61 image_models = {}
67 62 audio_models = {}
68 vision_models = [default_vision_model]
63 vision_models = []
69 64 model_aliases = {
70 65 "gpt-4.1-nano": "openai-fast",
71 66 "llama-4-scout": "llamascout",
@@ -104,42 +99,15 @@ class Pollinations(AsyncGeneratorProvider, ProviderModelMixin):
104 99 def get_models(
105 100 cls, api_key: Optional[str] = None, timeout: Optional[float] = None, **kwargs
106 101 ):
107 def get_alias(model: dict) -> str:
108 if isinstance(model, str):
109 return model
110 alias = model.get("name")
111 if model.get("aliases"):
112 alias = model.get("aliases")[0]
113 elif alias in cls.swap_model_aliases:
114 alias = cls.swap_model_aliases[alias]
115 if alias == "searchgpt":
116 return model.get("name")
117 return (
118 str(alias)
119 .replace("-instruct", "")
120 .replace("qwen-", "qwen")
121 .replace("qwen", "qwen-")
122 )
123
124 102 if not api_key or AppConfig.disable_custom_api_key:
125 103 api_key = AuthManager.load_api_key(cls)
126 if (
127 (not api_key or api_key.startswith("g4f_") or api_key.startswith("gfs_"))
128 and cls.balance
129 or cls.balance is None
130 and cls.get_balance(api_key, timeout)
131 and cls.balance > 0
132 ):
133 debug.log(f"Authenticated with Pollinations AI using G4F API.")
134 models_url = cls.worker_models_endpoint
135 image_url = cls.image_models_endpoint
136 elif api_key:
137 debug.log(f"Using Pollinations AI with provided API key.")
104 if (OpenaiTemplate.is_provider_api_key(api_key)):
105 debug.log(f"Using Pollinations with provided API key.")
138 106 models_url = cls.gen_text_api_endpoint
139 107 image_url = cls.gen_image_models_endpoint
140 108 else:
141 debug.log(f"Using Pollinations AI without authentication.")
142 models_url = cls.text_models_endpoint
109 debug.log(f"Authenticated with Pollinations using G4F API.")
110 models_url = cls.worker_models_endpoint
143 111 image_url = cls.image_models_endpoint
144 112
145 113 if cls.current_models_endpoint != models_url:
@@ -179,7 +147,6 @@ class Pollinations(AsyncGeneratorProvider, ProviderModelMixin):
179 147 if model.get("name") not in cls.video_models:
180 148 cls.image_models[model.get("name")] = {
181 149 "id": model.get("name"),
182 "label": get_alias(model),
183 150 **model,
184 151 }
185 152 if "image" in model.get("input_modalities", []):
@@ -189,13 +156,10 @@ class Pollinations(AsyncGeneratorProvider, ProviderModelMixin):
189 156 else:
190 157 cls.image_models[model] = {"id": model}
191 158
192 text_response = requests.get(cls.text_models_endpoint, timeout=timeout)
193 if not text_response.ok:
194 text_response = requests.get(
195 cls.text_models_endpoint, timeout=timeout
196 )
159 text_response = requests.get(models_url, timeout=timeout)
197 160 text_response.raise_for_status()
198 161 models = text_response.json()
162 models = models if isinstance(models, list) else models.get("data", [])
199 163
200 164 # Purpose of audio models
201 165 cls.audio_models = {
@@ -221,12 +185,12 @@ class Pollinations(AsyncGeneratorProvider, ProviderModelMixin):
221 185 cls.live += 1
222 186 cls.swap_model_aliases = {v: k for k, v in cls.model_aliases.items()}
223 187 cls.text_models = {
224 model.get("name"): {
225 "id": model.get("name"),
226 "label": get_alias(model),
188 model.get("id", model.get("name")): {
189 "id": model.get("id", model.get("name")),
227 190 **model,
228 191 }
229 192 for model in models
193 if not model.get("image") and not model.get("video")
230 194 }
231 195 cls.models = cls.text_models.copy()
232 196 cls.models.update(cls.image_models)
@@ -318,10 +282,6 @@ class Pollinations(AsyncGeneratorProvider, ProviderModelMixin):
318 282 has_audio = True
319 283 break
320 284 model = "openai-audio" if has_audio else cls.default_model
321 if AppConfig.disable_custom_api_key:
322 api_key = None
323 if not api_key or api_key.startswith("g4f_") or api_key.startswith("gfs_"):
324 api_key = AuthManager.load_api_key(cls) or api_key
325 285 if cls.get_models(api_key=api_key, timeout=kwargs.get("timeout", 15)):
326 286 if model in cls.model_aliases:
327 287 model = cls.model_aliases[model]
@@ -433,11 +393,7 @@ class Pollinations(AsyncGeneratorProvider, ProviderModelMixin):
433 393 encoded_prompt = quote_plus(encoded_prompt)[
434 394 : 4096 - len(cls.image_api_endpoint) - len(query) - 8
435 395 ].rstrip("%")
436 if (
437 api_key
438 and not api_key.startswith("g4f_")
439 and not api_key.startswith("gfs_")
440 ):
396 if (OpenaiTemplate.is_provider_api_key(api_key)):
441 397 url = cls.gen_image_api_endpoint
442 398 else:
443 399 url = cls.image_api_endpoint
@@ -579,20 +535,10 @@ class Pollinations(AsyncGeneratorProvider, ProviderModelMixin):
579 535 seed=None if "tools" in extra_body else seed,
580 536 **extra_body,
581 537 )
582 if (
583 (
584 not api_key
585 or api_key.startswith("g4f_")
586 or api_key.startswith("gfs_")
587 )
588 and cls.balance
589 and cls.balance > 0
590 ):
591 endpoint = cls.worker_api_endpoint
592 elif api_key:
538 if (OpenaiTemplate.is_provider_api_key(api_key)):
593 539 endpoint = cls.gen_text_api_endpoint
594 540 else:
595 endpoint = cls.text_api_endpoint
541 endpoint = cls.worker_api_endpoint
596 542 headers = None
597 543 if api_key:
598 544 headers = {"authorization": f"Bearer {api_key}"}