本文重点介绍如何将
deepseek-7b-chat
langchain
langchain.llms.base
llm
gradio
除了运行模型所需的基础依赖外,还需安装特定版本的 langchain 库:
pip install langchain==0.0.292
为了更高效地开发基于 LLM 的应用,我们可以基于本地部署的
deepseek-7b-chat
实现过程较为直接:只需继承
langchain.llms.base.LLM
_call
from langchain.llms.base import LLM
from typing import Any, List, Optional
from langchain.callbacks.manager import CallbackManagerForLLMRun
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
import torch
<p>class DeepSeek_LLM(LLM):</p>
<div class="aritcle_card">
<a class="aritcle_card_img" href="/ai/1334">
<img src="https://img.php.cn/upload/ai_manual/000/000/000/175679976228579.png" alt="火山方舟">
</a>
<div class="aritcle_card_info">
<a href="/ai/1334">火山方舟</a>
<p>火山引擎一站式大模型服务平台,已接入满血版DeepSeek</p>
<div class="">
<img src="/static/images/card_xiazai.png" alt="火山方舟">
<span>99</span>
</div>
</div>
<a href="/ai/1334" class="aritcle_card_btn">
<span>查看详情</span>
<img src="/static/images/cardxiayige-3.png" alt="火山方舟">
</a>
</div>
<h1>自定义基于本地 DeepSeek-7B-chat 的 LLM 类</h1><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">tokenizer: AutoTokenizer = None
model: AutoModelForCausalLM = None
def __init__(self, model_path: str):
# model_path: DeepSeek-7B-chat 模型的本地路径
# 初始化 tokenizer 和模型
super().__init__()
print("正在从本地加载模型...")
self.tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
self.model = AutoModelForCausalLM.from_pretrained(
model_path,
trust_remote_code=True,
torch_dtype=torch.bfloat16,
device_map="auto"
)
self.model.generation_config = GenerationConfig.from_pretrained(model_path)
self.model.generation_config.pad_token_id = self.model.generation_config.eos_token_id
self.model = self.model.eval()
print("模型加载完成")
def _call(self, prompt: str, stop: Optional[List[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
**kwargs: Any) -> str:
# 重写生成逻辑
messages = [
{"role": "user", "content": prompt}
]
# 构建输入张量
input_tensor = self.tokenizer.apply_chat_template(
messages,
add_generation_prompt=True,
return_tensors="pt"
).to(self.model.device)
# 生成输出
outputs = self.model.generate(input_tensor, max_new_tokens=100)
response = self.tokenizer.decode(outputs[0][input_tensor.shape[1]:], skip_special_tokens=True)
return response
@property
def _llm_type(self) -> str:
return "DeepSeek_LLM"</code></pre><h4>使用示例</h4><p>完成自定义类后,便可像使用任何 LangChain 内置大模型一样进行调用:</p><pre><code class="javascript">llm = DeepSeek_LLM('/root/autodl-tmp/deepseek-ai/deepseek-llm-7b-chat')llm('你好')
效果如下图所示:
![[大模型]DeepSeek-7B-chat langchain 接入](https://img.php.cn/upload/article/001/503/042/175428169821661.jpg)
以上就是[大模型]DeepSeek-7B-chat langchain 接入的详细内容,更多请关注php中文网其它相关文章!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号