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

Add EN/CN version of empty prompt #15

Open
wants to merge 1 commit into
base: ipex-llm
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion configs/prompt_config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,13 @@ PROMPT_TEMPLATES = {
'<已知信息>{{ context }}</已知信息>\n'
'<问题>{{ question }}</问题>\n',

"empty":
"empty_en":
"Please answer my question:\n"
"{{ question }}\n\n",

"empty_cn": # 搜不到知识库的时候使用
'请你回答我的问题:\n'
'{{ question }}\n\n',
},


Expand Down
6 changes: 5 additions & 1 deletion server/chat/file_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ async def file_chat(query: str = Body(..., description="用户输入", examples=
temperature: float = Body(TEMPERATURE, description="LLM 采样温度", ge=0.0, le=1.0),
max_tokens: Optional[int] = Body(None, description="限制LLM生成Token数量,默认None代表模型最大值"),
prompt_name: str = Body("default", description="使用的prompt模板名称(在configs/prompt_config.py中配置)"),
language: str = Body("English", description="当前界面语言")
):
if knowledge_id not in memo_faiss_pool.keys():
return BaseResponse(code=404, msg=f"未找到临时知识库 {knowledge_id},请先上传文件")
Expand All @@ -132,7 +133,10 @@ async def knowledge_base_chat_iterator() -> AsyncIterable[str]:

context = "\n".join([doc.page_content for doc in docs])
if len(docs) == 0: ## 如果没有找到相关文档,使用Empty模板
prompt_template = get_prompt_template("knowledge_base_chat", "empty")
if language == "English":
prompt_template = get_prompt_template("knowledge_base_chat", "empty_en")
else:
prompt_template = get_prompt_template("knowledge_base_chat", "empty_cn")
else:
prompt_template = get_prompt_template("knowledge_base_chat", prompt_name)
input_msg = History(role="user", content=prompt_template).to_msg_template(False)
Expand Down
9 changes: 8 additions & 1 deletion server/chat/knowledge_base_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ async def knowledge_base_chat(query: str = Body(..., description="用户输入",
"default",
description="使用的prompt模板名称(在configs/prompt_config.py中配置)"
),
language: str = Body(
"English",
"当前界面语言"
),
request: Request = None,
):
kb = KBServiceFactory.get_service_by_name(knowledge_base_name)
Expand Down Expand Up @@ -102,7 +106,10 @@ async def knowledge_base_chat_iterator(
context = "\n".join([doc.page_content for doc in docs])

if len(docs) == 0: # 如果没有找到相关文档,使用empty模板
prompt_template = get_prompt_template("knowledge_base_chat", "empty")
if language == "en":
prompt_template = get_prompt_template("knowledge_base_chat", "empty_en")
else:
prompt_template = get_prompt_template("knowledge_base_chat", "empty_cn")
else:
prompt_template = get_prompt_template("knowledge_base_chat", prompt_name)
input_msg = History(role="user", content=prompt_template).to_msg_template(False)
Expand Down
6 changes: 4 additions & 2 deletions webui_pages/dialogue/dialogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,8 @@ def on_feedback(
history=history,
model=llm_model,
prompt_name=prompt_template_name,
temperature=temperature):
temperature=temperature,
language=language):
if error_msg := check_error_msg(d): # check whether error occured
st.error(error_msg)
elif chunk := d.get("answer"):
Expand Down Expand Up @@ -585,7 +586,8 @@ def on_feedback(
history=history,
model=llm_model,
prompt_name=prompt_template_name,
temperature=temperature):
temperature=temperature,
language=language):
if error_msg := check_error_msg(d): # check whether error occured
st.error(error_msg)
elif chunk := d.get("answer"):
Expand Down
4 changes: 4 additions & 0 deletions webui_pages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ def knowledge_base_chat(
temperature: float = TEMPERATURE,
max_tokens: int = None,
prompt_name: str = "default",
language: str = "English"
):
'''
对应api.py/chat/knowledge_base_chat接口
Expand All @@ -359,6 +360,7 @@ def knowledge_base_chat(
"temperature": temperature,
"max_tokens": max_tokens,
"prompt_name": prompt_name,
"language": language
}

# print(f"received input message:")
Expand Down Expand Up @@ -420,6 +422,7 @@ def file_chat(
temperature: float = TEMPERATURE,
max_tokens: int = None,
prompt_name: str = "default",
language: str = "English"
):
'''
对应api.py/chat/file_chat接口
Expand All @@ -435,6 +438,7 @@ def file_chat(
"temperature": temperature,
"max_tokens": max_tokens,
"prompt_name": prompt_name,
"language": language
}

response = self.post(
Expand Down