Vertex AI RAG Engine์—์„œ Vertex AI ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์‚ฌ์šฉ

์ด ํŽ˜์ด์ง€์—์„œ๋Š” Vertex AI RAG Engine์„ Vertex AI ๋ฒกํ„ฐ ๊ฒ€์ƒ‰์— ์—ฐ๊ฒฐํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

์ด ๋…ธํŠธ๋ถ Vertex AI ๋ฒกํ„ฐ ๊ฒ€์ƒ‰์„ ์‚ฌ์šฉํ•œ Vertex AI RAG Engine์„ ์‚ฌ์šฉํ•˜์—ฌ ๋”ฐ๋ผํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

Vertex AI RAG Engine์€ Spanner๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜๋Š” ๊ธฐ๋ณธ ์ œ๊ณต ๋ฒกํ„ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ…์ŠคํŠธ ๋ฌธ์„œ์˜ ๋ฒกํ„ฐ ํ‘œํ˜„์„ ์ €์žฅํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ•๋ ฅํ•œ ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ๋ฒกํ„ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ํŠน์ • ์ฟผ๋ฆฌ์— ๋Œ€ํ•œ ๋ฌธ์„œ์˜ ์‹œ๋งจํ‹ฑ ์œ ์‚ฌ์„ฑ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ด€๋ จ ๋ฌธ์„œ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Vertex AI ๋ฒกํ„ฐ ๊ฒ€์ƒ‰์„ Vertex AI RAG Engine์— ์ถ”๊ฐ€ ๋ฒกํ„ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ํ†ตํ•ฉํ•˜๋ฉด ๋ฒกํ„ฐ ๊ฒ€์ƒ‰์˜ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜์—ฌ ์งง์€ ์ง€์—ฐ ์‹œ๊ฐ„์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜์—ฌ RAG ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์„ฑ๋Šฅ๊ณผ ํ™•์žฅ์„ฑ์„ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Vertex AI ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์„ค์ •

Vertex AI ๋ฒกํ„ฐ ๊ฒ€์ƒ‰์€ Google ์—ฐ๊ตฌํŒ€์—์„œ ๊ฐœ๋ฐœํ•œ ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ๊ธฐ์ˆ ์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•ฉ๋‹ˆ๋‹ค. ๋ฒกํ„ฐ ๊ฒ€์ƒ‰์„ ์‚ฌ์šฉํ•˜๋ฉด Google ๊ฒ€์ƒ‰, YouTube, Google Play์™€ ๊ฐ™์€ Google ์ œํ’ˆ์˜ ๊ธฐ๋ฐ˜์ด ๋˜๋Š” ๋™์ผํ•œ ์ธํ”„๋ผ๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Vertex AI RAG Engine๊ณผ ํ†ตํ•ฉํ•˜๋ ค๋ฉด ๋นˆ ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

Vertex AI SDK ์„ค์ •

Vertex AI SDK๋ฅผ ์„ค์ •ํ•˜๋ ค๋ฉด ์„ค์ •์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ ๋งŒ๋“ค๊ธฐ

RAG ์ฝ”ํผ์Šค์™€ ํ˜ธํ™˜๋˜๋Š” ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ์„ ๋งŒ๋“ค๋ ค๋ฉด ์ƒ‰์ธ์ด ๋‹ค์Œ ๊ธฐ์ค€์„ ์ถฉ์กฑํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

  1. IndexUpdateMethod๋Š” STREAM_UPDATE์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ŠคํŠธ๋ฆผ ์ƒ‰์ธ ๋งŒ๋“ค๊ธฐ๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”.

  2. ๊ฑฐ๋ฆฌ ์ธก์ • ์œ ํ˜•์€ ๋‹ค์Œ ์ค‘ ํ•˜๋‚˜๋กœ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

    • DOT_PRODUCT_DISTANCE
    • COSINE_DISTANCE
  3. ๋ฒกํ„ฐ์˜ ์ฐจ์›์€ RAG ์ฝ”ํผ์Šค์— ์‚ฌ์šฉํ•  ์ž„๋ฒ ๋”ฉ ๋ชจ๋ธ๊ณผ ์ผ์น˜ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์„ ํƒ์‚ฌํ•ญ์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์กฐ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Python์šฉ Vertex AI SDK

