forked from Significant-Gravitas/AutoGPT
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pinecone memory and memory usage tracking
- Loading branch information
1 parent
62dfd84
commit 475671d
Showing
9 changed files
with
167 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,61 @@ | ||
permanent_memory = [] | ||
from config import Config, Singleton | ||
import pinecone | ||
import openai | ||
|
||
cfg = Config() | ||
|
||
|
||
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): | ||
pinecone_api_key = cfg.pinecone_api_key | ||
pinecone_region = cfg.pinecone_region | ||
pinecone.init(api_key=pinecone_api_key, environment=pinecone_region) | ||
dimension = 1536 | ||
metric = "cosine" | ||
pod_type = "p1" | ||
table_name = "auto-gpt" | ||
# this assumes we don't start with memory. | ||
# for now this works. | ||
# we'll need a more complicated and robust system if we want to start with memory. | ||
self.vec_num = 0 | ||
if table_name not in pinecone.list_indexes(): | ||
pinecone.create_index(table_name, dimension=dimension, metric=metric, pod_type=pod_type) | ||
self.index = pinecone.Index(table_name) | ||
|
||
def add(self, data): | ||
vector = get_ada_embedding(data) | ||
# no metadata here. We may wish to change that long term. | ||
resp = self.index.upsert([(str(self.vec_num), vector, {"raw_text": data})]) | ||
_text = f"Inserting data into memory at index: {self.vec_num}:\n data: {data}" | ||
self.vec_num += 1 | ||
return _text | ||
|
||
def get(self, data): | ||
return self.get_relevant(data, 1) | ||
|
||
def clear(self): | ||
self.index.delete(deleteAll=True) | ||
return "Obliviated" | ||
|
||
def get_relevant(self, data, num_relevant=5): | ||
""" | ||
Returns all the data in the memory that is relevant to the given data. | ||
:param data: The data to compare to. | ||
:param num_relevant: The number of relevant data to return. Defaults to 5 | ||
""" | ||
query_embedding = get_ada_embedding(data) | ||
results = self.index.query(query_embedding, top_k=num_relevant, include_metadata=True) | ||
sorted_results = sorted(results.matches, key=lambda x: x.score) | ||
return [str(item['metadata']["raw_text"]) for item in sorted_results] | ||
|
||
def get_stats(self): | ||
return self.index.describe_index_stats() |