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,42 +1,48 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
""" """
agent.py OSVauco root agent using ADK 2.0. agent.py OSVauco root agent using ADK 1.x.
Requires: google-adk >= 1.29, google-cloud-aiplatform >= 1.111.0 Requires: google-adk >= 1.0.0,<2.0.0
google-cloud-aiplatform >= 1.112.0
""" """
import os import os
import logging
from google.adk.agents import Agent 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" logger = logging.getLogger(__name__)
LOCATION = "us-central1"
# --- Secret Manager integration (ADK >= 1.29) --- PROJECT_ID = os.environ.get("GOOGLE_CLOUD_PROJECT", "propane-will-491900-m5")
_sm = SecretManagerClient() LOCATION = os.environ.get("GOOGLE_CLOUD_LOCATION", "us-central1")
def _get_secret(name: str) -> str:
return _sm.get_secret(
f"projects/{PROJECT_ID}/secrets/{name}/versions/latest"
)
# --- RAG retrieval tool --- # RAG corpus resource name — injected via Secret Manager as env-var RAG_CORPUS
RAG_CORPUS = os.environ.get("RAG_CORPUS", "") # set via Secret Manager or env RAG_CORPUS = os.environ.get("RAG_CORPUS", "")
rag_tool = None rag_tool = None
if RAG_CORPUS: if RAG_CORPUS:
from google.adk.tools import VertexAiRagRetrieval 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
rag_tool = VertexAiRagRetrieval( rag_tool = VertexAiRagRetrieval(
name="retrieve_knowledge", name="retrieve_knowledge",
description="Retrieve relevant documentation from the knowledge base.", description="Retrieve relevant documentation and context from the OSVauco knowledge base.",
rag_resources=[rag.RagResource(rag_corpus=RAG_CORPUS)], rag_resources=[rag.RagResource(rag_corpus=RAG_CORPUS)],
similarity_top_k=10, similarity_top_k=10,
vector_distance_threshold=0.6, 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
root_agent = Agent( root_agent = Agent(
model="gemini-2.5-flash", # cost-optimized default model="gemini-2.5-flash",
name="oavauco_root", name="osvauco_root",
description="OSVauco enterprise agent for propane-will-491900-m5", description="OSVauco enterprise agent for propane-will-491900-m5",
instruction=( instruction=(
"You are OSVauco, a GCP knowledge and workflow agent. " "You are OSVauco, a GCP knowledge and workflow agent. "