Python์šฉ Vertex AI SDK๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Python์šฉ Vertex AI SDK ์„ค์น˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Python์šฉ Vertex AI SDK API ์ฐธ์กฐ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

def vector_search_create_streaming_index(
    project: str, location: str, display_name: str, gcs_uri: Optional[str] = None
) -> aiplatform.MatchingEngineIndex:
    """Create a vector search index.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        display_name (str): Required. The index display name
        gcs_uri (str): Optional. The Google Cloud Storage uri for index content

    Returns:
        The created MatchingEngineIndex.
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

    # Create Index
    index = aiplatform.MatchingEngineIndex.create_tree_ah_index(
        display_name=display_name,
        contents_delta_uri=gcs_uri,
        description="Matching Engine Index",
        dimensions=100,
        approximate_neighbors_count=150,
        leaf_node_embedding_count=500,
        leaf_nodes_to_search_percent=7,
        index_update_method="STREAM_UPDATE",  # Options: STREAM_UPDATE, BATCH_UPDATE
        distance_measure_type=aiplatform.matching_engine.matching_engine_index_config.DistanceMeasureType.DOT_PRODUCT_DISTANCE,
    )

    return index

๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ ์—”๋“œํฌ์ธํŠธ ๋งŒ๋“ค๊ธฐ

๊ณต๊ฐœ ์—”๋“œํฌ์ธํŠธ๋Š” Vertex AI RAG Engine์—์„œ ์ง€์›๋ฉ๋‹ˆ๋‹ค.

Python์šฉ Vertex AI SDK

Python์šฉ Vertex AI SDK๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Python์šฉ Vertex AI SDK ์„ค์น˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Python์šฉ Vertex AI SDK API ์ฐธ์กฐ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

def vector_search_create_index_endpoint(
    project: str, location: str, display_name: str
) -> None:
    """Create a vector search index endpoint.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        display_name (str): Required. The index endpoint display name
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

    # Create Index Endpoint
    index_endpoint = aiplatform.MatchingEngineIndexEndpoint.create(
        display_name=display_name,
        public_endpoint_enabled=True,
        description="Matching Engine Index Endpoint",
    )

    print(index_endpoint.name)

์ƒ‰์ธ ์—”๋“œํฌ์ธํŠธ์— ์ƒ‰์ธ ๋ฐฐํฌ

์ตœ๊ทผ์ ‘ ์ด์›ƒ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜๊ธฐ ์ „์— ์ƒ‰์ธ์„ ์ƒ‰์ธ ์—”๋“œํฌ์ธํŠธ์— ๋ฐฐํฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

Python์šฉ Vertex AI SDK

Python์šฉ Vertex AI SDK๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Python์šฉ Vertex AI SDK ์„ค์น˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Python์šฉ Vertex AI SDK API ์ฐธ์กฐ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.

def vector_search_deploy_index(
    project: str,
    location: str,
    index_name: str,
    index_endpoint_name: str,
    deployed_index_id: str,
) -> None:
    """Deploy a vector search index to a vector search index endpoint.

    Args:
        project (str): Required. Project ID
        location (str): Required. The region name
        index_name (str): Required. The index to update. A fully-qualified index
          resource name or a index ID.  Example:
          "projects/123/locations/us-central1/indexes/my_index_id" or
          "my_index_id".
        index_endpoint_name (str): Required. Index endpoint to deploy the index
          to.
        deployed_index_id (str): Required. The user specified ID of the
          DeployedIndex.
    """
    # Initialize the Vertex AI client
    aiplatform.init(project=project, location=location)

    # Create the index instance from an existing index
    index = aiplatform.MatchingEngineIndex(index_name=index_name)

    # Create the index endpoint instance from an existing endpoint.
    index_endpoint = aiplatform.MatchingEngineIndexEndpoint(
        index_endpoint_name=index_endpoint_name
    )

    # Deploy Index to Endpoint
    index_endpoint = index_endpoint.deploy_index(
        index=index, deployed_index_id=deployed_index_id
    )

    print(index_endpoint.deployed_indexes)

