0
0
LangChainframework~5 mins

Connecting to open-source models in LangChain

Choose your learning style9 modes available
Introduction

Connecting to open-source models lets you use powerful AI tools without paying for them. It helps you build smart apps that understand and generate text.

You want to build a chatbot that answers questions using free AI models.
You need to analyze text data without relying on paid services.
You want to experiment with AI models locally or on your own server.
You want to customize AI behavior using open-source tools.
You want to avoid API limits or costs from commercial AI providers.
Syntax
LangChain
from langchain.llms import HuggingFaceHub

llm = HuggingFaceHub(repo_id="repo-id")
response = llm("Your input prompt here")

Replace repo-id with the Hugging Face repository ID of the open-source model you want to use (e.g., google/flan-t5-small).

The llm object represents the language model you connect to.

Examples
This example connects to a HuggingFace open-source model for translation.
LangChain
from langchain.llms import HuggingFaceHub

llm = HuggingFaceHub(repo_id="google/flan-t5-small")
response = llm("Translate 'Hello' to French.")
This example uses a local HuggingFace pipeline with the GPT-2 model for text generation.
LangChain
from langchain.llms import HuggingFacePipeline
from transformers import pipeline

pipe = pipeline('text-generation', model='gpt2')
llm = HuggingFacePipeline(pipeline=pipe)
response = llm('Write a short poem about the sun.')
Sample Program

This program connects to the 'flan-t5-small' model on HuggingFace Hub and asks it to translate a phrase from English to French. It then prints the translated text.

LangChain
from langchain.llms import HuggingFaceHub

# Connect to an open-source model on HuggingFace Hub
llm = HuggingFaceHub(repo_id="google/flan-t5-small")

# Ask the model to translate English to French
prompt = "Translate 'Good morning' to French."
response = llm(prompt)

print(response)
OutputSuccess
Important Notes

Make sure you have internet access to connect to online open-source models.

Some models may require API keys or authentication on HuggingFace Hub.

Local models need enough memory and setup to run properly.

Summary

Connecting to open-source models lets you use free AI tools in your apps.

Langchain supports many ways to connect, like HuggingFaceHub and HuggingFacePipeline.

Always check model requirements and usage limits before connecting.