Commit 9fc844ec by Mingju

Add APIS implemention

parent 2b655e4e
import aiohttp
from chroma_memory_stream import MemoryStream
import chroma_retrieve
api_file = 'key.txt'
......@@ -9,6 +10,9 @@ def run_gpt_event_poignancy_prompt(api_key_f, record):
def run_gpt_chat_poignancy_prompt(api_key_f, chat_history):
raise NotImplementedError
def run_gpt_skill_poignancy_prompt(api_key_f, skill_desc):
raise NotImplementedError
def run_gpt_summary_event_prompt(api_key_f, record):
raise NotImplementedError
......@@ -19,19 +23,32 @@ def parse_observations(observation:dict):
description = ''
description += f"Name: {observation['name']}\n"
description += f"Entities: {observation['entities']}\n"
description += f"Health: {observation['health']} Food: {observation['food']}" +\
description += f"Health: {observation['health']} Food: {observation['food']} " +\
f"Saturation: {observation['saturation']} Oxygen: {observation['oxygen']}\n"
description += f""
pass
description += f"Position: {observation['position']} Velocity: {observation['velocity']} " + \
f"Yaw: {observation['yaw']} Pitch: {observation['pitch']} IsonGroud: {observation['groud']}\n"
description += f"TimeSinceOnGround: {observation['timeSinceOnGroud']}\n"
description += f"IsInwater: {observation['isInwater']} IsInLava: {observation['isInLava']}" + \
f"IsInweb: {observation['isInWeb']} IsCollidedHorizontally: {observation['isCollidedHorizontally']} "+ \
f"IsCollidedVertically: {observation['isCollidedVertically']}\n"
description += f"TimeofDay: {observation['timeOfDay']}\n"
description += f"InventoryUsed: {observation['inventoryUsed']}\n"
return description
class MemoryAPIs(object):
def __init__(self, db_file_name, collection_name) -> None:
def __init__(self, db_file_name, collection_name):
self.memory_stream = MemoryStream(db_file_name=db_file_name, collection_name=collection_name)
async def receive_web_info(self, ip, port):
pass
def add_chat_to_mem(self, chat_history, now_time):
async def send_result(self, ip, port, res):
pass
async def get_now_time(self, ip, port):
pass
def add_chat_to_mem(self, chat_history, summary, now_time):
"""
Add chat history to the memory.
The *chat_history* should be formulated as below:
......@@ -45,29 +62,80 @@ class MemoryAPIs(object):
r_type = 'chat'
description = chat_history
code = 'NULL'
summary = run_gpt_summary_chat_prompt(api_key_f=api_file, chat_history=description)
poignancy = run_gpt_chat_poignancy_prompt(api_key_f=api_file, chat_history=description)
self.memory_stream.add_record_in_mem(r_type, description, code,
summary, now_time, poignancy)
def add_event_to_mem(observation, now_time):
def add_event_to_mem(self, description, summary, now_time):
"""
Add observation to the memory.
The *observation* is a python dict
"""
r_type = 'event'
pass
code = 'NULL'
poignancy = run_gpt_event_poignancy_prompt(api_key_f=api_file, record=description)
def add_skill_to_mem(skill_info, now_time):
self.memory_stream.add_record_in_mem(r_type, description, code,
summary, now_time, poignancy)
pass
def add_bug_info_to_mem(bug_info, now_time):
pass
def add_skill_to_mem(self, skill_info, now_time):
"""
Add skill to the memory.
The *sill_info* is a python dict, which has the field 'code' and 'description'
"""
r_type = 'skill'
description = skill_info['description']
code = skill_info['code']
summary = 'NULL'
poignancy = run_gpt_skill_poignancy_prompt(api_key_f=api_file, skill_desc=description)
self.memory_stream.add_record_in_mem(r_type, description, code,
summary, now_time, poignancy)
def add_bug_info_to_mem(self, bug_info, summary, now_time):
"""
Add skill to the memory.
The *sill_info* is a python dict, which has the field 'code' and 'description'
"""
self.memory_stream.add_bug_report_in_mem(bug_info, summary, now_time)
def delete_record_in_mem():
def delete_record_in_mem(self):
pass
def retrieve_mem():
raise NotImplementedError
\ No newline at end of file
def retrieve_mem(self, msg):
return chroma_retrieve.retrieve(self.memory_stream.collection, [msg], topK=10)
def circle(self, msg, r_type):
"""
The main memory circle.
R_type can be: chat/event/skill/bug
"""
if r_type == 'chat':
desc = msg
summary = run_gpt_summary_chat_prompt(api_key_f=api_file, chat_history=desc)
retrieved_msgs = self.retrieve_mem(summary)
now_time = self.get_now_time()
self.add_chat_to_mem(desc, summary, now_time)
elif r_type == 'event':
desc = parse_observations(msg)
summary = run_gpt_summary_event_prompt(api_key_f=api_file, record=desc)
retrieved_msgs = self.retrieve_mem(summary)
now_time = self.get_now_time()
self.add_event_to_mem(desc, summary, now_time)
elif r_type == 'skill':
desc = msg['description']
retrieved_msgs = self.retrieve_mem(desc)
now_time = self.get_now_time()
self.add_skill_to_mem(msg, now_time)
elif r_type == 'bug':
desc = msg
summary = run_gpt_summary_event_prompt(api_key_f=api_file, record=desc)
now_time = self.get_now_time(),
retrieved_msgs = None
self.add_bug_info_to_mem(msg, summary, now_time)
else:
raise NotImplementedError("Not supported node type.")
return retrieved_msgs
\ No newline at end of file
......@@ -71,7 +71,7 @@ def get_res(records, idx):
res['summary'] = records['documents'][0][idx]
return res
def retrieve(collection:chromadb.Collection, msgs:list[str], topK=2):
def retrieve(collection:chromadb.Collection, msgs:list[str], topK=10):
relevance, relevance_records = extract_relevance(collection, msgs)
poignance = extract_poignance(relevance_records)
recency = extract_recency(relevance_records)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment