Skip to content

Commit

Permalink
Merge pull request fiatrete#72 from photosssa/MVP
Browse files Browse the repository at this point in the history
add query knowledge object in shell
  • Loading branch information
waterflier committed Sep 29, 2023
2 parents b6a06c4 + bc2488f commit f332639
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 7 deletions.
10 changes: 5 additions & 5 deletions src/aios_kernel/knowledge_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ async def query_objects(self, tokens: str, types: list[str], topk: int) -> [Obje
images = await self.store.get_vector_store(self._default_image_model).query(vector, topk)
return texts + images

def __load_object(self, object_id: ObjectID) -> KnowledgeObject:
def load_object(self, object_id: ObjectID) -> KnowledgeObject:
if object_id.get_object_type() == ObjectType.Document:
return DocumentObject.decode(self.store.get_object_store().get_object(object_id))
if object_id.get_object_type() == ObjectType.Image:
Expand Down Expand Up @@ -210,7 +210,7 @@ def tokens_from_objects(self, object_ids: [ObjectID]) -> list[str]:
# first element in result is the root object
root_object_id = result[0]
if root_object_id.get_object_type() == ObjectType.Email:
email = self.__load_object(root_object_id)
email = self.load_object(root_object_id)
desc = email.get_desc()
desc["type"] = "email"
desc["contents"] = []
Expand All @@ -224,13 +224,13 @@ def tokens_from_objects(self, object_ids: [ObjectID]) -> list[str]:
if object_id.get_object_type() == ObjectType.Chunk:
upper_list.append({"type": "text", "content": self.store.get_chunk_reader().get_chunk(object_id).read().decode("utf-8")})
if object_id.get_object_type() == ObjectType.Image:
# image = self.__load_object(object_id)
# image = self.load_object(object_id)
desc = dict()
desc["id"] = str(object_id)
desc["type"] = "image"
upper_list.append(desc)
if object_id.get_object_type() == ObjectType.Video:
video = self.__load_object(object_id)
video = self.load_object(object_id)
desc = video.get_desc()
desc["type"] = "video"
upper_list.append(desc)
Expand All @@ -257,7 +257,7 @@ def parse_object_in_message(self, message: str) -> KnowledgeObject:
return None

if object_id is not None:
return self.__load_object(ObjectID.from_base58(object_id))
return self.load_object(ObjectID.from_base58(object_id))


def bytes_from_object(self, object: KnowledgeObject) -> bytes:
Expand Down
1 change: 1 addition & 0 deletions src/knowledge/object/object_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(self, root_dir: str):
self.blob = FileBlobStorage(blob_dir)

def put_object(self, object_id: ObjectID, contents: bytes):
logging.info(f"will put object: {object_id}")
self.blob.put(object_id, contents)

def get_object(self, object_id: ObjectID) -> bytes:
Expand Down
19 changes: 17 additions & 2 deletions src/service/aios_shell/aios_shell.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,8 @@ async def handle_contact_commands(self,args):
async def handle_knowledge_commands(self, args):
show_text = FormattedText([("class:title", "sub command not support!\n"
"/knowledge add email | dir\n"
"/knowledge journal [$topn]\n")])
"/knowledge journal [$topn]\n"
"/knowledge query $object_id\n")])
if len(args) < 1:
return show_text
sub_cmd = args[0]
Expand Down Expand Up @@ -365,6 +366,18 @@ async def handle_knowledge_commands(self, args):
topn = 10 if len(args) == 1 else int(args[1])
journals = [str(journal) for journal in KnowledgePipline.get_instance().get_latest_journals(topn)]
print_formatted_text("\r\n".join(journals))
if sub_cmd == "query":
if len(args) < 2:
return show_text
from knowledge import ObjectID, ObjectType
object_id = ObjectID.from_base58(args[1])
if object_id.get_object_type() == ObjectType.Image:
from PIL import Image
import io
image = KnowledgeBase().load_object(object_id)
image_data = KnowledgeBase().bytes_from_object(image)
image = Image.open(io.BytesIO(image_data))
image.show()

async def call_func(self,func_name, args):
match func_name:
Expand Down Expand Up @@ -593,9 +606,10 @@ def print_welcome_screen():
\033[1;94m\tGive your Agent a Telegram account :\033[0m /connect $agent_name
\033[1;94m\tAdd personal files to the AI Knowledge Base. \033[0m
\t\t1) Copy your file to ~/myai/data
\t\t2) /knowlege add $dir
\t\t2) /knowlege add dir
\033[1;94m\tSearch your knowledge base :\033[0m /open Mia
\033[1;94m\tCheck the progress of AI reading personal data :\033[0m /knowledge journal
\033[1;94m\tQuery object with ID in knowledge base :\033[0m /knowledge query $object_id
\033[1;94m\tOpen AI Bash (For Developer Only):\033[0m /open ai_bash
\033[1;94m\tEnable AIGC Feature :\033[0m /enable aigc
\033[1;94m\tEnable llama (Local LLM Kernel) :\033[0m /enable llama
Expand Down Expand Up @@ -675,6 +689,7 @@ async def main():
'/contact $name',
'/knowledge add email | dir',
'/knowledge journal [$topn]',
'/knowledge query $object_id',
'/set_config $key',
'/enable $feature',
'/disable $feature',
Expand Down

0 comments on commit f332639

Please sign in to comment.