langChain通常使用api接口来调用大模型,可以直接访问官方的服务器或者用Ollama等搭建的本地服务器。langchain访问modelscope的官方服务可参照ModelScope | 🦜️🔗 LangChain
现在我们调用的是 modelscope上下载的用DeepSeek,由于本地显卡运算能力有限选用deepseek蒸馏出来的Qwen1.5B deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B
snapshot_download(llm_model_name, cache_dir=cache_apath)下载大模型到本地cache并返回大模型的路径
需要设计自己的类(基类为LLM),至少拥有3个函数__init__、_call、_llm_type
__init__:指定tokenizer和llm model
_call:对message进行tokenizer,输入大模型来生成响应,对生成的向量进行解码
_llm_type:给一个名字
import os
from langchain_core.prompts import PromptTemplate
from langchain.llms.base import LLM
from typing import Any, List, Optional
from modelscope import snapshot_download
from langchain.callbacks.manager import CallbackManagerForLLMRun
from transformers import AutoTokenizer, AutoModelForCausalLM, GenerationConfig
import tor

最低0.47元/天 解锁文章
1891

被折叠的 条评论
为什么被折叠?



