Vertex AI RAG 엔진과 함께 Weaviate 데이터베이스 사용

이 페이지에서는 RAG Engine 코퍼스를 Weaviate 데이터베이스에 연결하는 방법을 보여줍니다.

이 노트북 Weaviate를 사용한 RAG 엔진을 사용하여 따라할 수도 있습니다.

오픈소스 데이터베이스인 Weaviate 데이터베이스 인스턴스를 RAG 엔진과 함께 사용하여 색인을 생성하고 벡터 기반 유사성 검색을 실행할 수 있습니다. 유사성 검색은 찾고 있는 텍스트와 유사한 텍스트를 찾는 방법으로, 임베딩 모델을 사용해야 합니다. 임베딩 모델은 비교되는 각 텍스트에 대한 벡터 데이터를 생성합니다. 유사성 검색은 LLM에서 가장 정확한 콘텐츠를 반환하기 위해 그라운딩의 시맨틱 컨텍스트를 검색하는 데 사용됩니다.

RAG Engine을 사용하면 프로비저닝할 책임이 있는 완전 관리형 벡터 데이터베이스 인스턴스를 계속 사용할 수 있습니다. RAG 엔진은 저장소, 색인 관리, 검색에 벡터 데이터베이스를 사용합니다.

고려사항

Weaviate 데이터베이스를 사용하기 전에 다음 단계를 고려하세요.

  1. Weaviate 데이터베이스 인스턴스와 컬렉션을 만들고 구성하고 배포해야 합니다. Weaviate 컬렉션 만들기의 안내에 따라 스키마를 기반으로 컬렉션을 설정합니다.
  2. RAG 엔진이 Weaviate 데이터베이스와 상호작용할 수 있도록 하는 Weaviate API 키를 제공해야 합니다. RAG Engine은 Weaviate 데이터베이스에 연결되고 HTTPS 연결을 지원하는 API 키 기반 AuthNAuthZ를 지원합니다.
  3. RAG 엔진은 Weaviate API 키를 저장하고 관리하지 않습니다. 대신 다음을 실행해야 합니다.
    1. Secret Manager에 키를 저장합니다. Google Cloud
    2. 보안 비밀에 액세스할 수 있는 권한을 프로젝트의 서비스 계정에 부여합니다.
    3. RAG 엔진에 비밀번호의 리소스 이름에 대한 액세스 권한을 부여합니다.
    4. Weaviate 데이터베이스와 상호작용할 때 RAG 엔진은 서비스 계정을 사용하여 비밀 리소스에 액세스합니다.
  4. RAG Engine 코퍼스와 Weaviate 컬렉션은 일대일 매핑이 있습니다. RAG 파일은 Weaviate 데이터베이스 컬렉션에 저장됩니다. CreateRagCorpus API 또는 UpdateRagCorpus API를 호출하면 RAG 자료가 데이터베이스 컬렉션에 연결됩니다.
  5. 밀집 임베딩 기반의 시맨틱 검색 외에도 Weaviate 데이터베이스를 통해 RAG 엔진에서 하이브리드 검색도 지원됩니다. 하이브리드 검색에서 밀집 벡터 유사성과 희소 벡터 유사성 간의 가중치를 조정할 수도 있습니다.

Weaviate 데이터베이스 프로비저닝

RAG 엔진과 함께 Weaviate 데이터베이스를 사용하려면 다음을 실행해야 합니다.

  1. Weaviate 데이터베이스 인스턴스를 구성하고 배포합니다.
  2. HTTPS 엔드포인트를 준비합니다.
  3. Weaviate 컬렉션을 만듭니다.
  4. API 키를 사용하여 AuthNAuthZ를 사용하여 Weaviate를 프로비저닝합니다.
  5. RAG Engine 서비스 계정을 프로비저닝합니다.

Weaviate 데이터베이스 인스턴스 구성 및 배포

Weaviate 공식 가이드 빠른 시작을 따라야 합니다. 하지만 선택 사항으로 Google Cloud 마켓플레이스 가이드를 사용할 수 있습니다.

Weaviate 엔드포인트에 액세스하여 프로젝트에서 구성하고 배포할 수 있는 한 어디서나 Weaviate 인스턴스를 설정할 수 있습니다. 그런 다음 Weaviate 데이터베이스 인스턴스를 완전히 관리할 수 있습니다.

RAG 엔진은 Weaviate 데이터베이스 인스턴스 수명 주기의 어느 단계에도 관여하지 않으므로 Weaviate 데이터베이스에 데이터를 저장하고 검색할 수 있도록 RAG 엔진에 권한을 부여하는 것은 개발자의 책임입니다. 또한 데이터베이스의 데이터를 RAG 엔진에서 사용할 수 있도록 해야 합니다. 예를 들어 데이터를 변경하는 경우 RAG 엔진은 이러한 변경사항으로 인한 예기치 않은 동작에 대해 책임을 지지 않습니다.

HTTPS 엔드포인트 준비

Weaviate 프로비저닝 중에 HTTPS 엔드포인트를 만들어야 합니다. HTTP 연결은 지원되지만 RAG Engine 및 Weaviate 데이터베이스 트래픽은 HTTPS 연결을 사용하는 것이 좋습니다.

Weaviate 컬렉션 만들기

RAG Engine 코퍼스와 Weaviate 컬렉션은 일대일 매핑이므로 컬렉션을 RAG Engine 코퍼스와 연결하기 전에 Weaviate 데이터베이스에 컬렉션을 만들어야 합니다. 이 일회성 연결은 CreateRagCorpus API 또는 UpdateRagCorpus API를 호출할 때 이루어집니다.

Weaviate에서 컬렉션을 만들 때는 다음 스키마를 사용해야 합니다.

속성 이름 데이터 유형
fileId text
corpusId text
chunkId text
chunkDataType text
chunkData text
fileOriginalUri text

API 키를 사용하여 AuthNAuthZ를 사용하여 Weaviate 프로비저닝

Weaviate API 키를 프로비저닝하려면 다음 단계를 따르세요.

  1. Weaviate API 키를 만듭니다.
  2. Weaviate API 키를 사용하여 Weaviate를 구성합니다.
  3. Secret Manager에 Weaviate API 키를 저장합니다.

API 키 만들기

RAG 엔진은 인증 및 승인에 API 키를 사용하여 Weaviate 데이터베이스 인스턴스에만 연결할 수 있습니다. Weaviate 데이터베이스 인스턴스에서 API 키 기반 인증을 구성하려면 Weaviate 인증 공식 가이드를 따라야 합니다.

Weaviate API 키를 만들 때 RAG Engine에서 가져온 ID 정보와 연결해야 하는 ID 정보가 필요한 경우 첫 번째 코퍼스를 만들고 RAG Engine 서비스 계정을 ID로 사용해야 합니다.

Secret Manager에 API 키 저장

API 키에는 법적 요건이 적용되는 민감한 개인 식별 정보(SPII)가 포함됩니다. SPII 데이터가 유출되거나 오용되면 개인에게 심각한 위험이나 피해가 발생할 수 있습니다. RAG 엔진을 사용하는 동안 개인에 대한 위험을 최소화하려면 API 키를 저장 및 관리하지 마십시오. 또한 암호화되지 않은 API 키를 공유하지 마세요.

