오픈 소스 소프트웨어 임베딩 모델 등록 및 제공

이 Notebook은 벡터 검색에 사용할 수 있는 모델 서비스 엔드포인트에 오픈 소스 텍스트 포함 모델 e5-small-v2 설정합니다.

  • Hugging Face Hub에서 모델을 다운로드합니다.
  • MLflow 모델 레지스트리에 등록합니다.
  • 모델을 제공하기 위해 모델 서비스 엔드포인트를 시작합니다.

모델 e5-small-v2https://huggingface.co/intfloat/e5-small-v2에서 구입할 수 있습니다.

Databricks 런타임에 포함된 라이브러리 버전 목록은 Databricks 런타임 버전에 대한 릴리스 정보를 참조하세요.

Databricks Python SDK 설치

이 Notebook은 Python 클라이언트를 사용하여 서빙 엔드포인트와 작업을 수행합니다.

%pip install -U databricks-sdk python-snappy
%pip install sentence-transformers
dbutils.library.restartPython()

모델 다운로드

# Download model using the sentence_transformers library.
from sentence_transformers import SentenceTransformer

source_model_name = 'intfloat/e5-small-v2'  # model name on Hugging Face Hub
model = SentenceTransformer(source_model_name)
# Test the model, just to show it works.
sentences = ["This is an example sentence", "Each sentence is converted"]
embeddings = model.encode(sentences)
print(embeddings)

MLflow에 모델 등록

import mlflow
mlflow.set_registry_uri("databricks-uc")

# Specify the catalog and schema to use. You must have USE_CATALOG privilege on the catalog and USE_SCHEMA and CREATE_TABLE privileges on the schema.
# Change the catalog and schema here if necessary.
catalog = "main"
schema = "default"
model_name = "e5-small-v2"
# MLflow model name. The Model Registry uses this name for the model.
registered_model_name = f"{catalog}.{schema}.{model_name}"
# Compute input and output schema.
signature = mlflow.models.signature.infer_signature(sentences, embeddings)
print(signature)
model_info = mlflow.sentence_transformers.log_model(
  model,
  artifact_path="model",
  signature=signature,
  input_example=sentences,
  registered_model_name=registered_model_name)
inference_test = ["I enjoy pies of both apple and cherry.", "I prefer cookies."]

# Load the custom model by providing the URI for where the model was logged.
loaded_model_pyfunc = mlflow.pyfunc.load_model(model_info.model_uri)

# Perform a quick test to ensure that the loaded model generates the correct output.
embeddings_test = loaded_model_pyfunc.predict(inference_test)
embeddings_test
# Extract the version of the model you just registered.
mlflow_client = mlflow.MlflowClient()

def get_latest_model_version(model_name):
  client = mlflow_client
  model_version_infos = client.search_model_versions("name = '%s'" % model_name)
  return max([int(model_version_info.version) for model_version_info in model_version_infos])

model_version = get_latest_model_version(registered_model_name)
model_version

엔드포인트를 제공하는 모델 만들기

자세한 내용은 엔드포인트를 제공하는 기본 모델 만들기를 참조하세요.

참고: 이 예제에서는 0으로 축소되는 작은 CPU 엔드포인트를 만듭니다. 이는 빠르고 작은 테스트를 위한 것입니다. 보다 현실적인 사용 사례의 경우 모델 서비스 엔드포인트에 콜드 시작 오버헤드가 있기 때문에 계산을 더 빠르게 포함하기 위해 GPU 엔드포인트를 사용하고 자주 쿼리를 예상하는 경우 0으로 축소하지 않는 것이 좋습니다.

endpoint_name = "e5-small-v2"  # Name of endpoint to create
from databricks.sdk import WorkspaceClient
from databricks.sdk.service.serving import EndpointCoreConfigInput

w = WorkspaceClient()
endpoint_config_dict = {
    "served_entities": [
        {
            "name": f'{registered_model_name.replace(".", "_")}_{1}',
            "entity_name": registered_model_name,
            "entity_version": model_version,
            "workload_type": "CPU",
            "workload_size": "Small",
            "scale_to_zero_enabled": True,
        }
    ]
}

endpoint_config = EndpointCoreConfigInput.from_dict(endpoint_config_dict)

# The endpoint may take several minutes to get ready.
w.serving_endpoints.create_and_wait(name=endpoint_name, config=endpoint_config)

쿼리 엔드포인트

위의 create_and_wait 명령은 엔드포인트가 준비될 때까지 기다립니다. Databricks UI에서 서비스 엔드포인트의 상태를 확인할 수도 있습니다.

자세한 내용은 쿼리 기반 모델을 참조하세요.

# Only run this command after the Model Serving endpoint is in the Ready state.
import time

start = time.time()

# If the endpoint is not yet ready, you might get a timeout error. If so, wait and then rerun the command.
endpoint_response = w.serving_endpoints.query(name=endpoint_name, dataframe_records=['Hello world', 'Good morning'])

end = time.time()

print(endpoint_response)
print(f'Time taken for querying endpoint in seconds: {end-start}')

예제 노트

OSS 임베딩 모델 등록 및 배포

노트북 받기