์ƒ‰์ธ์„ ์ƒ‰์ธ ์—”๋“œํฌ์ธํŠธ์— ์ฒ˜์Œ ๋ฐฐํฌํ•˜๋Š” ๊ฒฝ์šฐ ์ƒ‰์ธ์„ ์ €์žฅํ•˜๊ธฐ ์ „์— ๋ฐฑ์—”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ๋นŒ๋“œํ•˜๊ณ  ์‹œ์ž‘ํ•˜๋Š” ๋ฐ ์•ฝ 30๋ถ„์ด ๊ฑธ๋ฆฝ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ๋ฐฐํฌ ํ›„ ๋ช‡ ์ดˆ ๋‚ด์— ์ƒ‰์ธ์ด ์ค€๋น„๋ฉ๋‹ˆ๋‹ค. ์ƒ‰์ธ ๋ฐฐํฌ ์ƒํƒœ๋ฅผ ๋ณด๋ ค๋ฉด ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ฝ˜์†”์„ ์—ด๊ณ  ์ƒ‰์ธ ์—”๋“œํฌ์ธํŠธ ํƒญ์„ ์„ ํƒํ•œ ๋‹ค์Œ ์ƒ‰์ธ ์—”๋“œํฌ์ธํŠธ๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.

์ƒ‰์ธ ๋ฐ ์ƒ‰์ธ ์—”๋“œํฌ์ธํŠธ์˜ ๋ฆฌ์†Œ์Šค ์ด๋ฆ„์„ ์‹๋ณ„ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์†Œ์Šค ์ด๋ฆ„์˜ ํ˜•์‹์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexes/${INDEX_ID}
  • projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexEndpoints/${INDEX_ENDPOINT_ID}.

Vertex AI RAG Engine์—์„œ Vertex AI ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์‚ฌ์šฉ

๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ธ์Šคํ„ด์Šค๊ฐ€ ์„ค์ •๋œ ํ›„ ์ด ์„น์…˜์˜ ๋‹จ๊ณ„์— ๋”ฐ๋ผ ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ธ์Šคํ„ด์Šค๋ฅผ RAG ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋ฒกํ„ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

๋ฒกํ„ฐ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์„ค์ •ํ•˜์—ฌ RAG ์ฝ”ํผ์Šค ๋งŒ๋“ค๊ธฐ

RAG ์ฝ”ํผ์Šค๋ฅผ ๋งŒ๋“ค ๋•Œ๋Š” ์ „์ฒด INDEX_ENDPOINT_NAME ๋ฐ INDEX_NAME๋งŒ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค. ์ƒ‰์ธ ๋ฐ ์ƒ‰์ธ ์—”๋“œํฌ์ธํŠธ ๋ฆฌ์†Œ์Šค ์ด๋ฆ„ ๋ชจ๋‘์— ์ˆซ์ž ID๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. RAG ์ฝ”ํผ์Šค๊ฐ€ ์ƒ์„ฑ๋˜๊ณ  ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ๊ณผ ์ž๋™์œผ๋กœ ์—ฐ๊ฒฐ๋ฉ๋‹ˆ๋‹ค. ๊ธฐ์ค€์— ๋Œ€ํ•ด ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ๊ฐ€ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค. ์š”๊ตฌ์‚ฌํ•ญ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ถฉ์กฑ๋˜์ง€ ์•Š์œผ๋ฉด ์š”์ฒญ์ด ๊ฑฐ๋ถ€๋ฉ๋‹ˆ๋‹ค.

Python

์ด ์ƒ˜ํ”Œ์„ ์‚ฌ์šฉํ•ด ๋ณด๊ธฐ ์ „์— Vertex AI ๋น ๋ฅธ ์‹œ์ž‘: ํด๋ผ์ด์–ธํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ์˜ Python ์„ค์ • ์•ˆ๋‚ด๋ฅผ ๋”ฐ๋ฅด์„ธ์š”. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Vertex AI Python API ์ฐธ๊ณ  ๋ฌธ์„œ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

