MindSearch 快速部署

news/2024/9/19 14:33:16/文章来源:https://www.cnblogs.com/ztyniubi/p/18420523

基础任务(完成此任务即完成闯关)

  • 按照教程,将 MindSearch 部署到 HuggingFace 并美化 Gradio 的界面,并提供截图和 Hugging Face 的Space的链接。

MindSearch 部署到Github Codespace 和 Hugging Face Space

和原有的CPU版本相比区别是把internstudio换成了github codespace。

随着硅基流动提供了免费的 InternLM2.5-7B-Chat 服务(免费的 InternLM2.5-7B-Chat 真的很香),MindSearch 的部署与使用也就迎来了纯 CPU 版本,进一步降低了部署门槛。那就让我们来一起看看如何使用硅基流动的 API 来部署 MindSearch 吧。

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

打开codespace主页,选择blank template。

我一直打不开

我一直都打不开,所以直接用开发机了

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

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

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

mkdir -p /workspaces/mindsearch
cd /workspaces/mindsearch
git clone https://github.com/InternLM/MindSearch.git
cd MindSearch && git checkout b832275 && cd ..

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

# 创建环境
conda create -n mindsearch python=3.10 -y
# 激活环境
conda activate mindsearch
# 安装依赖
pip install -r /workspaces/mindsearch/MindSearch/requirements.txt

2. 获取硅基流动 API Key

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

首先,我们打开 https://account.siliconflow.cn/login 来注册硅基流动的账号(如果注册过,则直接登录即可)。

在完成注册后,打开 https://cloud.siliconflow.cn/account/ak 来准备 API Key。首先创建新 API 密钥,然后点击密钥进行复制,以备后续使用。

3. 启动 MindSearch

3.1 启动后端

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

export SILICON_API_KEY=第二步中复制的密钥
conda activate mindsearch
cd /workspaces/mindsearch/MindSearch
python -m mindsearch.app --lang cn --model_format internlm_silicon --search_engine DuckDuckGoSearch

问题

3.2 启动前端

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

conda activate mindsearch
cd /workspaces/mindsearch/MindSearch
python frontend/mindsearch_gradio.py

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

由于使用codespace,这里我们不需要使用ssh端口转发了,github会自动提示我们打开一个在公网的前端地址。

然后就可以即刻体验啦。

如果遇到了 timeout 的问题,可以按照 文档 换用 Bing 的搜索接口。

4. 部署到 HuggingFace Space

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

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

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

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

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

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

# 创建新目录
mkdir -p /workspaces/mindsearch/mindsearch_deploy
# 准备复制文件
cd /workspaces/mindsearch
cp -r /workspaces/mindsearch/MindSearch/mindsearch /workspaces/mindsearch/mindsearch_deploy
cp /workspaces/mindsearch/MindSearch/requirements.txt /workspaces/mindsearch/mindsearch_deploy
# 创建 app.py 作为程序入口
touch /workspaces/mindsearch/mindsearch_deploy/app.py

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

