使用 Hugging Face 模型

使用 Hugging Face 模型之前,需要先设置环境变量

  1. import os
  2. os.environ['HUGGINGFACEHUB_API_TOKEN'] = ''

使用在线的 Hugging Face 模型

  1. from langchain import PromptTemplate, HuggingFaceHub, LLMChain
  2. template = """Question: {question}
  3. Answer: Let's think step by step."""
  4. prompt = PromptTemplate(template=template, input_variables=["question"])
  5. llm = HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":0, "max_length":64})
  6. llm_chain = LLMChain(prompt=prompt, llm=llm)
  7. question = "What NFL team won the Super Bowl in the year Justin Beiber was born?"
  8. print(llm_chain.run(question))

将 Hugging Face 模型直接拉到本地使用

  1. from langchain import PromptTemplate, LLMChain
  2. from langchain.llms import HuggingFacePipeline
  3. from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline, AutoModelForSeq2SeqLM
  4. model_id = 'google/flan-t5-large'
  5. tokenizer = AutoTokenizer.from_pretrained(model_id)
  6. model = AutoModelForSeq2SeqLM.from_pretrained(model_id)
  7. pipe = pipeline(
  8. "text2text-generation",
  9. model=model,
  10. tokenizer=tokenizer,
  11. max_length=100
  12. )
  13. local_llm = HuggingFacePipeline(pipeline=pipe)
  14. print(local_llm('What is the capital of France? '))
  15. template = """Question: {question} Answer: Let's think step by step."""
  16. prompt = PromptTemplate(template=template, input_variables=["question"])
  17. llm_chain = LLMChain(prompt=prompt, llm=local_llm)
  18. question = "What is the capital of England?"
  19. print(llm_chain.run(question))

将模型拉到本地使用的好处:

  • 训练模型
  • 可以使用本地的 GPU
  • 有些模型无法在 Hugging Face 运行