From 6206cb1bc6dd77601f95eb1e5159ac1c708add4f Mon Sep 17 00:00:00 2001 From: Roman Pshichenko Date: Thu, 24 Aug 2023 15:25:47 -0700 Subject: [PATCH] removes npm dependency by using mermaid ink service | removes references to npm mermaid and related issues --- .devcontainer/postCreateCommand.sh | 6 +-- Dockerfile | 7 ---- README.md | 24 +----------- config/config.yaml | 6 --- docs/FAQ-EN.md | 6 --- docs/README_CN.md | 8 +--- docs/README_JA.md | 26 +----------- metagpt/utils/common.py | 10 ----- metagpt/utils/mermaid.py | 63 ++++++++++++------------------ requirements.txt | 1 + setup.py | 20 +--------- 11 files changed, 33 insertions(+), 144 deletions(-) diff --git a/.devcontainer/postCreateCommand.sh b/.devcontainer/postCreateCommand.sh index 06d12e408..bd08d39cd 100644 --- a/.devcontainer/postCreateCommand.sh +++ b/.devcontainer/postCreateCommand.sh @@ -1,7 +1,3 @@ -# Step 1: Ensure that NPM is installed on your system. Then install mermaid-js. -npm --version -sudo npm install -g @mermaid-js/mermaid-cli - -# Step 2: Ensure that Python 3.9+ is installed on your system. You can check this by using: +# Step 1: Ensure that Python 3.9+ is installed on your system. You can check this by using: python --version python setup.py install \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 537bbc72e..a74de8550 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,13 +6,6 @@ RUN apt update &&\ apt install -y git chromium fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 --no-install-recommends &&\ apt clean && rm -rf /var/lib/apt/lists/* -# Install Mermaid CLI globally -ENV CHROME_BIN="/usr/bin/chromium" \ - PUPPETEER_CONFIG="/app/metagpt/config/puppeteer-config.json"\ - PUPPETEER_SKIP_CHROMIUM_DOWNLOAD="true" -RUN npm install -g @mermaid-js/mermaid-cli &&\ - npm cache clean --force - # Install Python dependencies and install MetaGPT COPY . /app/metagpt WORKDIR /app/metagpt diff --git a/README.md b/README.md index 33bbd9aba..4425a2e0b 100644 --- a/README.md +++ b/README.md @@ -45,14 +45,10 @@ It costs approximately **$0.2** (in GPT-4 API fees) to generate one example with ### Traditional Installation ```bash -# Step 1: Ensure that NPM is installed on your system. Then install mermaid-js. -npm --version -sudo npm install -g @mermaid-js/mermaid-cli - -# Step 2: Ensure that Python 3.9+ is installed on your system. You can check this by using: +# Step 1: Ensure that Python 3.9+ is installed on your system. You can check this by using: python --version -# Step 3: Clone the repository to your local machine, and install it. +# Step 2: Clone the repository to your local machine, and install it. git clone https://github.com/geekan/metagpt cd metagpt python setup.py install @@ -60,22 +56,6 @@ python setup.py install **Note:** -- If already have Chrome, Chromium, or MS Edge installed, you can skip downloading Chromium by setting the environment variable -`PUPPETEER_SKIP_CHROMIUM_DOWNLOAD` to `true`. - -- Some people are [having issues](https://github.com/mermaidjs/mermaid.cli/issues/15) installing this tool globally. Installing it locally is an alternative solution, - - ```bash - npm install @mermaid-js/mermaid-cli - ``` - -- don't forget to the configuration for mmdc in config.yml - - ```yml - PUPPETEER_CONFIG: "./config/puppeteer-config.json" - MMDC: "./node_modules/.bin/mmdc" - ``` - - if `python setup.py install` fails with error `[Errno 13] Permission denied: '/usr/local/lib/python3.11/dist-packages/test-easy-install-13129.write-test'`, try instead running `python setup.py install --user` ### Installation by Docker diff --git a/config/config.yaml b/config/config.yaml index 274cdf469..7983d5d0d 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -63,12 +63,6 @@ SD_T2I_API: "/sdapi/v1/txt2img" #### for Execution #LONG_TERM_MEMORY: false -#### for Mermaid CLI -## If you installed mmdc (Mermaid CLI) only for metagpt then enable the following configuration. -#PUPPETEER_CONFIG: "./config/puppeteer-config.json" -#MMDC: "./node_modules/.bin/mmdc" - - ### for calc_usage # CALC_USAGE: false diff --git a/docs/FAQ-EN.md b/docs/FAQ-EN.md index b5ae9184b..879ae9780 100644 --- a/docs/FAQ-EN.md +++ b/docs/FAQ-EN.md @@ -173,9 +173,3 @@ MetaGPT Community - The position of Chief Evangelist rotates on a monthly basis. 1. On the day Llama2 was released, some of the community members began experiments and found that output can be generated based on MetaGPT's structure. However, Llama2's context is too short to generate a complete project. Before regularly using Llama2, it's necessary to expand the context window to at least 8k. If anyone has good recommendations for expansion models or methods, please leave a comment. -1. `mermaid-cli getElementsByTagName SyntaxError: Unexpected token '.'` - - 1. Upgrade node to version 14.x or above: - - 1. `npm install -g n` - 1. `n stable` to install the stable version of node(v18.x) diff --git a/docs/README_CN.md b/docs/README_CN.md index 0ef54b017..15bb6f541 100644 --- a/docs/README_CN.md +++ b/docs/README_CN.md @@ -40,14 +40,10 @@ ### 传统安装 ```bash -# 第 1 步:确保您的系统上安装了 NPM。并使用npm安装mermaid-js -npm --version -sudo npm install -g @mermaid-js/mermaid-cli - -# 第 2 步:确保您的系统上安装了 Python 3.9+。您可以使用以下命令进行检查: +# 第 1 步:确保您的系统上安装了 Python 3.9+。您可以使用以下命令进行检查: python --version -# 第 3 步:克隆仓库到您的本地机器,并进行安装。 +# 第 2 步:克隆仓库到您的本地机器,并进行安装。 git clone https://github.com/geekan/metagpt cd metagpt python setup.py install diff --git a/docs/README_JA.md b/docs/README_JA.md index 57f6487a7..fcd270504 100644 --- a/docs/README_JA.md +++ b/docs/README_JA.md @@ -40,37 +40,15 @@ ### 伝統的なインストール ```bash -# ステップ 1: NPM がシステムにインストールされていることを確認してください。次に mermaid-js をインストールします。 -npm --version -sudo npm install -g @mermaid-js/mermaid-cli - -# ステップ 2: Python 3.9+ がシステムにインストールされていることを確認してください。これを確認するには: +# ステップ 1: Python 3.9+ がシステムにインストールされていることを確認してください。これを確認するには: python --version -# ステップ 3: リポジトリをローカルマシンにクローンし、インストールする。 +# ステップ 2: リポジトリをローカルマシンにクローンし、インストールする。 git clone https://github.com/geekan/metagpt cd metagpt python setup.py install ``` -**注:** - -- すでに Chrome、Chromium、MS Edge がインストールされている場合は、環境変数 `PUPPETEER_SKIP_CHROMIUM_DOWNLOAD` を `true` に設定することで、 -Chromium のダウンロードをスキップすることができます。 - -- このツールをグローバルにインストールする[問題を抱えている](https://github.com/mermaidjs/mermaid.cli/issues/15)人もいます。ローカルにインストールするのが代替の解決策です、 - - ```bash - npm install @mermaid-js/mermaid-cli - ``` - -- config.yml に mmdc のコンフィギュレーションを記述するのを忘れないこと - - ```yml - PUPPETEER_CONFIG: "./config/puppeteer-config.json" - MMDC: "./node_modules/.bin/mmdc" - ``` - ### Docker によるインストール ```bash diff --git a/metagpt/utils/common.py b/metagpt/utils/common.py index 7f090cf63..e734a3b27 100644 --- a/metagpt/utils/common.py +++ b/metagpt/utils/common.py @@ -15,16 +15,6 @@ from metagpt.logs import logger -def check_cmd_exists(command) -> int: - """ 检查命令是否存在 - :param command: 待检查的命令 - :return: 如果命令存在,返回0,如果不存在,返回非0 - """ - check_command = 'command -v ' + command + ' >/dev/null 2>&1 || { echo >&2 "no mermaid"; exit 1; }' - result = os.system(check_command) - return result - - class OutputParser: @classmethod diff --git a/metagpt/utils/mermaid.py b/metagpt/utils/mermaid.py index 24aabe8ae..18d6cd60d 100644 --- a/metagpt/utils/mermaid.py +++ b/metagpt/utils/mermaid.py @@ -2,58 +2,43 @@ # -*- coding: utf-8 -*- """ @Time : 2023/7/4 10:53 -@Author : alexanderwu +@Author : alexanderwu, imjohndoe @File : mermaid.py """ -import subprocess -from pathlib import Path -from metagpt.config import CONFIG from metagpt.const import PROJECT_ROOT from metagpt.logs import logger -from metagpt.utils.common import check_cmd_exists +import requests +import base64 +import os -def mermaid_to_file(mermaid_code, output_file_without_suffix, width=2048, height=2048) -> int: - """suffix: png/svg/pdf - +def mermaid_to_file(mermaid_code, output_file_without_suffix): + """suffix: jpeg/svg :param mermaid_code: mermaid code :param output_file_without_suffix: output filename - :param width: - :param height: - :return: 0 if succed, -1 if failed + :return: 0 if succeed, -1 if failed """ - # Write the Mermaid code to a temporary file - tmp = Path(f"{output_file_without_suffix}.mmd") - tmp.write_text(mermaid_code, encoding="utf-8") + encoded_string = base64.b64encode(mermaid_code.encode()).decode() - if check_cmd_exists("mmdc") != 0: - logger.warning("RUN `npm install -g @mermaid-js/mermaid-cli` to install mmdc") - return -1 + dir_name = os.path.dirname(output_file_without_suffix) + if dir_name and not os.path.exists(dir_name): + os.makedirs(dir_name) - for suffix in ["pdf", "svg", "png"]: + for suffix in ["svg", "png"]: output_file = f"{output_file_without_suffix}.{suffix}" - # Call the `mmdc` command to convert the Mermaid code to a PNG - logger.info(f"Generating {output_file}..") + path_type = "svg" if suffix == "svg" else "img" + url = f"https://mermaid.ink/{path_type}/{encoded_string}" + response = requests.get(url) - if CONFIG.puppeteer_config: - subprocess.run( - [ - CONFIG.mmdc, - "-p", - CONFIG.puppeteer_config, - "-i", - str(tmp), - "-o", - output_file, - "-w", - str(width), - "-H", - str(height), - ] - ) + if response.status_code == 200: + with open(output_file, 'wb') as f: + f.write(response.content) + logger.info(f"File saved to {output_file}") else: - subprocess.run([CONFIG.mmdc, "-i", str(tmp), "-o", output_file, "-w", str(width), "-H", str(height)]) + logger.warning(f"Failed to retrieve {suffix}") + return -1 + return 0 @@ -110,5 +95,5 @@ class KnowledgeBase { if __name__ == "__main__": # logger.info(print_members(print_members)) - mermaid_to_file(MMC1, PROJECT_ROOT / "tmp/1.png") - mermaid_to_file(MMC2, PROJECT_ROOT / "tmp/2.png") + mermaid_to_file(MMC1, PROJECT_ROOT / "tmp/1") + mermaid_to_file(MMC2, PROJECT_ROOT / "tmp/2") diff --git a/requirements.txt b/requirements.txt index efc2ea3e7..2046e3130 100644 --- a/requirements.txt +++ b/requirements.txt @@ -23,6 +23,7 @@ pydantic==1.10.8 pytest==7.2.2 python_docx==0.8.11 PyYAML==6.0 +requests==2.31.0 # sentence_transformers==2.2.2 setuptools==65.6.3 tenacity==8.2.2 diff --git a/setup.py b/setup.py index a88f9de92..f64741e3f 100644 --- a/setup.py +++ b/setup.py @@ -1,24 +1,9 @@ """wutils: handy tools """ -import subprocess from codecs import open from os import path -from setuptools import Command, find_packages, setup - - -class InstallMermaidCLI(Command): - """A custom command to run `npm install -g @mermaid-js/mermaid-cli` via a subprocess.""" - - description = "install mermaid-cli" - user_options = [] - - def run(self): - try: - subprocess.check_call(["npm", "install", "-g", "@mermaid-js/mermaid-cli"]) - except subprocess.CalledProcessError as e: - print(f"Error occurred: {e.output}") - +from setuptools import find_packages, setup here = path.abspath(path.dirname(__file__)) @@ -48,7 +33,4 @@ def run(self): "search-google": ["google-api-python-client==2.94.0"], "search-ddg": ["duckduckgo-search==3.8.5"], }, - cmdclass={ - "install_mermaid": InstallMermaidCLI, - }, )