import json
import osimport gradio as gr
import requests
from lagent.schema import AgentStatusCodeos.system("python -m mindsearch.app --lang cn --model_format internlm_silicon &")PLANNER_HISTORY = []
SEARCHER_HISTORY = []def rst_mem(history_planner: list, history_searcher: list):'''Reset the chatbot memory.'''history_planner = []history_searcher = []if PLANNER_HISTORY:PLANNER_HISTORY.clear()return history_planner, history_searcherdef format_response(gr_history, agent_return):if agent_return['state'] in [AgentStatusCode.STREAM_ING, AgentStatusCode.ANSWER_ING]:gr_history[-1][1] = agent_return['response']elif agent_return['state'] == AgentStatusCode.PLUGIN_START:thought = gr_history[-1][1].split('```')[0]if agent_return['response'].startswith('```'):gr_history[-1][1] = thought + '\n' + agent_return['response']elif agent_return['state'] == AgentStatusCode.PLUGIN_END:thought = gr_history[-1][1].split('```')[0]if isinstance(agent_return['response'], dict):gr_history[-1][1] = thought + '\n' + f'```json\n{json.dumps(agent_return["response"], ensure_ascii=False, indent=4)}\n```'  # noqa: E501elif agent_return['state'] == AgentStatusCode.PLUGIN_RETURN:assert agent_return['inner_steps'][-1]['role'] == 'environment'item = agent_return['inner_steps'][-1]gr_history.append([None,f"```json\n{json.dumps(item['content'], ensure_ascii=False, indent=4)}\n```"])gr_history.append([None, ''])returndef predict(history_planner, history_searcher):def streaming(raw_response):for chunk in raw_response.iter_lines(chunk_size=8192,decode_unicode=False,delimiter=b'\n'):if chunk:decoded = chunk.decode('utf-8')if decoded == '\r':continueif decoded[:6] == 'data: ':decoded = decoded[6:]elif decoded.startswith(': ping - '):continueresponse = json.loads(decoded)yield (response['response'], response['current_node'])global PLANNER_HISTORYPLANNER_HISTORY.append(dict(role='user', content=history_planner[-1][0]))new_search_turn = Trueurl = 'http://localhost:8002/solve'headers = {'Content-Type': 'application/json'}data = {'inputs': PLANNER_HISTORY}raw_response = requests.post(url,headers=headers,data=json.dumps(data),timeout=20,stream=True)for resp in streaming(raw_response):agent_return, node_name = respif node_name:if node_name in ['root', 'response']:continueagent_return = agent_return['nodes'][node_name]['detail']if new_search_turn:history_searcher.append([agent_return['content'], ''])new_search_turn = Falseformat_response(history_searcher, agent_return)if agent_return['state'] == AgentStatusCode.END:new_search_turn = Trueyield history_planner, history_searcherelse:new_search_turn = Trueformat_response(history_planner, agent_return)if agent_return['state'] == AgentStatusCode.END:PLANNER_HISTORY = agent_return['inner_steps']yield history_planner, history_searcherreturn history_planner, history_searcherwith gr.Blocks() as demo:gr.HTML("""<h1 align="center">MindSearch Gradio Demo</h1>""")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>""")gr.HTML("""<div style="text-align: center; font-size: 16px;"><a href="https://github.com/InternLM/MindSearch" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">🔗 GitHub</a><a href="https://arxiv.org/abs/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">📄 Arxiv</a><a href="https://huggingface.co/papers/2407.20183" style="margin-right: 15px; text-decoration: none; color: #4A90E2;">📚 Hugging Face Papers</a><a href="https://huggingface.co/spaces/internlm/MindSearch" style="text-decoration: none; color: #4A90E2;">🤗 Hugging Face Demo</a></div>""")with gr.Row():with gr.Column(scale=10):with gr.Row():with gr.Column():planner = gr.Chatbot(label='planner',height=700,show_label=True,show_copy_button=True,bubble_full_width=False,render_markdown=True)with gr.Column():searcher = gr.Chatbot(label='searcher',height=700,show_label=True,show_copy_button=True,bubble_full_width=False,render_markdown=True)with gr.Row():user_input = gr.Textbox(show_label=False,placeholder='帮我搜索一下 InternLM 开源体系',lines=5,container=False)with gr.Row():with gr.Column(scale=2):submitBtn = gr.Button('Submit')with gr.Column(scale=1, min_width=20):emptyBtn = gr.Button('Clear History')def user(query, history):return '', history + [[query, '']]submitBtn.click(user, [user_input, planner], [user_input, planner],queue=False).then(predict, [planner, searcher],[planner, searcher])emptyBtn.click(rst_mem, [planner, searcher], [planner, searcher],queue=False)demo.queue()
demo.launch(server_name='0.0.0.0',server_port=7860,inbrowser=True,share=True)

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

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

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

/workspaces/codespaces-blank
git clone https://huggingface.co/spaces/<你的名字>/<仓库名称>
# 把token挂到仓库上,让自己有写权限
git remote set-url space https://<你的名字>:<上面创建的token>@huggingface.co/spaces/<你的名字>/<仓库名称>

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