SPII를 보호하려면 다음 단계를 따르세요.

  1. Secret Manager에 API 키를 저장합니다.
  2. RAG Engine 서비스 계정에 보안 비밀에 대한 권한을 부여하고 보안 비밀 리소스 수준에서 액세스 제어를 관리합니다.
    1. 프로젝트 권한으로 이동합니다.
    2. Google 제공 역할 부여 포함 옵션을 사용 설정합니다.
    3. 다음과 같은 형식의 서비스 계정을 찾습니다.

      service-{project number}@gcp-sa-vertex-rag.iam.gserviceaccount.com

    4. 서비스 계정의 주 구성원을 수정합니다.
    5. 서비스 계정에 Secret Manager 보안 비밀 접근자 역할을 추가합니다.
  3. RAG 코퍼스를 만들거나 업데이트하는 동안 보안 비밀 리소스 이름을 RAG 엔진에 전달하고 보안 비밀 리소스 이름을 저장합니다.

Weaviate 데이터베이스 인스턴스에 API를 요청하면 RAG 엔진이 각 서비스 계정을 사용하여 프로젝트에서 Secret Manager의 비밀 리소스에 해당하는 API 키를 읽습니다.

RAG Engine 서비스 계정 프로비저닝

프로젝트에서 첫 번째 리소스를 만들면 RAG Engine에서 전용 서비스 계정을 만듭니다. 프로젝트의 IAM 페이지에서 서비스 계정을 찾을 수 있습니다. 서비스 계정은 다음 형식을 따릅니다.

service-{project number}@gcp-sa-vertex-rag.iam.gserviceaccount.com

예를 들면 service-123456789@gcp-sa-vertex-rag.iam.gserviceaccount.com입니다.

Weaviate 데이터베이스와 통합할 때 서비스 계정은 다음 시나리오에서 사용됩니다.

  • 서비스 계정을 사용하여 인증을 위한 Weaviate API 키를 생성할 수 있습니다. 경우에 따라 API 키를 생성할 때 사용자 정보가 필요하지 않습니다. 즉, API 키를 생성할 때 서비스 계정이 필요하지 않습니다.
  • Weaviate 데이터베이스에서 서비스 계정을 API 키와 결합하여 인증(AuthN) 및 승인(AuthZ)을 구성할 수 있습니다. 하지만 서비스 계정은 필요하지 않습니다.
  • 프로젝트에 API 키 Secret Manager를 저장하고 이러한 보안 비밀 리소스에 서비스 계정 권한을 부여할 수 있습니다.
  • RAG Engine은 서비스 계정을 사용하여 프로젝트의 Secret Manager에서 API 키에 액세스합니다.

Google Cloud 콘솔 환경 설정

환경 설정 방법을 알아보려면 클릭

다음 탭 중 하나를 선택하여 환경을 설정하는 방법을 알아보세요.

Python용 Vertex AI SDK

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI API.

    Enable the API

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI API.

    Enable the API

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. 다음 명령어를 실행하여 Vertex AI SDK for Python을 설치하거나 업데이트합니다.

    pip3 install --upgrade "google-cloud-aiplatform>=1.38"
        

Node.js

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI API.

    Enable the API

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI API.

    Enable the API

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. 다음 명령어를 실행하여 Node.js용 Vertex AI SDK를 설치하거나 업데이트합니다.

    npm install @google-cloud/vertexai
        

Java

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI API.

    Enable the API

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI API.

    Enable the API

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. google-cloud-vertexai를 종속 항목으로 추가하려면 환경에 적절한 코드를 추가합니다.

    BOM이 있는 Maven

    pom.xml에 다음 HTML을 추가합니다.

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>libraries-bom</artifactId>
          <version>26.32.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-vertexai</artifactId>
      </dependency>
    </dependencies>
                

    BOM이 없는 Maven

    pom.xml에 다음 HTML을 추가합니다.

    <dependency>
      <groupId>com.google.cloud</groupId>
      <artifactId>google-cloud-vertexai</artifactId>
      <version>0.4.0</version>
    </dependency>
              

    Gradle without BOM

    Add the following to your build.gradle

    implementation 'com.google.cloud:google-cloud-vertexai:0.4.0'

Go

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI API.

    Enable the API

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI API.

    Enable the API

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

  8. 사용 가능한 Vertex AI API Go 패키지를 검토하여 프로젝트 요구에 가장 적합한 패키지를 확인합니다.

    • 패키지 cloud.google.com/go/vertexai (권장)

      vertexai는 일반적인 기능에 대한 액세스를 제공하는 인간이 작성한 패키지입니다.

      이 패키지는 Vertex AI API를 사용하여 빌드하는 대부분의 개발자에게 시작점으로 권장됩니다. 이 패키지에 아직 포함되지 않은 기능을 이용하려면 자동 생성된 aiplatform을 사용하세요.

    • 패키지 cloud.google.com/go/aiplatform

      aiplatform은 자동 생성된 패키지입니다.

      이 패키지는 인간이 작성한 vertexai 패키지에서 아직 제공하지 않는 Vertex AI API 기능에 액세스해야 하는 프로젝트를 대상으로 합니다.

  9. 다음 명령어 중 하나를 실행하여 프로젝트 요구에 따라 원하는 Go 패키지를 설치합니다.

    # Human authored package. Recommended for most developers.
    go get cloud.google.com/go/vertexai
        
    # Auto-generated package. go get cloud.google.com/go/aiplatform

C#

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI API.

    Enable the API

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI API.

    Enable the API

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. If you're using a local shell, then create local authentication credentials for your user account:

    gcloud auth application-default login

    You don't need to do this if you're using Cloud Shell.

REST

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Enable the Vertex AI API.

    Enable the API

  4. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  5. Enable the Vertex AI API.

    Enable the API

  6. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  7. 다음을 입력하여 환경 변수를 구성합니다. PROJECT_ID를 Google Cloud 프로젝트의 ID로 바꿉니다.
    MODEL_ID="gemini-1.5-flash-002"
    PROJECT_ID="PROJECT_ID"
        
  8. 엔드포인트를 프로비저닝합니다.
    gcloud beta services identity create --service=aiplatform.googleapis.com --project=${PROJECT_ID}
        
  9. 선택사항: Cloud Shell을 사용 중이며 Cloud Shell을 승인하라는 메시지가 표시되면 승인을 클릭합니다.

RAG 코퍼스 준비

Weaviate 데이터베이스의 데이터에 액세스하려면 RAG 엔진이 RAG 코퍼스에 액세스할 수 있어야 합니다. 이 섹션에서는 단일 RAG 자료집과 추가 RAG 자료집을 만드는 단계를 설명합니다.

CreateRagCorpusUpdateRagCorpus API 사용

CreateRagCorpusUpdateRagCorpus API를 호출할 때 다음 필드를 지정해야 합니다.

  • rag_vector_db_config.weaviate: CreateRagCorpus API를 호출한 후에 벡터 데이터베이스 구성이 선택됩니다. 벡터 데이터베이스 구성에는 모든 구성 필드가 포함됩니다. rag_vector_db_config.weaviate 필드를 설정하지 않으면 기본적으로 rag_vector_db_config.rag_managed_db가 설정됩니다.
  • weaviate.http_endpoint: HTTPS 또는 HTTP Weaviate 엔드포인트는 Weaviate 데이터베이스 인스턴스 프로비저닝 중에 생성됩니다.
  • weaviate.collection_name: Weaviate 인스턴스 프로비저닝 중에 생성되는 컬렉션의 이름입니다. 이름은 대문자로 시작해야 합니다.
  • api_auth.api_key_config: 구성에서 API 키를 사용하여 벡터 데이터베이스에 대한 액세스를 승인하도록 지정합니다.
  • api_key_config.api_key_secret_version: Secret Manager에 저장된 보안 비밀의 리소스 이름으로, Weaviate API 키가 포함되어 있습니다.

데이터베이스 인스턴스에서 RAG 코퍼스를 만들고 Weaviate 컬렉션에 연결할 수 있습니다. 하지만 API 키를 생성하고 Weaviate 데이터베이스 인스턴스를 구성하려면 서비스 계정이 필요할 수 있습니다. 첫 번째 RAG 코퍼스를 만들면 서비스 계정이 생성됩니다. 첫 번째 RAG 코퍼스를 만든 후에는 Weaviate 데이터베이스와 API 키 간의 연결을 다른 RAG 코퍼스 만들기에 사용할 수 없을 수 있습니다.

데이터베이스와 키를 RAG 코퍼스에 연결할 준비가 되지 않은 경우 RAG 코퍼스에 다음을 실행합니다.

  1. rag_vector_db_configweaviate 필드를 설정합니다.

    • 연결된 벡터 데이터베이스는 변경할 수 없습니다.
    • http_endpointcollection_name 필드는 모두 비워 둡니다. 두 필드는 나중에 업데이트할 수 있습니다.
  2. Secret Manager에 API 키가 저장되어 있지 않으면 api_auth 필드를 비워 둘 수 있습니다. UpdateRagCorpus API를 호출할 때 api_auth 필드를 업데이트할 수 있습니다. Weaviate를 사용하려면 다음을 실행해야 합니다.

    1. api_auth 필드에서 api_key_config를 설정합니다.
    2. Secret Manager에서 Weaviate API 키의 api_key_secret_version을 설정합니다. api_key_secret_version 필드는 다음 형식을 사용합니다.

      projects/{project}/secrets/{secret}/versions/{version}

  3. http_endpoint 또는 collection_name와 같이 한 번만 설정할 수 있는 필드를 지정하는 경우 RAG 코퍼스를 삭제하고 다시 만들지 않는 한 변경할 수 없습니다. API 키 필드 api_key_secret_version와 같은 다른 필드는 업데이트할 수 있습니다.

  4. UpdateRagCorpus를 호출할 때 vector_db 필드를 설정할 수 있습니다. vector_dbCreateRagCorpus API 호출에 의해 weaviate로 설정되어야 합니다. 그렇지 않으면 시스템은 기본값인 RAG 관리형 데이터베이스 옵션을 선택합니다. UpdateRagCorpus API를 호출할 때는 이 옵션을 변경할 수 없습니다. UpdateRagCorpus를 호출하고 vector_db 필드가 부분적으로 설정된 경우 변경할 수 있음(변경 가능이라고도 함)으로 표시된 필드를 업데이트할 수 있습니다.

이 표에는 코드에서 사용되는 WeaviateConfig 변경 가능 필드와 변경 불가 필드가 나열되어 있습니다.

필드 이름 변경 가능 또는 변경 불가
http_endpoint 설정 후 변경 불가
collection_name 설정 후 변경 불가
api_key_authentication 변경 가능

첫 번째 RAG 코퍼스 만들기

RAG Engine 서비스 계정이 없는 경우 다음을 실행합니다.

  1. 빈 Weaviate 구성으로 RAG 엔진에서 RAG 코퍼스를 만듭니다. 그러면 RAG 엔진 프로비저닝이 시작되어 서비스 계정이 생성됩니다.
  2. 다음 형식에 따라 RAG Engine 서비스 계정의 이름을 선택합니다.

    service-{project number}@gcp-sa-vertex-rag.iam.gserviceaccount.com

    예를 들면 service-123456789@gcp-sa-vertex-rag.iam.gserviceaccount.com입니다.

  3. 서비스 계정을 사용하여 Weaviate API 키가 포함된 프로젝트의 Secret Manager에 저장된 보안 비밀에 액세스합니다.
  4. Weaviate 프로비저닝이 완료되면 다음 정보를 가져옵니다.
    • Weaviate HTTPS 또는 HTTP 엔드포인트
    • Weaviate 컬렉션의 이름
  5. CreateRagCorpus API를 호출하여 빈 Weaviate 구성으로 RAG 코퍼스를 만들고 UpdateRagCorpus API를 호출하여 다음 정보로 RAG 코퍼스를 업데이트합니다.
    • Weaviate HTTPS 또는 HTTP 엔드포인트
    • Weaviate 컬렉션의 이름
    • API 키 리소스 이름

다른 RAG 코퍼스 만들기

RAG Engine 서비스 계정이 있는 경우 다음을 실행합니다.

  1. 프로젝트의 권한에서 RAG Engine 서비스 계정을 가져옵니다.
  2. "Google 제공 역할 부여 포함" 옵션을 사용 설정합니다.
  3. 다음 형식에 따라 RAG Engine 서비스 계정의 이름을 선택합니다.

    service-{project number}@gcp-sa-vertex-rag.iam.gserviceaccount.com

  4. 서비스 계정을 사용하여 Weaviate API 키가 포함된 프로젝트의 Secret Manager에 저장된 보안 비밀에 액세스합니다.
  5. Weaviate 프로비저닝 중에 다음 정보를 가져옵니다.
    • Weaviate HTTPS 또는 HTTP 엔드포인트
    • Weaviate 컬렉션의 이름
  6. RAG 엔진에서 RAG 코퍼스를 만들고 다음 중 하나를 실행하여 Weaviate 컬렉션에 연결합니다.
    1. CreateRagCorpus API를 호출하여 채워진 Weaviate 구성으로 RAG 자료 모음을 만듭니다. 이 방법이 권장됩니다.
    2. CreateRagCorpus API를 호출하여 빈 Weaviate 구성으로 RAG 코퍼스를 만들고 UpdateRagCorpus API를 호출하여 다음 정보로 RAG 코퍼스를 업데이트합니다.
      • Weaviate 데이터베이스 HTTP 엔드포인트
      • Weaviate 컬렉션 이름
      • API 키

이 섹션에서는 Weaviate 데이터베이스, Secret Manager, RAG 코퍼스, RAG 파일을 설정하는 방법을 보여주는 샘플 코드를 제공합니다. 파일을 가져오고, 컨텍스트를 검색하고, 콘텐츠를 생성하고, RAG 코퍼스와 RAG 파일을 삭제하는 방법을 보여주는 샘플 코드도 제공됩니다.

Model Garden RAG API 노트북을 사용하려면 Llama 3에서 Weaviate 사용을 참고하세요.

Weaviate 데이터베이스 설정

이 코드 샘플은 Weaviate 데이터와 Secret Manager를 설정하는 방법을 보여줍니다.

REST

# TODO(developer): Update the variables.
# The HTTPS/HTTP Weaviate endpoint you created during provisioning.
HTTP_ENDPOINT_NAME="https://your.weaviate.endpoint.com"

# Your Weaviate API Key.
WEAVIATE_API_KEY="example-api-key"

