【书生大模型实战营】进阶岛 第6关 MindSearch 快速部署

2024-09-03 06:36

本文主要是介绍【书生大模型实战营】进阶岛 第6关 MindSearch 快速部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 【书生大模型实战营】进阶岛 第6关 MindSearch 快速部署
  • MindSearch 部署到Github Codespace 和 Hugging Face Space
  • 创建开发机 & 环境配置
  • MindSearch下载及环境配置
  • 获取硅基流动API Key
  • 作业 - 基础任务
  • 在Github codespaces 启动 MindSearch
  • 通过 Github Codespace 完成HuggingFace Space部署
  • 把刚才准备的文件都copy进来

【书生大模型实战营】进阶岛 第6关 MindSearch 快速部署

MindSearch 部署到Github Codespace 和 Hugging Face Space

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

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

创建开发机 & 环境配置

由于HuggingFace被墙,我们通过github-codespace 的 vscode Linux 环境提交到 hugging face。打开codespace主页,选择blank template,浏览器会自动在新的页面打开一个web版的vscode。
在这里插入图片描述

MindSearch下载及环境配置

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

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

获取硅基流动API Key

首先打开 https://account.siliconflow.cn/login 来注册硅基流动的账号。完成注册后,打开 https://cloud.siliconflow.cn/account/ak 来准备 API Key。首先创建新 API 密钥,然后点击密钥进行复制,以备后续使用。
在这里插入图片描述

作业 - 基础任务

将 MindSearch 部署到 HuggingFace,并提供截图。(记录复现过程并截图)

在Github codespaces 启动 MindSearch

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

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

cd /workspaces/mindsearch/MindSearch
python -m mindsearch.app --lang cn --model_format internlm_silicon --search_engine DuckDuckGoSearch
在这里插入图片描述

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

cd /workspaces/mindsearch/MindSearch
python frontend/mindsearch_gradio.py
在这里插入图片描述

通过InternLM 启动web浏览器访问
上述同样步骤在InternLM开发机(10%A100即可)部署启动后 把 8002 端口和 7882 端口都映射到本地:

ssh -CNg -L 8002:127.0.0.1:8002 -L 7882:127.0.0.1:7882 root@ssh.intern-ai.org.cn -p 42678
然后在本地浏览器中打开 localhost:7882 即可体验啦。
在这里插入图片描述
效果

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

通过 Github Codespace 完成HuggingFace Space部署

我们首先打开 https://huggingface.co/spaces ,并点击 Create new Space。然后进入 Settings,配置硅基流动的 API Key。选择 New secrets,name 一栏输入 SILICON_API_KEY,value 一栏输入你的 API Key 的内容。

在这里插入图片描述

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

#创建新目录
mkdir -p /root/mindsearch/mindsearch_deploy
#准备复制文件
cd /root/mindsearch
cp -r /root/mindsearch/MindSearch/mindsearch /root/mindsearch/mindsearch_deploy
cp /root/mindsearch/MindSearch/requirements.txt /root/mindsearch/mindsearch_deploy
#创建 app.py 作为程序入口
touch /root/mindsearch/mindsearch_deploy/app.py
其中,app.py 的内容如下:
import json
import os

import gradio as gr
import requests
from lagent.schema import AgentStatusCode

os.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_searcher

def 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: E501 elif 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, ‘’])
return

def 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_HISTORY
PLANNER_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_searcher
return history_planner, history_searcher

with gr.Blocks() as demo:
gr.HTML(“”“

MindSearch Gradio Demo

”“”)
gr.HTML(“”“

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).

”“”)
gr.HTML(“”"

🔗 GitHub
📄 Arxiv
📚 Hugging Face Papers
🤗 Hugging Face Demo

“”")
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。在Codespaces shell 命令行窗口中执行:

cd /workspaces/codespaces-blank
git clone https://zhangdeqiang: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@huggingface.co/spaces/zed5337/MyMindSearch
codespace就是本地仓库,huggingface space是远程仓库,接下来使用方法就和常规的git一样了。

cd /workspaces/codespaces-blank/MyMindSearch

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

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

把上述代码提交到huggingface space。

在这里插入图片描述
后续一样就不在多做赘述。

这篇关于【书生大模型实战营】进阶岛 第6关 MindSearch 快速部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1132302

相关文章

快速修复一个Panic的Linux内核的技巧

《快速修复一个Panic的Linux内核的技巧》Linux系统中运行了不当的mkinitcpio操作导致内核文件不能正常工作,重启的时候,内核启动中止于Panic状态,该怎么解决这个问题呢?下面我们就... 感谢China编程(www.chinasem.cn)网友 鸢一雨音 的投稿写这篇文章是有原因的。为了配置完

Python利用ElementTree实现快速解析XML文件

《Python利用ElementTree实现快速解析XML文件》ElementTree是Python标准库的一部分,而且是Python标准库中用于解析和操作XML数据的模块,下面小编就来和大家详细讲讲... 目录一、XML文件解析到底有多重要二、ElementTree快速入门1. 加载XML的两种方式2.

Python列表去重的4种核心方法与实战指南详解

《Python列表去重的4种核心方法与实战指南详解》在Python开发中,处理列表数据时经常需要去除重复元素,本文将详细介绍4种最实用的列表去重方法,有需要的小伙伴可以根据自己的需要进行选择... 目录方法1:集合(set)去重法(最快速)方法2:顺序遍历法(保持顺序)方法3:副本删除法(原地修改)方法4:

在Spring Boot中浅尝内存泄漏的实战记录

《在SpringBoot中浅尝内存泄漏的实战记录》本文给大家分享在SpringBoot中浅尝内存泄漏的实战记录,结合实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录使用静态集合持有对象引用,阻止GC回收关键点:可执行代码:验证:1,运行程序(启动时添加JVM参数限制堆大小):2,访问 htt

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

利用Python快速搭建Markdown笔记发布系统

《利用Python快速搭建Markdown笔记发布系统》这篇文章主要为大家详细介绍了使用Python生态的成熟工具,在30分钟内搭建一个支持Markdown渲染、分类标签、全文搜索的私有化知识发布系统... 目录引言:为什么要自建知识博客一、技术选型:极简主义开发栈二、系统架构设计三、核心代码实现(分步解析

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S