在Colab上试跑langchain 和 huggingface的整合

环境准备:

获取【HUGGINGFACEHUB_API_TOKEN】,登录huggingface官网并完成注册。

官网:https://huggingface.co/

获取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

猜你喜欢

转载自blog.csdn.net/qq_23938507/article/details/131323125