赞
踩
环境准备:
获取【HUGGINGFACEHUB_API_TOKEN】,登录huggingface官网并完成注册。
获取token:https://huggingface.co/settings/tokens
在colab上安装 huggingface_hub
!pip install -q huggingface_hub
【1】创建问答提示模版Creating a Question-Answering Prompt Template
- from langchain import PromptTemplate
-
- template = """Question: {question}
- Answer: """
- prompt = PromptTemplate(
- template=template,
- input_variables=['question']
- )
-
- # user question
- question = "What is the capital city of China?"
【2】使用huggingface_hub模型“google/flan-t5-large”来回答问题,huggingfaceHub 类将连接到 HuggingFace 的推理 API 并加载指定的模型。
- from langchain import HuggingFaceHub, LLMChain
-
- # initialize Hub LLM
- hub_llm = HuggingFaceHub(
- repo_id='google/flan-t5-large',
- model_kwargs={'temperature':0},
- huggingfacehub_api_token='your huggingfacehub_api_token'
- )
-
- # create prompt template > LLM chain
- llm_chain = LLMChain(
- prompt=prompt,
- llm=hub_llm
- )
-
- # ask the user question about the capital of France
- print(llm_chain.run(question))
【3】输出结果
beijing
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。