# Select your Weaviate collection name, which roughly corresponds to a Vertex AI Knowledge Engine Corpus.
# For example, "MyCollectionName"
# Note that the first letter needs to be capitalized.
# Otherwise, Weavaite will capitalize it for you.
WEAVIATE_COLLECTION_NAME="MyCollectionName"

# Create a collection in Weaviate which includes the required schema fields shown below.
echo '{
  "class": "'${WEAVIATE_COLLECTION_NAME}'",
  "properties": [
    { "name": "fileId", "dataType": [ "string" ] },
    { "name": "corpusId", "dataType": [ "string" ] },
    { "name": "chunkId", "dataType": [ "string" ] },
    { "name": "chunkDataType", "dataType": [ "string" ] },
    { "name": "chunkData", "dataType": [ "string" ] },
    { "name": "fileOriginalUri", "dataType": [ "string" ] }
  ]
}' | curl \
    -X POST \
    -H 'Content-Type: application/json' \
    -H "Authorization: Bearer "${WEAVIATE_API_KEY} \
    -d @- \
    ${HTTP_ENDPOINT_NAME}/v1/schema

Secret Manager 설정

Secret Manager를 설정하려면 Secret Manager를 사용 설정하고 권한을 설정해야 합니다.

보안 비밀 만들기

Secret Manager를 사용 설정하려면 다음 단계를 따르세요.

콘솔

  1. 보안 비밀 관리자 페이지로 이동합니다.

    Secret Manager로 이동

  2. + 보안 비밀 만들기를 클릭합니다.

  3. 보안 비밀의 이름을 입력합니다. 보안 비밀 이름에는 영문자(A~Z), 숫자(0~9), 대시(-), 밑줄(_)만 포함할 수 있습니다.

  4. 다음 필드 지정은 선택사항입니다.

    1. 보안 비밀이 포함된 파일을 업로드하려면 탐색을 클릭합니다.
    2. 복제 정책을 읽습니다.
    3. 보안 비밀의 위치를 직접 관리하려면 보안 비밀 위치 직접 관리를 선택합니다. 1개 이상의 리전을 선택해야 합니다.
    4. 암호화 옵션을 선택합니다.
    5. 로테이션 기간을 수동으로 설정하려면 로테이션 기간 설정을 선택합니다.
    6. 이벤트 알림을 수신하도록 게시 또는 구독할 주제를 지정하려면 주제 추가를 클릭합니다.
    7. 기본적으로 보안 비밀은 만료되지 않습니다. 만료일을 설정하려면 만료일 설정을 선택합니다.
    8. 기본적으로 보안 비밀 버전은 요청 시 폐기됩니다. 보안 비밀 버전의 폐기를 지연하려면 지연된 폐기 기간 설정을 선택합니다.
    9. 라벨을 사용하여 보안 비밀을 구성하고 분류하려면 + 라벨 추가를 클릭합니다.
    10. 주석을 사용하여 식별되지 않는 메타데이터를 보안 비밀에 연결하려면 + 주석 추가를 클릭합니다.
  5. 보안 비밀 만들기를 클릭합니다.

REST

# Create a secret in SecretManager.
curl "https://secretmanager.googleapis.com/v1/projects/${PROJECT_ID}/secrets?secretId=${SECRET_NAME}" \
    --request "POST" \
    --header "authorization: Bearer $(gcloud auth print-access-token)" \
    --header "content-type: application/json" \
    --data "{\"replication\": {\"automatic\": {}}}"

Python

이 샘플을 사용해 보기 전에 Vertex AI 빠른 시작: 클라이언트 라이브러리 사용Python 설정 안내를 따르세요. 자세한 내용은 Vertex AI Python API 참고 문서를 참조하세요.

Vertex AI에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

def create_secret(
    project_id: str, secret_id: str, ttl: Optional[str] = None
) -> secretmanager.Secret:
    """
    Create a new secret with the given name. A secret is a logical wrapper
    around a collection of secret versions. Secret versions hold the actual
    secret material.

     Args:
        project_id (str): The project ID where the secret is to be created.
        secret_id (str): The ID to assign to the new secret. This ID must be unique within the project.
        ttl (Optional[str]): An optional string that specifies the secret's time-to-live in seconds with
                             format (e.g., "900s" for 15 minutes). If specified, the secret
                             versions will be automatically deleted upon reaching the end of the TTL period.

    Returns:
        secretmanager.Secret: An object representing the newly created secret, containing details like the
                              secret's name, replication settings, and optionally its TTL.

    Example:
        # Create a secret with automatic replication and no TTL
        new_secret = create_secret("my-project", "my-new-secret")

        # Create a secret with a TTL of 30 days
        new_secret_with_ttl = create_secret("my-project", "my-timed-secret", "7776000s")
    """

    # Import the Secret Manager client library.
    from google.cloud import secretmanager

    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the parent project.
    parent = f"projects/{project_id}"

    # Create the secret.
    response = client.create_secret(
        request={
            "parent": parent,
            "secret_id": secret_id,
            "secret": {"replication": {"automatic": {}}, "ttl": ttl},
        }
    )

    # Print the new secret name.
    print(f"Created secret: {response.name}")

권한 설정

서비스 계정에 Secret Manager 권한을 부여해야 합니다.

콘솔

  1. Google Cloud 콘솔의 IAM 및 관리자 섹션에서 서비스 계정을 찾아 연필 아이콘을 클릭하여 수정합니다.

  2. 역할 필드에서 Secret Manager 보안 비밀 접근자를 선택합니다.

Python

이 샘플을 사용해 보기 전에 Vertex AI 빠른 시작: 클라이언트 라이브러리 사용Python 설정 안내를 따르세요. 자세한 내용은 Vertex AI Python API 참고 문서를 참조하세요.

Vertex AI에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

def iam_grant_access(
    project_id: str, secret_id: str, member: str
) -> iam_policy_pb2.SetIamPolicyRequest:
    """
    Grant the given member access to a secret.
    """

    # Import the Secret Manager client library.
    from google.cloud import secretmanager

    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the secret.
    name = client.secret_path(project_id, secret_id)

    # Get the current IAM policy.
    policy = client.get_iam_policy(request={"resource": name})

    # Add the given member with access permissions.
    policy.bindings.add(role="roles/secretmanager.secretAccessor", members=[member])

    # Update the IAM Policy.
    new_policy = client.set_iam_policy(request={"resource": name, "policy": policy})

    # Print data about the secret.
    print(f"Updated IAM policy on {secret_id}")

보안 비밀 버전 추가

REST

# TODO(developer): Update the variables.
# Select a resource name for your Secret, which contains your API Key.
SECRET_NAME="MyWeaviateApiKeySecret"

# Your Weaviate API Key.
WEAVIATE_API_KEY="example-api-key"
# Encode your WEAVIATE_API_KEY using base 64.
SECRET_DATA=$(echo ${WEAVIATE_API_KEY} | base64)

# Create a new version of your secret which uses SECRET_DATA as payload
curl "https://secretmanager.googleapis.com/v1/projects/${PROJECT_ID}/secrets/${SECRET_NAME}:addVersion" \
    --request "POST" \
    --header "authorization: Bearer $(gcloud auth print-access-token)" \
    --header "content-type: application/json" \
    --data "{\"payload\": {\"data\": \"${SECRET_DATA}\"}}"

Python

이 샘플을 사용해 보기 전에 Vertex AI 빠른 시작: 클라이언트 라이브러리 사용Python 설정 안내를 따르세요. 자세한 내용은 Vertex AI Python API 참고 문서를 참조하세요.

