Essayez d'exécuter l'intégration de langchain et huggingface sur Colab

Préparation de l'environnement :

Obtenez [HUGGINGFACEHUB_API_TOKEN], connectez-vous au site officiel de huggingface et terminez l'inscription.

Site officiel : https://huggingface.co/

Obtenez un jeton : https://huggingface.co/settings/tokens

Installer Huggingface_hub sur Colab

!pip install -q huggingface_hub

【1】Création d'un modèle d'invite de questions-réponsesCréation d'un modèle d'invite de questions-réponses

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】Utilisez le modèle Huggingface_hub "google/flan-t5-large" pour répondre à la question. La classe HuggingfaceHub se connectera à l'API d'inférence de HuggingFace et chargera le modèle spécifié.

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】Résultats de sortie

Pékin

 

Je suppose que tu aimes

Origine blog.csdn.net/qq_23938507/article/details/131323125
conseillé
Classement