From bfc3d1095165540c724ce9ebaa82294ae56b54d0 Mon Sep 17 00:00:00 2001 From: tpoisonooo Date: Tue, 3 Sep 2024 11:41:42 +0800 Subject: [PATCH 1/2] docs(en): add readthedocs --- docs/en/doc_add_readthedocs.md | 92 ++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 docs/en/doc_add_readthedocs.md diff --git a/docs/en/doc_add_readthedocs.md b/docs/en/doc_add_readthedocs.md new file mode 100644 index 00000000..a36a8c04 --- /dev/null +++ b/docs/en/doc_add_readthedocs.md @@ -0,0 +1,92 @@ +# Adding `chat_with_repo` to readthedocs for Free + +This article introduces how to implement `chat_with_repo` on Read the Docs at zero cost. See [HuixiangDou Read the Docs documentation](https://huixiangdou.readthedocs.io). + +The deployment workflow is as follows: + + + +Where: +* [Read the Docs](https://readthedocs.io) hosts Chinese and English documents +* [OpenXLab](https://openxlab.org.cn/apps) provides an HTTPS entrance (Read the Docs cannot embed HTTP) and CPU +* [SiliconCloud](https://siliconflow.cn/siliconcloud) provides text2vec, reranker, and LLM model APIs + +We need to use readthedocs custom theme, adding a button to the theme. + +1. When the button is clicked, create an `iframe` to load the HTTPS version of Huixiang Dou's Gradio UI +2. HTTPS requires domain name verification. You can use the random subdomain provided by OpenXLab +3. The GPU resources in OpenXLab are limited, so we use the free model APIs provided by SiliconCloud + +Here are the steps to follow. + +## I. Prepare Code and Documentation + +Assuming you use all the documents of `mmpose` as the knowledge base, put the knowledge base into repodir + +```bash +cd HuixiangDou +mkdir repodir +git clone https://github.com/open-mmlab/mmpose --depth=1 +# Remove the .git of the knowledge base +rm -rf .git +``` + +Adjust the default configuration of `gradio_ui.py` to use `config-cpu.ini` +```bash +# huixiangdou/gradio_ui.py + parser.add_argument( + '--config_path', + default='config-cpu.ini', + type=str, +.. +``` + +Commit the knowledge base and the Huixiangou project to Github together, for example, the `for-openxlab-readthedocs` branch of [huixiangdou-readthedocs](https://github.com/tpoisonooo/huixiangdou-readthedocs/tree/for-openxlab-readthedocs). + +## II. Create an OpenXLab Application + +Open [OpenXLab](https://openxlab.org.cn/apps) and create a `Gradio` type application. + +1. Enter the Github address and branch name from the previous step +2. Choose the CPU server + +After confirmation, modify the application settings: + +* Add `huixiangdou/gradio_ui.py` to `Customize Startup File` +* Since the code is open source, you need to configure environment variables. HuixiangDou prioritizes using the token in the `config-cpu.ini`. If it cannot be found, it will try to check `SILICONCLOUD_TOKEN` and `LLM_API_TOKEN`, as shown in the figure: + + + +Start the application. The first run takes about **10 minutes** to build the feature library. After it's done, you should see a gradio application. For example: + +```bash +https://openxlab.org.cn/apps/detail/tpoisonooo/HuixiangDou-readthedocs +``` + +Press F12 in the browser to check the source code and obtain the corresponding HTTPS address for this service: + +```JavaScript +src="https://g-app-center-000704-0786-wrbqzpv.openxlab.space" +``` + +As long as the application data is not deleted, this address is **fixed**. + +## III. Use Read the Docs Custom Theme + +Assuming you are already familiar with the basic usage of Read the Docs, you can directly copy the HuixiangDou docs directory + +* zh or en directory +* requirements/doc.txt sets the custom theme + +[Here](https://github.com/tpoisonooo/pytorch_sphinx_theme/) is the implementation of our custom theme, mainly: + +1. Create a `chatButton` and an empty container in [layout.html](https://github.com/tpoisonooo/pytorch_sphinx_theme/blob/3db120b0f1e064425f37e98368dcea49972702e9/pytorch_sphinx_theme/layout.html#L324) +2. Bind an event to `chatButton`. When the button is clicked, the empty container loads the HTTPS address, for example, the previous one: + + ```bash + https://g-app-center-000704-0786-wrbqzpv.openxlab.space + ``` + + You can modify your preferred style in [theme.css](https://github.com/tpoisonooo/pytorch_sphinx_theme/blob/master/pytorch_sphinx_theme/static/css/theme.css). + +Finally, configure your project on readthedocs.io, and `Build Version` will do. From 17fbe7bda9a2623eef241e5ac8965aded0569d30 Mon Sep 17 00:00:00 2001 From: tpoisonooo Date: Thu, 17 Oct 2024 11:55:03 +0800 Subject: [PATCH 2/2] fix(service/prompt.py): optimize ciation format --- huixiangdou/service/prompt.py | 58 +++++++++++++---------------------- 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/huixiangdou/service/prompt.py b/huixiangdou/service/prompt.py index c7539081..c6b6b9f3 100644 --- a/huixiangdou/service/prompt.py +++ b/huixiangdou/service/prompt.py @@ -1,5 +1,5 @@ from typing import List - +import re # PreprocNode SCORING_QUESTION_TEMPLATE_CN = '“{}”\n请仔细阅读以上内容,判断句子是否是个有主题的疑问句,结果用 0~10 表示。直接提供得分不要解释。\n判断标准:有主语谓语宾语并且是疑问句得 10 分;缺少主谓宾扣分;陈述句直接得 0 分;不是疑问句直接得 0 分。直接提供得分不要解释。' # modified from kimi @@ -111,38 +111,12 @@ GENERATE_TEMPLATE_CN = '材料:“{}”\n 问题:“{}” \n 请仔细阅读参考材料回答问题。' # noqa E501 GENERATE_TEMPLATE_EN = 'Background Information: "{}"\n Question: "{}"\n Please read the reference material carefully and answer the question.' # noqa E501 -GENERATE_TEMPLATE_CITATION_HEAD_CN = '''你是一个文本专家,擅长阅读理解任务,根据检索结果回答用户输入。 - -## 任务 -仅使用提供的搜索结果(其中一些可能不相关)来准确、吸引人且简洁地回答给定的问题,并正确引用它们。使用无偏见和新闻业语调。对于任何事实性声明都要引用。当引用多个搜索结果时,使用[1][2][3]。在每条句子中至少引用一个文档,最多引用三个文档。如果多个文档支持该句子,则只引用支持文档的最小必要子集。 - -## 指令遵循与提供有用的回复要求 -- 在满足安全合规要求下,注意并遵循用户问题中提到的每条指令,对于用户的问题你必须直接的给出回答。如果指令超出了你的能力范围,礼貌的告诉用户。 -- 请严格遵循指令,请说话不要啰嗦,不要不简洁明了。 --【重要!】对于数字比较问题,请先一步一步分析再回答。 - -## 输出格式与语言风格要求 -- 使用\(...\) 或\[...\]来输出数学公式,例如:使用\[x^2\]来表示x的平方。 -- 当你介绍自己时,请记住保持幽默和简短。 -- 你不会不用简洁简短的文字输出,你不会输出无关用户指令的文字。 -- 你不会重复表达和同义反复。 +GENERATE_TEMPLATE_CITATION_HEAD_CN = '''## 任务 +请使用仅提供的搜索结果(其中一些可能不相关)写出准确、有吸引力且简洁的回答,并正确引用它们。使用不偏不倚且新闻式的语气。对于任何事实性陈述都必须引用。引用多个搜索结果时,使用[1][2][3]格式。每个句子至少引用一个文档,最多引用三个文档。如果多个文档支持同一个句子,引用最小的必要子集。 ''' -GENERATE_TEMPLATE_CITATION_HEAD_EN = '''You are a text expert, proficient in reading comprehension tasks, answering user input based on search results. - -## Task -Write an accurate, engaging, and concise answer for the given question using only the provided search results (some of which might be irrelevant) and cite them properly. Use an unbiased and journalistic tone. Always cite for any factual claim. When citing several search results, use [1][2][3]. Cite at least one document and at most three documents in each sentence. If multiple documents support the sentence, only cite a minimum sufficient subset of the documents. - -## Instructions and Providing Helpful Responses -- While adhering to safety and compliance requirements, pay attention to and follow each instruction mentioned in the user's question. You must directly answer the user's question. If the instruction is beyond your capabilities, politely inform the user. -- Please strictly follow the instructions and avoid verbosity and ambiguity. -- [Important!] For numerical comparison questions, analyze step by step before answering. - -## Output Format and Language Style Requirements -- Use \(...\) or \[...\] to output mathematical formulas, for example: use \[x^2\] to represent the square of x. -- When introducing yourself, remember to be humorous and concise. -- You will not use verbose language and will not output text unrelated to the user's instructions. -- You will not repeat expressions and will avoid tautology. +GENERATE_TEMPLATE_CITATION_HEAD_EN = '''## Task +Please use only the provided search results (some of which may be irrelevant) to write accurate, engaging, and concise answers, and correctly cite them. Use an impartial and journalistic tone. For any factual statements, citations are required. When citing multiple search results, use the format [1][2][3]. Each sentence should reference at least one document, and no more than three. If multiple documents support the same sentence, cite the smallest necessary subset. ''' # WebSearchNode @@ -161,19 +135,29 @@ class CitationGeneratePrompt: def __init__(self, language: str): self.language = language + def remove_markdown_headers(self, texts: List[str]): + pure_texts = [] + for text in texts: + # 移除Markdown中的标题 + pure_text = re.sub(r'^#{1,6}\s*', '', text, flags=re.MULTILINE) + pure_texts.append(pure_text) + return pure_texts + def build(self, texts: List[str], question:str): + pure_texts = self.remove_markdown_headers(texts) + if self.language == 'zh': head = GENERATE_TEMPLATE_CITATION_HEAD_CN - question_prompt = '\n## 用户输入\n{}\n'.format(question*2) + question_prompt = '\n## 用户输入\n{}\n'.format(question) context_prompt = '' - for index, text in enumerate(texts): - context_prompt += '\n## 检索结果{}\n"""\n{}\n"""\n'.format(index+1, text) + for index, text in enumerate(pure_texts): + context_prompt += '\n## 检索结果{}\n{}\n'.format(index+1, text) elif self.language == 'en': head = GENERATE_TEMPLATE_CITATION_HEAD_EN - question_prompt = '\n## user input\n{}\n'.format(question*2) + question_prompt = '\n## user input\n{}\n'.format(question) context_prompt = '' - for index, text in enumerate(texts): - context_prompt += '\n## search result{}\n"""\n{}\n"""\n'.format(index+1, text) + for index, text in enumerate(pure_texts): + context_prompt += '\n## search result{}\n{}\n'.format(index+1, text) prompt = head + context_prompt + question_prompt return prompt \ No newline at end of file