Qwen+langchain使用流程

本文介绍了如何在本地加载HuggingFace的预训练模型,如Qwen-7B-Chat,构建pipeline并使用PromptTemplate创建模板驱动的文本生成任务,包括单一模型和多模型链式调用,以及利用FAISS进行向量知识库的查询和融合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

导入本地huggingface的模型:

tokenizer = AutoTokenizer.from_pretrained('./Qwen/Qwen-7B-Chat', trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained('./Qwen/Qwen-7B-Chat', device_map="cuda", trust_remote_code=True).eval()`

使用本地的模型构建pipeline(使用langchain的pipeline模型)

pipe1 = pipeline(
    "text2text-generation",
	model=model,
    tokenizer=tokenizer,
    max_length=512,
    temperature=0.8,
    top_p=0.6,
    repetition_penalty=1.5
)
llm = HuggingFacePipeline(pipeline=pipe1)

构建prompt

  1. 稍微规范一点的方法
    首先定义一个template,例如
		  """Tell me a story about {heros}"""

再使用template定义一个prompt,
其中如果想简单使用的话可以直接调用
个人理解是会自动分出其中的input但是没法指定output

		 PromptTemplate.from_template(template = template)

如果想规定多输入输出(链式流水线需要使用)

	  PromptTemplate( 
	 template = template, 
	  input_variables =["heros","xxx"],
	  output_variables = {"xxx"},
	  verbose = True
	   )
  1. 不是很规范的方法其实跟上面是一样的,(直接按照template的格式写进去)
PromptTemplate.from_template(
"""
	tell me a story about {heros}	
"""
)

构建chain

chain的用处是使用流水线式的大模型链式调用,当然同样可以run或者invoke单一模型
运行单一模型的方式

#方法一  
llm.run(prompt.format(heros = "MonkeyKing"))  
# 相当于使用llm直接run了tell me a story about MonkeyKing
#----------------------------------------------------------
#方法二(前文已经定义了llm)  
chain = LLMChain(llm = llm,prompt = prompt,outputkey = "XXX")
#其中llm和prompt是前文自己定义的,outputkey最好与你的output相同
chain.run(her
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值