Vertex AI์— ์ธ์ฆํ•˜๋ ค๋ฉด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ธฐ๋ณธ ์‚ฌ์šฉ์ž ์ธ์ฆ ์ •๋ณด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋กœ์ปฌ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์˜ ์ธ์ฆ ์„ค์ •์„ ์ฐธ์กฐํ•˜์„ธ์š”.


from vertexai import rag
import vertexai

# TODO(developer): Update and un-comment below lines
# PROJECT_ID = "your-project-id"
# vector_search_index_name = "projects/{PROJECT_ID}/locations/{LOCATION}/indexes/{INDEX_ID}"
# vector_search_index_endpoint_name = "projects/{PROJECT_ID}/locations/{LOCATION}/indexEndpoints/{INDEX_ENDPOINT_ID}"
# 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.RagEmbeddingModelConfig(
    vertex_prediction_endpoint=rag.VertexPredictionEndpoint(
        publisher_model="publishers/google/models/text-embedding-005"
    )
)

# Configure Vector DB
vector_db = rag.VertexVectorSearch(
    index=vector_search_index_name, index_endpoint=vector_search_index_endpoint_name
)

corpus = rag.create_corpus(
    display_name=display_name,
    description=description,
    backend_config=rag.RagVectorDbConfig(
        rag_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=...
# ...

REST

  # TODO(developer): Update and un-comment the following lines:
  # CORPUS_DISPLAY_NAME = "YOUR_CORPUS_DISPLAY_NAME"
  # Full index/indexEndpoint resource name
  # Index: projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexes/${INDEX_ID}
  # IndexEndpoint: projects/${PROJECT_ID}/locations/${LOCATION_ID}/indexEndpoints/${INDEX_ENDPOINT_ID}
  # INDEX_RESOURCE_NAME = "YOUR_INDEX_ENDPOINT_RESOURCE_NAME"
  # INDEX_NAME = "YOUR_INDEX_RESOURCE_NAME"
  # Call CreateRagCorpus API to create a new RagCorpus
  curl -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/json" https://${LOCATION_ID}-aiplatform.googleapis.com/v1beta1/projects/${PROJECT_ID}/locations/${LOCATION_ID}/ragCorpora -d '{
        "display_name" : '\""${CORPUS_DISPLAY_NAME}"\"',
        "rag_vector_db_config" : {
                "vertex_vector_search": {
                  "index":'\""${INDEX_NAME}"\"'
              "index_endpoint":'\""${INDEX_ENDPOINT_NAME}"\"'
                }
          }
    }'

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

RAG API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ

ImportRagFiles API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Cloud Storage ๋˜๋Š” Google Drive์˜ ํŒŒ์ผ์„ ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ์œผ๋กœ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค. ํŒŒ์ผ์€ ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ์— ์‚ฝ์ž…๋˜์–ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค.

REST

# TODO(developer): Update and uncomment the following lines:
# RAG_CORPUS_ID = "your-rag-corpus-id"
#
# Google Cloud Storage bucket/file location.
# For example, "gs://rag-fos-test/"
# GCS_URIS= "your-gcs-uris"

# Call ImportRagFiles API to embed files and store in the BigQuery table
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/${RAG_CORPUS_ID}/ragFiles:import \
-d '{
  "import_rag_files_config": {
    "gcs_source": {
      "uris": '\""${GCS_URIS}"\"'
    },
    "rag_file_chunking_config": {
      "chunk_size": 512
    }
  }
}'

# Call ListRagFiles API to verify the files are imported successfully
curl -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/${RAG_CORPUS_ID}/ragFiles

Python์šฉ Vertex AI SDK

Python์šฉ Vertex AI SDK๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Python์šฉ Vertex AI SDK ์„ค์น˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Python์šฉ Vertex AI SDK API ์ฐธ์กฐ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.


from vertexai 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,
    transformation_config=rag.TransformationConfig(
        rag.ChunkingConfig(chunk_size=512, chunk_overlap=100)
    ),
    import_result_sink="gs://sample-existing-folder/sample_import_result_unique.ndjson",  # Optional, this has to be an existing storage bucket folder, and file name has to be unique (non-existent).
    max_embedding_requests_per_min=900,  # Optional
)
print(f"Imported {response.imported_rag_files_count} files.")
# Example response:
# Imported 2 files.

