Skip to content

Commit

Permalink
raise error messages when optional dependencies are missing
Browse files Browse the repository at this point in the history
  • Loading branch information
JianxinMa committed Aug 5, 2024
1 parent 9789baa commit 3c4f8d0
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 14 deletions.
2 changes: 1 addition & 1 deletion qwen_agent/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.0.6'
__version__ = '0.0.7'
from .agent import Agent
from .multi_agent_hub import MultiAgentHub

Expand Down
15 changes: 4 additions & 11 deletions qwen_agent/gui/gradio.py
Original file line number Diff line number Diff line change
@@ -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
5 changes: 3 additions & 2 deletions qwen_agent/memory/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')]

Expand Down
17 changes: 17 additions & 0 deletions qwen_agent/tools/code_interpreter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

Expand Down
19 changes: 19 additions & 0 deletions qwen_agent/tools/retrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}'
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 3c4f8d0

Please sign in to comment.