안녕하세요 이번시간에는 vertex ai pipelines 에 대해 알아보고 docker gcr image를 사용하여 프로젝트를 구성하는 방법을 알아보겠습니다.
GCR을 사용하여 Component를 구성하는 방법이 궁금하신 분은 오른쪽의 Docker Google Cloud Artifact Registry 로 이동해주세요.
먼저 Vertex ai pipeline에 대해 알아보겠습니다.
Vertex AI pipeline 라는 단어에는 2가지의 내용을 포함합니다. Vertex AI와 pipeline이 있습니다.
Vertex AI 는 머신러닝 플랫폼입니다. Google에서 지속적으로 개발하고 있으며 ML 모델과 AI 애플리케이션을 학습 및 배포하고 AI 기반 애플리케이션을 사용할 대규모 언어 모델을 쉽게 설정할 수 있도록 지원합니다.
Pipeline은 머신러닝 워크플로우에서 사용되는 일련의 동작, 과정을 의미합니다.
워크플로우가 구축 - 학습 - 평가 - 배포 라는 프로세스를 가진다고 했을 때 각 단계가 테스크, 동작이 되며 이 전체 과정이 파이프라인입니다.
이 파이프라인을 통해 프로세스를 자동화 하고 모니터링을 쉽게 할 수 있습니다. 동일한 프로세스로 데이터를 변경하여 계속 재학습을할 수 있습니다.
Vertex ai pipeline 을 사용하면 머신러닝 시스템을 자동화하고 모니터링을 쉽고 하며 제어할 수 있습니다. 이 과정에서 Kubeflow pipelines를 사용하여 정의된 ML 파이프라인을 쉽게 실행할 수 있습니다.
Google couldskills boost : Vertex ai pipelines qwik start
위 링크를 들어가보시면 쉽고 빠르게 vertex ai pipelines를 경험하실 수 있습니다.
관련 라이브러리
google-cloud-aiplatform==1.70.0
google-cloud-pipeline-components==0.1.1 # Keep the specific version you used
kfp~=1.8.22
vertexai==1.70.0
...
파이프라인은
job에 대한 내용들을 컴파일, AIPlatformClient 정의 -> 이 컴파일한 json 파일을 AIPlatformClient가 생싱 및 실행합니다.
그래서 아래에서 보시는 것 처럼
컴파일, api_client 정의 - create_run 메서드 실행을 하게 되어 정의한 job들을 Vertex AI 플랫폼에서 실행할 수 있게 됩니다.
compiler.Compiler().compile(
pipeline_func=intro_pipeline, package_path="intro_pipeline_job.json"
)
api_client = AIPlatformClient(
project_id=config["gemini"]["project_id"],
region=config["gemini"]["region"],
)
response = api_client.create_run_from_job_spec(
job_spec_path="intro_pipeline_job.json",
service_account=config["gemini"]["service_account"],
pipeline_root=PIPELINE_ROOT
# this argument is necessary if you did not specify PIPELINE_ROOT as part of the pipeline definition.
)
파이프라인을 이용하여 개발을 진행할 때 파이프라인을 실행하는 각 컴포넌트는 stateless 하므로 컴포넌트 함수 내에서 정의하지 않을 경우 컴포넌트는 컴포넌트 함수 외부에 작성된 코드나 데이터에 대해서는 알 수 없게 됩니다.
그래서 docker Image를 활용해야합니다.
컴포넌트를 정의할 때에는 base_image를 정의해주게 됩니다. 그러므로 저희가 원하는 Image를 사용하면 그 안에 정의된 code들을 호출하고 사용할 수 있습니다.
사용하는 방법은 아래와 같습니다.
- docker Image를 build 한다
- build한 Image에 tag를 통해 image 이름을 변경한다
- gcr.io/ 로 시작하는 Image를 push 한다
- component에서 base_image를 정의한 docker Image로 설정한다
그럼 아래에서 각 단계별로 자세하게 알아보겠습니다.
Docker Image Build
Dockerfile이 있는 위치에서 아래 명령어를 실행합니다.
docker build -t username/project-name:tag .
ex) docker build -t leejake/chatbot:0.1
Docker tag
docker tag username/image-name:0.1 gcr.io/google-cloud-project-id/image-name:0.1
ex) docker tag leejake/chatbot:0.1 gcr.io/englishcentral-project/chatbot:0.1
Docker push
docker push gcr.io/google-cloud-project-id/image-name:0.1
ex) docker push gcr.io/englishcentral-project/chatbot:0.1
Use Docker Image in component
@component(base_image="gcr.io/englishcentral-project/chatbot:0.1",
output_component_file="component_resource/llm-based-assessor-component_resource.yaml")
def llm_based_assessor(output: Output[Dataset], input_rp_conversation: InputPath(Artifact)):
from chatbot.service.chat_service import ChatService
chat_service = ChatService()
# 원하는 서비스 로직 호출
chat = chat_service.chat()
hello = chat_service.hello()
# 원하는 내용 metadata 로 전송
output.metadata["chat"] = chat
output.metadata["hello"] = hello
인프라 구성은 Google Cloud의 Artifact Registry 로 이동하여 gcr.io 이름으로 레포지토리를 생성합니다.
Docker 계정은 해당 Registry 를 사용하려는 google cloud의 project 멤버여야 합니다.
마침내! 위와 같이 Docker Image를 사용하여 stateless 한 component를 개발할 수 있게 되었습니다.
위 코드를 보시면 chatbot.service... 와 같이 chatbot package에서 class를 import 하고 있습니다. 이는 docker Image를 build 할 때 아래와 같이 구조를 잡고 설정을 해주었기 때문인데요. 아래에 Dockerfile과 같이 설정하여 component 내에서 import 할 수 있게 해주었습니다.
# Base image
FROM python:3.9-bullseye
# Execute updates
RUN apt-get -y update
# Create work directory
RUN mkdir /opt/chatbot && mkdir /opt/chatbot/constants && mkdir /opt/chatbot/element && mkdir /opt/chatbot/nlg && mkdir /opt/chatbot/service
WORKDIR /opt
# Copy work directory and py files
COPY __init__.py /opt/chatbot/__init__.py
COPY chatbot_config.yaml /opt/chatbot/chatbot_config.yaml
COPY ./service /opt/chatbot/service/
COPY ./service/__init__.py /opt/chatbot/service/__init__.py
COPY requirements.txt /opt/chatbot/requirements.txt
ENV PYTHONPATH="/opt"
# Install Python dependencies with no cache
RUN pip install --no-cache-dir -r /opt/chatbot/requirements.txt
# Add the main entry point (optional, for testing)
CMD ["python", "-m", "main"]