Skip to content

Commit bbc60da

Browse files
committed
add web ui
1 parent 7dea0ad commit bbc60da

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed

‎README.md‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
法律AI助手
22
=========
33

4-
法律AI助手,法律RAG,通过倒入全部200+本法律手册、网页搜索内容结合LLM回答你的问题,并且给出对应的法规和网站,基于langchain,openai,chroma,duckduckgo-search
4+
法律AI助手,法律RAG,通过倒入全部200+本法律手册、网页搜索内容结合LLM回答你的问题,并且给出对应的法规和网站,基于langchain,openai,chroma,duckduckgo-search, Gradio
55

66
## 原理
77

@@ -40,6 +40,14 @@ pip install -r requirements.txt
4040
python manager.py --init
4141
```
4242

43+
## 运行web ui
44+
45+
```
46+
python manager.py --web
47+
```
48+
49+
<a href="https://sm.ms/image/DbP3TiHZConUFe7" target="_blank"><img src="https://s2.loli.net/2023/10/20/DbP3TiHZConUFe7.png" ></a>
50+
4351
## 运行对话
4452

4553
```
@@ -51,5 +59,7 @@ python manager.py --shell
5159
## 配置修改
5260

5361
如果你想修改回答中的法律条数和网页条数,可以修改config.py
54-
- 法律条数:LAW_VS_SEARCH_K
55-
- 网页条数:WEB_VS_SEARCH_K
62+
- 法律条数: LAW_VS_SEARCH_K
63+
- 网页条数: WEB_VS_SEARCH_K
64+
- web ui地址: WEB_HOST
65+
- web ui端口: WEB_PORT

‎config.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,8 @@ class Config:
1111
WEB_VS_COLLECTION_NAME = "web"
1212
WEB_VS_SEARCH_K = 2
1313

14+
WEB_HOST = "0.0.0.0"
15+
WEB_PORT = 7860
16+
1417

1518
config = Config()

‎manager.py‎

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def init_vectorstore():
2929
pprint(dict(info))
3030

3131

32-
async def run_shell():
32+
def get_law_chain():
3333
llm = get_llm()
3434
law_vs = get_vectorstore(config.LAW_VS_COLLECTION_NAME)
3535
web_vs = get_vectorstore(config.WEB_VS_COLLECTION_NAME)
@@ -47,6 +47,12 @@ async def run_shell():
4747
return_source_documents=True,
4848
)
4949

50+
return chain
51+
52+
53+
async def run_shell():
54+
chain = get_law_chain()
55+
5056
while True:
5157
query = input("\n用户:")
5258
if query.strip() == "stop":
@@ -55,8 +61,8 @@ async def run_shell():
5561
callback = AsyncIteratorCallbackHandler()
5662
task = asyncio.create_task(
5763
chain.ainvoke({"query": query}, config={"callbacks": [callback]}))
58-
async for t in callback.aiter():
59-
print(t, end="", flush=True)
64+
async for new_token in callback.aiter():
65+
print(new_token, end="", flush=True)
6066

6167
print("\n")
6268
res = await task
@@ -65,6 +71,34 @@ async def run_shell():
6571
print(f"{source_text(docs)}")
6672

6773

74+
async def run_web():
75+
import gradio as gr
76+
77+
chain = get_law_chain()
78+
79+
async def chat(message, history):
80+
callback = AsyncIteratorCallbackHandler()
81+
task = asyncio.create_task(
82+
chain.ainvoke({"query": message}, config={"callbacks": [callback]}))
83+
84+
response = ""
85+
async for new_token in callback.aiter():
86+
response += new_token
87+
yield response
88+
89+
res = await task
90+
_, docs = res['result'], res['source_documents']
91+
92+
response += "\n" + source_text(docs)
93+
yield response
94+
95+
demo = gr.ChatInterface(
96+
fn=chat, examples=["故意杀了一个人,会判几年?", "杀人自首会减刑吗?"], title="法律AI小助手")
97+
98+
demo.queue()
99+
demo.launch(server_name=config.WEB_HOST, server_port=config.WEB_PORT)
100+
101+
68102
if __name__ == '__main__':
69103
import argparse
70104
parser = argparse.ArgumentParser(
@@ -85,6 +119,15 @@ async def run_shell():
85119
run shell
86120
''')
87121
)
122+
parser.add_argument(
123+
"-w",
124+
"--web",
125+
action="store_true",
126+
help=('''
127+
run web
128+
''')
129+
)
130+
88131

89132
if len(sys.argv) <= 1:
90133
parser.print_help()
@@ -95,3 +138,5 @@ async def run_shell():
95138
init_vectorstore()
96139
if args.shell:
97140
asyncio.run(run_shell())
141+
if args.web:
142+
asyncio.run(run_web())

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ openai==0.28.1
33
chromadb==0.4.14
44
tiktoken==0.5.1
55
duckduckgo-search==3.9.3
6+
gradio==3.50.0

0 commit comments

Comments
 (0)