书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

随笔2周前发布 枕樎
36 0 0
基础任务
  • 按照教程,将 MindSearch 部署到 HuggingFace 并美化 Gradio 的界面,并提供截图和 Hugging Face 的Space的链接。

MindSearch 部署到Github Codespace 和 Hugging Face Space

1. 创建开发机 & 环境配置

打开codespace主页,选择blank template。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

浏览器会自动在新的页面打开一个web版的vscode。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

接下来的操作就和我们使用vscode基本没差别了。

然后我们新建一个目录用于存放 MindSearch 的相关代码,并把 MindSearch 仓库 clone 下来。在终端中运行下面的命令:

  1. mkdir -p /workspaces/mindsearch

  2. cd /workspaces/mindsearch

  3. git clone https://github.com/InternLM/MindSearch.git

  4. cd MindSearch && git checkout b832275 && cd ..

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

接下来,我们创建一个 conda 环境来安装相关依赖。

  1. # 创建环境

  2. conda create -n hjl python=3.10 -y

  3. # 激活环境

  4. conda activate hjl

  5. # 安装依赖

  6. pip install -r /workspaces/mindsearch/MindSearch/requirements.txt

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

2. 获取硅基流动 API Key

因为要使用硅基流动的 API Key,所以接下来便是注册并获取 API Key 了。

首先,我们打开 硅基流动统一登录 来注册硅基流动的账号(如果注册过,则直接登录即可)。

在完成注册后,打开 硅基流动统一登录 来准备 API Key。首先创建新 API 密钥,然后点击密钥进行复制,以备后续使用。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

3. 启动 MindSearch

3.1 启动后端

由于硅基流动 API 的相关配置已经集成在了 MindSearch 中,所以我们可以直接执行下面的代码来启动 MindSearch 的后端。

  1. export SILICON_API_KEY=第二步中复制的密钥

  2. conda activate hjl

  3. cd /workspaces/mindsearch/MindSearch

  4. python -m mindsearch.app --lang cn --model_format internlm_silicon --search_engine DuckDuckGoSearch

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

3.2 启动前端

在后端启动完成后,我们打开新终端运行如下命令来启动 MindSearch 的前端。

  1. conda activate hjl

  2. cd /workspaces/mindsearch/MindSearch

  3. python frontend/mindsearch_gradio.py

前后端都启动后,我们应该可以看到github自动为这两个进程做端口转发。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

部署完成!

4. 部署到 HuggingFace Space

最后,我们来将 MindSearch 部署到 HuggingFace Space。

我们首先打开 https://huggingface.co/spaces ,并点击 Create new Space,如下图所示。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

在输入 Space name 并选择 License 后,选择配置如下所示。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

然后,我们进入 Settings,配置硅基流动的 API Key。如下图所示。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

选择 New secrets,name 一栏输入 SILICON_API_KEY,value 一栏输入你的 API Key 的内容。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

最后,我们先新建一个目录,准备提交到 HuggingFace Space 的全部文件。

  1. # 创建新目录

  2. mkdir -p /workspaces/mindsearch/mindsearch_deploy

  3. # 准备复制文件

  4. cd /workspaces/mindsearch

  5. cp -r /workspaces/mindsearch/MindSearch/mindsearch /workspaces/mindsearch/mindsearch_deploy

  6. cp /workspaces/mindsearch/MindSearch/requirements.txt /workspaces/mindsearch/mindsearch_deploy

  7. # 创建 app.py 作为程序入口

  8. touch /workspaces/mindsearch/mindsearch_deploy/app.py

