Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removes npm dependency by using mermaid ink service | removes referen… #265

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions .devcontainer/postCreateCommand.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 0 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 2 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,37 +45,17 @@ 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
```

**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
Expand Down
6 changes: 0 additions & 6 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 0 additions & 6 deletions docs/FAQ-EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
8 changes: 2 additions & 6 deletions docs/README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 2 additions & 24 deletions docs/README_JA.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions metagpt/utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
63 changes: 24 additions & 39 deletions metagpt/utils/mermaid.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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")
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 1 addition & 19 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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__))

Expand Down Expand Up @@ -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,
},
)