Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

added weaviate to the supported vector memory providers

cs0lar5fe784aabe1e
diff --cc .env.template
index 7dc1d2bb11,01735615ca..bc3bea20c5
--- a/.env.template
+++ b/.env.template
@@@ -7,14 -7,9 +7,19 @@@ FAST_LLM_MODEL=gpt-3.5-turb
GOOGLE_API_KEY=
CUSTOM_SEARCH_ENGINE_ID=
USE_AZURE=False
+OPENAI_API_BASE=your-base-url-for-azure
+OPENAI_API_VERSION=api-version-for-azure
+OPENAI_DEPLOYMENT_ID=deployment-id-for-azure
+WEAVIATE_HOST="http://127.0.0.1"
+WEAVIATE_PORT="8080"
+WEAVIATE_USERNAME=
+WEAVIATE_PASSWORD=
- WEAVIATE_INDEX="Autogpt"
- MEMORY_PROVIDER="weaviate"
+IMAGE_PROVIDER=dalle
+HUGGINGFACE_API_TOKEN=
+ OPENAI_AZURE_API_BASE=your-base-url-for-azure
+ OPENAI_AZURE_API_VERSION=api-version-for-azure
+ OPENAI_AZURE_DEPLOYMENT_ID=deployment-id-for-azure
+ IMAGE_PROVIDER=dalle
-HUGGINGFACE_API_TOKEN=
+ USE_MAC_OS_TTS=False
++MEMORY_INDEX="auto-gpt"
++MEMORY_BACKEND="local"
diff --cc README.md
index e29b7d9ee3,749c879151..3e41536192
--- a/README.md
+++ b/README.md
@@@ -156,11 -185,16 +185,18 @@@ Pinecone enables the storage of vast am
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:
++
+ ### Setting up environment variables
+
+ Simply set them in the `.env` file.
+
+ Alternatively, you can set them from the command line (advanced):
+
+ For Windows Users:
++
```
setx PINECONE_API_KEY "YOUR_PINECONE_API_KEY"
- export PINECONE_ENV="Your pinecone region" # something like: us-east4-gcp
+ setx PINECONE_ENV "Your pinecone region" # something like: us-east4-gcp
```
For macOS and Linux users:
@@@ -170,23 -204,6 +206,22 @@@ export PINECONE_ENV="Your pinecone regi
```
- Or you can set them in the `.env` file.
-
- ### Weaviate Setup
++## Weaviate Setup
+
+[Weaviate](https://weaviate.io/) is an open-source vector database. It allows to store data objects and vector embeddings from ML-models and scales seamlessly to billion of data objects. [An instance of Weaviate can be created locally (using Docker), on Kubernetes or using Weaviate Cloud Services](https://weaviate.io/developers/weaviate/quickstart).
+
+#### Setting up enviornment variables
+
+In your `.env` file set the following:
+
+```
++MEMORY_BACKEND=weaviate
+WEAVIATE_HOST="http://127.0.0.1" # the URL of the running Weaviate instance
+WEAVIATE_PORT="8080"
+WEAVIATE_USERNAME="your username"
+WEAVIATE_PASSWORD="your password"
- WEAVIATE_INDEX="Autogpt" # name of the index to create for the application
++MEMORY_INDEX="Autogpt" # name of the index to create for the application
+```
## View Memory Usage
diff --cc requirements.txt
index aed03226a2,6a9ba64330..8004319d57
--- a/requirements.txt
+++ b/requirements.txt
@@@ -12,4 -12,6 +12,7 @@@ docke
duckduckgo-search
google-api-python-client #(https://developers.google.com/custom-search/v1/overview)
pinecone-client==2.2.1
+ redis
+ orjson
+ Pillow
+weaviate-client==3.15.4
diff --cc scripts/config.py
index 08869a6d7b,27cc946cf0..2a0c0a94b5
--- a/scripts/config.py
+++ b/scripts/config.py
@@@ -53,14 -64,6 +64,12 @@@ class Config(metaclass=Singleton)
self.pinecone_api_key = os.getenv("PINECONE_API_KEY")
self.pinecone_region = os.getenv("PINECONE_ENV")
+ self.weaviate_host = os.getenv("WEAVIATE_HOST")
+ self.weaviate_port = os.getenv("WEAVIATE_PORT")
+ self.weaviate_username = os.getenv("WEAVIATE_USERNAME", None)
+ self.weaviate_password = os.getenv("WEAVIATE_PASSWORD", None)
+ self.weaviate_scopes = os.getenv("WEAVIATE_SCOPES", None)
- self.weaviate_index = os.getenv("WEAVIATE_INDEX", 'auto-gpt')
+
self.image_provider = os.getenv("IMAGE_PROVIDER")
self.huggingface_api_token = os.getenv("HUGGINGFACE_API_TOKEN")
diff --cc scripts/main.py
index 4740c626fd,8661bfad14..217461188d
--- a/scripts/main.py
+++ b/scripts/main.py
@@@ -381,4 -415,4 +415,3 @@@ while True
chat.create_chat_message(
"system", "Unable to execute command"))
print_to_console("SYSTEM: ", Fore.YELLOW, "Unable to execute command")
--
diff --cc scripts/memory/__init__.py
index 0000000000,a441a46aa9..69bc8b787e
mode 000000,100644..100644
--- a/scripts/memory/__init__.py
+++ b/scripts/memory/__init__.py
@@@ -1,0 -1,44 +1,56 @@@
+ 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
+
++try:
++ from memory.weaviate import WeaviateMemory
++except ImportError:
++ print("Weaviate not installed. Skipping import.")
++ WeaviateMemory = 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)
-
++ elif cfg.memory_backend == "weaviate":
++ if not WeaviateMemory:
++ print("Error: Weaviate is not installed. Please install weaviate-client to"
++ " use Weaviate as a memory backend.")
++ else:
++ memory = WeaviateMemory(cfg)
++
+ if memory is None:
+ memory = LocalCache(cfg)
+ if init:
+ memory.clear()
+ return memory
+
+
+ __all__ = [
+ "get_memory",
+ "LocalCache",
+ "RedisMemory",
+ "PineconeMemory",
++ "WeaviateMemory"
+ ]
diff --cc scripts/memory/pinecone.py
index e8cf019ab1,8e1eaa570f..287bc1dc26
--- a/scripts/memory/pinecone.py
+++ b/scripts/memory/pinecone.py
@@@ -1,14 -1,11 +1,10 @@@
- from config import Config
- from providers.memory import Memory, get_ada_embedding
-
import pinecone
- cfg = Config()
+ from memory.base import MemoryProviderSingleton, get_ada_embedding
- class PineconeMemory(Memory):
- def __init__(self):
- # 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")
+ 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 --cc scripts/memory/weaviate.py
index 21718a0336,0000000000..b6032648d5
mode 100644,000000..100644
--- a/scripts/memory/weaviate.py
+++ b/scripts/memory/weaviate.py
@@@ -1,100 -1,0 +1,100 @@@
+from config import Config
- from providers.memory import Memory, get_ada_embedding
- from weaviate import Client
- import weaviate
++from memory.base import MemoryProviderSingleton, get_ada_embedding
+import uuid
-
++import weaviate
++from weaviate import Client
+from weaviate.util import generate_uuid5
+
- cfg = Config()
-
- SCHEMA = {
- "class": cfg.weaviate_index,
- "properties": [
- {
- "name": "raw_text",
- "dataType": ["text"],
- "description": "original text for the embedding"
- }
- ],
- }
-
- class WeaviateMemory(Memory):
-
- def __init__(self):
- auth_credentials = self._build_auth_credentials()
++def default_schema(weaviate_index):
++ return {
++ "class": weaviate_index,
++ "properties": [
++ {
++ "name": "raw_text",
++ "dataType": ["text"],
++ "description": "original text for the embedding"
++ }
++ ],
++ }
++
++class WeaviateMemory(MemoryProviderSingleton):
++ def __init__(self, cfg):
++ auth_credentials = self._build_auth_credentials(cfg)
+
+ url = f'{cfg.weaviate_host}:{cfg.weaviate_port}'
+
+ self.client = Client(url, auth_client_secret=auth_credentials)
-
++ self.index = cfg.memory_index
+ self._create_schema()
+
+ def _create_schema(self):
- if not self.client.schema.contains(SCHEMA):
- self.client.schema.create_class(SCHEMA)
++ schema = default_schema(self.index)
++ if not self.client.schema.contains(schema):
++ self.client.schema.create_class(schema)
+
- @staticmethod
- def _build_auth_credentials():
++ def _build_auth_credentials(self, cfg):
+ if cfg.weaviate_username and cfg.weaviate_password:
+ return weaviate_auth.AuthClientPassword(cfg.weaviate_username, cfg.weaviate_password)
+ else:
+ return None
+
+ def add(self, data):
+ vector = get_ada_embedding(data)
+
- doc_uuid = generate_uuid5(data, cfg.weaviate_index)
++ doc_uuid = generate_uuid5(data, self.index)
+ data_object = {
- 'class': cfg.weaviate_index,
++ 'class': self.index,
+ 'raw_text': data
+ }
+
+ with self.client.batch as batch:
+ batch.add_data_object(
+ uuid=doc_uuid,
+ data_object=data_object,
- class_name=cfg.weaviate_index,
++ class_name=self.index,
+ vector=vector
+ )
+
+ batch.flush()
+
+ return f"Inserting data into memory at uuid: {doc_uuid}:\n data: {data}"
+
+
+ def get(self, data):
+ return self.get_relevant(data, 1)
+
+
+ def clear(self):
+ self.client.schema.delete_all()
+
+ # weaviate does not yet have a neat way to just remove the items in an index
+ # without removing the entire schema, therefore we need to re-create it
+ # after a call to delete_all
+ self._create_schema()
+
+ return 'Obliterated'
+
+ def get_relevant(self, data, num_relevant=5):
+ query_embedding = get_ada_embedding(data)
+ try:
- results = self.client.query.get(cfg.weaviate_index, ['raw_text']) \
++ results = self.client.query.get(self.index, ['raw_text']) \
+ .with_near_vector({'vector': query_embedding, 'certainty': 0.7}) \
+ .with_limit(num_relevant) \
+ .do()
+
- print(results)
-
- if len(results['data']['Get'][cfg.weaviate_index]) > 0:
- return [str(item['raw_text']) for item in results['data']['Get'][cfg.weaviate_index]]
++ if len(results['data']['Get'][self.index]) > 0:
++ return [str(item['raw_text']) for item in results['data']['Get'][self.index]]
+ else:
+ return []
+
+ except Exception as err:
+ print(f'Unexpected error {err=}, {type(err)=}')
+ return []
+
+ def get_stats(self):
- return self.client.index_stats.get(cfg.weaviate_index)
++ result = self.client.query.aggregate(self.index) \
++ .with_meta_count() \
++ .do()
++ class_data = result['data']['Aggregate'][self.index]
++
++ return class_data[0]['meta'] if class_data else {}
diff --cc tests/test_weaviate_memory.py
index 0000000000,0000000000..41709bc084
new file mode 100644
--- /dev/null
+++ b/tests/test_weaviate_memory.py
@@@ -1,0 -1,0 +1,99 @@@
++import unittest
++from unittest import mock
++import sys
++import os
++
++from weaviate import Client
++from weaviate.util import get_valid_uuid
++from uuid import uuid4
++
++sys.path.append(os.path.abspath('./scripts'))
++from config import Config
++from memory.weaviate import WeaviateMemory
++from memory.base import get_ada_embedding
++
[email protected](os.environ, {
++ "WEAVIATE_HOST": "http://127.0.0.1",
++ "WEAVIATE_PORT": "8080",
++ "WEAVIATE_USERNAME": '',
++ "WEAVIATE_PASSWORD": '',
++ "MEMORY_INDEX": "AutogptTests"
++})
++class TestWeaviateMemory(unittest.TestCase):
++ """
++ In order to run these tests you will need a local instance of
++ Weaviate running. Refer to https://weaviate.io/developers/weaviate/installation/docker-compose
++ for creating local instances using docker.
++ """
++ def setUp(self):
++ self.cfg = Config()
++
++ self.client = Client('http://127.0.0.1:8080')
++
++ try:
++ self.client.schema.delete_class(self.cfg.memory_index)
++ except:
++ pass
++
++ self.memory = WeaviateMemory(self.cfg)
++
++ def test_add(self):
++ doc = 'You are a Titan name Thanos and you are looking for the Infinity Stones'
++ self.memory.add(doc)
++ result = self.client.query.get(self.cfg.memory_index, ['raw_text']).do()
++ actual = result['data']['Get'][self.cfg.memory_index]
++
++ self.assertEqual(len(actual), 1)
++ self.assertEqual(actual[0]['raw_text'], doc)
++
++ def test_get(self):
++ doc = 'You are an Avenger and swore to defend the Galaxy from a menace called Thanos'
++
++ with self.client.batch as batch:
++ batch.add_data_object(
++ uuid=get_valid_uuid(uuid4()),
++ data_object={'raw_text': doc},
++ class_name=self.cfg.memory_index,
++ vector=get_ada_embedding(doc)
++ )
++
++ batch.flush()
++
++ actual = self.memory.get(doc)
++
++ self.assertEqual(len(actual), 1)
++ self.assertEqual(actual[0], doc)
++
++
++ def test_get_stats(self):
++ docs = [
++ 'You are now about to count the number of docs in this index',
++ 'And then you about to find out if you can count correctly'
++ ]
++
++ [self.memory.add(doc) for doc in docs]
++
++ stats = self.memory.get_stats()
++
++ self.assertTrue(stats)
++ self.assertTrue('count' in stats)
++ self.assertEqual(stats['count'], 2)
++
++
++ def test_clear(self):
++ docs = [
++ 'Shame this is the last test for this class',
++ 'Testing is fun when someone else is doing it'
++ ]
++
++ [self.memory.add(doc) for doc in docs]
++
++ self.assertEqual(self.memory.get_stats()['count'], 2)
++
++ self.memory.clear()
++
++ self.assertEqual(self.memory.get_stats()['count'], 0)
++
++
++if __name__ == '__main__':
++ unittest.main()