diff --git a/README_ZH.md b/README_ZH.md index 53de97fd..438e770e 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -1,3 +1,16 @@ [**English**](./README.md) | 中文 # ModelScope + + +## 概念解释 + + +- service: 在顶层的交互对象,用于定义operation的使用范围 + +- operation: 读写记忆等,对于记忆的操作方法,是worker的有序组合(workflow) + +- workflow: 在operation中组合worker的方式 + +- worker: 框架中的基本工作模块 + diff --git a/config/test_config.yaml b/config/test_config.yaml index d7cc6725..2668a5e7 100644 --- a/config/test_config.yaml +++ b/config/test_config.yaml @@ -3,62 +3,68 @@ global_config: max_workers: 5 dash_scope_apikey: open_ai_apikey: + memory_chat: cli_memory_chat: - class: chat.cli_memory_chat + class: chat.cli_memory_chat # select class memory_service: memory_chat_service generation_model: dashscope_generation human_name: human assistant_name: assistant + memory_service: memory_chat_service: - class: memory.service.chat_memory_service + class: memory.service.chat_memory_service # select class history_msg_count: 32 contextual_msg_count: 6 read_memory_key: read_memory memory_operations: - read_message: + read_message: # define operation class: memory.operation.read_memory - workflow: dummy_worker + workflow: dummy_workflow # select workflow description: "read session messages of the user" read_memory: class: memory.operation.read_memory - workflow: dummy_worker + workflow: dummy_workflow description: "read related memories of the user" list_memory: class: memory.operation.read_memory - workflow: dummy_worker + workflow: dummy_workflow description: "read all memories of the user" write_memory: class: memory.operation.write_memory - workflow: dummy_worker + workflow: dummy_workflow description: "write observation memories of the user" interval_time: 60 summary_memory: class: memory.operation.summary_memory - workflow: dummy_worker + workflow: dummy_workflow description: "summary observation memories of the user" interval_time: 300 + models: dashscope_generation: - class: models.llama_index_generation_model + class: models.llama_index_generation_model # select class module_name: dashscope_generation model_name: qwen-max dashscope_embedding: - class: models.llama_index_embedding_model + class: models.llama_index_embedding_model # select class module_name: dashscope_embedding model_name: text-embedding-v2 dashscope_rank: - class: models.llama_index_rank_model + class: models.llama_index_rank_model # select class module_name: dashscope_rank model_name: gte-rerank + vector_store: - class: storage.dummy_vector_store + class: storage.dummy_vector_store # select class embedding_model: dashscope_embedding + monitor: - class: storage.dummy_monitor + class: storage.dummy_monitor # select class + worker: - dummy_worker: + dummy_workflow: class: memory.worker.dummy_worker generation_model: dashscope_generation embedding_model: dashscope_embedding diff --git a/memory_scope/chat/cli_memory_chat.py b/memory_scope/chat/cli_memory_chat.py index 05b9c879..5550df71 100644 --- a/memory_scope/chat/cli_memory_chat.py +++ b/memory_scope/chat/cli_memory_chat.py @@ -60,6 +60,8 @@ class CliMemoryChat(BaseMemoryChat): @property def memory_service(self) -> BaseMemoryService: if isinstance(self._memory_service, str): + if self._memory_service not in G_CONTEXT.memory_service_dict: + raise ValueError("Missing declaration of memory_service in yaml configuration: " + self._memory_service) self._memory_service = G_CONTEXT.memory_service_dict[self._memory_service] self._memory_service.start_service() return self._memory_service @@ -67,6 +69,8 @@ class CliMemoryChat(BaseMemoryChat): @property def generation_model(self) -> BaseModel: if isinstance(self._generation_model, str): + if self._generation_model not in G_CONTEXT.model_dict: + raise ValueError("Missing declaration of generation model in yaml configuration: " + self._generation_model) self._generation_model = G_CONTEXT.model_dict[self._generation_model] return self._generation_model diff --git a/memory_scope/cli.py b/memory_scope/cli.py index a2ef4ae7..95e8d372 100644 --- a/memory_scope/cli.py +++ b/memory_scope/cli.py @@ -31,6 +31,7 @@ class CliJob(object): self.config = json.load(f) else: raise RuntimeError("not supported config file type!") + self.init_global_content_by_config() def set_global_config(self): G_CONTEXT.global_config = global_config = self.config["global_config"] @@ -67,7 +68,6 @@ class CliJob(object): def run(self, config: str): self.load_config(config) - self.init_global_content_by_config() with G_CONTEXT.thread_pool: memory_chat = list(G_CONTEXT.memory_chat_dict.values())[0] diff --git a/memory_scope/memory/operation/base_workflow.py b/memory_scope/memory/operation/base_workflow.py index fee81862..5f3fedb7 100644 --- a/memory_scope/memory/operation/base_workflow.py +++ b/memory_scope/memory/operation/base_workflow.py @@ -33,7 +33,7 @@ class BaseWorkflow(object): self.logger: Logger = Logger.get_logger() if self.workflow: - self._parse_workflow() + self.workflow_worker_list = self._parse_workflow() self._print_workflow() def _parse_workflow(self): @@ -63,6 +63,7 @@ class BaseWorkflow(object): for sub_item in sub_split: self.worker_dict[sub_item] = is_multi_thread self.workflow_worker_list.append(line_split_split) + return self.workflow_worker_list def _print_workflow(self): self.logger.info(f"----- print_workflow_{self.name}_begin -----") diff --git a/memory_scope/utils/tool_functions.py b/memory_scope/utils/tool_functions.py index a7bf4426..4faab83d 100644 --- a/memory_scope/utils/tool_functions.py +++ b/memory_scope/utils/tool_functions.py @@ -30,6 +30,29 @@ def init_instance_by_config(config: dict, default_class_path: str = "memory_scope", suffix_name: str = "", **kwargs): + """ + Initialize an instance of a class specified in the configuration dictionary. + + This function dynamically imports a class from a module path, allowing for + user-defined classes or default paths. It supports adding a suffix to the + class name, merging additional keyword arguments with the config, and handling + nested module paths. + + Args: + config (dict): A dictionary containing the configuration, including + the 'class' key that specifies the class's module path. + default_class_path (str, optional): The default module path prefix + to use if not explicitly defined in + 'config'. Defaults to "memory_scope". + suffix_name (str, optional): A string to append to the class name, + ensuring the final class name ends with it. + Defaults to "". + **kwargs: Additional keyword arguments to pass to the class constructor. + + Returns: + object: An instance of the class initialized with the provided config and kwargs. + """ + config_copy = deepcopy(config) origin_class_path: str = config_copy.pop("class") if not origin_class_path: