导入本地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
- 稍微规范一点的方法
首先定义一个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
)
- 不是很规范的方法
其实跟上面是一样的,(直接按照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