fix: 07-rag-setup.sh — correct Serverless syntax, try RagManagedDb then plain fallback
This commit is contained in:
parent
8b2f4023c2
commit
2f5bff562d
|
|
@ -1,6 +1,6 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
# 07-rag-setup.sh — Create Vertex AI RAG Engine corpus in Serverless mode
|
# 07-rag-setup.sh — Create Vertex AI RAG Engine corpus in Serverless mode
|
||||||
# Serverless mode does NOT require allowlist (unlike Spanner mode)
|
# Serverless = RagManagedDb (no Spanner, no allowlist needed)
|
||||||
# Idempotent — safe to run multiple times
|
# Idempotent — safe to run multiple times
|
||||||
# Source .env before running: source .env
|
# Source .env before running: source .env
|
||||||
|
|
||||||
|
|
@ -25,7 +25,7 @@ gcloud services enable aiplatform.googleapis.com \
|
||||||
--project="${PROJECT_ID}" --quiet
|
--project="${PROJECT_ID}" --quiet
|
||||||
echo "✓ APIs enabled"
|
echo "✓ APIs enabled"
|
||||||
|
|
||||||
# ── GCS bucket for corpus source documents (idempotent) ──────────────────
|
# ── GCS bucket (idempotent) ──────────────────────────────────────────────
|
||||||
CORPUS_BUCKET="${PROJECT_ID}-agent-corpus"
|
CORPUS_BUCKET="${PROJECT_ID}-agent-corpus"
|
||||||
if ! gsutil ls -b "gs://${CORPUS_BUCKET}" &>/dev/null; then
|
if ! gsutil ls -b "gs://${CORPUS_BUCKET}" &>/dev/null; then
|
||||||
gsutil mb -l "${REGION}" -b on "gs://${CORPUS_BUCKET}"
|
gsutil mb -l "${REGION}" -b on "gs://${CORPUS_BUCKET}"
|
||||||
|
|
@ -34,17 +34,17 @@ else
|
||||||
echo "✓ GCS corpus bucket exists: gs://${CORPUS_BUCKET}"
|
echo "✓ GCS corpus bucket exists: gs://${CORPUS_BUCKET}"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ── Upload seed documents if present ─────────────────────────────────────
|
# ── Upload seed docs ──────────────────────────────────────────────────────────
|
||||||
SEED_DIR="$(dirname "$0")/../docs/corpus-seed"
|
SEED_DIR="$(dirname "$0")/../docs/corpus-seed"
|
||||||
if [[ -d "${SEED_DIR}" ]] && ls "${SEED_DIR}"/*.md &>/dev/null; then
|
if [[ -d "${SEED_DIR}" ]] && ls "${SEED_DIR}"/*.md &>/dev/null; then
|
||||||
gsutil -m cp "${SEED_DIR}"/*.md "gs://${CORPUS_BUCKET}/seed/" 2>/dev/null || true
|
gsutil -m cp "${SEED_DIR}"/*.md "gs://${CORPUS_BUCKET}/seed/" 2>/dev/null || true
|
||||||
echo "✓ Seed documents uploaded to gs://${CORPUS_BUCKET}/seed/"
|
echo "✓ Seed documents uploaded to gs://${CORPUS_BUCKET}/seed/"
|
||||||
else
|
else
|
||||||
echo " No seed documents found in docs/corpus-seed/ — skipping upload"
|
echo " No seed documents in docs/corpus-seed/ — skipping"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# ── Create corpus in Serverless mode ─────────────────────────────────────
|
# ── Python: create corpus ───────────────────────────────────────────────────────
|
||||||
python3 - << PYEOF
|
python3 - << 'PYEOF'
|
||||||
import os, sys
|
import os, sys
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
@ -52,7 +52,6 @@ try:
|
||||||
from vertexai.preview import rag
|
from vertexai.preview import rag
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print("ERROR: google-cloud-aiplatform not installed.")
|
print("ERROR: google-cloud-aiplatform not installed.")
|
||||||
print("Run: pip install google-cloud-aiplatform>=1.87.0")
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
PROJECT_ID = os.environ["PROJECT_ID"]
|
PROJECT_ID = os.environ["PROJECT_ID"]
|
||||||
|
|
@ -62,7 +61,7 @@ CORPUS_BUCKET = f"{PROJECT_ID}-agent-corpus"
|
||||||
|
|
||||||
vertexai.init(project=PROJECT_ID, location=RAG_REGION)
|
vertexai.init(project=PROJECT_ID, location=RAG_REGION)
|
||||||
|
|
||||||
# Check for existing corpus
|
# Check if corpus already exists
|
||||||
corpus = None
|
corpus = None
|
||||||
try:
|
try:
|
||||||
for c in rag.list_corpora():
|
for c in rag.list_corpora():
|
||||||
|
|
@ -74,11 +73,18 @@ except Exception as e:
|
||||||
print(f"WARNING: Could not list corpora: {e}")
|
print(f"WARNING: Could not list corpora: {e}")
|
||||||
|
|
||||||
if corpus is None:
|
if corpus is None:
|
||||||
print(f" Creating corpus in Serverless mode in {RAG_REGION}...")
|
print(f" Creating corpus '{DISPLAY_NAME}' in {RAG_REGION}...")
|
||||||
embedding_config = rag.EmbeddingModelConfig(
|
embedding_config = rag.EmbeddingModelConfig(
|
||||||
publisher_model="publishers/google/models/text-embedding-005"
|
publisher_model="publishers/google/models/text-embedding-005"
|
||||||
)
|
)
|
||||||
# Serverless mode: RagVectorDbConfig with rag_managed_db (no Spanner)
|
|
||||||
|
# Try Serverless mode (RagManagedDb) first, fall back to plain create
|
||||||
|
created = False
|
||||||
|
for attempt in ["serverless", "plain"]:
|
||||||
|
try:
|
||||||
|
if attempt == "serverless":
|
||||||
|
# SDK >= 1.87: RagVectorDbConfig with rag_managed_db
|
||||||
|
try:
|
||||||
vector_db = rag.RagVectorDbConfig(
|
vector_db = rag.RagVectorDbConfig(
|
||||||
rag_managed_db=rag.RagManagedDb()
|
rag_managed_db=rag.RagManagedDb()
|
||||||
)
|
)
|
||||||
|
|
@ -87,7 +93,23 @@ if corpus is None:
|
||||||
embedding_model_config=embedding_config,
|
embedding_model_config=embedding_config,
|
||||||
vector_db=vector_db,
|
vector_db=vector_db,
|
||||||
)
|
)
|
||||||
print(f"✓ RAG corpus created (Serverless): {corpus.name}")
|
except TypeError:
|
||||||
|
# Older SDK: RagManagedDb not a kwarg — skip to plain
|
||||||
|
raise
|
||||||
|
else:
|
||||||
|
# Plain create — lets Google pick default (Serverless on new projects)
|
||||||
|
corpus = rag.create_corpus(
|
||||||
|
display_name=DISPLAY_NAME,
|
||||||
|
embedding_model_config=embedding_config,
|
||||||
|
)
|
||||||
|
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}")
|
||||||
|
sys.exit(1)
|
||||||
|
print(f" [{attempt}] failed: {e} — retrying with plain...")
|
||||||
|
|
||||||
# Import seed documents (non-fatal)
|
# Import seed documents (non-fatal)
|
||||||
gcs_uri = f"gs://{CORPUS_BUCKET}/seed/"
|
gcs_uri = f"gs://{CORPUS_BUCKET}/seed/"
|
||||||
|
|
@ -111,7 +133,7 @@ print(f" Corpus resource name : {corpus.name}")
|
||||||
print(f" Region : {RAG_REGION}")
|
print(f" Region : {RAG_REGION}")
|
||||||
print(f"")
|
print(f"")
|
||||||
print(f" ACTION REQUIRED — add to .env:")
|
print(f" ACTION REQUIRED — add to .env:")
|
||||||
print(f" export RAG_CORPUS_NAME=\"{corpus.name}\"")
|
print(f' export RAG_CORPUS_NAME="{corpus.name}"')
|
||||||
PYEOF
|
PYEOF
|
||||||
|
|
||||||
echo ""
|
echo ""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user