Vertex AI에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

from google.cloud import secretmanager
import google_crc32c  # type: ignore


def add_secret_version(
    project_id: str, secret_id: str, payload: str
) -> secretmanager.SecretVersion:
    """
    Add a new secret version to the given secret with the provided payload.
    """

    # Create the Secret Manager client.
    client = secretmanager.SecretManagerServiceClient()

    # Build the resource name of the parent secret.
    parent = client.secret_path(project_id, secret_id)

    # Convert the string payload into a bytes. This step can be omitted if you
    # pass in bytes instead of a str for the payload argument.
    payload_bytes = payload.encode("UTF-8")

    # Calculate payload checksum. Passing a checksum in add-version request
    # is optional.
    crc32c = google_crc32c.Checksum()
    crc32c.update(payload_bytes)

    # Add the secret version.
    response = client.add_secret_version(
        request={
            "parent": parent,
            "payload": {
                "data": payload_bytes,
                "data_crc32c": int(crc32c.hexdigest(), 16),
            },
        }
    )

    # Print the new secret version name.
    print(f"Added secret version: {response.name}")

Llama 3에서 Weaviate 사용

Model Garden RAG API 노트북에서는 Weaviate 자료 및 Llama 3 모델과 함께 Python용 Vertex AI SDK를 사용하는 방법을 보여줍니다. 노트북을 사용하려면 다음 단계를 따르세요.

  1. Weaviate 데이터베이스를 설정합니다.

  2. Secret Manager를 설정합니다.

  3. Model Garden RAG API 노트북을 사용합니다.

더 많은 예는 예시를 참고하세요.

RAG 코퍼스 만들기

이 코드 샘플은 RAG 코퍼스를 만들고 Weaviate 인스턴스를 벡터 데이터베이스로 설정하는 방법을 보여줍니다.

REST

  # TODO(developer): Update the variables.
  PROJECT_ID = "YOUR_PROJECT_ID"
  # The HTTPS/HTTP Weaviate endpoint you created during provisioning.
  HTTP_ENDPOINT_NAME="https://your.weaviate.endpoint.com"

  # Your Weaviate collection name, which roughly corresponds to a Vertex AI Knowledge Engine Corpus.
  # For example, "MyCollectionName"
  # Note that the first letter needs to be capitalized.
  # Otherwise, Weaviate will capitalize it for you.
  WEAVIATE_COLLECTION_NAME="MyCollectionName"

  # The resource name of your Weaviate API Key your Secret.
  SECRET_NAME="MyWeaviateApiKeySecret"
  # The Secret Manager resource name containing the API Key for your Weaviate endpoint.
  # For example, projects/{project}/secrets/{secret}/versions/latest
  APIKEY_SECRET_VERSION="projects/${PROJECT_ID}/secrets/${SECRET_NAME}/versions/latest"

  # Select a Corpus display name.
  CORPUS_DISPLAY_NAME="SpecialCorpus"

  # Call CreateRagCorpus API and set all Vector DB Config parameters for Weaviate to create a new corpus associated to your selected Weaviate collection.
  curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora \
  -d '{
        "display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
        "rag_vector_db_config" : {
                "weaviate": {
                      "http_endpoint": '\""${HTTP_ENDPOINT_NAME}"\"',
                      "collection_name": '\""${WEAVIATE_COLLECTION_NAME}"\"'
                },
          "api_auth" : {
                  "api_key_config": {
                        "api_key_secret_version": '\""${APIKEY_SECRET_VERSION}"\"'
                  }
          }
        }
    }'

  # TODO(developer): Update the variables.
  # Get operation_id returned in CreateRagCorpus.
  OPERATION_ID="your-operation-id"

  # Poll Operation status until done = true in the response.
  curl -X GET \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
  https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/operations/${OPERATION_ID}

  # Call ListRagCorpora API to verify the RAG corpus is created successfully.
  curl -sS -X GET \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora"

Python

이 샘플을 사용해 보기 전에 Vertex AI 빠른 시작: 클라이언트 라이브러리 사용Python 설정 안내를 따르세요. 자세한 내용은 Vertex AI Python API 참고 문서를 참조하세요.

Vertex AI에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# weaviate_http_endpoint = "weaviate-http-endpoint"
# weaviate_collection_name = "weaviate-collection-name"
# weaviate_api_key_secret_manager_version = "projects/{PROJECT_ID}/secrets/{SECRET_NAME}/versions/latest"
# display_name = "test_corpus"
# description = "Corpus Description"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

# Configure embedding model (Optional)
embedding_model_config = rag.EmbeddingModelConfig(
    publisher_model="publishers/google/models/text-embedding-004"
)

# Configure Vector DB
vector_db = rag.Weaviate(
    weaviate_http_endpoint=weaviate_http_endpoint,
    collection_name=weaviate_collection_name,
    api_key=weaviate_api_key_secret_manager_version,
)

corpus = rag.create_corpus(
    display_name=display_name,
    description=description,
    embedding_model_config=embedding_model_config,
    vector_db=vector_db,
)
print(corpus)
# Example response:
# RagCorpus(name='projects/1234567890/locations/us-central1/ragCorpora/1234567890',
# display_name='test_corpus', description='Corpus Description', embedding_model_config=...
# ...

RAG 파일 사용

RAG API는 파일 업로드, 가져오기, 등록정보, 삭제를 처리합니다.

REST

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • PROJECT_ID: 프로젝트 ID입니다.
  • LOCATION: 요청을 처리하는 리전입니다.
  • RAG_CORPUS_ID: RagCorpus 리소스의 ID입니다.
  • INPUT_FILE: 로컬 파일의 경로입니다.
  • FILE_DISPLAY_NAME: RagFile의 표시 이름
  • RAG_FILE_DESCRIPTION: RagFile에 대한 설명

HTTP 메서드 및 URL:

POST https://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload

JSON 요청 본문:

{
 "rag_file": {
  "display_name": "FILE_DISPLAY_NAME",
  "description": "RAG_FILE_DESCRIPTION"
 }
}

요청을 보내려면 다음 옵션 중 하나를 선택합니다.

curl

요청 본문을 INPUT_FILE 파일에 저장하고 다음 명령어를 실행합니다.

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @INPUT_FILE \
"https://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload"

PowerShell

