在人工智能飞速发展的当下,大模型的应用越来越广泛。DeepSeek作为一款强大的模型,能够在本地进行搭建部署,从而实现个性化的知识库构建以及智能体应用,这为开发者和爱好者提供了极大的便利。本文将以详细的图文步骤,带领大家一步步完成DeepSeek的本地搭建部署、知识库搭建以及智能体的创建,让你轻松掌握这一前沿技术。
virtualenv
(如果未安装):pip install virtualenv
- 创建虚拟环境,假设环境名为`deepseek_env`:
virtualenv -p python3.8 deepseek_env
- 在Windows系统中激活虚拟环境:
deepseek_env\Scripts\activate
- 在Ubuntu系统中激活虚拟环境:
source deepseek_env/bin/activate
requirements.txt
文件,内容如下:torch
transformers
sentencepiece
然后在命令行中运行以下命令安装依赖:
pip install -r requirements.txt
安装过程可能需要一些时间,取决于网络速度。
deepseek_model
。run_deepseek.py
,内容如下:import torch
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained('deepseek_model')
model = AutoModel.from_pretrained('deepseek_model')
text = "你好,DeepSeek"
inputs = tokenizer(text, return_tensors='pt')
outputs = model(**inputs)
print(outputs)
python run_deepseek.py
如果一切顺利,你将看到模型输出的结果,这表明DeepSeek模型已在本地成功部署运行。
re
库等工具去除文本中的噪声,如HTML标签、特殊字符、多余的空格等。示例代码如下:import re
def clean_text(text):
text = re.sub('<.*?>', '', text) # 去除HTML标签
text = re.sub('[^a-zA-Z0-9\s]', '', text) # 去除特殊字符
text = re.sub('\s+',' ', text).strip() # 去除多余空格并修剪字符串
return text
nltk
库进行句子分割,示例代码如下:import nltk
nltk.download('punkt')
from nltk.tokenize import sent_tokenize
def split_text(text):
sentences = sent_tokenize(text)
return sentences
sentence-transformers
库为例,对文本进行向量化:from sentence_transformers import SentenceTransformer
model = SentenceTransformer('all-MiniLM-L6-v2')
texts = ["这是一个示例句子", "另一个示例句子"]
embeddings = model.encode(texts)
print(embeddings)
pip install faiss - gpu
import faiss
import numpy as np
# 假设embeddings是之前生成的文本向量,texts是对应的原始文本
d = embeddings.shape[1] # 向量维度
index = faiss.IndexFlatL2(d)
index.add(embeddings)
# 可以将原始文本存储在一个列表中,通过索引对应
text_list = texts
这样,知识库就搭建完成,后续可以通过向量检索从知识库中获取相关信息。
LangChain
框架来创建智能体,它提供了丰富的工具和接口,方便与各种模型和数据源集成。LangChain
库:pip install langchain
agent_setup.py
,内容如下:from langchain.llms import HuggingFacePipeline
from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser
from langchain.prompts import StringPromptTemplate
from langchain import SerpAPIWrapper, LLMChain
from langchain.schema import AgentAction, AgentFinish
import re
from langchain.vectorstores import FAISS
from langchain.embeddings import SentenceTransformerEmbeddings
from langchain.chains import RetrievalQA
from transformers import pipeline
# 加载本地DeepSeek模型作为LLM
pipe = pipeline(
"text-generation",
model='deepseek_model',
tokenizer='deepseek_model',
max_length=100
)
llm = HuggingFacePipeline(pipeline=pipe)
# 加载知识库
embeddings = SentenceTransformerEmbeddings(model_name='all-MiniLM-L6-v2')
vectorstore = FAISS.load_local('faiss_index', embeddings)
retriever = vectorstore.as_retriever()
qa_chain = RetrievalQA.from_chain_type(llm, retriever=retriever)
# 定义工具
tools = [
Tool(
name="KnowledgeBase",
func=qa_chain.run,
description="Useful for answering questions related to the specific knowledge in the database. Input should be a question."
)
]
# 定义提示模板
template = """You are a helpful AI assistant. You have access to the following tools:
{tools}
Use the following format:
Question: the input question you must answer
Thought: you should always think about which tool to use
Action: the action to take, should be one of [{tool_names}]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
Begin!
Question: {input}"""
class CustomOutputParser(AgentOutputParser):
def parse(self, llm_output):
if "Final Answer:" in llm_output:
return AgentFinish(
return_values={"output": llm_output.split("Final Answer:")[-1].strip()},
log=llm_output
)
regex = r"Action: (.*?)\nAction Input:[\s]*(.*)"
match = re.search(regex, llm_output)
if not match:
raise ValueError(f"Could not parse LLM output: `{llm_output}`")
action = match.group(1).strip()
action_input = match.group(2)
return AgentAction(tool=action, tool_input=action_input.strip(" ").strip('"'), log=llm_output)
prompt = StringPromptTemplate(
template=template,
tools=tools,
input_variables=["input"]
)
output_parser = CustomOutputParser()
llm_chain = LLMChain(llm=llm, prompt=prompt)
agent = LLMSingleActionAgent(
llm_chain=llm_chain,
output_parser=output_parser,
tools=tools,
verbose=True
)
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
在agent_setup.py
脚本所在目录的命令行中运行以下代码,测试智能体:
question = "关于知识库中的某个问题"
agent_executor.run(question)
智能体将根据输入的问题,利用知识库中的信息和DeepSeek模型进行回答。
通过以上详细的步骤,我们完成了DeepSeek的本地搭建部署、知识库的搭建以及智能体的创建。这一过程不仅让我们深入了解了大模型的应用,还为我们开发个性化的智能应用奠定了基础。在实际应用中,可以根据具体需求进一步优化模型、知识库和智能体的性能和功能。希望本文能帮助大家快速上手DeepSeek相关技术,开启属于自己的人工智能创新之旅。