RAG API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ด€๋ จ ์ปจํ…์ŠคํŠธ ๊ฐ€์ ธ์˜ค๊ธฐ

ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๊ธฐ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด RetrieveContexts API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ์—์„œ ๊ด€๋ จ ์ปจํ…์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

REST

# TODO(developer): Update and uncomment the following lines:
# RETRIEVAL_QUERY="your-retrieval-query"
#
# Full RAG corpus resource name
# Format:
# "projects/${PROJECT_ID}/locations/us-central1/ragCorpora/${RAG_CORPUS_ID}"
# RAG_CORPUS_RESOURCE="your-rag-corpus-resource"

# Call RetrieveContexts API to retrieve relevant contexts
curl -X POST \
  -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:retrieveContexts \
  -d '{
    "vertex_rag_store": {
      "rag_resources": {
          "rag_corpus": '\""${RAG_CORPUS_RESOURCE}"\"',
        },
    },
    "query": {
      "text": '\""${RETRIEVAL_QUERY}"\"',
      "similarity_top_k": 10
    }
  }'

Python์šฉ Vertex AI SDK

Python์šฉ Vertex AI SDK๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Python์šฉ Vertex AI SDK ์„ค์น˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Python์šฉ Vertex AI SDK API ์ฐธ์กฐ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.


from vertexai 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!",
    rag_retrieval_config=rag.RagRetrievalConfig(
        top_k=10,
        filter=rag.utils.resources.Filter(vector_distance_threshold=0.5),
    ),
)
print(response)
# Example response:
# contexts {
#   contexts {
#     source_uri: "gs://your-bucket-name/file.txt"
#     text: "....
#   ....

Vertex AI Gemini API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ˜ํ…์ธ  ์ƒ์„ฑ

Gemini ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ˜ํ…์ธ ๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด Vertex AI GenerateContent API๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. ์š”์ฒญ์— RAG_CORPUS_RESOURCE๋ฅผ ์ง€์ •ํ•˜๋ฉด API๊ฐ€ ๋ฒกํ„ฐ ๊ฒ€์ƒ‰ ์ƒ‰์ธ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ž๋™์œผ๋กœ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

REST

# TODO(developer): Update and uncomment the following lines:
# MODEL_ID=gemini-2.0-flash
# GENERATE_CONTENT_PROMPT="your-generate-content-prompt"

# GenerateContent with contexts retrieved from the FeatureStoreOnline index
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/publishers/google/models/${MODEL_ID}:generateContent \
-d '{
  "contents": {
    "role": "user",
    "parts": {
      "text": '\""${GENERATE_CONTENT_PROMPT}"\"'
    }
  },
  "tools": {
    "retrieval": {
      "vertex_rag_store": {
        "rag_resources": {
            "rag_corpus": '\""${RAG_CORPUS_RESOURCE}"\"',
          },
        "similarity_top_k": 8,
        "vector_distance_threshold": 0.32
      }
    }
  }
}'

Python์šฉ Vertex AI SDK

Python์šฉ Vertex AI SDK๋ฅผ ์„ค์น˜ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•˜๋Š” ๋ฐฉ๋ฒ•์€ Python์šฉ Vertex AI SDK ์„ค์น˜๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”. ์ž์„ธํ•œ ๋‚ด์šฉ์€ Python์šฉ Vertex AI SDK API ์ฐธ์กฐ ๋ฌธ์„œ๋ฅผ ํ™•์ธํ•˜์„ธ์š”.


from vertexai import rag
from vertexai.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", ...],
                )
            ],
            rag_retrieval_config=rag.RagRetrievalConfig(
                top_k=10,
                filter=rag.utils.resources.Filter(vector_distance_threshold=0.5),
            ),
        ),
    )
)

rag_model = GenerativeModel(
    model_name="gemini-2.0-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....
#   ...

๋‹ค์Œ ๋‹จ๊ณ„