요청 본문을 INPUT_FILE 파일에 저장하고 다음 명령어를 실행합니다.

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile INPUT_FILE `
-Uri "https://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:upload" | Select-Object -Expand Content
응답에 성공하면 RagFile 리소스가 반환됩니다. RagFile.name 필드의 마지막 구성요소는 서버에서 생성된 rag_file_id입니다.

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"
# path = "path/to/local/file.txt"
# display_name = "file_display_name"
# description = "file description"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_file = rag.upload_file(
    corpus_name=corpus_name,
    path=path,
    display_name=display_name,
    description=description,
)
print(rag_file)
# RagFile(name='projects/[PROJECT_ID]/locations/us-central1/ragCorpora/1234567890/ragFiles/09876543',
#  display_name='file_display_name', description='file description')

RAG 파일 가져오기

Drive 또는 Cloud Storage에서 파일과 폴더를 가져올 수 있습니다.

REST

response.metadata를 사용하여 SDK의 response 객체에서 부분 실패, 요청 시간, 응답 시간을 확인합니다.

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • PROJECT_ID: 프로젝트 ID입니다.
  • LOCATION: 요청을 처리하는 리전입니다.
  • RAG_CORPUS_ID: RagCorpus 리소스의 ID입니다.
  • GCS_URIS: Cloud Storage 위치 목록입니다. 예: gs://my-bucket1, gs://my-bucket2
  • DRIVE_RESOURCE_ID: Drive 리소스의 ID입니다. 예를 들면 다음과 같습니다.
    • https://drive.google.com/file/d/ABCDE
    • https://drive.google.com/corp/drive/u/0/folders/ABCDEFG
  • DRIVE_RESOURCE_TYPE: Drive 리소스 유형입니다. 옵션:
    • RESOURCE_TYPE_FILE - 파일
    • RESOURCE_TYPE_FOLDER - 폴더
  • CHUNK_SIZE: 선택사항: 각 청크에 있어야 하는 토큰 수입니다.
  • CHUNK_OVERLAP: 선택사항: 청크 간에 겹치는 토큰 수입니다.

HTTP 메서드 및 URL:

POST https://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import

JSON 요청 본문:

{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": GCS_URIS
    },
    "google_drive_source": {
      "resource_ids": {
        "resource_id": DRIVE_RESOURCE_ID,
        "resource_type": DRIVE_RESOURCE_TYPE
      },
    }
  }
}

요청을 보내려면 다음 옵션 중 하나를 선택합니다.

curl

요청 본문을 request.json 파일에 저장하고 다음 명령어를 실행합니다.

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import"

PowerShell

요청 본문을 request.json 파일에 저장하고 다음 명령어를 실행합니다.

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/upload/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles:import" | Select-Object -Expand Content
응답에 성공하면 ImportRagFilesOperationMetadata 리소스가 반환됩니다.

다음 샘플은 Cloud Storage에서 파일을 가져오는 방법을 보여줍니다. max_embedding_requests_per_min 제어 필드를 사용하여 RAG 엔진이 ImportRagFiles 색인 생성 프로세스 중에 임베딩 모델을 호출하는 속도를 제한합니다. 이 필드의 기본값은 분당 1000 호출입니다.

// Cloud Storage bucket/file location.
// Such as "gs://rag-e2e-test/"
GCS_URIS=YOUR_GCS_LOCATION

// Enter the QPM rate to limit RAG's access to your embedding model
// Example: 1000
EMBEDDING_MODEL_QPM_RATE=MAX_EMBEDDING_REQUESTS_PER_MIN_LIMIT

// ImportRagFiles
// Import a single Cloud Storage file or all files in a Cloud Storage bucket.
// Input: ENDPOINT, PROJECT_ID, RAG_CORPUS_ID, GCS_URIS
// Output: ImportRagFilesOperationMetadataNumber
// Use ListRagFiles to find the server-generated rag_file_id.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${ENDPOINT}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/ragCorpora/${RAG_CORPUS_ID}/ragFiles:import \
-d '{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": '\""${GCS_URIS}"\"'
    },
    "rag_file_chunking_config": {
      "chunk_size": 512
    },
    "max_embedding_requests_per_min": '"${EMBEDDING_MODEL_QPM_RATE}"'
  }
}'

// Poll the operation status.
// The response contains the number of files imported.
OPERATION_ID=OPERATION_ID
poll_op_wait ${OPERATION_ID}

다음 샘플은 Drive에서 파일을 가져오는 방법을 보여줍니다. max_embedding_requests_per_min 제어 필드를 사용하여 RAG 엔진이 ImportRagFiles 색인 생성 프로세스 중에 임베딩 모델을 호출하는 속도를 제한합니다. 이 필드의 기본값은 분당 1000 호출입니다.

// Google Drive folder location.
FOLDER_RESOURCE_ID=YOUR_GOOGLE_DRIVE_FOLDER_RESOURCE_ID

// Enter the QPM rate to limit RAG's access to your embedding model
// Example: 1000
EMBEDDING_MODEL_QPM_RATE=MAX_EMBEDDING_REQUESTS_PER_MIN_LIMIT

// ImportRagFiles
// Import all files in a Google Drive folder.
// Input: ENDPOINT, PROJECT_ID, RAG_CORPUS_ID, FOLDER_RESOURCE_ID
// Output: ImportRagFilesOperationMetadataNumber
// Use ListRagFiles to find the server-generated rag_file_id.
curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
https://${ENDPOINT}/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION}/ragCorpora/${RAG_CORPUS_ID}/ragFiles:import \
-d '{
  "import_rag_files_config": {
    "google_drive_source": {
      "resource_ids": {
        "resource_id": '\""${FOLDER_RESOURCE_ID}"\"',
        "resource_type": "RESOURCE_TYPE_FOLDER"
      }
    },
    "max_embedding_requests_per_min": '"${EMBEDDING_MODEL_QPM_RATE}"'
  }
}'

// Poll the operation status.
// The response contains the number of files imported.
OPERATION_ID=OPERATION_ID
poll_op_wait ${OPERATION_ID}

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"
# paths = ["https://drive.google.com/file/123", "gs://my_bucket/my_files_dir"]  # Supports Google Cloud Storage and Google Drive Links

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

response = rag.import_files(
    corpus_name=corpus_name,
    paths=paths,
    chunk_size=512,  # Optional
    chunk_overlap=100,  # Optional
    max_embedding_requests_per_min=900,  # Optional
)
print(f"Imported {response.imported_rag_files_count} files.")
# Example response:
# Imported 2 files.

RAG 파일 받기

REST

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • PROJECT_ID: 프로젝트 ID입니다.
  • LOCATION: 요청을 처리하는 리전입니다.
  • RAG_CORPUS_ID: RagCorpus 리소스의 ID입니다.
  • RAG_FILE_ID: RagFile 리소스의 ID입니다.

HTTP 메서드 및 URL:

GET https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID

요청을 보내려면 다음 옵션 중 하나를 선택합니다.

curl

다음 명령어를 실행합니다.

curl -X GET \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID"

PowerShell

다음 명령어를 실행합니다.

$headers = @{  }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content
응답에 성공하면 RagFile 리소스가 반환됩니다.

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# file_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}/ragFiles/{rag_file_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_file = rag.get_file(name=file_name)
print(rag_file)
# Example response:
# RagFile(name='projects/1234567890/locations/us-central1/ragCorpora/11111111111/ragFiles/22222222222',
# display_name='file_display_name', description='file description')

RAG 파일 나열

REST

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • PROJECT_ID: 프로젝트 ID입니다.
  • LOCATION: 요청을 처리하는 리전입니다.
  • RAG_CORPUS_ID: RagCorpus 리소스의 ID입니다.
  • PAGE_SIZE: 표준 목록 페이지 크기입니다. page_size 매개변수를 업데이트하여 페이지당 반환할 RagFiles 수를 조정할 수 있습니다.
  • PAGE_TOKEN: 표준 목록 페이지 토큰입니다. 일반적으로 이전 VertexRagDataService.ListRagFiles 호출의 ListRagFilesResponse.next_page_token을 사용하여 가져옵니다.

HTTP 메서드 및 URL:

GET https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN

요청을 보내려면 다음 옵션 중 하나를 선택합니다.

curl

다음 명령어를 실행합니다.

curl -X GET \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN"

PowerShell

다음 명령어를 실행합니다.

$headers = @{  }

Invoke-WebRequest `
-Method GET `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles?page_size=PAGE_SIZE&page_token=PAGE_TOKEN" | Select-Object -Expand Content
해당 RAG_CORPUS_ID 아래의 RagFiles 목록과 함께 성공적인 상태 코드(2xx)를 받아야 합니다.

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

files = rag.list_files(corpus_name=corpus_name)
for file in files:
    print(file.display_name)
    print(file.name)
# Example response:
# g-drive_file.txt
# projects/1234567890/locations/us-central1/ragCorpora/111111111111/ragFiles/222222222222
# g_cloud_file.txt
# projects/1234567890/locations/us-central1/ragCorpora/111111111111/ragFiles/333333333333

RAG 파일 삭제

REST

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • PROJECT_ID: 프로젝트 ID입니다.
  • LOCATION: 요청을 처리하는 리전입니다.
  • RAG_CORPUS_ID: RagCorpus 리소스의 ID입니다.
  • RAG_FILE_ID: RagFile 리소스의 ID입니다. 형식은 projects/{project}/locations/{location}/ragCorpora/{rag_corpus}/ragFiles/{rag_file_id}입니다.

HTTP 메서드 및 URL:

DELETE https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID

요청을 보내려면 다음 옵션 중 하나를 선택합니다.

curl

다음 명령어를 실행합니다.

curl -X DELETE \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID"

PowerShell

다음 명령어를 실행합니다.

$headers = @{  }

Invoke-WebRequest `
-Method DELETE `
-Headers $headers `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/ragCorpora/RAG_CORPUS_ID/ragFiles/RAG_FILE_ID" | Select-Object -Expand Content
응답에 성공하면 DeleteOperationMetadata 리소스가 반환됩니다.

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# file_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}/ragFiles/{rag_file_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag.delete_file(name=file_name)
print(f"File {file_name} deleted.")
# Example response:
# Successfully deleted the RagFile.
# File projects/1234567890/locations/us-central1/ragCorpora/1111111111/ragFiles/2222222222 deleted.

