当前位置:   article > 正文

langchain 如何使用本地大模型(LLM)_langchain调用本地大模型

langchain调用本地大模型

langchain 很多例子里面,默认都是调用的OpenAI的模型,但是有时候我们希望使用自己本地的大模型。具体代码如下: 

  1. from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
  2. from langchain import LLMChain,HuggingFacePipeline,PromptTemplate
  3. import torch
  4. model_path = "写入模型存在路径"
  5. device = torch.device("cuda:0")
  6. tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
  7. model = AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, device_map="auto").half()
  8. pipe = pipeline(
  9. "text-generation",
  10. model=model,
  11. tokenizer=tokenizer,
  12. max_length=512,
  13. top_p=1,
  14. repetition_penalty=1.15
  15. )
  16. llama_model = HuggingFacePipeline(pipeline=pipe)
  17. template = '''
  18. #背景信息#
  19. 你是一名知识丰富的导航助手,了解中国每一个地方的名胜古迹及旅游景点.
  20. #问题#
  21. 游客:我想去{地方}旅游,给我推荐一下值得玩的地方?"
  22. '''
  23. prompt = PromptTemplate(
  24. input_variables=["地方"],
  25. template=template
  26. )
  27. chain = LLMChain(llm=llama_model, prompt=prompt)
  28. print(chain.run("天津"))

注意: 

这行代码一定要写成 device_map="auto"

AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, device_map="auto").half() 

如果代码写成.cuda(),具体如下

AutoModelForCausalLM.from_pretrained(model_path, trust_remote_code=True, ).half().cuda

会报错:

RuntimeError: Expected all tensors to be on the same device, but found at least two devices.

或者

warning : 

You are calling .generate() with the input_ids being on a device type different than your model's device. input_ids is on cuda, whereas the model is on cpu. You may experience unexpected behaviors or slower generation. Please make sure that you have put input_ids to the correct device by calling for example input_ids = input_ids.to('cpu') before running .generate().
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/821648
推荐阅读
相关标签
  

闽ICP备14008679号