From 3c4f8d00af41be2ec043f13f4aca119f63e42c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=BC=E6=AC=A3?= Date: Mon, 5 Aug 2024 15:46:59 +0800 Subject: [PATCH] raise error messages when optional dependencies are missing --- qwen_agent/__init__.py | 2 +- qwen_agent/gui/gradio.py | 15 ++++----------- qwen_agent/memory/memory.py | 5 +++-- qwen_agent/tools/code_interpreter.py | 17 +++++++++++++++++ qwen_agent/tools/retrieval.py | 19 +++++++++++++++++++ 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/qwen_agent/__init__.py b/qwen_agent/__init__.py index 85a9214..1a2eda2 100644 --- a/qwen_agent/__init__.py +++ b/qwen_agent/__init__.py @@ -1,4 +1,4 @@ -__version__ = '0.0.6' +__version__ = '0.0.7' from .agent import Agent from .multi_agent_hub import MultiAgentHub diff --git a/qwen_agent/gui/gradio.py b/qwen_agent/gui/gradio.py index ae8cedd..7843767 100644 --- a/qwen_agent/gui/gradio.py +++ b/qwen_agent/gui/gradio.py @@ -1,14 +1,7 @@ try: import gradio as gr - if gr.__version__ < '4.0': - raise ImportError('Incompatible "gradio" version detected. ' - 'Please install the correct version with: pip install "gradio>=4.0"') -except (ModuleNotFoundError, AttributeError): - raise ImportError('Requirement "gradio" not installed. ' - 'Please install it by: pip install -U "gradio>=4.0"') - -try: + assert gr.__version__ >= '4.0' import modelscope_studio as mgr # noqa -except ModuleNotFoundError: - raise ImportError('Requirement "modelscope-studio" not installed. ' - 'Please install it by: pip install -U "modelscope-studio>=0.2.1"') +except Exception as e: + raise ImportError('The dependencies for GUI support are not installed. ' + 'Please install the required dependencies by running: pip install qwen-agent[gui]') from e diff --git a/qwen_agent/memory/memory.py b/qwen_agent/memory/memory.py index b001d11..7f7f91f 100644 --- a/qwen_agent/memory/memory.py +++ b/qwen_agent/memory/memory.py @@ -114,14 +114,15 @@ def _run(self, messages: List[Message], lang: str = 'en', **kwargs) -> Iterator[ except Exception: query = query - content = self._call_tool( - 'retrieval', + content = self.function_map['retrieval'].call( { 'query': query, 'files': rag_files }, **kwargs, ) + if not isinstance(content, str): + content = json.dumps(content, ensure_ascii=False, indent=4) yield [Message(role=ASSISTANT, content=content, name='memory')] diff --git a/qwen_agent/tools/code_interpreter.py b/qwen_agent/tools/code_interpreter.py index e6ce3fd..1da1256 100644 --- a/qwen_agent/tools/code_interpreter.py +++ b/qwen_agent/tools/code_interpreter.py @@ -63,6 +63,7 @@ def __init__(self, cfg: Optional[Dict] = None): self.work_dir: str = os.getenv('M6_CODE_INTERPRETER_WORK_DIR', self.work_dir) self.work_dir: str = self.cfg.get('work_dir', self.work_dir) self.instance_id: str = str(uuid.uuid4()) + _check_deps_for_code_interpreter() @property def args_format(self) -> str: @@ -261,6 +262,22 @@ def _serve_image(self, image_base64: str) -> str: return local_image_file +def _check_deps_for_code_interpreter(): + try: + import matplotlib # noqa + import matplotlib.pyplot as plt # noqa + import numpy as np # noqa + import pandas as pd # noqa + import PIL.Image # noqa + import seaborn as sns # noqa + from jupyter_client import BlockingKernelClient # noqa + from sympy import Eq, solve, symbols # noqa + except ImportError as e: + raise ImportError( + 'The dependencies for Code Interpreter support are not installed. ' + 'Please install the required dependencies by running: pip install qwen-agent[code_interpreter]') from e + + def _fix_matplotlib_cjk_font_issue(): import matplotlib diff --git a/qwen_agent/tools/retrieval.py b/qwen_agent/tools/retrieval.py index 638fb28..09e42f0 100644 --- a/qwen_agent/tools/retrieval.py +++ b/qwen_agent/tools/retrieval.py @@ -8,6 +8,22 @@ from qwen_agent.tools.simple_doc_parser import PARSER_SUPPORTED_FILE_TYPES +def _check_deps_for_rag(): + try: + import charset_normalizer # noqa + import jieba # noqa + import pdfminer # noqa + import pdfplumber # noqa + import rank_bm25 # noqa + import snowballstemmer # noqa + from bs4 import BeautifulSoup # noqa + from docx import Document # noqa + from pptx import Presentation # noqa + except ImportError as e: + raise ImportError('The dependencies for RAG support are not installed. ' + 'Please install the required dependencies by running: pip install qwen-agent[rag]') from e + + @register_tool('retrieval') class Retrieval(BaseTool): description = f'从给定文件列表中检索出和问题相关的内容,支持文件类型包括:{"/".join(PARSER_SUPPORTED_FILE_TYPES)}' @@ -51,6 +67,9 @@ def call(self, params: Union[str, dict], **kwargs) -> list: The parsed file list or retrieved file list. """ + # TODO: It this a good place to check the RAG deps? + _check_deps_for_rag() + params = self._verify_json_format_args(params) files = params.get('files', []) if isinstance(files, str):