Skip to content

Commit

Permalink
Update: sentence embedding support, 52%!
Browse files Browse the repository at this point in the history
  • Loading branch information
patham9 committed Apr 29, 2023
1 parent 1e5f8b7 commit 4ad89a4
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 25 deletions.
2 changes: 1 addition & 1 deletion All.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Correct.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions INT_Inf_benchmarkTest.csv
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
1,#setdate 10/21/2016,,,,,,
1,#settime 9:00 AM,,,,,,
1,#setGlobal resetCoreDuringBatch false,,,,,,
1,the date is 10/21/2016,,,,,,
1,the time is 9:00 AM,,,,,,
5,Mr. Magoo is a popular cartoon character from the 1940s.,,,,,,
5,He is known for being comically near-sighted.,,,,,,
5,Mr. Magoo was voiced by Bobby Backus.,,,,,,
Expand Down
2 changes: 1 addition & 1 deletion Incorrect.json

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions Memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def Term_AsSentence(T):
def Term_Embedded(T):
return get_embedding_robust(Term_AsSentence(T).replace("-"," ").replace("_"," "))

def RetrieveQuestionContent(memory, attention_buf, inp, max_LTM_retrievals=5):
def RetrieveQuestionContent(memory, attention_buf, inp, max_LTM_retrievals=30):
primed = {}
qu_embed = get_embedding_robust(inp)
for m in list(memory.items()):
Expand All @@ -86,28 +86,28 @@ def RetrieveQuestionContent(memory, attention_buf, inp, max_LTM_retrievals=5):
primed.sort(key=lambda x: (-x[1][0], -Truth_Expectation(x[1][1][2]))) #sort by query match first then by truth expectation
primed = primed[:max_LTM_retrievals]
#for m in primed:
# print("//Retrieved from LTM:", m)
# print("//Retrieved from LTM:", m[0], m[1][:-1])
primed = [(x[0],x[1][1]) for x in primed]
return list(reversed(primed))

def Memory_attention_buffer(memory, attention_buffer_size, inpQuestion = None):
attention_buf=[]
relevant_item_list = list(memory.items())
#relevant_item_list = list(memory.items())
#find attention_buffer_size/2 newest items:
relevant_item_list.sort(key=lambda x: -x[1][0])
attention_buf += reversed(relevant_item_list[0:int(attention_buffer_size/2)]) #newer comes later in prompt
#relevant_item_list.sort(key=lambda x: -x[1][0])
#attention_buf += reversed(relevant_item_list[0:int(attention_buffer_size/2)]) #newer comes later in prompt
#find additional attention_buffer_size/2 useful items which were not already part of the newest
relevant_item_list.sort(key=lambda x: -x[1][1])
for x in attention_buf:
if x in relevant_item_list:
relevant_item_list.remove(x) #so we won't select it as it is already part of mem
i = 0
while len(attention_buf) < attention_buffer_size and i < len(relevant_item_list):
attention_buf = [relevant_item_list[i]] + attention_buf
i += 1
#relevant_item_list.sort(key=lambda x: -x[1][1])
#for x in attention_buf:
# if x in relevant_item_list:
# relevant_item_list.remove(x) #so we won't select it as it is already part of mem
#i = 0
#while len(attention_buf) < attention_buffer_size and i < len(relevant_item_list):
# attention_buf = [relevant_item_list[i]] + attention_buf
# i += 1
#pull in question content that is not already included
if inpQuestion is not None:
attention_buf = RetrieveQuestionContent(memory, attention_buf, inpQuestion) + attention_buf
attention_buf = RetrieveQuestionContent(memory, attention_buf, inpQuestion) #+ attention_buf
return attention_buf

def Memory_generate_prompt(currentTime, memory, prompt_start, prompt_end, attention_buffer_size, inpQuestion = None, TimeHandling = True):
Expand Down Expand Up @@ -232,7 +232,7 @@ def ProcessInput(currentTime, memory, inputforNAR, backups = ["input", "answers"
relations = set(["isa", "are", "hasproperty"])
def Relation(inp, currentTime, memory, s, v, p, punctuation_tv):
global relations
if s.replace("_", " ") not in inp or p.replace("_", " ") not in inp:
if s.replace("_", " ") not in inp.replace(". "," ").replace("'","") or p.replace("_", " ") not in inp.replace(". "," ").replace("'",""):
#print("//!!!! filtered out", s, v, p)
return False, currentTime
s = Lemmatize(s, wordnet.NOUN)
Expand All @@ -250,7 +250,7 @@ def Relation(inp, currentTime, memory, s, v, p, punctuation_tv):
return True, currentTime

def Property(inp, currentTime, memory, s, p, punctuation_tv):
if s.replace("_", " ") not in inp or p.replace("_", " ") not in inp:
if s.replace("_", " ") not in inp.replace(". "," ").replace("'","") or p.replace("_", " ") not in inp.replace(". "," ").replace("'",""):
#print("//!!!! filtered out", s, "hasproperty", p)
return False, currentTime
s = Lemmatize(s, wordnet.NOUN)
Expand All @@ -264,6 +264,7 @@ def Property(inp, currentTime, memory, s, p, punctuation_tv):
hadRelation = set([])
def Memory_digest_sentence(inp, currentTime, memory, sentence, truth, PrintMemoryUpdates, TimeHandling):
global lastTime, hadRelation
#print(">>>>", sentence)
if currentTime != lastTime:
hadRelation = set([])
if sentence in hadRelation:
Expand Down
6 changes: 6 additions & 0 deletions NarsGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
if x.startswith("API_KEY="):
openai.api_key = x.split("API_KEY=")[1]
(memory, currentTime) = Memory_load(filename) #the ONA memory

with open("keys.json", 'w') as f:
json.dump(list(memory.keys()), f)



if currentTime != 1:
NAR.AddInput(str(currentTime-1), Print=False)

Expand Down
2 changes: 1 addition & 1 deletion OUT.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion QUESTIONS.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion scores_NoDate.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Correct": 295, "Incorrect": 357, "Ratio": 0.45245398773006135}
{"Correct": 339, "Incorrect": 313, "Ratio": 0.5199386503067485}

0 comments on commit 4ad89a4

Please sign in to comment.