Initializes the SpannerVectorStore with validated settings and clients. This constructor sets up the connection to a specific Spanner database and configures the necessary clients for vector operations. Args: settings (SpannerToolSettings): The settings for the tool. creden
(
self,
settings: SpannerToolSettings,
credentials: Credentials | None = None,
spanner_client: spanner.Client | None = None,
genai_client: Client | None = None,
)
| 191 | SPANNER_VECTOR_STORE_USER_AGENT = "adk-spanner-vector-store" |
| 192 | |
| 193 | def __init__( |
| 194 | self, |
| 195 | settings: SpannerToolSettings, |
| 196 | credentials: Credentials | None = None, |
| 197 | spanner_client: spanner.Client | None = None, |
| 198 | genai_client: Client | None = None, |
| 199 | ): |
| 200 | """Initializes the SpannerVectorStore with validated settings and clients. |
| 201 | |
| 202 | This constructor sets up the connection to a specific Spanner database and |
| 203 | configures the necessary clients for vector operations. |
| 204 | |
| 205 | Args: |
| 206 | settings (SpannerToolSettings): The settings for the tool. |
| 207 | credentials (Credentials | None): Credentials for Spanner operations. This |
| 208 | is used to initialize a new Spanner client only if `spanner_client` |
| 209 | is not explicitly provided. |
| 210 | spanner_client (spanner.Client | None): An pre-configured `spanner.Client` |
| 211 | instance. If not provided, a new client will be created. |
| 212 | genai_client (Client | None): Google GenAI client used for |
| 213 | generating vector embeddings. |
| 214 | """ |
| 215 | |
| 216 | if not settings.vector_store_settings: |
| 217 | raise ValueError("Spanner vector store settings are not set.") |
| 218 | |
| 219 | self._settings = settings |
| 220 | |
| 221 | if not spanner_client: |
| 222 | self._spanner_client = client.get_spanner_client( |
| 223 | project=self._vector_store_settings.project_id, |
| 224 | credentials=credentials, |
| 225 | ) |
| 226 | else: |
| 227 | self._spanner_client = spanner_client |
| 228 | client_user_agent = self._spanner_client._client_info.user_agent |
| 229 | if not client_user_agent: |
| 230 | self._spanner_client._client_info.user_agent = client.USER_AGENT |
| 231 | elif client.USER_AGENT not in client_user_agent: |
| 232 | self._spanner_client._client_info.user_agent = " ".join( |
| 233 | [client_user_agent, client.USER_AGENT] |
| 234 | ) |
| 235 | self._spanner_client._client_info.user_agent = " ".join([ |
| 236 | self._spanner_client._client_info.user_agent, |
| 237 | self.SPANNER_VECTOR_STORE_USER_AGENT, |
| 238 | ]) |
| 239 | |
| 240 | instance = self._spanner_client.instance( |
| 241 | self._vector_store_settings.instance_id |
| 242 | ) |
| 243 | if not instance.exists(): |
| 244 | raise ValueError( |
| 245 | "Instance id {} doesn't exist.".format( |
| 246 | self._vector_store_settings.instance_id |
| 247 | ) |
| 248 | ) |
| 249 | self._database = instance.database( |
| 250 | self._vector_store_settings.database_id, |