其中,app.py 的内容如下:

  1. import json

  2. import os

  3. import gradio as gr

  4. import requests

  5. from lagent.schema import AgentStatusCode

  6. os.system("python -m mindsearch.app --lang cn --model_format internlm_silicon &")

  7. PLANNER_HISTORY = []

  8. SEARCHER_HISTORY = []

  9. def rst_mem(history_planner: list, history_searcher: list):

  10. '''

  11. Reset the chatbot memory.

  12. '''

  13. history_planner = []

  14. history_searcher = []

  15. if PLANNER_HISTORY:

  16. PLANNER_HISTORY.clear()

  17. return history_planner, history_searcher

  18. def format_response(gr_history, agent_return):

  19. if agent_return['state'] in [

  20. AgentStatusCode.STREAM_ING, AgentStatusCode.ANSWER_ING

  21. ]:

  22. gr_history[-1][1] = agent_return['response']

  23. elif agent_return['state'] == AgentStatusCode.PLUGIN_START:

  24. thought = gr_history[-1][1].split('```')[0]

  25. if agent_return['response'].startswith('```'):

  26. gr_history[-1][1] = thought + ' ' + agent_return['response']

  27. elif agent_return['state'] == AgentStatusCode.PLUGIN_END:

  28. thought = gr_history[-1][1].split('```')[0]

  29. if isinstance(agent_return['response'], dict):

  30. gr_history[-1][

  31. 1] = thought + ' ' + f'```json {json.dumps(agent_return["response"], ensure_ascii=False, indent=4)} ```' # noqa: E501

  32. elif agent_return['state'] == AgentStatusCode.PLUGIN_RETURN:

  33. assert agent_return['inner_steps'][-1]['role'] == 'environment'

  34. item = agent_return['inner_steps'][-1]

  35. gr_history.append([

  36. None,

  37. f"```json {json.dumps(item['content'], ensure_ascii=False, indent=4)} ```"

  38. ])

  39. gr_history.append([None, ''])

  40. return

  41. def predict(history_planner, history_searcher):

  42. def streaming(raw_response):

  43. for chunk in raw_response.iter_lines(chunk_size=8192,

  44. decode_unicode=False,

  45. delimiter=b' '):

  46. if chunk:

  47. decoded = chunk.decode('utf-8')

  48. if decoded == ' ':

  49. continue

  50. if decoded[:6] == 'data: ':

  51. decoded = decoded[6:]

  52. elif decoded.startswith(': ping - '):

  53. continue

  54. response = json.loads(decoded)

  55. yield (response['response'], response['current_node'])

  56. global PLANNER_HISTORY

  57. PLANNER_HISTORY.append(dict(role='user', content=history_planner[-1][0]))

  58. new_search_turn = True

  59. url = 'http://localhost:8002/solve'

  60. headers = {'Content-Type': 'application/json'}

  61. data = {'inputs': PLANNER_HISTORY}

  62. raw_response = requests.post(url,

  63. headers=headers,

  64. data=json.dumps(data),

  65. timeout=20,

  66. stream=True)

  67. for resp in streaming(raw_response):

  68. agent_return, node_name = resp

  69. if node_name:

  70. if node_name in ['root', 'response']:

  71. continue

  72. agent_return = agent_return['nodes'][node_name]['detail']

  73. if new_search_turn:

  74. history_searcher.append([agent_return['content'], ''])

  75. new_search_turn = False

  76. format_response(history_searcher, agent_return)

  77. if agent_return['state'] == AgentStatusCode.END:

  78. new_search_turn = True

  79. yield history_planner, history_searcher

  80. else:

  81. new_search_turn = True

  82. format_response(history_planner, agent_return)

  83. if agent_return['state'] == AgentStatusCode.END:

  84. PLANNER_HISTORY = agent_return['inner_steps']

  85. yield history_planner, history_searcher

  86. return history_planner, history_searcher

  87. with gr.Blocks() as demo:

  88. gr.HTML("""<h1 align="center">MindSearch Gradio Demo</h1>""")

  89. gr.HTML("""<p style="text-align: center; font-family: Arial, sans-serif;">MindSearch is an open-source AI Search Engine Framework with Perplexity.ai Pro performance. You can deploy your own Perplexity.ai-style search engine using either closed-source LLMs (GPT, Claude) or open-source LLMs (InternLM2.5-7b-chat).</p>""")

  90. gr.HTML("""

  91. <div style="text-align: center; font-size: 16px;">

  92. <a href="https://github.com/InternLM/MindSearch" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">🔗 GitHub</a>

  93. <a href="https://arxiv.org/abs/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">📄 Arxiv</a>

  94. <a href="https://huggingface.co/papers/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">📚 Hugging Face Papers</a>

  95. <a href="https://huggingface.co/spaces/internlm/MindSearch" style="text-decoration: none; color: #4A90E2;">🤗 Hugging Face Demo</a>

  96. </div>

  97. """)

  98. with gr.Row():

  99. with gr.Column(scale=10):

  100. with gr.Row():

  101. with gr.Column():

  102. planner = gr.Chatbot(label='planner',

  103. height=700,

  104. show_label=True,

  105. show_copy_button=True,

  106. bubble_full_width=False,

  107. render_markdown=True)

  108. with gr.Column():

  109. searcher = gr.Chatbot(label='searcher',

  110. height=700,

  111. show_label=True,

  112. show_copy_button=True,

  113. bubble_full_width=False,

  114. render_markdown=True)

  115. with gr.Row():

  116. user_input = gr.Textbox(show_label=False,

  117. placeholder='帮我搜索一下 InternLM 开源体系',

  118. lines=5,

  119. container=False)

  120. with gr.Row():

  121. with gr.Column(scale=2):

  122. submitBtn = gr.Button('Submit')

  123. with gr.Column(scale=1, min_width=20):

  124. emptyBtn = gr.Button('Clear History')

  125. def user(query, history):

  126. return '', history + [[query, '']]

  127. submitBtn.click(user, [user_input, planner], [user_input, planner],

  128. queue=False).then(predict, [planner, searcher],

  129. [planner, searcher])

  130. emptyBtn.click(rst_mem, [planner, searcher], [planner, searcher],

  131. queue=False)

  132. demo.queue()

  133. demo.launch(server_name='0.0.0.0',

  134. server_port=7860,

  135. inbrowser=True,

  136. share=True)