컨텍스트 검색

사용자가 질문하거나 프롬프트를 제공하면 RAG의 검색 구성요소는 기술 자료를 통해 쿼리와 관련된 정보를 찾습니다.

REST

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • LOCATION: 요청을 처리하는 리전입니다.
  • PROJECT_ID: 프로젝트 ID
  • RAG_CORPUS_RESOURCE: RagCorpus 리소스의 이름입니다. 형식은 projects/{project}/locations/{location}/ragCorpora/{rag_corpus}입니다.
  • VECTOR_DISTANCE_THRESHOLD: 벡터 거리가 기준점보다 작은 컨텍스트만 반환됩니다.
  • TEXT: 관련 컨텍스트를 가져올 쿼리 텍스트입니다.
  • SIMILARITY_TOP_K: 검색할 상위 컨텍스트 수입니다.

HTTP 메서드 및 URL:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts

JSON 요청 본문:

{
 "vertex_rag_store": {
    "rag_resources": {
      "rag_corpus": "RAG_CORPUS_RESOURCE",
    },
    "vector_distance_threshold": 0.8
  },
  "query": {
   "text": "TEXT",
   "similarity_top_k": SIMILARITY_TOP_K
  }
 }

요청을 보내려면 다음 옵션 중 하나를 선택합니다.

curl

요청 본문을 request.json 파일에 저장하고 다음 명령어를 실행합니다.

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts"

PowerShell

요청 본문을 request.json 파일에 저장하고 다음 명령어를 실행합니다.

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION:retrieveContexts" | Select-Object -Expand Content
성공 상태 코드(2xx) 및 관련 RagFiles 목록을 받아야 합니다.

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/[PROJECT_ID]/locations/us-central1/ragCorpora/[rag_corpus_id]"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

response = rag.retrieval_query(
    rag_resources=[
        rag.RagResource(
            rag_corpus=corpus_name,
            # Optional: supply IDs from `rag.list_files()`.
            # rag_file_ids=["rag-file-1", "rag-file-2", ...],
        )
    ],
    text="Hello World!",
    similarity_top_k=10,  # Optional
    vector_distance_threshold=0.5,  # Optional
    # vector_search_alpha=0.5, # Optional - Only supported for Weaviate
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

콘텐츠 생성

예측은 콘텐츠를 생성하는 LLM 메서드를 제어합니다.

REST

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • PROJECT_ID: 프로젝트 ID입니다.
  • LOCATION: 요청을 처리하는 리전입니다.
  • MODEL_ID: 콘텐츠 생성을 위한 LLM 모델입니다. 예를 들면 gemini-1.5-pro-002입니다.
  • GENERATION_METHOD: 콘텐츠 생성을 위한 LLM 방법입니다. 선택사항: generateContent, streamGenerateContent
  • INPUT_PROMPT: 콘텐츠 생성을 위해 LLM에 전송된 텍스트입니다. 업로드된 래그 파일과 관련된 프롬프트를 사용해 보세요.
  • RAG_CORPUS_RESOURCE: RagCorpus 리소스의 이름입니다. 형식은 projects/{project}/locations/{location}/ragCorpora/{rag_corpus}입니다.
  • SIMILARITY_TOP_K: 선택사항: 검색할 인기 컨텍스트 수입니다.
  • VECTOR_DISTANCE_THRESHOLD: 선택사항: 벡터 거리가 기준점보다 작은 컨텍스트가 반환됩니다.

HTTP 메서드 및 URL:

POST https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD

JSON 요청 본문:

{
 "contents": {
  "role": "user",
  "parts": {
    "text": "INPUT_PROMPT"
  }
 },
 "tools": {
  "retrieval": {
   "disable_attribution": false,
   "vertex_rag_store": {
    "rag_resources": {
      "rag_corpus": "RAG_CORPUS_RESOURCE",
    },
    "similarity_top_k": SIMILARITY_TOP_K,
    "vector_distance_threshold": VECTOR_DISTANCE_THRESHOLD
   }
  }
 }
}

요청을 보내려면 다음 옵션 중 하나를 선택합니다.

curl

요청 본문을 request.json 파일에 저장하고 다음 명령어를 실행합니다.

curl -X POST \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD"

PowerShell

요청 본문을 request.json 파일에 저장하고 다음 명령어를 실행합니다.

$headers = @{  }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1beta1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/MODEL_ID:GENERATION_METHOD" | Select-Object -Expand Content
응답에 성공하면 인용과 함께 생성된 콘텐츠가 반환됩니다.

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
from vertexai.preview.generative_models import GenerativeModel, Tool
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_retrieval_tool = Tool.from_retrieval(
    retrieval=rag.Retrieval(
        source=rag.VertexRagStore(
            rag_resources=[
                rag.RagResource(
                    rag_corpus=corpus_name,
                    # Optional: supply IDs from `rag.list_files()`.
                    # rag_file_ids=["rag-file-1", "rag-file-2", ...],
                )
            ],
            similarity_top_k=3,  # Optional
            vector_distance_threshold=0.5,  # Optional
        ),
    )
)

rag_model = GenerativeModel(
    model_name="gemini-1.5-flash-001", tools=[rag_retrieval_tool]
)
response = rag_model.generate_content("Why is the sky blue?")
print(response.text)
# Example response:
#   The sky appears blue due to a phenomenon called Rayleigh scattering.
#   Sunlight, which contains all colors of the rainbow, is scattered
#   by the tiny particles in the Earth's atmosphere....
#   ...

하이브리드 검색은 시맨틱 검색과 키워드 검색을 모두 결합하여 검색 결과의 관련성을 개선하는 Weaviate 데이터베이스에서 지원됩니다. 검색 결과를 검색하는 동안 시맨틱(밀집 벡터) 및 키워드 일치(희소 벡터)의 유사성 점수를 조합하여 최종 순위 결과를 생성합니다.

RAG 엔진 검색 API를 사용하는 하이브리드 검색

다음은 RAG 엔진 검색 API를 사용하여 하이브리드 검색을 사용 설정하는 방법의 예입니다.

REST

  # TODO(developer): Update the variables.
  PROJECT_ID = "YOUR_PROJECT_ID"
  # The HTTPS/HTTP Weaviate endpoint you created during provisioning.
  HTTP_ENDPOINT_NAME="https://your.weaviate.endpoint.com"

  # Your Weaviate collection name, which roughly corresponds to a Vertex AI Knowledge Engine Corpus.
  # For example, "MyCollectionName"
  # Note that the first letter needs to be capitalized.
  # Otherwise, Weaviate will capitalize it for you.
  WEAVIATE_COLLECTION_NAME="MyCollectionName"

  # The resource name of your Weaviate API Key your Secret.
  SECRET_NAME="MyWeaviateApiKeySecret"
  # The Secret Manager resource name containing the API Key for your Weaviate endpoint.
  # For example, projects/{project}/secrets/{secret}/versions/latest
  APIKEY_SECRET_VERSION="projects/${PROJECT_ID}/secrets/${SECRET_NAME}/versions/latest"

  # Select a Corpus display name.
  CORPUS_DISPLAY_NAME="SpecialCorpus"

  # Call CreateRagCorpus API and set all Vector DB Config parameters for Weaviate to create a new corpus associated to your selected Weaviate collection.
  curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora \
  -d '{
        "display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
        "rag_vector_db_config" : {
                "weaviate": {
                      "http_endpoint": '\""${HTTP_ENDPOINT_NAME}"\"',
                      "collection_name": '\""${WEAVIATE_COLLECTION_NAME}"\"'
                },
          "api_auth" : {
                  "api_key_config": {
                        "api_key_secret_version": '\""${APIKEY_SECRET_VERSION}"\"'
                  }
          }
        }
    }'

  # TODO(developer): Update the variables.
  # Get operation_id returned in CreateRagCorpus.
  OPERATION_ID="your-operation-id"

  # Poll Operation status until done = true in the response.
  curl -X GET \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
  https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/operations/${OPERATION_ID}

  # Call ListRagCorpora API to verify the RAG corpus is created successfully.
  curl -sS -X GET \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora"

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/[PROJECT_ID]/locations/us-central1/ragCorpora/[rag_corpus_id]"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

