update flow for the docker image

This commit is contained in:
Vasilije 2023-09-10 13:10:29 +02:00
parent 8113787cce
commit d9a2ee6646
2 changed files with 198 additions and 56 deletions

View file

@ -808,63 +808,86 @@ class EpisodicBuffer(BaseMemory):
# check if modulators exist, initialize the modulators if needed # check if modulators exist, initialize the modulators if needed
if attention_modulators is None: if attention_modulators is None:
try: # try:
attention_modulators = await self.fetch_memories(observation="Attention modulators", print("Starting with attention mods")
namespace="BUFFERMEMORY") attention_modulators = await self.fetch_memories(observation="Attention modulators",
lookup_value_episodic = await self.fetch_memories( namespace="BUFFERMEMORY")
observation=str(output), namespace="EPISODICCMEMORY"
) print("Attention modulators exist", str(attention_modulators))
lookup_value_episodic = await self.fetch_memories(
observation=str(output), namespace="EPISODICMEMORY"
)
# lookup_value_episodic= lookup_value_episodic["data"]["Get"]["EPISODICMEMORY"][0]["text"]
prompt_classify = ChatPromptTemplate.from_template(
"""You are a classifier. Determine if based on the previous query if the user was satisfied with the output : {query}"""
)
json_structure = [{
"name": "classifier",
"description": "Classification indicating if it's output is satisfactory",
"parameters": {
"type": "object",
"properties": {
"classification": {
"type": "boolean",
"description": "The classification true or false"
}
}, "required": ["classification"]}
}]
chain_filter = prompt_classify | self.llm.bind(function_call= {"name": "classifier"}, functions= json_structure)
classifier_output = await chain_filter.ainvoke({"query": lookup_value_episodic})
arguments_str = classifier_output.additional_kwargs['function_call']['arguments']
print("This is the arguments string", arguments_str)
arguments_dict = json.loads(arguments_str)
classfier_value = arguments_dict.get('classification', None)
print("This is the classifier value", classfier_value)
if classfier_value:
# adjust the weights of the modulators by adding a positive value
print("Lookup value, episodic", lookup_value_episodic["data"]["Get"]["EPISODICMEMORY"][0]["text"])
prompt_classify = ChatPromptTemplate.from_template( prompt_classify = ChatPromptTemplate.from_template(
"""You are a classifier. Determine if based on the previous query if the user was satisfied with the output : {query}""" """ We know we need to increase the classifiers for our AI system. The classifiers are {modulators} The query is: {query}. Which of the classifiers should we decrease? Return just the modulator and desired value"""
) )
json_structure = { chain_modulator = prompt_classify | self.llm
"name": "classifier", classifier_output = await chain_modulator.ainvoke({"query": lookup_value_episodic, "modulators": str(attention_modulators)})
"description": "Classification indicating if it's output is satisfactory", print("classifier output 1", classifier_output)
"type": "boolean", diff_layer = DifferentiableLayer(attention_modulators)
"required": True adjusted_modulator = await diff_layer.adjust_weights(classifier_output)
} _input = prompt.format_prompt(query=adjusted_modulator)
chain_filter = prompt_classify | self.llm.bind(function_call= {"name": "classifier"}, functions= json_structure) document_context_result = self.llm_base(_input.to_string())
classifier_output = await chain_filter.ainvoke({"query": lookup_value_episodic}) document_context_result_parsed = parser.parse(document_context_result)
arguments_str = classifier_output.additional_kwargs['function_call']['arguments'] print("Updating with the following weights", str(document_context_result_parsed))
arguments_dict = json.loads(arguments_str) await self.add_memories(observation=str(document_context_result_parsed), params=params, namespace="BUFFERMEMORY")
classfier_value = arguments_dict.get('classifier', None) else:
# adjust the weights of the modulators by adding a negative value
print("Lookup value, episodic", lookup_value_episodic)
prompt_classify = ChatPromptTemplate.from_template(
""" We know we need to decrease the classifiers for our AI system. The classifiers are {modulators} The query is: {query}. Which of the classifiers should we decrease? Return just the modulator and desired value"""
)
chain_modulator_reduction = prompt_classify | self.llm
if classfier_value: classifier_output = await chain_modulator_reduction.ainvoke({"query": lookup_value_episodic, "modulators": str(attention_modulators)})
# adjust the weights of the modulators by adding a positive value print("classifier output 2", classifier_output)
prompt_classify = ChatPromptTemplate.from_template( diff_layer = DifferentiableLayer(attention_modulators)
""" We know we need to increase the classifiers for our AI system. The classifiers are {modulators} The query is: {query}. Which of the classifiers should we decrease? Return just the modulator and desired value""" adjusted_modulator =diff_layer.adjust_weights(classifier_output)
) _input = prompt.format_prompt(query=adjusted_modulator)
chain_modulator = prompt_classify | self.llm document_context_result = self.llm_base(_input.to_string())
classifier_output = await chain_modulator.ainvoke({"query": lookup_value_episodic, "modulators": str(attention_modulators)}) document_context_result_parsed = parser.parse(document_context_result)
diff_layer = DifferentiableLayer(attention_modulators) print("Updating with the following weights", str(document_context_result_parsed))
adjusted_modulator = diff_layer.adjust_weights(classifier_output) await self.add_memories(observation=str(document_context_result_parsed), params=params, namespace="BUFFERMEMORY")
_input = prompt.format_prompt(query=adjusted_modulator) # except:
document_context_result = self.llm_base(_input.to_string()) # # initialize the modulators with default values if they are not provided
document_context_result_parsed = parser.parse(document_context_result) # print("Starting with default modulators")
await self.add_memories(observation=document_context_result_parsed, namespace="BUFFERMEMORY") # attention_modulators = {
else: # "freshness": 0.5,
# adjust the weights of the modulators by adding a negative value # "frequency": 0.5,
prompt_classify = ChatPromptTemplate.from_template( # "relevance": 0.5,
""" We know we need to decrease the classifiers for our AI system. The classifiers are {modulators} The query is: {query}. Which of the classifiers should we decrease? Return just the modulator and desired value""" # "saliency": 0.5,
) # }
chain_modulator_reduction = prompt_classify | self.llm # _input = prompt.format_prompt(query=attention_modulators)
# document_context_result = self.llm_base(_input.to_string())
classifier_output = await chain_modulator_reduction.ainvoke({"query": lookup_value_episodic, "modulators": str(attention_modulators)}) # document_context_result_parsed = parser.parse(document_context_result)
diff_layer = DifferentiableLayer(attention_modulators) # await self.add_memories(observation=str(document_context_result_parsed), params=params, namespace="BUFFERMEMORY")
adjusted_modulator =diff_layer.adjust_weights(classifier_output)
_input = prompt.format_prompt(query=adjusted_modulator)
document_context_result = self.llm_base(_input.to_string())
document_context_result_parsed = parser.parse(document_context_result)
await self.add_memories(observation=document_context_result_parsed, namespace="BUFFERMEMORY")
except:
# initialize the modulators with default values if they are not provided
print("Starting with default modulators")
attention_modulators = {
"freshness": 0.5,
"frequency": 0.5,
"relevance": 0.5,
"saliency": 0.5,
}
elif attention_modulators: elif attention_modulators:
pass pass
@ -1140,7 +1163,8 @@ class EpisodicBuffer(BaseMemory):
query=user_input, steps=str(tasks_list) query=user_input, steps=str(tasks_list)
, buffer=str(result_tasks), date= date, attention_modulators=attention_modulators , buffer=str(result_tasks), date= date, attention_modulators=attention_modulators
) )
print("HERE ARE THE STEPS, BUFFER AND DATE", str(tasks_list))
print("here are the result_tasks", str(result_tasks))
# return "a few things to do like load episodic memory in a structured format" # return "a few things to do like load episodic memory in a structured format"
output = self.llm_base(_input.to_string()) output = self.llm_base(_input.to_string())
result_parsing = parser.parse(output) result_parsing = parser.parse(output)
@ -1373,13 +1397,20 @@ async def main():
# print(load_jack_london) # print(load_jack_london)
modulator = {"relevance": 0.0, "saliency": 0.0, "frequency": 0.0} modulator = {"relevance": 0.0, "saliency": 0.0, "frequency": 0.0}
# #
run_main_buffer = await memory._run_main_buffer( run_main_buffer = await memory._run_main_buffer(
user_input="I want to know how does Buck adapt to life in the wild and then have that info translated to german ", user_input="I want to know how does Buck adapt to life in the wild and then have that info translated to german ",
params=params, params=params,
attention_modulators=modulator, attention_modulators=modulator,
) )
print(run_main_buffer) print(run_main_buffer)
# #
# run_main_buffer = await memory._run_main_buffer(
# user_input="I want to know how does Buck adapt to life in the wild and then have that info translated to german ",
# params=params,
# attention_modulators=None,
# )
# print(run_main_buffer)
# del_semantic = await memory._delete_semantic_memory() # del_semantic = await memory._delete_semantic_memory()
# print(del_semantic) # print(del_semantic)

111
level_2/utils.py Normal file

File diff suppressed because one or more lines are too long