RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
Change default error message to English (#3838)
Jin Hai6657ca7cde6e
diff --git a/rag/llm/chat_model.py b/rag/llm/chat_model.pyindex 90786c58fa..c65078d2d3 100644--- a/rag/llm/chat_model.py+++ b/rag/llm/chat_model.py@@ -22,7 +22,7 @@ from abc import ABCfrom openai import OpenAIimport openaifrom ollama import Client-from rag.nlp import is_english+from rag.nlp import is_chinesefrom rag.utils import num_tokens_from_stringfrom groq import Groqimport os@@ -30,6 +30,8 @@ import jsonimport requestsimport asyncio+LENGTH_NOTIFICATION_CN = "······\n由于长度的原因,回答被截断了,要继续吗?"+LENGTH_NOTIFICATION_EN = "...\nFor the content length reason, it stopped, continue?"class Base(ABC):def __init__(self, key, model_name, base_url):@@ -47,8 +49,10 @@ class Base(ABC):**gen_conf)ans = response.choices[0].message.content.strip()if response.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENreturn ans, response.usage.total_tokensexcept openai.APIError as e:return "**ERROR**: " + str(e), 0@@ -80,8 +84,10 @@ class Base(ABC):else: total_tokens = resp.usage.total_tokensif resp.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENyield ansexcept openai.APIError as e:@@ -167,8 +173,10 @@ class BaiChuanChat(Base):**self._format_params(gen_conf))ans = response.choices[0].message.content.strip()if response.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese([ans]):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENreturn ans, response.usage.total_tokensexcept openai.APIError as e:return "**ERROR**: " + str(e), 0@@ -207,8 +215,10 @@ class BaiChuanChat(Base):else resp.usage["total_tokens"])if resp.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese([ans]):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENyield ansexcept Exception as e:@@ -242,8 +252,10 @@ class QWenChat(Base):ans += response.output.choices[0]['message']['content']tk_count += response.usage.total_tokensif response.output.choices[0].get("finish_reason", "") == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese([ans]):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENreturn ans, tk_countreturn "**ERROR**: " + response.message, tk_count@@ -276,8 +288,10 @@ class QWenChat(Base):ans = resp.output.choices[0]['message']['content']tk_count = resp.usage.total_tokensif resp.output.choices[0].get("finish_reason", "") == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENyield anselse:yield ans + "\n**ERROR**: " + resp.message if not re.search(r" (key|quota)", str(resp.message).lower()) else "Out of credit. Please set the API key in **settings > Model providers.**"@@ -308,8 +322,10 @@ class ZhipuChat(Base):)ans = response.choices[0].message.content.strip()if response.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENreturn ans, response.usage.total_tokensexcept Exception as e:return "**ERROR**: " + str(e), 0@@ -333,8 +349,10 @@ class ZhipuChat(Base):delta = resp.choices[0].delta.contentans += deltaif resp.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENtk_count = resp.usage.total_tokensif resp.choices[0].finish_reason == "stop": tk_count = resp.usage.total_tokensyield ans@@ -525,8 +543,10 @@ class MiniMaxChat(Base):response = response.json()ans = response["choices"][0]["message"]["content"].strip()if response["choices"][0]["finish_reason"] == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENreturn ans, response["usage"]["total_tokens"]except Exception as e:return "**ERROR**: " + str(e), 0@@ -594,8 +614,10 @@ class MistralChat(Base):**gen_conf)ans = response.choices[0].message.contentif response.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENreturn ans, response.usage.total_tokensexcept openai.APIError as e:return "**ERROR**: " + str(e), 0@@ -618,8 +640,10 @@ class MistralChat(Base):ans += resp.choices[0].delta.contenttotal_tokens += 1if resp.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENyield ansexcept openai.APIError as e:@@ -811,8 +835,10 @@ class GroqChat:)ans = response.choices[0].message.contentif response.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENreturn ans, response.usage.total_tokensexcept Exception as e:return ans + "\n**ERROR**: " + str(e), 0@@ -838,8 +864,10 @@ class GroqChat:ans += resp.choices[0].delta.contenttotal_tokens += 1if resp.choices[0].finish_reason == "length":- ans += "...\nFor the content length reason, it stopped, continue?" if is_english(- [ans]) else "······\n由于长度的原因,回答被截断了,要继续吗?"+ if is_chinese(ans):+ ans += LENGTH_NOTIFICATION_CN+ else:+ ans += LENGTH_NOTIFICATION_ENyield ansexcept Exception as e:diff --git a/rag/nlp/__init__.py b/rag/nlp/__init__.pyindex 41b895978a..ddca9b580a 100644--- a/rag/nlp/__init__.py+++ b/rag/nlp/__init__.py@@ -230,6 +230,14 @@ def is_english(texts):return Truereturn False+def is_chinese(text):+ chinese = 0+ for ch in text:+ if '\u4e00' <= ch <= '\u9fff':+ chinese += 1+ if chinese / len(text) > 0.2:+ return True+ return Falsedef tokenize(d, t, eng):d["content_with_weight"] = t