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

XFEstudio/gpt4free

feat: add image size support and update nodriver args handling

- **ApiAirforce.py**: Added `use_image_size = True` class attribute. - **EasyChat.py**: Added `user_data_dir=None` argument when calling `get_args_from_nodriver`. - **LMArenaBeta.py**: - Only set `cls.share_url` from `G4F_SHARE_URL` if `cls.share_url` is `None`. - Added `user_data_dir=None` argument when calling `get_args_from_nodriver`. - Modified media filtering to check `url` is a `str` before `startswith("https://")`. - **OpenaiTemplate.py**: - Added `use_image_size = False` class attribute. - Modified image generation payload to optionally include `"size"` key when `use_image_size` is `True` and width/height present. - Refactored `read_response` to store `content` in a variable, strip on first chunk, and yield `ToolCalls` directly from `tool_calls` variable. - **g4f/requests/__init__.py**: - Added `user_data_dir` parameter (default `"nodriver"`) to `get_args_from_nodriver`. - Passed `user_data_dir` to `get_nodriver` call.

4132b0a4
hlohaus <983577+hlohaus@users.noreply.github.com>
提交于

代码差异

5 个文件 +21 -18
Modified g4f/Provider/ApiAirforce.py +2 -1
@@ -9,4 +9,5 @@ class ApiAirforce(OpenaiTemplate):
9 9 login_url = "https://panel.api.airforce/dashboard"
10 10 api_base = "https://api.airforce/v1"
11 11 working = True
12 active_by_default = True
12 active_by_default = True
13 use_image_size = True
Modified g4f/Provider/EasyChat.py +1 -1
@@ -120,7 +120,7 @@ class EasyChat(OpenaiTemplate, AuthFileMixin):
120 120 return
121 121 for _ in range(2):
122 122 if not args:
123 args = await get_args_from_nodriver(cls.url, proxy=proxy, callback=callback)
123 args = await get_args_from_nodriver(cls.url, proxy=proxy, callback=callback, user_data_dir=None)
124 124 if extra_body is None:
125 125 extra_body = {}
126 126 extra_body.setdefault("captchaToken", cls.captchaToken)
Modified g4f/Provider/needs_auth/LMArenaBeta.py +4 -3
@@ -194,7 +194,8 @@ class LMArenaBeta(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
194 194 else:
195 195 raise ModelNotFoundError(f"Model '{model}' is not supported by LMArena Beta.")
196 196
197 cls.share_url = os.getenv("G4F_SHARE_URL")
197 if cls.share_url is None:
198 cls.share_url = os.getenv("G4F_SHARE_URL")
198 199 prompt = get_last_user_message(messages)
199 200 cache_file = cls.get_cache_file()
200 201 args = None
@@ -233,7 +234,7 @@ class LMArenaBeta(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
233 234 await asyncio.sleep(1)
234 235 while not await page.evaluate('document.querySelector(\'textarea\')'):
235 236 await asyncio.sleep(1)
236 args = await get_args_from_nodriver(cls.url, proxy=proxy, callback=callback)
237 args = await get_args_from_nodriver(cls.url, proxy=proxy, callback=callback, user_data_dir=None)
237 238 elif not cls.looked:
238 239 cls.looked = True
239 240 try:
@@ -275,7 +276,7 @@ class LMArenaBeta(AsyncGeneratorProvider, ProviderModelMixin, AuthFileMixin):
275 276 "url": url
276 277 }
277 278 for url, name in list(merge_media(media, messages))
278 if url.startswith("https://")
279 if isinstance(url, str) and url.startswith("https://")
279 280 ],
280 281 "parentMessageIds": [] if conversation is None else conversation.message_ids,
281 282 "participantPosition": "a",
Modified g4f/Provider/template/OpenaiTemplate.py +11 -11
@@ -27,6 +27,7 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
27 27 use_model_names = False
28 28 ssl = None
29 29 add_user = True
30 use_image_size = False
30 31
31 32 @classmethod
32 33 def get_models(cls, api_key: str = None, api_base: str = None) -> list[str]:
@@ -101,11 +102,10 @@ class OpenaiTemplate(AsyncGeneratorProvider, ProviderModelMixin, RaiseErrorMixin
101 102 # Proxy for image generation feature
102 103 if model and model in cls.image_models:
103 104 prompt = format_media_prompt(messages, prompt)
104 data = {
105 "prompt": prompt,
106 "model": model,
107 **use_aspect_ratio({"width": kwargs.get("width"), "height": kwargs.get("height")}, kwargs.get("aspect_ratio", None))
108 }
105 size = use_aspect_ratio({"width": kwargs.get("width"), "height": kwargs.get("height")}, kwargs.get("aspect_ratio", None))
106 size = {"size": f"{size['width']}x{size['height']}", **size} if cls.use_image_size and "width" in size and "height" in size else size
107 data = {"prompt": prompt, "model": model, **size}
108
109 109 # Handle media if provided
110 110 if media is not None:
111 111 data["image_url"] = next(iter([data for data, _ in media if data and isinstance(data, str) and data.startswith("http://") or data.startswith("https://")]), None)
@@ -202,19 +202,19 @@ async def read_response(response: StreamResponse, stream: bool, prompt: str, pro
202 202 model_returned = True
203 203 choice = next(iter(data["choices"]), None)
204 204 if choice:
205 if "content" in choice["delta"] and choice["delta"]["content"]:
206 delta = choice["delta"]["content"]
205 content = choice.get("delta", {}).get("content")
206 if content:
207 207 if first:
208 delta = delta.lstrip()
209 if delta:
208 content = content.lstrip()
209 if content:
210 210 first = False
211 211 if reasoning:
212 212 yield Reasoning(status="")
213 213 reasoning = False
214 yield delta
214 yield content
215 215 tool_calls = choice.get("delta", {}).get("tool_calls")
216 216 if tool_calls:
217 yield ToolCalls(choice["delta"]["tool_calls"])
217 yield ToolCalls(tool_calls)
218 218 reasoning_content = choice.get("delta", {}).get("reasoning_content", choice.get("delta", {}).get("reasoning"))
219 219 if reasoning_content:
220 220 reasoning = True
Modified g4f/requests/__init__.py +3 -2
@@ -92,10 +92,11 @@ async def get_args_from_nodriver(
92 92 wait_for: str = None,
93 93 callback: callable = None,
94 94 cookies: Cookies = None,
95 browser: Browser = None
95 browser: Browser = None,
96 user_data_dir: str = "nodriver"
96 97 ) -> dict:
97 98 if browser is None:
98 browser, stop_browser = await get_nodriver(proxy=proxy, timeout=timeout)
99 browser, stop_browser = await get_nodriver(proxy=proxy, timeout=timeout, user_data_dir=user_data_dir)
99 100 else:
100 101 def stop_browser():
101 102 pass