Llama2 - Huggingface 튜토리얼
Llama2 - Huggingface 튜토리얼
Huggingface는 머신러닝 모델을 배포하기 위한 오픈소스 플랫폼이에요. LiteLLM을 사용하면 공개, 비공개 또는 기본 huggingface 엔드포인트를 쉽게 호출할 수 있습니다.
출처: 문서
본문
Huggingface Inference Endpoints로 Llama2 호출
3가지 모델을 호출해 보세요:
| Model | Type of Endpoint | | deepset/deberta-v3-large-squad2 | Default Huggingface Endpoint | | meta-llama/Llama-2-7b-hf | Public Endpoint | | meta-llama/Llama-2-7b-chat-hf | Private Endpoint |
사례 1: 기본 huggingface 엔드포인트 호출
완전한 예시는 다음과 같아요:
from litellm import completion model = "deepset/deberta-v3-large-squad2"messages = [{"role": "user", "content": "Hey, how's it going?"}] # LiteLLM follows the OpenAI format ### CALLING ENDPOINTcompletion(model=model, messages=messages, custom_llm_provider="huggingface")
무슨 일이 일어나고 있나요?
model: huggingface에 배포된 모델의 이름messages: 입력. OpenAI 채팅 형식을 사용합니다. huggingface의 경우 기본적으로 리스트를 순회하며 message["content"]를 프롬프트에 추가해요. 관련 코드custom_llm_provider: 선택적 파라미터. Azure, Replicate, Huggingface, Together-ai(자체 모델을 배포하는 플랫폼)에만 필요한 선택 플래그예요. 이로 인해 litellm이 모델에 맞는 올바른 제공자로 라우팅할 수 있어요.
사례 2: 공개 Huggingface 엔드포인트로 Llama2 호출
meta-llama/Llama-2-7b-hf를 공개 엔드포인트 https://ag3dkq4zui5nu8g3.us-east-1.aws.endpoints.huggingface.cloud 뒤에 배포했어요. 시도해 보세요:
from litellm import completion model = "meta-llama/Llama-2-7b-hf"messages = [{"role": "user", "content": "Hey, how's it going?"}] # LiteLLM follows the OpenAI format api_base = "https://ag3dkq4zui5nu8g3.us-east-1.aws.endpoints.huggingface.cloud"### CALLING ENDPOINTcompletion(model=model, messages=messages, custom_llm_provider="huggingface", api_base=api_base)
무슨 일이 일어나고 있나요?
api_base: 선택적 파라미터. 배포된 엔드포인트(기본 huggingface 추론 엔드포인트가 아님)를 사용하므로 이를 LiteLLM에 전달해요.
사례 3: 비공개 Huggingface 엔드포인트로 Llama2 호출
공개 엔드포인트와 유일한 차이는 이 엔드포인트에는 api_key가 필요하다는 점이에요. LiteLLM에서는 api_key를 전달하는 3가지 방법이 있습니다. 환경 변수로 설정, 패키지 변수로 설정, 또는 completion() 호출 시 전달할 수 있어요.
환경 변수로 설정하기
추가해야 할 코드 한 줄은 다음과 같아요:
os.environ["HF_TOKEN"] = "..."
전체 코드:
from litellm import completion os.environ["HF_TOKEN"] = "..."model = "meta-llama/Llama-2-7b-hf"messages = [{"role": "user", "content": "Hey, how's it going?"}] # LiteLLM follows the OpenAI format api_base = "https://ag3dkq4zui5nu8g3.us-east-1.aws.endpoints.huggingface.cloud"### CALLING ENDPOINTcompletion(model=model, messages=messages, custom_llm_provider="huggingface", api_base=api_base)
패키지 변수로 설정하기
추가해야 할 코드 한 줄은 다음과 같아요:
litellm.huggingface_key = "..."
전체 코드:
import litellmfrom litellm import completion litellm.huggingface_key = "..."model = "meta-llama/Llama-2-7b-hf"messages = [{"role": "user", "content": "Hey, how's it going?"}] # LiteLLM follows the OpenAI format api_base = "https://ag3dkq4zui5nu8g3.us-east-1.aws.endpoints.huggingface.cloud"### CALLING ENDPOINTcompletion(model=model, messages=messages, custom_llm_provider="huggingface", api_base=api_base)
completion 호출 중 전달하기
completion(..., api_key="...")
전체 코드:
from litellm import completion model = "meta-llama/Llama-2-7b-hf"messages = [{"role": "user", "content": "Hey, how's it going?"}] # LiteLLM follows the OpenAI format api_base = "https://ag3dkq4zui5nu8g3.us-east-1.aws.endpoints.huggingface.cloud"### CALLING ENDPOINTcompletion(model=model, messages=messages, custom_llm_provider="huggingface", api_base=api_base, api_key="...")