Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix: chart tool can't display chinese (#9686)

非法操作e2710161f6d7
diff --git a/api/Dockerfile b/api/Dockerfile
index c6381859b30..d58b4d02e21 100644
--- a/api/Dockerfile
+++ b/api/Dockerfile
@@ -56,6 +56,8 @@ RUN apt-get update \
&& apt-get update \
# For Security
&& apt-get install -y --no-install-recommends zlib1g=1:1.3.dfsg+really1.3.1-1 expat=2.6.3-1 libldap-2.5-0=2.5.18+dfsg-3 perl=5.38.2-5 libsqlite3-0=3.46.1-1 \
+ # install a chinese font to support the use of tools like matplotlib
+ && apt-get install -y fonts-noto-cjk \
&& apt-get autoremove -y \
&& rm -rf /var/lib/apt/lists/*
diff --git a/api/core/tools/provider/builtin/chart/chart.py b/api/core/tools/provider/builtin/chart/chart.py
index 8a24d334285..209d6ecba48 100644
--- a/api/core/tools/provider/builtin/chart/chart.py
+++ b/api/core/tools/provider/builtin/chart/chart.py
@@ -1,77 +1,36 @@
import matplotlib.pyplot as plt
-from fontTools.ttLib import TTFont
-from matplotlib.font_manager import findSystemFonts
+from matplotlib.font_manager import FontProperties
-from core.tools.errors import ToolProviderCredentialValidationError
-from core.tools.provider.builtin.chart.tools.line import LinearChartTool
from core.tools.provider.builtin_tool_provider import BuiltinToolProviderController
-# use a business theme
-plt.style.use("seaborn-v0_8-darkgrid")
-plt.rcParams["axes.unicode_minus"] = False
-
-
-def init_fonts():
- fonts = findSystemFonts()
- popular_unicode_fonts = [
+def set_chinese_font():
+ font_list = [
+ "PingFang SC",
+ "SimHei",
+ "Microsoft YaHei",
+ "STSong",
+ "SimSun",
"Arial Unicode MS",
- "DejaVu Sans",
- "DejaVu Sans Mono",
- "DejaVu Serif",
- "FreeMono",
- "FreeSans",
- "FreeSerif",
- "Liberation Mono",
- "Liberation Sans",
- "Liberation Serif",
- "Noto Mono",
- "Noto Sans",
- "Noto Serif",
- "Open Sans",
- "Roboto",
- "Source Code Pro",
- "Source Sans Pro",
- "Source Serif Pro",
- "Ubuntu",
- "Ubuntu Mono",
+ "Noto Sans CJK SC",
+ "Noto Sans CJK JP",
]
- supported_fonts = []
+ for font in font_list:
+ chinese_font = FontProperties(font)
+ if chinese_font.get_name() == font:
+ return chinese_font
- for font_path in fonts:
- try:
- font = TTFont(font_path)
- # get family name
- family_name = font["name"].getName(1, 3, 1).toUnicode()
- if family_name in popular_unicode_fonts:
- supported_fonts.append(family_name)
- except:
- pass
+ return FontProperties()
- plt.rcParams["font.family"] = "sans-serif"
- # sort by order of popular_unicode_fonts
- for font in popular_unicode_fonts:
- if font in supported_fonts:
- plt.rcParams["font.sans-serif"] = font
- break
-
-init_fonts()
+# use a business theme
+plt.style.use("seaborn-v0_8-darkgrid")
+plt.rcParams["axes.unicode_minus"] = False
+font_properties = set_chinese_font()
+plt.rcParams["font.family"] = font_properties.get_name()
class ChartProvider(BuiltinToolProviderController):
def _validate_credentials(self, credentials: dict) -> None:
- try:
- LinearChartTool().fork_tool_runtime(
- runtime={
- "credentials": credentials,
- }
- ).invoke(
- user_id="",
- tool_parameters={
- "data": "1,3,5,7,9,2,4,6,8,10",
- },
- )
- except Exception as e:
- raise ToolProviderCredentialValidationError(str(e))
+ pass