2023-04-12 17:47:51 +00:00
|
|
|
import argparse
|
|
|
|
import logging
|
2023-04-14 19:42:28 +00:00
|
|
|
|
2023-05-01 05:57:16 +00:00
|
|
|
from autogpt.commands.file_operations import ingest_file, list_files
|
2023-04-17 20:41:42 +00:00
|
|
|
from autogpt.config import Config
|
2023-05-25 18:31:11 +00:00
|
|
|
from autogpt.memory.vector import VectorMemory, get_memory
|
2023-04-12 17:47:51 +00:00
|
|
|
|
|
|
|
cfg = Config()
|
|
|
|
|
2023-04-14 08:35:30 +00:00
|
|
|
|
2023-04-12 17:47:51 +00:00
|
|
|
def configure_logging():
|
2023-04-14 19:42:28 +00:00
|
|
|
logging.basicConfig(
|
|
|
|
format="%(asctime)s,%(msecs)d %(name)s %(levelname)s %(message)s",
|
|
|
|
datefmt="%H:%M:%S",
|
|
|
|
level=logging.DEBUG,
|
2023-04-30 04:40:57 +00:00
|
|
|
handlers=[
|
2023-05-01 05:57:16 +00:00
|
|
|
logging.FileHandler(filename="log-ingestion.txt", mode="a"),
|
2023-04-30 04:40:57 +00:00
|
|
|
logging.StreamHandler(),
|
|
|
|
],
|
2023-04-14 19:42:28 +00:00
|
|
|
)
|
|
|
|
return logging.getLogger("AutoGPT-Ingestion")
|
2023-04-12 17:47:51 +00:00
|
|
|
|
|
|
|
|
2023-05-25 18:31:11 +00:00
|
|
|
def ingest_directory(directory: str, memory: VectorMemory, args):
|
2023-04-12 18:19:36 +00:00
|
|
|
"""
|
|
|
|
Ingest all files in a directory by calling the ingest_file function for each file.
|
|
|
|
|
|
|
|
:param directory: The directory containing the files to ingest
|
|
|
|
:param memory: An object with an add() method to store the chunks in memory
|
|
|
|
"""
|
2023-05-25 18:31:11 +00:00
|
|
|
logger = logging.getLogger("AutoGPT-Ingestion")
|
2023-04-12 18:19:36 +00:00
|
|
|
try:
|
2023-05-01 05:57:16 +00:00
|
|
|
files = list_files(directory)
|
2023-04-12 18:19:36 +00:00
|
|
|
for file in files:
|
|
|
|
ingest_file(file, memory, args.max_length, args.overlap)
|
|
|
|
except Exception as e:
|
2023-04-30 04:40:57 +00:00
|
|
|
logger.error(f"Error while ingesting directory '{directory}': {str(e)}")
|
2023-04-12 18:19:36 +00:00
|
|
|
|
|
|
|
|
2023-04-15 00:04:48 +00:00
|
|
|
def main() -> None:
|
2023-04-12 17:47:51 +00:00
|
|
|
logger = configure_logging()
|
|
|
|
|
2023-04-14 19:42:28 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
2023-04-15 00:04:48 +00:00
|
|
|
description="Ingest a file or a directory with multiple files into memory. "
|
|
|
|
"Make sure to set your .env before running this script."
|
2023-04-14 19:42:28 +00:00
|
|
|
)
|
2023-04-12 17:47:51 +00:00
|
|
|
group = parser.add_mutually_exclusive_group(required=True)
|
|
|
|
group.add_argument("--file", type=str, help="The file to ingest.")
|
2023-04-14 19:42:28 +00:00
|
|
|
group.add_argument(
|
|
|
|
"--dir", type=str, help="The directory containing the files to ingest."
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--init",
|
|
|
|
action="store_true",
|
|
|
|
help="Init the memory and wipe its content (default: False)",
|
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--overlap",
|
|
|
|
type=int,
|
|
|
|
help="The overlap size between chunks when ingesting files (default: 200)",
|
|
|
|
default=200,
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--max_length",
|
|
|
|
type=int,
|
|
|
|
help="The max_length of each chunk when ingesting files (default: 4000)",
|
|
|
|
default=4000,
|
|
|
|
)
|
2023-04-12 17:47:51 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Initialize memory
|
|
|
|
memory = get_memory(cfg, init=args.init)
|
2023-04-30 04:40:57 +00:00
|
|
|
logger.debug("Using memory of type: " + memory.__class__.__name__)
|
2023-04-12 17:47:51 +00:00
|
|
|
|
|
|
|
if args.file:
|
|
|
|
try:
|
2023-04-12 18:19:36 +00:00
|
|
|
ingest_file(args.file, memory, args.max_length, args.overlap)
|
2023-04-30 04:40:57 +00:00
|
|
|
logger.info(f"File '{args.file}' ingested successfully.")
|
2023-04-12 17:47:51 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error while ingesting file '{args.file}': {str(e)}")
|
|
|
|
elif args.dir:
|
|
|
|
try:
|
2023-04-12 18:19:36 +00:00
|
|
|
ingest_directory(args.dir, memory, args)
|
2023-04-30 04:40:57 +00:00
|
|
|
logger.info(f"Directory '{args.dir}' ingested successfully.")
|
2023-04-12 17:47:51 +00:00
|
|
|
except Exception as e:
|
|
|
|
logger.error(f"Error while ingesting directory '{args.dir}': {str(e)}")
|
|
|
|
else:
|
2023-04-30 04:40:57 +00:00
|
|
|
logger.warn(
|
2023-04-15 12:56:23 +00:00
|
|
|
"Please provide either a file path (--file) or a directory name (--dir)"
|
|
|
|
" inside the auto_gpt_workspace directory as input."
|
2023-04-14 19:42:28 +00:00
|
|
|
)
|
2023-04-12 17:47:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|