LangChain에서 Hugging Face 모델 사용하기!
Hugging Face에는 누구나 내려받아 사용할 수 있는 수천 개의 모델이 있습니다. Hugging Face는 파트너 라이브러리인 langchain-huggingface를 통해 LangChain과 매우 잘 통합되어 있으며, 이 라이브러리를 바로 사용하실 수 있어요.
이 연습 문제에서는 Hugging Face의 crumb/nano-mistral 모델을 로드하고 호출해 보겠습니다. 이 모델은 성능 향상을 위해 파인튜닝하기에 적합한 초경량 LLM입니다.
이 연습은 강의의 일부입니다
LangChain으로 LLM 애플리케이션 개발하기
연습 안내
- Hugging Face 모델을 사용하려면
langchain_huggingface에서HuggingFacePipeline을 임포트하세요. HuggingFacePipeline.from_model_id()를 호출하여 텍스트 생성 LLM을 정의하세요.- 사용할 Hugging Face 모델을 지정하기 위해
model_id매개변수를 설정하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import the HuggingFacePipeline class for defining Hugging Face pipelines
from langchain_huggingface import ____
# Define the LLM from the Hugging Face model ID
llm = ____.from_model_id(
____="crumb/nano-mistral",
task="text-generation",
pipeline_kwargs={"max_new_tokens": 20}
)
prompt = "Hugging Face is"
# Invoke the model
response = llm.invoke(prompt)
print(response)