fix: agent.py — bruk riktig import-path for VertexAiRagRetrieval (ADK 1.x)

This commit is contained in:
chrischristiansen-glitch 2026-05-24 15:12:08 +02:00
parent 250c150d73
commit 0248081b83

View File

@ -1,48 +1,54 @@
#!/usr/bin/env python3
"""
agent.py OSVauco root agent using ADK 2.0.
Requires: google-adk >= 1.29, google-cloud-aiplatform >= 1.111.0
agent.py OSVauco root agent using ADK 1.x.
Requires: google-adk >= 1.0.0,<2.0.0
google-cloud-aiplatform >= 1.112.0
"""
import os
import logging
from google.adk.agents import Agent
from google.adk.integrations.secret_manager.secret_client import SecretManagerClient
from vertexai.preview import rag
PROJECT_ID = "propane-will-491900-m5"
LOCATION = "us-central1"
logger = logging.getLogger(__name__)
# --- Secret Manager integration (ADK >= 1.29) ---
_sm = SecretManagerClient()
def _get_secret(name: str) -> str:
return _sm.get_secret(
f"projects/{PROJECT_ID}/secrets/{name}/versions/latest"
)
PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
LOCATION = os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1")
# --- RAG retrieval tool ---
RAG_CORPUS = os.environ.get("RAG_CORPUS", "") # set via Secret Manager or env
# RAG corpus resource name — injected via Secret Manager as env-var RAG_CORPUS
RAG_CORPUS = os.environ.get("RAG_CORPUS", "")
rag_tool = None
if RAG_CORPUS:
from google.adk.tools import VertexAiRagRetrieval
rag_tool = VertexAiRagRetrieval(
name="retrieve_knowledge",
description="Retrieve relevant documentation from the knowledge base.",
rag_resources=[rag.RagResource(rag_corpus=RAG_CORPUS)],
similarity_top_k=10,
vector_distance_threshold=0.6,
)
try:
# ADK 1.x: VertexAiRagRetrieval lives in google.adk.tools.retrieval
from google.adk.tools.retrieval.vertex_ai_rag_retrieval import VertexAiRagRetrieval
from vertexai.preview import rag
# --- Root agent ---
rag_tool = VertexAiRagRetrieval(
name="retrieve_knowledge",
description="Retrieve relevant documentation and context from the OSVauco knowledge base.",
rag_resources=[rag.RagResource(rag_corpus=RAG_CORPUS)],
similarity_top_k=10,
vector_distance_threshold=0.6,
)
logger.info(f"RAG tool initialised with corpus: {RAG_CORPUS}")
except ImportError as e:
logger.warning(f"VertexAiRagRetrieval not available — running without RAG: {e}")
rag_tool = None
else:
logger.warning("RAG_CORPUS env var not set — running without RAG retrieval")
# Root agent
root_agent = Agent(
model="gemini-2.5-flash", # cost-optimized default
name="oavauco_root",
model="gemini-2.5-flash",
name="osvauco_root",
description="OSVauco enterprise agent for propane-will-491900-m5",
instruction=(
"You are OSVauco, a GCP knowledge and workflow agent. "
"Use the retrieve_knowledge tool to answer questions from the knowledge base. "
"Always prefer grounded, documented answers over speculation."
"Always respond in Norwegian (Bokmål) regardless of the language used in the query."
"Always prefer grounded, documented answers over speculation. "
"Always respond in Norwegian (Bokmål) regardless of the language used in the query."
),
tools=[rag_tool] if rag_tool else [],
)