Skip to content

Commit

Permalink
tests(gradio.py): update
Browse files Browse the repository at this point in the history
  • Loading branch information
tpoisonooo committed Aug 20, 2024
1 parent 81f0b69 commit 6c9c300
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ English | [简体中文](README_zh.md)

</div>

HuixiangDou is a **group chat** assistant based on LLM (Large Language Model).
HuixiangDou is a **professional knowledge assistant** based on LLM.

Advantages:

1. Design a three-stage pipeline of preprocess, rejection and response to cope with group chat scenario, answer user questions without message flooding, see [2401.08772](https://arxiv.org/abs/2401.08772), [2405.02817](https://arxiv.org/abs/2405.02817), [Hybrid Retrieval](./docs/knowledge_graph_en.md) and [Precision Report](./evaluation/).
2. No training required, with CPU-only, 2G, 10G and 80G configuration
1. Design three-stage pipelines of preprocess, rejection and response
* `chat_in_group` copes with **group chat** scenario, answer user questions without message flooding, see [2401.08772](https://arxiv.org/abs/2401.08772), [2405.02817](https://arxiv.org/abs/2405.02817), [Hybrid Retrieval](./docs/knowledge_graph_en.md) and [Precision Report](./evaluation/)
* `chat_with_repo` for **real-time streaming** chat
2. No training required, with CPU-only, 2G, 10G, 20G and 80G configuration
3. Offers a complete suite of Web, Android, and pipeline source code, industrial-grade and commercially viable

Check out the [scenes in which HuixiangDou are running](./huixiangdou-inside.md) and join [WeChat Group](resource/figures/wechat.jpg) to try AI assistant inside.
Expand All @@ -46,6 +48,7 @@ If this helps you, please give it a star ⭐

Our Web version has been released to [OpenXLab](https://openxlab.org.cn/apps/detail/tpoisonooo/huixiangdou-web), where you can create knowledge base, update positive and negative examples, turn on web search, test chat, and integrate into Feishu/WeChat groups. See [BiliBili](https://www.bilibili.com/video/BV1S2421N7mn) and [YouTube](https://www.youtube.com/watch?v=ylXrT-Tei-Y) !

- \[2024/08\] `chat_with_repo` [pipeline](./huixiangdou/service/parallel_pipeline.py) 👍
- \[2024/07\] Image and text retrieval & Removal of `langchain` 👍
- \[2024/07\] [Hybrid Knowledge Graph and Dense Retrieval](./docs/knowledge_graph_en.md) improve 1.7% F1 score 🎯
- \[2024/06\] [Evaluation of chunksize, splitter, and text2vec model](./evaluation) 🎯
Expand Down
9 changes: 6 additions & 3 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@

</div>

茴香豆是一个基于 LLM **群聊**知识助手,优势:
茴香豆是一个基于 LLM 的专业知识助手,优势:

1. 设计预处理、拒答、响应三阶段 pipeline 应对群聊场景,解答问题同时不会消息泛滥。精髓见 [2401.08772](https://arxiv.org/abs/2401.08772)[2405.02817](https://arxiv.org/abs/2405.02817)[混合检索](./docs/knowledge_graph_zh.md)[业务数据精度测试](./evaluation)
2. 无需训练适用各行业,提供 CPU-only、2G、10G、80G 规格配置
1. 设计预处理、拒答、响应三阶段 pipeline:
* `chat_in_group` 群聊场景,解答问题时不会消息泛滥。见 [2401.08772](https://arxiv.org/abs/2401.08772)[2405.02817](https://arxiv.org/abs/2405.02817)[混合检索](./docs/knowledge_graph_zh.md)[业务数据精度测试](./evaluation)
* `chat_with_repo` 实时聊天场景,响应更快
2. 无需训练适用各行业,提供 CPU-only、2G、10G、20G、80G 规格配置
3. 提供一整套前后端 web、android、算法源码,工业级开源可商用

查看[茴香豆已运行在哪些场景](./huixiangdou-inside.md);加入[微信群](resource/figures/wechat.jpg)直接体验群聊助手效果。
Expand All @@ -45,6 +47,7 @@

Web 版视频教程见 [BiliBili](https://www.bilibili.com/video/BV1S2421N7mn)[YouTube](https://www.youtube.com/watch?v=ylXrT-Tei-Y)

- \[2024/08\] `chat_with_repo` [pipeline](./huixiangdou/service/parallel_pipeline.py) 👍
- \[2024/07\] 图文检索 & 移除 `langchain` 👍
- \[2024/07\] [混合知识图谱和稠密检索,F1 提升 1.7%](./docs/knowledge_graph_zh.md) 🎯
- \[2024/06\] [评估 chunksize,splitter 和 text2vec 模型](./evaluation) 🎯
Expand Down
41 changes: 23 additions & 18 deletions huixiangdou/gradio.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ def parse_args():
args = parser.parse_args()
return args

language='zh'
enable_web_search=True
pipeline='parallel'
language='en'
enable_web_search=False
pipeline='chat_with_repo'
main_args = None
paralle_assistant = None
serial_assistant = None
Expand Down Expand Up @@ -71,7 +71,7 @@ def format_refs(refs: List[str]):
if language == 'zh':
text += '参考资料:\r\n'
else:
text += 'references:\r\n'
text += '**References:**\r\n'

for file_or_url in refs_filter:
text += '* {}\r\n'.format(file_or_url)
Expand All @@ -95,7 +95,7 @@ async def predict(text:str, image:str):
image_path = None

query = Query(text, image_path)
if 'serial' in pipeline:
if 'chat_in_group' in pipeline:
if serial_assistant is None:
serial_assistant = SerialPipeline(work_dir=main_args.work_dir, config_path=main_args.config_path)
args = {'query':query, 'history': [], 'groupname':''}
Expand Down Expand Up @@ -125,16 +125,15 @@ async def predict(text:str, image:str):
args = {'query':query, 'history':[], 'language':language}
args['enable_web_search'] = enable_web_search

sentence = None
sentence = ''
async for sess in paralle_assistant.generate(**args):
if len(sess.delta) > 0:
if sentence is None:
sentence = format_refs(sess.references)
if sentence == '' and len(sess.references) > 0:
sentence = format_refs(sess.references)

if len(sess.delta) > 0:
sentence += sess.delta
yield sentence


yield sentence

if __name__ == '__main__':
Expand All @@ -145,25 +144,31 @@ async def predict(text:str, image:str):
# hybrid llm serve
start_llm_server(config_path=main_args.config_path)

with gr.Blocks(theme=gr.themes.Soft()) as demo:
with gr.Blocks(theme=gr.themes.Soft(), title='HuixiangDou AI assistant', analytics_enabled=True) as demo:
with gr.Row():
gr.Markdown("""
#### [HuixiangDou](https://github.com/internlm/huixiangdou) AI assistant
""", label='Reply', header_links=True, line_breaks=True,)
with gr.Row():
with gr.Column():
ui_language = gr.Radio(["zh", "en"], label="Language", info="Use `zh` by default")
ui_language.change(fn=on_language_changed, inputs=ui_language, outputs=[])
with gr.Column():
ui_pipeline = gr.Radio(["parallel", "serial"], label="Pipeline type", info="Serial pipeline is very slow but more accurate, default value is `parallel`")
ui_pipeline = gr.Radio(["chat_with_repo", "chat_in_group"], label="Pipeline type", info="Group-chat is slow but accurate and safe, default value is `chat_with_repo`")
ui_pipeline.change(fn=on_pipeline_changed, inputs=ui_pipeline, outputs=[])
with gr.Column():
ui_web_search = gr.Radio(["yes", "no"], label="Enable web search", info="Enable by default")
ui_language = gr.Radio(["en", "zh"], label="Language", info="Use `en` by default ")
ui_language.change(fn=on_language_changed, inputs=ui_language, outputs=[])
with gr.Column():
ui_web_search = gr.Radio(["no", "yes"], label="Enable web search", info="Disable by default ")
ui_web_search.change(on_web_search_changed, inputs=ui_web_search, outputs=[])

with gr.Row():
input_question = gr.TextArea(label='Input your question.', placeholder='how to install opencompass ?', show_copy_button=True, lines=9)
input_question = gr.TextArea(label='Input your question', placeholder='how to install opencompass ?', show_copy_button=True, lines=9)
input_image = gr.Image(label='[Optional] Image-text retrieval needs `config-multimodal.ini`')
with gr.Row():
run_button = gr.Button()
with gr.Row():
result = gr.TextArea(label='Reply', show_copy_button=True, placeholder='Text Reply or inner status callback, depends on `pipeline type`')
result = gr.Markdown('>Text reply or inner status callback here, depends on `pipeline type`', label='Reply', show_label=True, header_links=True, line_breaks=True, show_copy_button=True)
# result = gr.TextArea(label='Reply', show_copy_button=True, placeholder='Text Reply or inner status callback, depends on `pipeline type`')

run_button.click(predict, [input_question, input_image], [result])
demo.queue()
demo.launch(share=False, server_name='0.0.0.0', debug=True)
28 changes: 27 additions & 1 deletion huixiangdou/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import uvicorn
import json

assistant = SerialPipeline(work_dir='workdir', config_path='config.ini')
assistant = None
app = FastAPI(docs_url='/')

class Talk(BaseModel):
Expand Down Expand Up @@ -66,5 +66,31 @@ def event_stream():
yield json.dumps(pipeline)
return StreamingResponse(event_stream(), media_type="text/event-stream")

def parse_args():
"""Parse args."""
parser = argparse.ArgumentParser(description='SerialPipeline.')
parser.add_argument('--work_dir',
type=str,
default='workdir',
help='Working directory.')
parser.add_argument(
'--config_path',
default='config.ini',
type=str,
help='Configuration path. Default value is config.ini')
parser.add_argument('--standalone',
action='store_true',
default=False,
help='Auto deploy required Hybrid LLM Service.')
parser.add_argument('--pipeline', type=str, choices=['chat_with_repo', 'chat_in_group'], default='chat_with_repo',
help='Select pipeline type for difference scenario, default value is `chat_with_repo`')
args = parser.parse_args()
return args

if __name__ == '__main__':
args = parse_args()
if 'chat_with_repo' in args.pipeline:
assistant = ParallelPipeline(work_dir=args.work_dir, config_path=args.config_path)
elif 'chat_in_group' in args.pipeline:
assistant = SerialPipeline(work_dir=args.work_dir, config_path=args.config_path)
uvicorn.run(app, host='0.0.0.0', port=23333, log_level='info')
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@ fastapi
uvicorn
termcolor
opencv-python-headless
gradio
gradio>=4.41

0 comments on commit 6c9c300

Please sign in to comment.