cd <仓库名称>
# 把刚才准备的文件都copy进来
cp /workspaces/mindsearch/mindsearch_deploy/* .

最终的文件目录是这样。

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

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

git add .
git commit -m "update"
git push

然后就可以测试啦。

不仅搜到了最近火热的黑悟空,还搜到了龙珠超中的黑悟空,非常给力!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/799871.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

小程序隐私合规自查指南

一 背景:小程序作为一种轻量级应用,广泛应用于各大互联网平台。工信部通报2022年第5批侵害用户权益名单中首次出现8款违规小程序。各监管单位对“小程序”违规收集个人信息监控手段和监控力度不断加强。 工信部APP违法违规通报 上海市委网信办查处违规小程序二、小程序隐私合…

Jmeter的简单使用一:http请求

1、创建线程组setUp和tearDown线程组类似测试用例的测试开始之前执行某些初始化操作,如环境准备、数据库连接和释放数据库连接2、设置线程组Ramp-Up时间(以秒为单位)是指从开始到所有线程都达到活动状态的时间。例如,如果你设置了10个线程,并且Ramp-Up时间为20秒,那么JMe…

Flags

Flags是位字段的序列,当其中任何一个位不为零且广播可连接时广播包中应包含flags. 否则,flags可以被忽略。flags只能包含在广播包中,扫描响应包中不能包含flags。flags的作用是在广播包中加入如下标志:有限可发现模式;一般可发现模式;不支持BR/EDR;设备同时支持LE和BR/E…

Oracle 19c OCP 认证考试 082 题库(第23题)- 2024年修正版

【优技教育】Oracle 19c OCP 082题库(Q 23题)- 2024年修正版 考试科目:1Z0-082 考试题量:90 通过分数:60% 考试时间:150min 本文为(CUUG 原创)整理并解析,转发请注明出处,禁止抄袭及未经注明出处的转载。 原文地址:http://www.cuug.com/index.php?s=/home/article/deta…

windows7遇到不兼容如何解决

概述: 低版本的Windows缺乏一些高版本中所新增的系统接口,而VxKex可以为程序提供这些缺失的接口从而使其正常运行 当然VxKex不仅可以用于lucky也可以使其他一些最低要求为win10的程序在win7上运行起来 详情见其github项目地址 不过目前对游戏的效果不佳 国内加速下载下载:http…

一文搞定WeakHashMapE0

写在前面 在缓存场景下,由于内存是有限的,不能缓存所有对象,因此就需要一定的删除机制,淘汰掉一些对象。这个时候可能很快就想到了各种Cache数据过期策略,目前也有一些优秀的包提供了功能丰富的Cache,比如Google的Guava Cache,它支持数据定期过期、LRU、LFU等策略,但它…

windows7不支持一些程序的运行,如何解决

低版本的Windows缺乏一些高版本中所新增的系统接口,而VxKex可以为程序提供这些缺失的接口从而使其正常运行 当然VxKex不仅可以用于lucky也可以使其他一些最低要求为win10的程序在win7上运行起来 详情见其github项目地址 不过目前对游戏的效果不佳 国内加速下载下载:https://da…

MBR4045PT-ASEMI低Low VF肖特基MBR4045PT

MBR4045PT-ASEMI低Low VF肖特基MBR4045PT编辑:ll MBR4045PT-ASEMI低Low VF肖特基MBR4045PT 型号:MBR4045PT 品牌:ASEMI 封装:TO-247 安装方式:插件 批号:最新 恢复时间:35ns 最大平均正向电流(IF):40A 最大循环峰值反向电压(VRRM):45V 最大正向电压(VF):0.75V~…

快速比较两个数据库所有表的字段是否一致

背景 在开发时,常常会有开发环境,测试环境,生产环境。当开发环境中的数据库结构发生变化时,往往需要同步到测试环境和生产环境,但是有时候会忘记同步了。那么,如何快速判断两个数据库的所有表字段是否一致呢? 需要工具:navicat(或类似数据库工具),Beyond Comapre(或…

Hadoop(二十)Yarn工作原理

Yarn资源调度器Yarn是一个资源调度平台,负责为运算程序提供服务器运算资源,相当于一个分布式的操作系统平台,而MapReduce等运算程序则相当于运行于操作系统之上的应用程序一、基础架构YARN主要由ResourceManager、NodeManager、ApplicationMaster和Container等组件构成二、Y…

springcloud负载均衡组件ribbon使用

一、微服务负载均衡ribbon策略如下: 1、线性轮询策略: RoundRibbonRule 2、重试策略:RetryRule 3、加权响应时间策略:WeightedResponseTimeRule 4、随机策略:RandomRule 5、最空闲策略:BestAvailableRule 6、区域感知轮询策略:ZoneAvoidanceRule(默认) 每个策略对应什…

LangChain4j炸裂!Java开发者打造AI应用从未如此简单

LangChain4j 的目标是简化将大语言模型(LLM)集成到 Java 应用程序中的过程。 1 实现方式 1.1 标准化 API LLM 提供商(如 OpenAI 或 Google Vertex AI)和向量嵌入存储(如 Pinecone 或 Milvus)使用专有 API。LangChain4j 提供了标准化 API,避免了每次都需要学习和实现特定…