From ae978ad541d2335ea341e6254095ddfa21fb1b54 Mon Sep 17 00:00:00 2001 From: hlohaus <983577+hlohaus@users.noreply.github.com> Date: Wed, 15 Jan 2025 22:45:52 +0100 Subject: [PATCH 1/2] Add missing webview requirements Fix web_search in Web UI --- g4f/gui/server/api.py | 11 +++-------- g4f/tools/run_tools.py | 10 ++++++---- requirements.txt | 2 ++ setup.py | 3 +++ 4 files changed, 14 insertions(+), 12 deletions(-) diff --git a/g4f/gui/server/api.py b/g4f/gui/server/api.py index 2fdc59403b3..159be0b29a0 100644 --- a/g4f/gui/server/api.py +++ b/g4f/gui/server/api.py @@ -99,14 +99,9 @@ def _prepare_conversation_kwargs(self, json_data: dict, kwargs: dict): }, "type": "function" }] - do_web_search = json_data.get('web_search') - if do_web_search and provider: - kwargs["tool_calls"].append({ - "function": { - "name": "safe_search_tool" - }, - "type": "function" - }) + web_search = json_data.get('web_search') + if web_search: + kwargs["web_search"] = web_search action = json_data.get('action') if action == "continue": kwargs["tool_calls"].append({ diff --git a/g4f/tools/run_tools.py b/g4f/tools/run_tools.py index fe97dc9c837..ab13caaab16 100644 --- a/g4f/tools/run_tools.py +++ b/g4f/tools/run_tools.py @@ -29,10 +29,11 @@ def validate_arguments(data: dict) -> dict: async def async_iter_run_tools(async_iter_callback, model, messages, tool_calls: Optional[list] = None, **kwargs): # Handle web_search from kwargs - if kwargs.get('web_search'): + web_search = kwargs.get('web_search') + if web_search: try: messages = messages.copy() - messages[-1]["content"] = await do_search(messages[-1]["content"]) + messages[-1]["content"] = await do_search(messages[-1]["content"], web_search if web_search != "true" else None) except Exception as e: debug.log(f"Couldn't do web search: {e.__class__.__name__}: {e}") # Keep web_search in kwargs for provider native support @@ -78,10 +79,11 @@ def iter_run_tools( **kwargs ) -> AsyncIterator: # Handle web_search from kwargs - if kwargs.get('web_search'): + web_search = kwargs.get('web_search') + if web_search: try: messages = messages.copy() - messages[-1]["content"] = asyncio.run(do_search(messages[-1]["content"])) + messages[-1]["content"] = asyncio.run(do_search(messages[-1]["content"], web_search if web_search != "true" else None)) except Exception as e: debug.log(f"Couldn't do web search: {e.__class__.__name__}: {e}") # Keep web_search in kwargs for provider native support diff --git a/requirements.txt b/requirements.txt index 13d951e46b9..078b87a795e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,6 +16,8 @@ brotli beautifulsoup4 aiohttp_socks pywebview +plyer +setuptools cryptography nodriver python-multipart diff --git a/setup.py b/setup.py index e09787713eb..26854957c38 100644 --- a/setup.py +++ b/setup.py @@ -36,6 +36,9 @@ "uvicorn", # api "nodriver", "python-multipart", + "pywebview", + "plyer", + "setuptools", "pypdf2", # files "docx", "odfpy", From 78fa74569856cbe17e912559219deacce3d13804 Mon Sep 17 00:00:00 2001 From: hlohaus <983577+hlohaus@users.noreply.github.com> Date: Wed, 15 Jan 2025 23:08:40 +0100 Subject: [PATCH 2/2] Fix web_search in Web UI --- g4f/tools/run_tools.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/g4f/tools/run_tools.py b/g4f/tools/run_tools.py index ab13caaab16..7cc9e3e2d79 100644 --- a/g4f/tools/run_tools.py +++ b/g4f/tools/run_tools.py @@ -33,7 +33,8 @@ async def async_iter_run_tools(async_iter_callback, model, messages, tool_calls: if web_search: try: messages = messages.copy() - messages[-1]["content"] = await do_search(messages[-1]["content"], web_search if web_search != "true" else None) + web_search = web_search if isinstance(web_search, str) and web_search != "true" else None + messages[-1]["content"] = await do_search(messages[-1]["content"], web_search) except Exception as e: debug.log(f"Couldn't do web search: {e.__class__.__name__}: {e}") # Keep web_search in kwargs for provider native support @@ -83,7 +84,8 @@ def iter_run_tools( if web_search: try: messages = messages.copy() - messages[-1]["content"] = asyncio.run(do_search(messages[-1]["content"], web_search if web_search != "true" else None)) + web_search = web_search if isinstance(web_search, str) and web_search != "true" else None + messages[-1]["content"] = asyncio.run(do_search(messages[-1]["content"], web_search)) except Exception as e: debug.log(f"Couldn't do web search: {e.__class__.__name__}: {e}") # Keep web_search in kwargs for provider native support