在最后,将 /root/mindsearch/mindsearch_deploy 目录下的文件(使用 git)提交到 HuggingFace Space 即可完成部署了。将代码提交到huggingface space的流程如下:

首先创建一个有写权限的token。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

然后从huggingface把空的代码仓库clone到codespace。

  1. cd /workspaces/codespaces-blank

  2. git clone https://huggingface.co/spaces/<你的名字>/<仓库名称>

  3. # 把token挂到仓库上,让自己有写权限

  4. git remote set-url space https://<你的名字>:<上面创建的token>@huggingface.co/spaces/<你的名字>/<仓库名称>

如果报error: No such remote ‘space’,就git remote add space <远程仓库地址>

现在codespace就是本地仓库,huggingface space是远程仓库,接下来使用方法就和常规的git一样了。

  1. cd <仓库名称>

  2. # 把刚才准备的文件都copy进来

  3. cp -r /workspaces/mindsearch/mindsearch_deploy/* .

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

注意mindsearch文件夹其实是Mindsearch项目中的一个子文件夹,如果把这个Mindsearch的整个目录copy进来会有很多问题(git submodule无法提交代码,space中项目启动失败等)。

最后把代码提交到huggingface space会自动启动项目。

  1. git add .

  2. git commit -m "update"

  3. git push

总结下git push 遇到的错误:

1.一直说没有给token令牌

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

报错原因:运行 git remote -v,可以看到有一个origin分支,git push默认使用这个的token,然而这个并没有设置token,如图:书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

解决办法:把space设置成主main,执行git push –set-upstream space main,然后可以git push了。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

然后就可以测试啦。

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

成功!

5.美化 Gradio 的界面

5.1 打开mindsearch_gradio.py文件

对以下代码进行修改:

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

添加以下代码

gr.Blocks(css=".gradio-container {background: url('file=https://img.zcool.cn/community/011db3571600c432f8758c9b65191d.jpg@2o.jpg')}")

5.2 重新运行mindsearch_gradio.py文件

  1. conda activate hjl

  2. cd /workspaces/mindsearch/MindSearch

  3. python frontend/mindsearch_gradio.py

运行效果:

书生大模型实战营(暑假场)进阶岛关卡六——MindSearch 快速部署

© 版权声明

相关文章

暂无评论

您必须登录才能参与评论!
立即登录
暂无评论...