From eea7e8497f795d93e30ecf0fe2d0c1a201a74026 Mon Sep 17 00:00:00 2001 From: chrischristiansen-glitch Date: Sun, 24 May 2026 13:59:27 +0200 Subject: [PATCH] =?UTF-8?q?fix:=2007-rag-setup.sh=20=E2=80=94=20upgrade=20?= =?UTF-8?q?SDK=20+=20multi-attempt=20serverless=20corpus=20creation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- infrastructure/07-rag-setup.sh | 90 +++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 24 deletions(-) diff --git a/infrastructure/07-rag-setup.sh b/infrastructure/07-rag-setup.sh index 10d639c..bc17208 100644 --- a/infrastructure/07-rag-setup.sh +++ b/infrastructure/07-rag-setup.sh @@ -25,6 +25,11 @@ gcloud services enable aiplatform.googleapis.com \ --project="${PROJECT_ID}" --quiet echo "✓ APIs enabled" +# ── Upgrade SDK to ensure RagManagedDb support ─────────────────────────────── +echo " Upgrading google-cloud-aiplatform SDK..." +pip install --quiet --upgrade google-cloud-aiplatform +echo "✓ SDK upgraded" + # ── GCS bucket (idempotent) ────────────────────────────────────────────── CORPUS_BUCKET="${PROJECT_ID}-agent-corpus" if ! gsutil ls -b "gs://${CORPUS_BUCKET}" &>/dev/null; then @@ -43,7 +48,7 @@ else echo " No seed documents in docs/corpus-seed/ — skipping" fi -# ── Python: create corpus ─────────────────────────────────────────────────────── +# ── Python: create corpus ──────────────────────────────────────────────────── python3 - << 'PYEOF' import os, sys @@ -78,38 +83,75 @@ if corpus is None: publisher_model="publishers/google/models/text-embedding-005" ) - # Try Serverless mode (RagManagedDb) first, fall back to plain create - created = False - for attempt in ["serverless", "plain"]: + # Attempt order: + # 1. SDK >= 1.87 : RagVectorDbConfig(rag_managed_db=RagManagedDb()) + # 2. Older SDK : RagVectorDbConfig(rag_managed_db=RagManagedDbConfig()) + # 3. REST fallback: gapic-style with vector_db proto dict + attempts = ["new_sdk", "old_sdk", "proto_dict"] + for attempt in attempts: try: - if attempt == "serverless": - # SDK >= 1.87: RagVectorDbConfig with rag_managed_db - try: - vector_db = rag.RagVectorDbConfig( - rag_managed_db=rag.RagManagedDb() - ) - corpus = rag.create_corpus( - display_name=DISPLAY_NAME, - embedding_model_config=embedding_config, - vector_db=vector_db, - ) - except TypeError: - # Older SDK: RagManagedDb not a kwarg — skip to plain - raise - else: - # Plain create — lets Google pick default (Serverless on new projects) + if attempt == "new_sdk": + vector_db = rag.RagVectorDbConfig( + rag_managed_db=rag.RagManagedDb() + ) corpus = rag.create_corpus( display_name=DISPLAY_NAME, embedding_model_config=embedding_config, + vector_db=vector_db, ) + + elif attempt == "old_sdk": + # Some SDK versions use RagManagedDbConfig instead + RagManagedDbConfig = getattr(rag, "RagManagedDbConfig", None) + if RagManagedDbConfig is None: + raise AttributeError("RagManagedDbConfig not in this SDK version") + vector_db = rag.RagVectorDbConfig( + rag_managed_db=RagManagedDbConfig() + ) + corpus = rag.create_corpus( + display_name=DISPLAY_NAME, + embedding_model_config=embedding_config, + vector_db=vector_db, + ) + + elif attempt == "proto_dict": + # Last resort: pass vector_db as a plain dict understood by gapic + from google.cloud.aiplatform_v1beta1.types import ( + RagCorpus, RagVectorDbConfig, RagManagedDb, + RagEmbeddingModelConfig + ) + from google.cloud import aiplatform_v1beta1 as aip + + client = aip.VertexRagDataServiceClient( + client_options={"api_endpoint": f"{RAG_REGION}-aiplatform.googleapis.com"} + ) + parent = f"projects/{PROJECT_ID}/locations/{RAG_REGION}" + rag_corpus = RagCorpus( + display_name=DISPLAY_NAME, + rag_embedding_model_config=RagEmbeddingModelConfig( + vertex_prediction_endpoint=RagEmbeddingModelConfig.VertexPredictionEndpoint( + publisher_model="publishers/google/models/text-embedding-005" + ) + ), + rag_vector_db_config=RagVectorDbConfig( + rag_managed_db=RagManagedDb() + ), + ) + op = client.create_rag_corpus(parent=parent, rag_corpus=rag_corpus) + result = op.result() + # Wrap in a simple namespace so rest of script works + class _C: + name = result.name + corpus = _C() + print(f"✓ RAG corpus created [{attempt}]: {corpus.name}") - created = True break + except Exception as e: - if attempt == "plain": - print(f"ERROR: Could not create corpus: {e}") + if attempt == attempts[-1]: + print(f"ERROR: All attempts failed. Last error: {e}") sys.exit(1) - print(f" [{attempt}] failed: {e} — retrying with plain...") + print(f" [{attempt}] failed: {e} — trying next method...") # Import seed documents (non-fatal) gcs_uri = f"gs://{CORPUS_BUCKET}/seed/"