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

XFEstudio/gpt4free

feat: update PuterJS to use gpt-5.1 as default model and streamline model fetching logic feat: enhance Usage class to include prompt and completion token counts

074280ca
hlohaus <hlohaus@users.noreply.github.com>
提交于

代码差异

2 个文件 +20 -63
Modified g4f/Provider/needs_auth/PuterJS.py +14 -63
@@ -18,17 +18,15 @@ class PuterJS(AsyncGeneratorProvider, ProviderModelMixin):
18 18 label = "Puter.js"
19 19 url = "https://docs.puter.com/playground"
20 20 login_url = "https://github.com/HeyPuter/puter-cli"
21 models_endpoint = "https://api.puter.com/puterai/chat/models/"
21 22 api_endpoint = "https://api.puter.com/drivers/call"
22 23 working = True
23 24 active_by_default = True
24 25 needs_auth = True
25 26 quota_url = "https://api.puter.com/metering/usage"
26 27
27 default_model = 'gpt-4o'
28 default_model = 'gpt-5.1'
28 29 default_vision_model = default_model
29 openai_models = [default_vision_model,"gpt-4o-mini", "o1", "o1-mini", "o1-pro", "o3", "o3-mini", "o4-mini", "gpt-4.1", "gpt-4.1-mini", "gpt-4.1-nano", "gpt-4.5-preview"]
30 mistral_models = ["ministral-3b-2410","ministral-3b-latest","ministral-8b-2410","ministral-8b-latest","open-mistral-7b","mistral-tiny","mistral-tiny-2312","open-mixtral-8x7b","mistral-small","mistral-small-2312","open-mixtral-8x22b","open-mixtral-8x22b-2404","mistral-large-2411","mistral-large-latest","pixtral-large-2411","pixtral-large-latest","mistral-large-pixtral-2411","codestral-2501","codestral-latest","codestral-2412","codestral-2411-rc5","pixtral-12b-2409","pixtral-12b","pixtral-12b-latest","mistral-small-2503","mistral-small-latest"]
31
32 30 model_aliases = {
33 31 ### mistral_models ###
34 32 "mixtral-8x22b": "open-mixtral-8x22b",
@@ -210,16 +208,13 @@ class PuterJS(AsyncGeneratorProvider, ProviderModelMixin):
210 208 def get_models(cls, **kwargs) -> list[str]:
211 209 if not cls.models:
212 210 try:
213 url = "https://api.puter.com/puterai/chat/models/"
211 url = cls.models_endpoint
214 212 cls.models = requests.get(url).json().get("models", [])
215 cls.models = [model for model in cls.models if model.startswith("openrouter:") or "/" not in model and model not in ["abuse", "costly", "fake", "model-fallback-test-1"]]
213 cls.models = [model for model in cls.models if model not in ["abuse", "costly", "fake", "model-fallback-test-1"]]
216 214 cls.live += 1
217 215 except Exception as e:
218 216 debug.log(f"PuterJS: Failed to fetch models from API: {e}")
219 217 cls.models = []
220 cls.models += [model for model in cls.model_aliases.keys() if model not in cls.models]
221 openrouter_models = [model for model in cls.models if "openrouter:" in model]
222 cls.models = [model for model in cls.models if model not in openrouter_models] + openrouter_models
223 218 cls.vision_models = []
224 219 for model in cls.models:
225 220 for tag in ["vision", "multimodal", "gpt", "o1", "o3", "o4"]:
@@ -227,50 +222,6 @@ class PuterJS(AsyncGeneratorProvider, ProviderModelMixin):
227 222 cls.vision_models.append(model)
228 223 return cls.models
229 224
230 @staticmethod
231 def get_driver_for_model(model: str) -> str:
232 """Determine the appropriate driver based on the model name."""
233 if "openrouter:" in model:
234 return "openrouter"
235 elif model in PuterJS.openai_models or model.startswith("gpt-"):
236 return "openai-completion"
237 elif model in PuterJS.mistral_models:
238 return "mistral"
239 elif "grok" in model:
240 return "xai"
241 elif "claude" in model:
242 return "claude"
243 elif "deepseek" in model:
244 return "deepseek"
245 elif "gemini" in model:
246 return "gemini"
247 else:
248 raise ModelNotFoundError(f"Model {model} not found in known drivers")
249
250 @classmethod
251 def get_model(cls, model: str) -> str:
252 """Get the internal model name from the user-provided model name."""
253
254 if not model:
255 return cls.default_model
256
257 # Check if there's an alias for this model
258 if model in cls.model_aliases:
259 alias = cls.model_aliases[model]
260 # If the alias is a list, randomly select one of the options
261 if isinstance(alias, list):
262 selected_model = random.choice(alias)
263 debug.log(f"PuterJS: Selected model '{selected_model}' from alias '{model}'")
264 return selected_model
265 debug.log(f"PuterJS: Using model '{alias}' for alias '{model}'")
266 return alias
267
268 # Check if the model exists directly in our models list
269 if model in cls.models:
270 return model
271
272 raise ModelNotFoundError(f"Model {model} not found")
273
274 225 @classmethod
275 226 async def create_async_generator(
276 227 cls,
@@ -287,9 +238,6 @@ class PuterJS(AsyncGeneratorProvider, ProviderModelMixin):
287 238 if not api_key:
288 239 raise MissingAuthError("API key is required for Puter.js API")
289 240
290 if not cls.models:
291 cls.get_models()
292
293 241 # Check if we need to use a vision model
294 242 if not model and media is not None and len(media) > 0:
295 243 model = cls.default_vision_model
@@ -327,12 +275,9 @@ class PuterJS(AsyncGeneratorProvider, ProviderModelMixin):
327 275 "accept-language": "en-US,en;q=0.9"
328 276 }
329 277
330 # Determine the appropriate driver based on the model
331 driver = cls.get_driver_for_model(model)
332
333 278 json_data = {
334 279 "interface": "puter-image-generation" if model in cls.image_models else "puter-chat-completion",
335 "driver": driver,
280 "driver": "ai-chat",
336 281 "test_mode": messages[0]["content"] == "test",
337 282 "method": "generate" if model in cls.image_models else "complete",
338 283 "args": {"prompt": format_media_prompt(messages, prompt)} if model in cls.image_models else {
@@ -387,9 +332,9 @@ class PuterJS(AsyncGeneratorProvider, ProviderModelMixin):
387 332 else:
388 333 raise ResponseError(result)
389 334 message = choice.get("message", {})
390 reasoning_content = message.get("reasoning_content")
391 if reasoning_content:
392 yield Reasoning(reasoning_content)
335 reasoning = message.get("reasoning")
336 if reasoning:
337 yield Reasoning(reasoning)
393 338 content = message.get("content", "")
394 339 if isinstance(content, list):
395 340 for item in content:
@@ -409,5 +354,11 @@ class PuterJS(AsyncGeneratorProvider, ProviderModelMixin):
409 354 data = json.loads(line)
410 355 if data.get("type") == "text":
411 356 yield data.get("text", "")
357 elif data.get("type") == "reasoning":
358 yield Reasoning(data.get("reasoning", ""))
359 elif data.get("type") == "tool_calls":
360 yield ToolCalls(data.get("tool_calls", []))
361 elif data.get("type") == "usage":
362 yield Usage(**data.get("usage", {}))
412 363 else:
413 364 raise ResponseError(f"Unexpected content type: {mime_type}")
Modified g4f/providers/response.py +6 -0
@@ -210,6 +210,8 @@ class Usage(JsonMixin, HiddenResponse):
210 210 promptTokenCount: int = None,
211 211 candidatesTokenCount: int = None,
212 212 totalTokenCount: int = None,
213 prompt: int = None,
214 completion: int = None,
213 215 **kwargs
214 216 ):
215 217 if promptTokens is not None:
@@ -226,6 +228,10 @@ class Usage(JsonMixin, HiddenResponse):
226 228 kwargs["completion_tokens"] = candidatesTokenCount
227 229 if totalTokenCount is not None:
228 230 kwargs["total_tokens"] = totalTokenCount
231 if prompt is not None:
232 kwargs["prompt_tokens"] = prompt
233 if completion is not None:
234 kwargs["completion_tokens"] = completion
229 235 if output_tokens_details is not None:
230 236 for key, value in output_tokens_details.items():
231 237 kwargs[key] = value