@@ -373,6 +373,7 @@ async def ainit_vectorstore_table(
373373 self ,
374374 table_name : str ,
375375 vector_size : int ,
376+ schema_name : str = "public" ,
376377 content_column : str = "content" ,
377378 embedding_column : str = "embedding" ,
378379 metadata_columns : List [Column ] = [],
@@ -387,6 +388,8 @@ async def ainit_vectorstore_table(
387388 Args:
388389 table_name (str): The Postgres database table name.
389390 vector_size (int): Vector size for the embedding model to be used.
391+ schema_name (str): The schema name to store Postgres database table.
392+ Default: "public".
390393 content_column (str): Name of the column to store document content.
391394 Default: "page_content".
392395 embedding_column (str) : Name of the column to store vector embeddings.
@@ -407,9 +410,9 @@ async def ainit_vectorstore_table(
407410 await self ._aexecute ("CREATE EXTENSION IF NOT EXISTS vector" )
408411
409412 if overwrite_existing :
410- await self ._aexecute (f'DROP TABLE IF EXISTS "{ table_name } "' )
413+ await self ._aexecute (f'DROP TABLE IF EXISTS "{ schema_name } "." { table_name } "' )
411414
412- query = f"""CREATE TABLE "{ table_name } "(
415+ query = f"""CREATE TABLE "{ schema_name } "." { table_name } "(
413416 "{ id_column } " UUID PRIMARY KEY,
414417 "{ content_column } " TEXT NOT NULL,
415418 "{ embedding_column } " vector({ vector_size } ) NOT NULL"""
@@ -426,6 +429,7 @@ def init_vectorstore_table(
426429 self ,
427430 table_name : str ,
428431 vector_size : int ,
432+ schema_name : str = "public" ,
429433 content_column : str = "content" ,
430434 embedding_column : str = "embedding" ,
431435 metadata_columns : List [Column ] = [],
@@ -440,6 +444,8 @@ def init_vectorstore_table(
440444 Args:
441445 table_name (str): The Postgres database table name.
442446 vector_size (int): Vector size for the embedding model to be used.
447+ schema_name (str): The schema name to store Postgres database table.
448+ Default: "public".
443449 content_column (str): Name of the column to store document content.
444450 Default: "page_content".
445451 embedding_column (str) : Name of the column to store vector embeddings.
@@ -458,6 +464,7 @@ def init_vectorstore_table(
458464 self .ainit_vectorstore_table (
459465 table_name ,
460466 vector_size ,
467+ schema_name ,
461468 content_column ,
462469 embedding_column ,
463470 metadata_columns ,
@@ -468,41 +475,51 @@ def init_vectorstore_table(
468475 )
469476 )
470477
471- async def ainit_chat_history_table (self , table_name : str ) -> None :
478+ async def ainit_chat_history_table (
479+ self , table_name : str , schema_name : str = "public"
480+ ) -> None :
472481 """Create a Cloud SQL table to store chat history.
473482
474483 Args:
475484 table_name (str): Table name to store chat history.
485+ schema_name (str): Schema name to store chat history table.
486+ Default: "public".
476487
477488 Returns:
478489 None
479490 """
480- create_table_query = f"""CREATE TABLE IF NOT EXISTS "{ table_name } "(
491+ create_table_query = f"""CREATE TABLE IF NOT EXISTS "{ schema_name } "." { table_name } "(
481492 id SERIAL PRIMARY KEY,
482493 session_id TEXT NOT NULL,
483494 data JSONB NOT NULL,
484495 type TEXT NOT NULL
485496 );"""
486497 await self ._aexecute (create_table_query )
487498
488- def init_chat_history_table (self , table_name : str ) -> None :
499+ def init_chat_history_table (
500+ self , table_name : str , schema_name : str = "public"
501+ ) -> None :
489502 """Create a Cloud SQL table to store chat history.
490503
491504 Args:
492505 table_name (str): Table name to store chat history.
506+ schema_name (str): Schema name to store chat history table.
507+ Default: "public".
493508
494509 Returns:
495510 None
496511 """
497512 return self ._run_as_sync (
498513 self .ainit_chat_history_table (
499514 table_name ,
515+ schema_name ,
500516 )
501517 )
502518
503519 async def ainit_document_table (
504520 self ,
505521 table_name : str ,
522+ schema_name : str = "public" ,
506523 content_column : str = "page_content" ,
507524 metadata_columns : List [Column ] = [],
508525 metadata_json_column : str = "langchain_metadata" ,
@@ -513,6 +530,8 @@ async def ainit_document_table(
513530
514531 Args:
515532 table_name (str): The PgSQL database table name.
533+ schema_name (str): The schema name to store PgSQL database table.
534+ Default: "public".
516535 content_column (str): Name of the column to store document content.
517536 Default: "page_content".
518537 metadata_columns (List[sqlalchemy.Column]): A list of SQLAlchemy Columns
@@ -526,7 +545,7 @@ async def ainit_document_table(
526545 :class:`DuplicateTableError <asyncpg.exceptions.DuplicateTableError>`: if table already exists.
527546 """
528547
529- query = f"""CREATE TABLE "{ table_name } "(
548+ query = f"""CREATE TABLE "{ schema_name } "." { table_name } "(
530549 { content_column } TEXT NOT NULL
531550 """
532551 for column in metadata_columns :
@@ -542,6 +561,7 @@ async def ainit_document_table(
542561 def init_document_table (
543562 self ,
544563 table_name : str ,
564+ schema_name : str = "public" ,
545565 content_column : str = "page_content" ,
546566 metadata_columns : List [Column ] = [],
547567 metadata_json_column : str = "langchain_metadata" ,
@@ -552,6 +572,8 @@ def init_document_table(
552572
553573 Args:
554574 table_name (str): The PgSQL database table name.
575+ schema_name (str): The schema name to store PgSQL database table.
576+ Default: "public".
555577 content_column (str): Name of the column to store document content.
556578 metadata_columns (List[sqlalchemy.Column]): A list of SQLAlchemy Columns
557579 to create for custom metadata. Optional.
@@ -561,6 +583,7 @@ def init_document_table(
561583 return self ._run_as_sync (
562584 self .ainit_document_table (
563585 table_name ,
586+ schema_name ,
564587 content_column ,
565588 metadata_columns ,
566589 metadata_json_column ,
@@ -571,6 +594,7 @@ def init_document_table(
571594 async def _aload_table_schema (
572595 self ,
573596 table_name : str ,
597+ schema_name : str = "public" ,
574598 ) -> Table :
575599 """
576600 Load table schema from existing table in PgSQL database.
@@ -580,11 +604,15 @@ async def _aload_table_schema(
580604 metadata = MetaData ()
581605 async with self ._engine .connect () as conn :
582606 try :
583- await conn .run_sync (metadata .reflect , only = [table_name ])
607+ await conn .run_sync (
608+ metadata .reflect , schema = schema_name , only = [table_name ]
609+ )
584610 except InvalidRequestError as e :
585- raise ValueError (f"Table, { table_name } , does not exist: " + str (e ))
611+ raise ValueError (
612+ f"Table, '{ schema_name } '.'{ table_name } ', does not exist: " + str (e )
613+ )
586614
587- table = Table (table_name , metadata )
615+ table = Table (table_name , metadata , schema = schema_name )
588616 # Extract the schema information
589617 schema = []
590618 for column in table .columns :
@@ -597,4 +625,4 @@ async def _aload_table_schema(
597625 }
598626 )
599627
600- return metadata .tables [table_name ]
628+ return metadata .tables [f" { schema_name } . { table_name } " ]
0 commit comments