From 6819799ebe435e8e512bf07f1cd1d52f58a1b2f9 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Thu, 6 Apr 2023 22:25:17 -0500 Subject: [PATCH 01/23] Create an abstract MemoryProviderSingleton class. Pass config instead of instantiating a new one where used. --- scripts/commands.py | 4 +-- scripts/config.py | 7 ++++- scripts/main.py | 4 +-- scripts/memory/__init__.py | 0 scripts/memory/base.py | 34 +++++++++++++++++++++++ scripts/{memory.py => memory/pinecone.py} | 18 +++--------- scripts/memory/redismem.py | 0 7 files changed, 48 insertions(+), 19 deletions(-) create mode 100644 scripts/memory/__init__.py create mode 100644 scripts/memory/base.py rename scripts/{memory.py => memory/pinecone.py} (80%) create mode 100644 scripts/memory/redismem.py diff --git a/scripts/commands.py b/scripts/commands.py index fc10d1d052..f00875f063 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -1,6 +1,6 @@ import browse import json -from memory import PineconeMemory +from memory.pinecone import PineconeMemory import datetime import agent_manager as agents import speak @@ -52,7 +52,7 @@ def get_command(response): def execute_command(command_name, arguments): - memory = PineconeMemory() + memory = PineconeMemory(cfg=cfg) try: if command_name == "google": diff --git a/scripts/config.py b/scripts/config.py index fe48d29800..1b716a3eba 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -1,3 +1,4 @@ +import abc import os import openai from dotenv import load_dotenv @@ -5,7 +6,7 @@ from dotenv import load_dotenv load_dotenv() -class Singleton(type): +class Singleton(abc.ABCMeta, type): """ Singleton metaclass for ensuring only one instance of a class. """ @@ -20,6 +21,10 @@ class Singleton(type): return cls._instances[cls] +class AbstractSingleton(abc.ABC, metaclass=Singleton): + pass + + class Config(metaclass=Singleton): """ Configuration class to store the state of bools for different scripts access. diff --git a/scripts/main.py b/scripts/main.py index a79fd553ce..acb63a39be 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -1,7 +1,7 @@ import json import random import commands as cmd -from memory import PineconeMemory +from memory.pinecone import PineconeMemory import data import chat from colorama import Fore, Style @@ -283,7 +283,7 @@ user_input = "Determine which next command to use, and respond using the format # Initialize memory and make sure it is empty. # this is particularly important for indexing and referencing pinecone memory -memory = PineconeMemory() +memory = PineconeMemory(cfg) memory.clear() print('Using memory of type: ' + memory.__class__.__name__) diff --git a/scripts/memory/__init__.py b/scripts/memory/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/scripts/memory/base.py b/scripts/memory/base.py new file mode 100644 index 0000000000..29f5d56be1 --- /dev/null +++ b/scripts/memory/base.py @@ -0,0 +1,34 @@ +import abc +from config import AbstractSingleton +import openai + + +def get_ada_embedding(text): + text = text.replace("\n", " ") + return openai.Embedding.create(input=[text], model="text-embedding-ada-002")["data"][0]["embedding"] + + +def get_text_from_embedding(embedding): + return openai.Embedding.retrieve(embedding, model="text-embedding-ada-002")["data"][0]["text"] + + +class MemoryProviderSingleton(AbstractSingleton): + @abc.abstractmethod + def add(self, data): + pass + + @abc.abstractmethod + def get(self, data): + pass + + @abc.abstractmethod + def clear(self): + pass + + @abc.abstractmethod + def get_relevant(self, data, num_relevant=5): + pass + + @abc.abstractmethod + def get_stats(self): + pass diff --git a/scripts/memory.py b/scripts/memory/pinecone.py similarity index 80% rename from scripts/memory.py rename to scripts/memory/pinecone.py index 0d265a31d8..8e1eaa570f 100644 --- a/scripts/memory.py +++ b/scripts/memory/pinecone.py @@ -1,21 +1,11 @@ -from config import Config, Singleton + import pinecone -import openai -cfg = Config() +from memory.base import MemoryProviderSingleton, get_ada_embedding -def get_ada_embedding(text): - text = text.replace("\n", " ") - return openai.Embedding.create(input=[text], model="text-embedding-ada-002")["data"][0]["embedding"] - - -def get_text_from_embedding(embedding): - return openai.Embedding.retrieve(embedding, model="text-embedding-ada-002")["data"][0]["text"] - - -class PineconeMemory(metaclass=Singleton): - def __init__(self): +class PineconeMemory(MemoryProviderSingleton): + def __init__(self, cfg): pinecone_api_key = cfg.pinecone_api_key pinecone_region = cfg.pinecone_region pinecone.init(api_key=pinecone_api_key, environment=pinecone_region) diff --git a/scripts/memory/redismem.py b/scripts/memory/redismem.py new file mode 100644 index 0000000000..e69de29bb2 From 5a1d9e6d0a1752cf08cf747f9279f8b316f3a8c4 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 00:08:25 -0500 Subject: [PATCH 02/23] Implement redis memory backend. --- README.md | 21 ++++++ requirements.txt | 1 + scripts/commands.py | 6 +- scripts/config.py | 7 +- scripts/main.py | 8 ++- scripts/memory/base.py | 1 + scripts/memory/redismem.py | 135 +++++++++++++++++++++++++++++++++++++ 7 files changed, 175 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a89c5d03b7..921f297eed 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,27 @@ are loaded for the agent at any given time. 2. Choose the `Starter` plan to avoid being charged. 3. Find your API key and region under the default project in the left sidebar. + +## Redis Setup + +Install docker desktop. + +Run: +``` +docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest +``` + +Set the following environment variables: +``` +MEMORY_BACKEND=redis +REDIS_HOST=localhost +REDIS_PORT=6379 +REDIS_PASSWORD= +``` + +Note that this is not intended to be run facing the internet and is not secure, do not expose redis to the internet without a password or at all really. + + ### Setting up environment variables For Windows Users: ``` diff --git a/requirements.txt b/requirements.txt index ce24709858..9cfddad627 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,3 +12,4 @@ docker duckduckgo-search google-api-python-client #(https://developers.google.com/custom-search/v1/overview) pinecone-client==2.2.1 +redis \ No newline at end of file diff --git a/scripts/commands.py b/scripts/commands.py index f00875f063..98be77727b 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -1,6 +1,7 @@ import browse import json from memory.pinecone import PineconeMemory +from memory.redismem import RedisMemory import datetime import agent_manager as agents import speak @@ -52,7 +53,10 @@ def get_command(response): def execute_command(command_name, arguments): - memory = PineconeMemory(cfg=cfg) + if cfg.memory_backend == "pinecone": + memory = PineconeMemory(cfg=cfg) + else: + memory = RedisMemory(cfg=cfg) try: if command_name == "google": diff --git a/scripts/config.py b/scripts/config.py index 1b716a3eba..77498d6c91 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -61,7 +61,12 @@ class Config(metaclass=Singleton): # User agent headers to use when browsing web # Some websites might just completely deny request with an error code if no user agent was found. self.user_agent_header = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} - + self.redis_host = os.getenv("REDIS_HOST") + self.redis_port = os.getenv("REDIS_PORT") + self.redis_password = os.getenv("REDIS_PASSWORD") + # Note that indexes must be created on db 0 in redis, this is not configureable. + + self.memory_backend = os.getenv("MEMORY_BACKEND", 'pinecone') # Initialize the OpenAI API client openai.api_key = self.openai_api_key diff --git a/scripts/main.py b/scripts/main.py index acb63a39be..eecdd7f80e 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -2,6 +2,7 @@ import json import random import commands as cmd from memory.pinecone import PineconeMemory +from memory.redismem import RedisMemory import data import chat from colorama import Fore, Style @@ -283,8 +284,11 @@ user_input = "Determine which next command to use, and respond using the format # Initialize memory and make sure it is empty. # this is particularly important for indexing and referencing pinecone memory -memory = PineconeMemory(cfg) -memory.clear() +if cfg.memory_backend == "pinecone": + memory = PineconeMemory(cfg) + memory.clear() +else: + memory = RedisMemory(cfg) print('Using memory of type: ' + memory.__class__.__name__) diff --git a/scripts/memory/base.py b/scripts/memory/base.py index 29f5d56be1..72349f6be1 100644 --- a/scripts/memory/base.py +++ b/scripts/memory/base.py @@ -1,3 +1,4 @@ +"""Base class for memory providers.""" import abc from config import AbstractSingleton import openai diff --git a/scripts/memory/redismem.py b/scripts/memory/redismem.py index e69de29bb2..162b9269bd 100644 --- a/scripts/memory/redismem.py +++ b/scripts/memory/redismem.py @@ -0,0 +1,135 @@ +"""Redis memory provider.""" +from typing import Any, List, Optional +import redis +from redis.commands.search.field import VectorField, TextField +from redis.commands.search.query import Query +from redis.commands.search.indexDefinition import IndexDefinition, IndexType +import traceback +import numpy as np + +from memory.base import MemoryProviderSingleton, get_ada_embedding + + +SCHEMA = [ + TextField("data"), + VectorField( + "embedding", + "HNSW", + { + "TYPE": "FLOAT32", + "DIM": 1536, + "DISTANCE_METRIC": "COSINE" + } + ), +] + + +class RedisMemory(MemoryProviderSingleton): + def __init__(self, cfg): + """ + Initializes the Redis memory provider. + + Args: + cfg: The config object. + + Returns: None + """ + redis_host = cfg.redis_host + redis_port = cfg.redis_port + redis_password = cfg.redis_password + self.dimension = 1536 + self.redis = redis.Redis( + host=redis_host, + port=redis_port, + password=redis_password, + db=0 # Cannot be changed + ) + self.redis.flushall() + try: + self.redis.ft("gpt").create_index( + fields=SCHEMA, + definition=IndexDefinition( + prefix=["gpt:"], + index_type=IndexType.HASH + ) + ) + except Exception as e: + print("Error creating Redis search index: ", e) + self.vec_num = 0 + + def add(self, data: str) -> str: + """ + Adds a data point to the memory. + + Args: + data: The data to add. + + Returns: Message indicating that the data has been added. + """ + vector = get_ada_embedding(data) + vector = np.array(vector).astype(np.float32).tobytes() + data_dict = { + b"data": data, + "embedding": vector + } + self.redis.hset(f"gpt:{self.vec_num}", mapping=data_dict) + _text = f"Inserting data into memory at index: {self.vec_num}:\n"\ + f"data: {data}" + self.vec_num += 1 + return _text + + def get(self, data: str) -> Optional[List[Any]]: + """ + Gets the data from the memory that is most relevant to the given data. + + Args: + data: The data to compare to. + + Returns: The most relevant data. + """ + return self.get_relevant(data, 1) + + def clear(self) -> str: + """ + Clears the redis server. + + Returns: A message indicating that the memory has been cleared. + """ + self.redis.flushall() + return "Obliviated" + + def get_relevant( + self, + data: str, + num_relevant: int = 5 + ) -> Optional[List[Any]]: + """ + Returns all the data in the memory that is relevant to the given data. + Args: + data: The data to compare to. + num_relevant: The number of relevant data to return. + + Returns: A list of the most relevant data. + """ + query_embedding = get_ada_embedding(data) + base_query = f"*=>[KNN {num_relevant} @embedding $vector AS vector_score]" + query = Query(base_query).return_fields( + "data", + "vector_score" + ).sort_by("vector_score").dialect(2) + query_vector = np.array(query_embedding).astype(np.float32).tobytes() + + try: + results = self.redis.ft("gpt").search( + query, query_params={"vector": query_vector} + ) + except Exception as e: + print("Error calling Redis search: ", e) + return None + return list(results.docs) + + def get_stats(self): + """ + Returns: The stats of the memory index. + """ + return self.redis.ft("mem").info() From cce79695fa43a9abb5aca8e368f7951924c3ae9c Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 00:48:27 -0500 Subject: [PATCH 03/23] Save redis memory state, with the default being to wipe on start still. --- scripts/config.py | 3 ++- scripts/memory/redismem.py | 12 +++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index 77498d6c91..8c582a1572 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -64,8 +64,9 @@ class Config(metaclass=Singleton): self.redis_host = os.getenv("REDIS_HOST") self.redis_port = os.getenv("REDIS_PORT") self.redis_password = os.getenv("REDIS_PASSWORD") + self.wipe_redis_on_start = os.getenv("WIPE_REDIS_ON_START", "True") == 'True' # Note that indexes must be created on db 0 in redis, this is not configureable. - + self.memory_backend = os.getenv("MEMORY_BACKEND", 'pinecone') # Initialize the OpenAI API client openai.api_key = self.openai_api_key diff --git a/scripts/memory/redismem.py b/scripts/memory/redismem.py index 162b9269bd..e7021066fa 100644 --- a/scripts/memory/redismem.py +++ b/scripts/memory/redismem.py @@ -44,7 +44,8 @@ class RedisMemory(MemoryProviderSingleton): password=redis_password, db=0 # Cannot be changed ) - self.redis.flushall() + if cfg.wipe_redis_on_start: + self.redis.flushall() try: self.redis.ft("gpt").create_index( fields=SCHEMA, @@ -55,7 +56,9 @@ class RedisMemory(MemoryProviderSingleton): ) except Exception as e: print("Error creating Redis search index: ", e) - self.vec_num = 0 + existing_vec_num = self.redis.get('vec_num') + self.vec_num = int(existing_vec_num.decode('utf-8')) if\ + existing_vec_num else 0 def add(self, data: str) -> str: """ @@ -72,10 +75,13 @@ class RedisMemory(MemoryProviderSingleton): b"data": data, "embedding": vector } - self.redis.hset(f"gpt:{self.vec_num}", mapping=data_dict) + pipe = self.redis.pipeline() + pipe.hset(f"gpt:{self.vec_num}", mapping=data_dict) _text = f"Inserting data into memory at index: {self.vec_num}:\n"\ f"data: {data}" self.vec_num += 1 + pipe.set('vec_num', self.vec_num) + pipe.execute() return _text def get(self, data: str) -> Optional[List[Any]]: From 43746b1396fe47feae9447a72bbaa15ce2c0960a Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 00:58:57 -0500 Subject: [PATCH 04/23] Update README with WIPE_REDIS_ON_START setting. --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 921f297eed..7d83b4633f 100644 --- a/README.md +++ b/README.md @@ -169,6 +169,13 @@ REDIS_PASSWORD= Note that this is not intended to be run facing the internet and is not secure, do not expose redis to the internet without a password or at all really. +You can optionally set + +``` +WIPE_REDIS_ON_START=False +``` + +To persist memory stored in Redis. ### Setting up environment variables For Windows Users: From f0162037c341e31583d09626da2c853563cc4776 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 15:02:22 -0500 Subject: [PATCH 05/23] Fix README --- README.md | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 7d83b4633f..b7f514e830 100644 --- a/README.md +++ b/README.md @@ -140,16 +140,6 @@ export CUSTOM_SEARCH_ENGINE_ID="YOUR_CUSTOM_SEARCH_ENGINE_ID" ``` -## ๐ŸŒฒ Pinecone API Key Setup - -Pinecone enable a vector based memory so a vast memory can be stored and only relevant memories -are loaded for the agent at any given time. - -1. Go to app.pinecone.io and make an account if you don't already have one. -2. Choose the `Starter` plan to avoid being charged. -3. Find your API key and region under the default project in the left sidebar. - - ## Redis Setup Install docker desktop. @@ -177,6 +167,15 @@ WIPE_REDIS_ON_START=False To persist memory stored in Redis. +## ๐ŸŒฒ Pinecone API Key Setup + +Pinecone enable a vector based memory so a vast memory can be stored and only relevant memories +are loaded for the agent at any given time. + +1. Go to app.pinecone.io and make an account if you don't already have one. +2. Choose the `Starter` plan to avoid being charged. +3. Find your API key and region under the default project in the left sidebar. + ### Setting up environment variables For Windows Users: ``` From 5d13fb2546916f2b5ff360720b07706ab31e6e21 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 15:03:20 -0500 Subject: [PATCH 06/23] Remove unused function. --- scripts/memory/base.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/scripts/memory/base.py b/scripts/memory/base.py index 72349f6be1..d7ab7fcf1f 100644 --- a/scripts/memory/base.py +++ b/scripts/memory/base.py @@ -9,10 +9,6 @@ def get_ada_embedding(text): return openai.Embedding.create(input=[text], model="text-embedding-ada-002")["data"][0]["embedding"] -def get_text_from_embedding(embedding): - return openai.Embedding.retrieve(embedding, model="text-embedding-ada-002")["data"][0]["text"] - - class MemoryProviderSingleton(AbstractSingleton): @abc.abstractmethod def add(self, data): From 14e10c9c4ddc1d0736b4161e96d0c2517c65b12a Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 15:27:48 -0500 Subject: [PATCH 07/23] Add configurable index key for redis. --- scripts/config.py | 1 + scripts/memory/redismem.py | 15 ++++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index 8c582a1572..637c17fdf8 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -65,6 +65,7 @@ class Config(metaclass=Singleton): self.redis_port = os.getenv("REDIS_PORT") self.redis_password = os.getenv("REDIS_PASSWORD") self.wipe_redis_on_start = os.getenv("WIPE_REDIS_ON_START", "True") == 'True' + self.memory_index = os.getenv("MEMORY_INDEX", 'gpt') # Note that indexes must be created on db 0 in redis, this is not configureable. self.memory_backend = os.getenv("MEMORY_BACKEND", 'pinecone') diff --git a/scripts/memory/redismem.py b/scripts/memory/redismem.py index e7021066fa..20be4a4e84 100644 --- a/scripts/memory/redismem.py +++ b/scripts/memory/redismem.py @@ -44,19 +44,20 @@ class RedisMemory(MemoryProviderSingleton): password=redis_password, db=0 # Cannot be changed ) + self.cfg = cfg if cfg.wipe_redis_on_start: self.redis.flushall() try: - self.redis.ft("gpt").create_index( + self.redis.ft(f"{cfg.memory_index}").create_index( fields=SCHEMA, definition=IndexDefinition( - prefix=["gpt:"], + prefix=[f"{cfg.memory_index}:"], index_type=IndexType.HASH ) ) except Exception as e: print("Error creating Redis search index: ", e) - existing_vec_num = self.redis.get('vec_num') + existing_vec_num = self.redis.get(f'{cfg.memory_index}-vec_num') self.vec_num = int(existing_vec_num.decode('utf-8')) if\ existing_vec_num else 0 @@ -76,11 +77,11 @@ class RedisMemory(MemoryProviderSingleton): "embedding": vector } pipe = self.redis.pipeline() - pipe.hset(f"gpt:{self.vec_num}", mapping=data_dict) + pipe.hset(f"{self.cfg.memory_index}:{self.vec_num}", mapping=data_dict) _text = f"Inserting data into memory at index: {self.vec_num}:\n"\ f"data: {data}" self.vec_num += 1 - pipe.set('vec_num', self.vec_num) + pipe.set(f'{self.cfg.memory_index}-vec_num', self.vec_num) pipe.execute() return _text @@ -126,7 +127,7 @@ class RedisMemory(MemoryProviderSingleton): query_vector = np.array(query_embedding).astype(np.float32).tobytes() try: - results = self.redis.ft("gpt").search( + results = self.redis.ft(f"{self.cfg.memory_index}").search( query, query_params={"vector": query_vector} ) except Exception as e: @@ -138,4 +139,4 @@ class RedisMemory(MemoryProviderSingleton): """ Returns: The stats of the memory index. """ - return self.redis.ft("mem").info() + return self.redis.ft(f"{self.cfg.memory_index}").info() From ea6b97050948487cee5ee50a12f7eb2a161e0648 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 15:28:48 -0500 Subject: [PATCH 08/23] Update README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index b7f514e830..5ce001b094 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,12 @@ WIPE_REDIS_ON_START=False To persist memory stored in Redis. +You can specify the memory index for redis using the following: + +```` +MEMORY_INDEX=whatever +```` + ## ๐ŸŒฒ Pinecone API Key Setup Pinecone enable a vector based memory so a vast memory can be stored and only relevant memories From cb14c8d999c32c89215be04d27fe132a149eb047 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 18:13:18 -0500 Subject: [PATCH 09/23] Implement local memory. --- requirements.txt | 3 +- scripts/commands.py | 8 ++- scripts/config.py | 4 +- scripts/main.py | 5 +- scripts/memory/local.py | 111 +++++++++++++++++++++++++++++++++++++ scripts/memory/redismem.py | 1 - 6 files changed, 125 insertions(+), 7 deletions(-) create mode 100644 scripts/memory/local.py diff --git a/requirements.txt b/requirements.txt index 9cfddad627..5bcc74957e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,4 +12,5 @@ docker duckduckgo-search google-api-python-client #(https://developers.google.com/custom-search/v1/overview) pinecone-client==2.2.1 -redis \ No newline at end of file +redis +orjson \ No newline at end of file diff --git a/scripts/commands.py b/scripts/commands.py index 98be77727b..a88ad0ae09 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -1,5 +1,6 @@ import browse import json +from memory.local import LocalCache from memory.pinecone import PineconeMemory from memory.redismem import RedisMemory import datetime @@ -55,11 +56,14 @@ def get_command(response): def execute_command(command_name, arguments): if cfg.memory_backend == "pinecone": memory = PineconeMemory(cfg=cfg) - else: + elif cfg.memory_backend == "redis": memory = RedisMemory(cfg=cfg) + else: + memory = LocalCache(cfg=cfg) + try: if command_name == "google": - + # Check if the Google API key is set and use the official search method # If the API key is not set or has only whitespaces, use the unofficial search method if cfg.google_api_key and (cfg.google_api_key.strip() if cfg.google_api_key else None): diff --git a/scripts/config.py b/scripts/config.py index 637c17fdf8..9afeb1d257 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -65,10 +65,10 @@ class Config(metaclass=Singleton): self.redis_port = os.getenv("REDIS_PORT") self.redis_password = os.getenv("REDIS_PASSWORD") self.wipe_redis_on_start = os.getenv("WIPE_REDIS_ON_START", "True") == 'True' - self.memory_index = os.getenv("MEMORY_INDEX", 'gpt') + self.memory_index = os.getenv("MEMORY_INDEX", 'auto-gpt') # Note that indexes must be created on db 0 in redis, this is not configureable. - self.memory_backend = os.getenv("MEMORY_BACKEND", 'pinecone') + self.memory_backend = os.getenv("MEMORY_BACKEND", 'local') # Initialize the OpenAI API client openai.api_key = self.openai_api_key diff --git a/scripts/main.py b/scripts/main.py index eecdd7f80e..e49f1810d2 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -1,6 +1,7 @@ import json import random import commands as cmd +from memory.local import LocalCache from memory.pinecone import PineconeMemory from memory.redismem import RedisMemory import data @@ -287,8 +288,10 @@ user_input = "Determine which next command to use, and respond using the format if cfg.memory_backend == "pinecone": memory = PineconeMemory(cfg) memory.clear() -else: +elif cfg.memory_backend == "redis": memory = RedisMemory(cfg) +else: + memory = LocalCache(cfg) print('Using memory of type: ' + memory.__class__.__name__) diff --git a/scripts/memory/local.py b/scripts/memory/local.py new file mode 100644 index 0000000000..fb10522426 --- /dev/null +++ b/scripts/memory/local.py @@ -0,0 +1,111 @@ +import dataclasses +import orjson +from typing import Any, List, Optional +import numpy as np +import os +from memory.base import MemoryProviderSingleton, get_ada_embedding + + +EMBED_DIM = 1536 +SAVE_OPTIONS = orjson.OPT_SERIALIZE_NUMPY | orjson.OPT_SERIALIZE_DATACLASS + + +def create_default_embeddings(): + return np.zeros((0, EMBED_DIM)).astype(np.float32) + + +@dataclasses.dataclass +class CacheContent: + texts: List[str] = dataclasses.field(default_factory=list) + embeddings: np.ndarray = dataclasses.field( + default_factory=create_default_embeddings + ) + + +class LocalCache(MemoryProviderSingleton): + + # on load, load our database + def __init__(self, cfg) -> None: + self.filename = f"{cfg.memory_index}.json" + if os.path.exists(self.filename): + with open(self.filename, 'rb') as f: + loaded = orjson.loads(f.read()) + self.data = CacheContent(**loaded) + else: + self.data = CacheContent() + + def add(self, text: str): + """ + Add text to our list of texts, add embedding as row to our + embeddings-matrix + + Args: + text: str + + Returns: None + """ + self.data.texts.append(text) + + embedding = get_ada_embedding(text) + + vector = np.array(embedding).astype(np.float32) + vector = vector[np.newaxis, :] + self.data.embeddings = np.concatenate( + [ + vector, + self.data.embeddings, + ], + axis=0, + ) + + with open(self.filename, 'wb') as f: + out = orjson.dumps( + self.data, + option=SAVE_OPTIONS + ) + f.write(out) + + def clear(self) -> str: + """ + Clears the redis server. + + Returns: A message indicating that the memory has been cleared. + """ + self.data = CacheContent() + return "Obliviated" + + def get(self, data: str) -> Optional[List[Any]]: + """ + Gets the data from the memory that is most relevant to the given data. + + Args: + data: The data to compare to. + + Returns: The most relevant data. + """ + return self.get_relevant(data, 1) + + def get_relevant(self, text: str, k: int) -> List[Any]: + """" + matrix-vector mult to find score-for-each-row-of-matrix + get indices for top-k winning scores + return texts for those indices + Args: + text: str + k: int + + Returns: List[str] + """ + embedding = get_ada_embedding(text) + + scores = np.dot(self.data.embeddings, embedding) + + top_k_indices = np.argsort(scores)[-k:][::-1] + + return [self.data.texts[i] for i in top_k_indices] + + def get_stats(self): + """ + Returns: The stats of the local cache. + """ + return len(self.data.texts), self.data.embeddings.shape diff --git a/scripts/memory/redismem.py b/scripts/memory/redismem.py index 20be4a4e84..296d0cce2c 100644 --- a/scripts/memory/redismem.py +++ b/scripts/memory/redismem.py @@ -4,7 +4,6 @@ import redis from redis.commands.search.field import VectorField, TextField from redis.commands.search.query import Query from redis.commands.search.indexDefinition import IndexDefinition, IndexType -import traceback import numpy as np from memory.base import MemoryProviderSingleton, get_ada_embedding From 503b58b7948fe3a37622919864015607352e76e6 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 18:30:04 -0500 Subject: [PATCH 10/23] Refactor memory into factory. --- scripts/commands.py | 11 ++-------- scripts/main.py | 12 ++--------- scripts/memory/__init__.py | 42 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/scripts/commands.py b/scripts/commands.py index a88ad0ae09..783e6bd295 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -1,8 +1,6 @@ import browse import json -from memory.local import LocalCache -from memory.pinecone import PineconeMemory -from memory.redismem import RedisMemory +from memory import get_memory import datetime import agent_manager as agents import speak @@ -54,12 +52,7 @@ def get_command(response): def execute_command(command_name, arguments): - if cfg.memory_backend == "pinecone": - memory = PineconeMemory(cfg=cfg) - elif cfg.memory_backend == "redis": - memory = RedisMemory(cfg=cfg) - else: - memory = LocalCache(cfg=cfg) + memory = get_memory(cfg) try: if command_name == "google": diff --git a/scripts/main.py b/scripts/main.py index e49f1810d2..11bf0dc1b2 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -1,9 +1,7 @@ import json import random import commands as cmd -from memory.local import LocalCache -from memory.pinecone import PineconeMemory -from memory.redismem import RedisMemory +from memory import get_memory import data import chat from colorama import Fore, Style @@ -285,13 +283,7 @@ user_input = "Determine which next command to use, and respond using the format # Initialize memory and make sure it is empty. # this is particularly important for indexing and referencing pinecone memory -if cfg.memory_backend == "pinecone": - memory = PineconeMemory(cfg) - memory.clear() -elif cfg.memory_backend == "redis": - memory = RedisMemory(cfg) -else: - memory = LocalCache(cfg) +memory = get_memory(cfg, init=True) print('Using memory of type: ' + memory.__class__.__name__) diff --git a/scripts/memory/__init__.py b/scripts/memory/__init__.py index e69de29bb2..dacb05b328 100644 --- a/scripts/memory/__init__.py +++ b/scripts/memory/__init__.py @@ -0,0 +1,42 @@ +from memory.local import LocalCache +try: + from memory.redismem import RedisMemory +except ImportError: + print("Redis not installed. Skipping import.") + RedisMemory = None + +try: + from memory.pinecone import PineconeMemory +except ImportError: + print("Pinecone not installed. Skipping import.") + PineconeMemory = None + + +def get_memory(cfg, init=False): + memory = None + if cfg.memory_backend == "pinecone": + if not PineconeMemory: + print("Error: Pinecone is not installed. Please install pinecone" + " to use Pinecone as a memory backend.") + else: + memory = PineconeMemory(cfg) + if init: + memory.clear() + elif cfg.memory_backend == "redis": + if not RedisMemory: + print("Error: Redis is not installed. Please install redis-py to" + " use Redis as a memory backend.") + else: + memory = RedisMemory(cfg) + + if memory is None: + memory = LocalCache(cfg) + return memory + + +__all__ = [ + "get_memory", + "LocalCache", + "RedisCache", + "PineconeCache", +] From a34c51bf8622cf83a34493718c8be60c0676e603 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Fri, 7 Apr 2023 20:58:00 -0500 Subject: [PATCH 11/23] Update scripts/config.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Jason Kรถlker --- scripts/config.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index 9afeb1d257..1601dcc43f 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -61,9 +61,9 @@ class Config(metaclass=Singleton): # User agent headers to use when browsing web # Some websites might just completely deny request with an error code if no user agent was found. self.user_agent_header = {"User-Agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36"} - self.redis_host = os.getenv("REDIS_HOST") - self.redis_port = os.getenv("REDIS_PORT") - self.redis_password = os.getenv("REDIS_PASSWORD") + self.redis_host = os.getenv("REDIS_HOST", "localhost") + self.redis_port = os.getenv("REDIS_PORT", "6379") + self.redis_password = os.getenv("REDIS_PASSWORD", "") self.wipe_redis_on_start = os.getenv("WIPE_REDIS_ON_START", "True") == 'True' self.memory_index = os.getenv("MEMORY_INDEX", 'auto-gpt') # Note that indexes must be created on db 0 in redis, this is not configureable. From 3f66a6a0a307d015f166e213d56698bfa92cefcc Mon Sep 17 00:00:00 2001 From: Bill Morgan Date: Fri, 7 Apr 2023 15:36:03 -0500 Subject: [PATCH 12/23] fix typo in prompt.txt --- scripts/data/prompt.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/data/prompt.txt b/scripts/data/prompt.txt index 363342c071..77a449de52 100644 --- a/scripts/data/prompt.txt +++ b/scripts/data/prompt.txt @@ -18,7 +18,7 @@ COMMANDS: 12. Append to file: "append_to_file", args: "file": "", "text": "" 13. Delete file: "delete_file", args: "file": "" 14. Search Files: "search_files", args: "directory": "" -15. Evaluate Code: "evaluate_code", args: "code": "" +15. Evaluate Code: "evaluate_code", args: "code": "" 16. Get Improved Code: "improve_code", args: "suggestions": "", "code": "" 17. Write Tests: "write_tests", args: "code": "", "focus": "" 18. Execute Python File: "execute_python_file", args: "file": "" From 7cba76228e0db94aa2b4de6e63096c3f7e65fe62 Mon Sep 17 00:00:00 2001 From: Bill Morgan Date: Sat, 8 Apr 2023 08:22:27 -0500 Subject: [PATCH 13/23] fix command error check --- scripts/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/main.py b/scripts/main.py index 17385bf339..d36f979f67 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -358,7 +358,7 @@ while True: f"COMMAND = {Fore.CYAN}{command_name}{Style.RESET_ALL} ARGUMENTS = {Fore.CYAN}{arguments}{Style.RESET_ALL}") # Execute command - if command_name.lower() == "error": + if command_name.lower().startswith( "error" ): result = f"Command {command_name} threw the following error: " + arguments elif command_name == "human_feedback": result = f"Human feedback: {user_input}" From d1777e39a8668674d40b06f3e3690e68e5daa27d Mon Sep 17 00:00:00 2001 From: Toran Bruce Richards Date: Sun, 9 Apr 2023 02:31:51 +0100 Subject: [PATCH 14/23] Fixes incorrect class names in __all__ Changes "Cache" to "Memory". --- scripts/memory/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/memory/__init__.py b/scripts/memory/__init__.py index dacb05b328..9952307943 100644 --- a/scripts/memory/__init__.py +++ b/scripts/memory/__init__.py @@ -37,6 +37,6 @@ def get_memory(cfg, init=False): __all__ = [ "get_memory", "LocalCache", - "RedisCache", - "PineconeCache", + "RedisMemory", + "PineconeMemory", ] From 47c6117e1886d9b69f95807df1e4ee6a2c76eb64 Mon Sep 17 00:00:00 2001 From: Ryan Peach Date: Sat, 8 Apr 2023 22:59:28 -0400 Subject: [PATCH 15/23] Added time and date to the system message for each context --- scripts/chat.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/scripts/chat.py b/scripts/chat.py index 8da074c6bf..0b110bbae9 100644 --- a/scripts/chat.py +++ b/scripts/chat.py @@ -26,7 +26,10 @@ def create_chat_message(role, content): def generate_context(prompt, relevant_memory, full_message_history, model): current_context = [ create_chat_message( - "system", prompt), create_chat_message( + "system", prompt), + create_chat_message( + "system", f"The current time and date is {time.strftime('%c')}"), + create_chat_message( "system", f"Permanent memory: {relevant_memory}")] # Add messages from the full message history until we reach the token limit @@ -95,7 +98,7 @@ def chat_with_ai( # Count the currently used tokens current_tokens_used += tokens_to_add - + # Move to the next most recent message in the full message history next_message_to_add_index -= 1 From 2db7f0815eed6e96b94423c96a40b95fc47750d4 Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Sat, 8 Apr 2023 22:25:59 -0500 Subject: [PATCH 16/23] Update main.py Remove pinecone config requirement --- scripts/main.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index e16fb9d14d..10f9d0dcaa 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -281,8 +281,6 @@ next_action_count = 0 # Make a constant: user_input = "Determine which next command to use, and respond using the format specified above:" -# raise an exception if pinecone_api_key or region is not provided -if not cfg.pinecone_api_key or not cfg.pinecone_region: raise Exception("Please provide pinecone_api_key and pinecone_region") # Initialize memory and make sure it is empty. # this is particularly important for indexing and referencing pinecone memory memory = get_memory(cfg, init=True) From 9e139fb314b7b5c9b538a85d204ff08ce59e10bd Mon Sep 17 00:00:00 2001 From: Toran Bruce Richards Date: Sun, 9 Apr 2023 05:22:03 +0100 Subject: [PATCH 17/23] Wipe local memory on load --- scripts/memory/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/memory/__init__.py b/scripts/memory/__init__.py index 9952307943..a441a46aa9 100644 --- a/scripts/memory/__init__.py +++ b/scripts/memory/__init__.py @@ -31,6 +31,8 @@ def get_memory(cfg, init=False): if memory is None: memory = LocalCache(cfg) + if init: + memory.clear() return memory From a861dec6764254b581d23d4573f1da8307bf533a Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Sat, 8 Apr 2023 23:33:18 -0500 Subject: [PATCH 18/23] Memory fixes. --- scripts/memory/local.py | 3 +++ scripts/memory/redismem.py | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/memory/local.py b/scripts/memory/local.py index fb10522426..8dc90021ff 100644 --- a/scripts/memory/local.py +++ b/scripts/memory/local.py @@ -44,6 +44,8 @@ class LocalCache(MemoryProviderSingleton): Returns: None """ + if 'Command Error:' in text: + return "" self.data.texts.append(text) embedding = get_ada_embedding(text) @@ -64,6 +66,7 @@ class LocalCache(MemoryProviderSingleton): option=SAVE_OPTIONS ) f.write(out) + return text def clear(self) -> str: """ diff --git a/scripts/memory/redismem.py b/scripts/memory/redismem.py index 296d0cce2c..2082fe5887 100644 --- a/scripts/memory/redismem.py +++ b/scripts/memory/redismem.py @@ -69,6 +69,8 @@ class RedisMemory(MemoryProviderSingleton): Returns: Message indicating that the data has been added. """ + if 'Command Error:' in data: + return "" vector = get_ada_embedding(data) vector = np.array(vector).astype(np.float32).tobytes() data_dict = { @@ -132,7 +134,7 @@ class RedisMemory(MemoryProviderSingleton): except Exception as e: print("Error calling Redis search: ", e) return None - return list(results.docs) + return [result.data for result in results.docs] def get_stats(self): """ From d8410d9ca308d2430a7928b61db002046e293b17 Mon Sep 17 00:00:00 2001 From: Toran Bruce Richards Date: Sun, 9 Apr 2023 06:16:42 +0100 Subject: [PATCH 19/23] Makes it clearer to the AI exactly what memories are. --- scripts/chat.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/chat.py b/scripts/chat.py index 8da074c6bf..5ab52f993e 100644 --- a/scripts/chat.py +++ b/scripts/chat.py @@ -27,7 +27,7 @@ def generate_context(prompt, relevant_memory, full_message_history, model): current_context = [ create_chat_message( "system", prompt), create_chat_message( - "system", f"Permanent memory: {relevant_memory}")] + "system", f"This reminds you of these events from your past:\n{relevant_memory}\n\n")] # Add messages from the full message history until we reach the token limit next_message_to_add_index = len(full_message_history) - 1 From a2fe619c7b7352b9aab35bc10b12f94946712282 Mon Sep 17 00:00:00 2001 From: Toran Bruce Richards Date: Sun, 9 Apr 2023 06:44:10 +0100 Subject: [PATCH 20/23] Improves response to AI that sends wrong output. --- scripts/commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/commands.py b/scripts/commands.py index 1f255751c0..ba5383957a 100644 --- a/scripts/commands.py +++ b/scripts/commands.py @@ -109,7 +109,7 @@ def execute_command(command_name, arguments): elif command_name == "task_complete": shutdown() else: - return f"Unknown command {command_name}" + return f"Unknown command '{command_name}'. Please refer to the 'COMMANDS' list for availabe commands and only respond in the specified JSON format." # All errors, return "Error: + error message" except Exception as e: return "Error: " + str(e) From 97711584c3d9cb904a42974f8d3879af9fd9431c Mon Sep 17 00:00:00 2001 From: Richard Beales Date: Sun, 9 Apr 2023 07:36:00 +0100 Subject: [PATCH 21/23] Update README to indicate Python 3.8 minimum Due to tiktoken dependency. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 760e62cf59..7154446e2a 100644 --- a/README.md +++ b/README.md @@ -57,7 +57,7 @@ Your support is greatly appreciated - ๐Ÿ—ƒ๏ธ File storage and summarization with GPT-3.5 ## ๐Ÿ“‹ Requirements -- [Python 3.7 or later](https://www.tutorialspoint.com/how-to-install-python-in-windows) +- [Python 3.8 or later](https://www.tutorialspoint.com/how-to-install-python-in-windows) - OpenAI API key - PINECONE API key From 3efdb4896166c38ff70b2f208c4a644774c1c92b Mon Sep 17 00:00:00 2001 From: BillSchumacher <34168009+BillSchumacher@users.noreply.github.com> Date: Sun, 9 Apr 2023 02:38:06 -0500 Subject: [PATCH 22/23] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 760e62cf59..61181ebf97 100644 --- a/README.md +++ b/README.md @@ -149,6 +149,7 @@ Run: ``` docker run -d --name redis-stack-server -p 6379:6379 redis/redis-stack-server:latest ``` +See https://hub.docker.com/r/redis/redis-stack-server for setting a password and additional configuration. Set the following environment variables: ``` From 546d8783e78096d737351fca00d2cd701b9b72e5 Mon Sep 17 00:00:00 2001 From: Alexander Nikulin Date: Sun, 9 Apr 2023 14:33:30 +0400 Subject: [PATCH 23/23] put debug setting to cfg and use it in when calling chat.chat_with_at and fix_json --- scripts/config.py | 4 ++++ scripts/json_parser.py | 2 +- scripts/main.py | 6 +++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/scripts/config.py b/scripts/config.py index d5f1a3f066..4d7adec1c0 100644 --- a/scripts/config.py +++ b/scripts/config.py @@ -31,6 +31,7 @@ class Config(metaclass=Singleton): """ def __init__(self): + self.debug = False self.continuous_mode = False self.speak_mode = False # TODO - make these models be self-contained, using langchain, so we can configure them once and call it good @@ -110,3 +111,6 @@ class Config(metaclass=Singleton): def set_pinecone_region(self, value: str): self.pinecone_region = value + + def set_debug_mode(self, value: bool): + self.debug = value diff --git a/scripts/json_parser.py b/scripts/json_parser.py index 8ec9238b4d..c863ccdbb0 100644 --- a/scripts/json_parser.py +++ b/scripts/json_parser.py @@ -40,7 +40,7 @@ def fix_and_parse_json(json_str: str, try_to_fix_with_gpt: bool = True): if try_to_fix_with_gpt: print(f"Warning: Failed to parse AI output, attempting to fix.\n If you see this warning frequently, it's likely that your prompt is confusing the AI. Try changing it up slightly.") # Now try to fix this up using the ai_functions - ai_fixed_json = fix_json(json_str, json_schema, False) + ai_fixed_json = fix_json(json_str, json_schema, cfg.debug) if ai_fixed_json != "failed": return json.loads(ai_fixed_json) else: diff --git a/scripts/main.py b/scripts/main.py index a0a1898cc4..f96afeb163 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -266,6 +266,10 @@ def parse_arguments(): print_to_console("GPT3.5 Only Mode: ", Fore.GREEN, "ENABLED") cfg.set_smart_llm_model(cfg.fast_llm_model) + if args.debug: + print_to_console("Debug Mode: ", Fore.GREEN, "ENABLED") + cfg.set_debug_mode(True) + # TODO: fill in llm values here @@ -295,7 +299,7 @@ while True: user_input, full_message_history, memory, - cfg.fast_token_limit) # TODO: This hardcodes the model to use GPT3.5. Make this an argument + cfg.fast_token_limit, cfg.debug) # TODO: This hardcodes the model to use GPT3.5. Make this an argument # Print Assistant thoughts print_assistant_thoughts(assistant_reply)