MarkeD
05/25/2023, 6:19 PMgaryaustin
05/25/2023, 6:35 PMMarkeD
05/25/2023, 8:30 PM`
-- Enable the pgvector extension to work with embedding vectors
--create extension vector;
-- Create a table to store your documents
create table fnd (
id bigserial primary key,
content text, -- corresponds to Document.pageContent
metadata jsonb, -- corresponds to Document.metadata
embedding vector(1536) -- 1536 works for OpenAI embeddings, change if needed
);
garyaustin
05/25/2023, 8:33 PMMarkeD
05/26/2023, 10:57 AM`
import psycopg2
from psycopg2.errors import DuplicateObject
import logging
import os
def execute_supabase_from_file(filepath, params, return_rows=False):
# Get the directory of this Python script
dir_path = os.path.dirname(os.path.realpath(__file__))
# Build the full filepath by joining the directory with the filename
filepath = os.path.join(dir_path, filepath)
rows = []
# read the SQL file
with open(filepath, 'r') as file:
sql = file.read()
# substitute placeholders in the SQL
sql = sql.format(**params)
connection_string = os.getenv('DB_CONNECTION_STRING', None)
if connection_string is None:
raise ValueError("No connection string")
try:
connection = psycopg2.connect(connection_string)
cursor = connection.cursor()
# execute the SQL - raise the error if already found
cursor.execute(sql)
# commit the transaction to save changes to the database
connection.commit()
if return_rows:
rows = cursor.fetchall()
logging.info(f"Successfully executed SQL script from {filepath}")
except (psycopg2.errors.DuplicateObject,
psycopg2.errors.DuplicateTable,
psycopg2.errors.DuplicateFunction) as e:
logging.info(str(e))
print(str(e))
except (Exception, psycopg2.Error) as error:
logging.error("Error while connecting to PostgreSQL", exc_info=True)
finally:
if (connection):
cursor.close()
connection.close()
logging.info("PostgreSQL connection is closed")
if rows:
return rows
return True
`