add db connector

This commit is contained in:
Sarah Wooders
2023-10-26 12:02:29 -07:00
parent 942666d3b5
commit f484436c43
2 changed files with 37 additions and 1 deletions

View File

@@ -9,7 +9,6 @@ memgpt load <data-connector-type> --name <dataset-name> [ADDITIONAL ARGS]
"""
from llama_index import download_loader
from llama_index.embeddings import OpenAIEmbedding
from typing import List
import os
import typer
@@ -64,3 +63,33 @@ def load_webpage(
# save connector information into .memgpt metadata file
save_index(index, name)
@app.command("database")
def load_database(
name: str = typer.Option(help="Name of dataset to load."),
scheme: str = typer.Option(help="Database scheme."),
host: str = typer.Option(help="Database host."),
port: int = typer.Option(help="Database port."),
user: str = typer.Option(help="Database user."),
password: str = typer.Option(help="Database password."),
dbname: str = typer.Option(help="Database name."),
query: str = typer.Option(None, help="Database query."),
):
from llama_index.readers.database import DatabaseReader
db = DatabaseReader(
scheme="postgresql", # Database Scheme
host="localhost", # Database Host
port="5432", # Database Port
user="postgres", # Database User
password="FakeExamplePassword", # Database Password
dbname="postgres", # Database Name
)
# load data
docs = db.load_data(query=query)
index = index_docs(docs)
save_index(index, name)

View File

@@ -415,6 +415,13 @@ def save_index(index, name):
# TODO: load directory from config
# TODO: save to vectordb/local depending on config
dir = f"{MEMGPT_DIR}/archival/{name}"
# check if directory exists
if os.path.exists(dir):
confirm = typer.confirm(typer.style(f"Index with name {name} already exists -- overwrite?", fg="red"), default=False)
if not confirm:
typer.secho("Aborting.", fg="red")
exit()
# create directory, even if it already exists
os.makedirs(dir, exist_ok=True)
index.storage_context.persist(dir)