@@ -90,31 +90,52 @@ def _parse_source_display_name(
9090
9191
9292class VertexAiRagMemoryService (BaseMemoryService ):
93- """A memory service that uses Vertex AI RAG for storage and retrieval."""
93+ """A memory service that uses Agent Platform RAG for storage and retrieval."""
9494
9595 def __init__ (
9696 self ,
9797 rag_corpus : Optional [str ] = None ,
9898 similarity_top_k : Optional [int ] = None ,
9999 vector_distance_threshold : float = 10 ,
100+ project : Optional [str ] = None ,
101+ location : Optional [str ] = None ,
100102 ):
101103 """Initializes a VertexAiRagMemoryService.
102104
103105 Args:
104- rag_corpus: The name of the Vertex AI RAG corpus to use. Format:
106+ rag_corpus: The name of the Agent Platform RAG corpus to use. Format:
105107 ``projects/{project}/locations/{location}/ragCorpora/{rag_corpus_id}``
106108 or ``{rag_corpus_id}``
107109 similarity_top_k: The number of contexts to retrieve.
108110 vector_distance_threshold: Only returns contexts with vector distance
109111 smaller than the threshold.
112+ project: The project to use for the Agent Platform RAG corpus. If not
113+ set, the value of the GOOGLE_CLOUD_PROJECT environment variable is
114+ used.
115+ location: The location to use for the Agent Platform RAG corpus. If not
116+ set, the value of the GOOGLE_CLOUD_LOCATION environment variable is
117+ used.
110118 """
111119 try :
112- import vertexai # noqa: F401
120+ import agentplatform # noqa: F401
113121 except ImportError as e :
114122 from ..utils ._dependency import missing_extra
115123
116124 raise missing_extra ("google-cloud-aiplatform" , "gcp" ) from e
117125
126+ self ._project = project or os .environ .get ("GOOGLE_CLOUD_PROJECT" )
127+ self ._location = location or os .environ .get ("GOOGLE_CLOUD_LOCATION" )
128+
129+ # Fallback: if the fully-qualified corpus name is provided, use it to
130+ # determine the project and location, if they are not already set.
131+ if (not self ._project or not self ._location ) and (
132+ rag_corpus and rag_corpus .startswith ("projects/" )
133+ ):
134+ parts = rag_corpus .split ("/" )
135+ if len (parts ) >= 4 and parts [0 ] == "projects" and parts [2 ] == "locations" :
136+ self ._project = self ._project or parts [1 ]
137+ self ._location = self ._location or parts [3 ]
138+
118139 self ._vertex_rag_store = types .VertexRagStore (
119140 rag_resources = [
120141 types .VertexRagStoreRagResource (rag_corpus = rag_corpus ),
@@ -153,14 +174,16 @@ async def add_session_to_memory(self, session: Session) -> None:
153174 if not self ._vertex_rag_store .rag_resources :
154175 raise ValueError ("Rag resources must be set." )
155176
156- from ..dependencies .vertexai import rag
177+ import agentplatform
178+
179+ client = agentplatform .Client (
180+ project = self ._project , location = self ._location
181+ )
157182
158183 for rag_resource in self ._vertex_rag_store .rag_resources :
159- rag .upload_file (
184+ client . rag .upload_file (
160185 corpus_name = rag_resource .rag_corpus ,
161186 path = temp_file_path ,
162- # this is the temp workaround as upload file does not support
163- # adding metadata, thus use display_name to store the session info.
164187 display_name = _build_source_display_name (
165188 session .app_name , session .user_id , session .id
166189 ),
@@ -173,17 +196,19 @@ async def search_memory(
173196 self , * , app_name : str , user_id : str , query : str
174197 ) -> SearchMemoryResponse :
175198 """Searches for sessions that match the query using rag.retrieval_query."""
176- from ..dependencies .vertexai import rag
199+ import agentplatform
200+ from agentplatform import types as agentplatform_types
201+
177202 from ..events .event import Event
178203
179- response = rag .retrieval_query (
180- text = query ,
181- rag_resources = self ._vertex_rag_store .rag_resources ,
182- rag_corpora = self ._vertex_rag_store .rag_corpora ,
183- similarity_top_k = self ._vertex_rag_store .similarity_top_k ,
184- vector_distance_threshold = self ._vertex_rag_store .vector_distance_threshold ,
204+ client = agentplatform .Client (
205+ project = self ._project , location = self ._location
185206 )
186207
208+ response = client .rag .retrieve_contexts (
209+ vertex_rag_store = self ._vertex_rag_store ,
210+ query = agentplatform_types .RagQuery (text = query ),
211+ )
187212 memory_results = []
188213 session_events_map : OrderedDict [str , list [list [Event ]]] = OrderedDict ()
189214 for context in response .contexts .contexts :
0 commit comments