🚀 Try Zilliz Cloud, the fully managed Milvus, for free—experience 10x faster performance! Try Now>>

Milvus
Zilliz

How do I load and use a pre-trained model in LangChain?

To load and use a pre-trained model in LangChain, start by selecting a compatible model provider and initializing the model with the required parameters. LangChain supports integrations with services like OpenAI, Hugging Face, and others. For example, to use OpenAI’s GPT-3, install the langchain-openai package, import the ChatOpenAI class, and instantiate it with your API key and model name. For Hugging Face models, use the HuggingFaceHub class (for cloud-hosted models) or HuggingFacePipeline (for local models) from the langchain-community package. Each provider requires specific setup, such as API keys or model repository IDs, which you’ll pass during initialization. For instance, llm = HuggingFaceHub(repo_id="google/flan-t5-xxl", api_key="YOUR_KEY") loads a text generation model from Hugging Face’s platform.

Once the model is loaded, interact with it using methods like invoke() or generate(). These methods accept input prompts and return model-generated text. For example, response = llm.invoke("Explain quantum computing") sends the prompt to the model and retrieves the output. You can customize behavior using parameters like temperature (controls randomness) or max_tokens (limits response length). If using a local model via HuggingFacePipeline, ensure the model and tokenizer are downloaded first, typically via the transformers library. For example, load a model with pipeline("text-generation", model="gpt2") and pass it to HuggingFacePipeline(pipeline=pipe) to create a LangChain-compatible interface.

To integrate the model into a LangChain workflow, combine it with components like prompts, chains, or agents. For example, create a prompt template with PromptTemplate and link it to the model using LLMChain. This allows you to build reusable workflows:

from langchain_core.prompts import PromptTemplate 
from langchain.chains import LLMChain 
prompt = PromptTemplate.from_template("Summarize this article: {input}") 
chain = LLMChain(llm=llm, prompt=prompt) 
summary = chain.invoke({"input": article_text}) 

This approach lets you chain multiple steps, such as fetching data from a document loader, processing it with the model, and storing results. LangChain’s modular design ensures the pre-trained model works seamlessly with other tools, enabling tasks like retrieval-augmented generation or agent-based decision-making without rewriting boilerplate code.

Like the article? Spread the word