fix: agent.py — bruk riktig import-path for VertexAiRagRetrieval (ADK 1.x)
This commit is contained in:
parent
250c150d73
commit
0248081b83
|
|
@ -1,48 +1,54 @@
|
||||||
#!/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:
|
||||||
rag_tool = VertexAiRagRetrieval(
|
# ADK 1.x: VertexAiRagRetrieval lives in google.adk.tools.retrieval
|
||||||
name="retrieve_knowledge",
|
from google.adk.tools.retrieval.vertex_ai_rag_retrieval import VertexAiRagRetrieval
|
||||||
description="Retrieve relevant documentation from the knowledge base.",
|
from vertexai.preview import rag
|
||||||
rag_resources=[rag.RagResource(rag_corpus=RAG_CORPUS)],
|
|
||||||
similarity_top_k=10,
|
|
||||||
vector_distance_threshold=0.6,
|
|
||||||
)
|
|
||||||
|
|
||||||
# --- 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(
|
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. "
|
||||||
"Use the retrieve_knowledge tool to answer questions from the knowledge base. "
|
"Use the retrieve_knowledge tool to answer questions from the knowledge base. "
|
||||||
"Always prefer grounded, documented answers over speculation."
|
"Always prefer grounded, documented answers over speculation. "
|
||||||
"Always respond in Norwegian (Bokmål) regardless of the language used in the query."
|
"Always respond in Norwegian (Bokmål) regardless of the language used in the query."
|
||||||
),
|
),
|
||||||
tools=[rag_tool] if rag_tool else [],
|
tools=[rag_tool] if rag_tool else [],
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user