response = rag.retrieval_query(
    rag_resources=[
        rag.RagResource(
            rag_corpus=corpus_name,
            # Optional: supply IDs from `rag.list_files()`.
            # rag_file_ids=["rag-file-1", "rag-file-2", ...],
        )
    ],
    text="Hello World!",
    similarity_top_k=10,  # Optional
    vector_distance_threshold=0.5,  # Optional
    # vector_search_alpha=0.5, # Optional - Only supported for Weaviate
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

기반 생성을 위해 하이브리드 검색 및 RAG 엔진 사용

이는 기반 생성을 위해 하이브리드 검색 및 RAG 엔진을 사용하는 방법의 예입니다.

REST

  # TODO(developer): Update the variables.
  PROJECT_ID = "YOUR_PROJECT_ID"
  # The HTTPS/HTTP Weaviate endpoint you created during provisioning.
  HTTP_ENDPOINT_NAME="https://your.weaviate.endpoint.com"

  # Your Weaviate collection name, which roughly corresponds to a Vertex AI Knowledge Engine Corpus.
  # For example, "MyCollectionName"
  # Note that the first letter needs to be capitalized.
  # Otherwise, Weaviate will capitalize it for you.
  WEAVIATE_COLLECTION_NAME="MyCollectionName"

  # The resource name of your Weaviate API Key your Secret.
  SECRET_NAME="MyWeaviateApiKeySecret"
  # The Secret Manager resource name containing the API Key for your Weaviate endpoint.
  # For example, projects/{project}/secrets/{secret}/versions/latest
  APIKEY_SECRET_VERSION="projects/${PROJECT_ID}/secrets/${SECRET_NAME}/versions/latest"

  # Select a Corpus display name.
  CORPUS_DISPLAY_NAME="SpecialCorpus"

  # Call CreateRagCorpus API and set all Vector DB Config parameters for Weaviate to create a new corpus associated to your selected Weaviate collection.
  curl -X POST \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
  -H "Content-Type: application/json" \
  https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora \
  -d '{
        "display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
        "rag_vector_db_config" : {
                "weaviate": {
                      "http_endpoint": '\""${HTTP_ENDPOINT_NAME}"\"',
                      "collection_name": '\""${WEAVIATE_COLLECTION_NAME}"\"'
                },
          "api_auth" : {
                  "api_key_config": {
                        "api_key_secret_version": '\""${APIKEY_SECRET_VERSION}"\"'
                  }
          }
        }
    }'

  # TODO(developer): Update the variables.
  # Get operation_id returned in CreateRagCorpus.
  OPERATION_ID="your-operation-id"

  # Poll Operation status until done = true in the response.
  curl -X GET \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
  https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/operations/${OPERATION_ID}

  # Call ListRagCorpora API to verify the RAG corpus is created successfully.
  curl -sS -X GET \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://us-central1-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/us-central1/ragCorpora"

Python용 Vertex AI SDK

Python용 Vertex AI SDK를 설치하거나 업데이트하는 방법은 Python용 Vertex AI SDK 설치를 참조하세요. 자세한 내용은 Python용 Vertex AI SDK API 참조 문서를 확인하세요.


from vertexai.preview import rag
from vertexai.preview.generative_models import GenerativeModel, Tool
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# corpus_name = "projects/{PROJECT_ID}/locations/us-central1/ragCorpora/{rag_corpus_id}"

# Initialize Vertex AI API once per session
vertexai.init(project=PROJECT_ID, location="us-central1")

rag_retrieval_tool = Tool.from_retrieval(
    retrieval=rag.Retrieval(
        source=rag.VertexRagStore(
            rag_resources=[
                rag.RagResource(
                    rag_corpus=corpus_name,
                    # Optional: supply IDs from `rag.list_files()`.
                    # rag_file_ids=["rag-file-1", "rag-file-2", ...],
                )
            ],
            similarity_top_k=3,  # Optional
            vector_distance_threshold=0.5,  # Optional
        ),
    )
)

rag_model = GenerativeModel(
    model_name="gemini-1.5-flash-001", tools=[rag_retrieval_tool]
)
response = rag_model.generate_content("Why is the sky blue?")
print(response.text)
# Example response:
#   The sky appears blue due to a phenomenon called Rayleigh scattering.
#   Sunlight, which contains all colors of the rainbow, is scattered
#   by the tiny particles in the Earth's atmosphere....
#   ...

다음 단계