diff --git a/.flake8 b/.flake8 index b55b82c1..d7c02373 100644 --- a/.flake8 +++ b/.flake8 @@ -1,12 +1,6 @@ [flake8] exclude = - scripts/* - src/agentscope/rpc/* max-line-length = 120 inline-quotes = " avoid-escape = no -ignore = - F401 - F403 - W503 - E731 \ No newline at end of file +ignore = \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e65b4ae1..f7498a36 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -25,12 +25,12 @@ repos: hooks: - id: mypy exclude: - (?x)( - pb2\.py$ - | grpc\.py$ - | ^docs - | \.html$ - ) + (?x)( + pb2\.py$ + | grpc\.py$ + | ^docs + | \.html$ + ) args: [ --disallow-untyped-defs, --disallow-incomplete-defs, --ignore-missing-imports, @@ -42,7 +42,7 @@ repos: --disable-error-code=truthy-function, --follow-imports=skip, --explicit-package-bases, - ] + ] # - repo: https://github.com/numpy/numpydoc # rev: v1.6.0 # hooks: @@ -50,25 +50,25 @@ repos: - repo: https://github.com/psf/black rev: 23.3.0 hooks: - - id: black - args: [--line-length=79] + - id: black + args: [ --line-length=79 ] - repo: https://github.com/PyCQA/flake8 rev: 6.1.0 hooks: - id: flake8 - args: ["--extend-ignore=E203"] + args: [ "--extend-ignore=E203" ] - repo: https://github.com/pylint-dev/pylint rev: v3.0.2 hooks: - id: pylint exclude: - (?x)( - ^docs - | pb2\.py$ - | grpc\.py$ - | \.demo$ - | \.md$ - | \.html$ + (?x)( + ^docs + | pb2\.py$ + | grpc\.py$ + | \.demo$ + | \.md$ + | \.html$ ) args: [ --disable=W0511, @@ -101,4 +101,4 @@ repos: rev: "4.0" hooks: - id: pyroma - args: [--min=10, .] + args: [ --min=10, . ] diff --git a/README_ZH.md b/README_ZH.md index 438e770e..4ae08bf3 100644 --- a/README_ZH.md +++ b/README_ZH.md @@ -2,10 +2,8 @@ # ModelScope - ## 概念解释 - - service: 在顶层的交互对象,用于定义operation的使用范围 - operation: 读写记忆等,对于记忆的操作方法,是worker的有序组合(workflow) diff --git a/memory_scope/memory/operation/base_operation.py b/memory_scope/memory/operation/base_operation.py index 8bce8c57..8904530c 100644 --- a/memory_scope/memory/operation/base_operation.py +++ b/memory_scope/memory/operation/base_operation.py @@ -14,7 +14,7 @@ class BaseOperation(metaclass=ABCMeta): description (str): A description of the operation. kwargs (dict): Additional keyword arguments for operation configuration. """ - + operation_type: OPERATION_TYPE = "frontend" def __init__(self, name: str, description: str = ""): diff --git a/memory_scope/memory/operation/base_workflow.py b/memory_scope/memory/operation/base_workflow.py index 5ee5b765..1b5d7119 100644 --- a/memory_scope/memory/operation/base_workflow.py +++ b/memory_scope/memory/operation/base_workflow.py @@ -50,7 +50,7 @@ class BaseWorkflow(object): pattern = r'(\[[^\]]*\]|[^,]+)' # Find all matches in the workflow string based on the pattern. workflow_split = re.findall(pattern, self.workflow) - + for workflow_part in workflow_split: # e.g., [d,e,f|g,h] workflow_part = workflow_part.strip() @@ -59,7 +59,7 @@ class BaseWorkflow(object): # Split the part by '|' to identify potential parallel task groups. line_split = [x.strip() for x in workflow_part.split("|") if x] - + # Skip if no valid tasks are identified after splitting. if len(line_split) <= 0: continue @@ -75,7 +75,7 @@ class BaseWorkflow(object): # add workers for sub_item in sub_split: self.worker_dict[sub_item] = is_multi_thread - + # Append the parsed and structured tasks to the workflow execution plan. self.workflow_worker_list.append(line_split_split) @@ -162,7 +162,7 @@ class BaseWorkflow(object): """ with Timer(f"workflow.{self.name}", time_log_type="wrap"): self.context[WORKFLOW_NAME] = self.name - + # Iterate over each part of the workflow for workflow_part in self.workflow_worker_list: # Sequential execution for single-item parts @@ -175,7 +175,7 @@ class BaseWorkflow(object): # Submit tasks to the thread pool for sub_workflow in workflow_part: t_list.append(G_CONTEXT.thread_pool.submit(self._run_sub_workflow, sub_workflow)) - + # Check results; if any task returns False, stop the workflow flag = True for future in as_completed(t_list): @@ -183,4 +183,3 @@ class BaseWorkflow(object): flag = False if not flag: break - diff --git a/memory_scope/memory/worker/frontend/extract_time_worker.yaml b/memory_scope/memory/worker/frontend/extract_time_worker.yaml index b44e8bbf..854509f1 100644 --- a/memory_scope/memory/worker/frontend/extract_time_worker.yaml +++ b/memory_scope/memory/worker/frontend/extract_time_worker.yaml @@ -4,7 +4,7 @@ extract_time_system: en: | Instructions: From the sentences and the time when they occurred, infer and extract the time periods indicated in the content of the sentences. Answer with the most complete time periods possible. The format of the answers must strictly adhere to the specifications in the examples provided. If the sentence does not involve time, respond with "none." - + extract_time_few_shot: cn: | @@ -104,7 +104,7 @@ extract_time_few_shot: Time: January 23, 2015, 4th week of 2015, Thursday, 7:38:0. Answer: None - + extract_time_user_query: cn: | diff --git a/memory_scope/memory/worker/frontend/fuse_rerank_worker.py b/memory_scope/memory/worker/frontend/fuse_rerank_worker.py index 8dcaf5a9..da76d684 100644 --- a/memory_scope/memory/worker/frontend/fuse_rerank_worker.py +++ b/memory_scope/memory/worker/frontend/fuse_rerank_worker.py @@ -57,7 +57,7 @@ class FuseRerankWorker(MemoryBaseWorker): # Parse input parameters from the worker's context extract_time_dict: Dict[str, str] = self.get_context(EXTRACT_TIME_DICT) memory_node_list: List[MemoryNode] = self.memory_handler.get_memories(RANKED_MEMORY_NODES) - + # Check if memory nodes are available; warn and return if not if not memory_node_list: self.logger.warning("Ranked memory nodes list is empty.") @@ -69,16 +69,16 @@ class FuseRerankWorker(MemoryBaseWorker): # Skip nodes below the fuse score threshold if node.score_rank < self.fuse_score_threshold: continue - + # Calculate type-based adjustment factor if node.memory_type not in self.fuse_ratio_dict: self.logger.warning(f"{node.memory_type} 'factor is not configured!") type_ratio: float = self.fuse_ratio_dict.get(node.memory_type, 0.1) - + # Determine time relevance adjustment factor match_event_flag, match_msg_flag = self.match_node_time(extract_time_dict=extract_time_dict, node=node) fuse_time_ratio: float = self.fuse_time_ratio if match_event_flag or match_msg_flag else 1.0 - + # Apply reranking score adjustments node.score_rerank = node.score_rank * type_ratio * fuse_time_ratio reranked_memory_nodes.append(node) @@ -89,7 +89,6 @@ class FuseRerankWorker(MemoryBaseWorker): key=lambda x: x.score_rerank, reverse=True)[: self.fuse_rerank_top_k] for i, node in enumerate(reranked_memory_nodes): - # Log reranking details including flags for event and message matches self.logger.info(f"Rerank Stage: Content={node.content}, Score={node.score_rerank}, " f"Event Flag={node.meta_data["match_event_flag"]}, " diff --git a/memory_scope/memory/worker/frontend/semantic_rank_worker.py b/memory_scope/memory/worker/frontend/semantic_rank_worker.py index a7e4dcfd..32f5d8b9 100644 --- a/memory_scope/memory/worker/frontend/semantic_rank_worker.py +++ b/memory_scope/memory/worker/frontend/semantic_rank_worker.py @@ -51,7 +51,7 @@ class SemanticRankWorker(MemoryBaseWorker): # sort by score memory_node_list = sorted(memory_node_list, key=lambda n: n.score_rank, reverse=True) - + # log ranked nodes for node in memory_node_list: self.logger.info(f"Rank stage: Content={node.content}, Score={node.score_rank}") diff --git a/memory_scope/memory/worker/summary/update_insight_worker.py b/memory_scope/memory/worker/summary/update_insight_worker.py index 57df7bab..61505da1 100644 --- a/memory_scope/memory/worker/summary/update_insight_worker.py +++ b/memory_scope/memory/worker/summary/update_insight_worker.py @@ -199,4 +199,3 @@ class UpdateInsightWorker(MemoryBaseWorker): for node in not_reflected_nodes: node.obs_updated = 1 node.action_status = ActionStatusEnum.MODIFIED - diff --git a/memory_scope/memory/worker/write/get_observation_with_time_worker.py b/memory_scope/memory/worker/write/get_observation_with_time_worker.py index 5e1a3459..cb08a7c9 100644 --- a/memory_scope/memory/worker/write/get_observation_with_time_worker.py +++ b/memory_scope/memory/worker/write/get_observation_with_time_worker.py @@ -1,4 +1,5 @@ from typing import List + from memory_scope.constants.common_constants import NEW_OBS_WITH_TIME_NODES from memory_scope.constants.language_constants import COLON_WORD from memory_scope.memory.worker.write.get_observation_worker import GetObservationWorker @@ -54,10 +55,10 @@ class GetObservationWithTimeWorker(GetObservationWorker): # Construct the system prompt with the count of observations system_prompt = self.prompt_handler.get_observation_with_time_system.format(num_obs=len(user_query_list), user_name=self.target_name) - + # Retrieve the few-shot examples for the prompt few_shot = self.prompt_handler.get_observation_with_time_few_shot.format(user_name=self.target_name) - + # Format the user query section with the concatenated list of timestamped queries user_query = self.prompt_handler.get_observation_with_time_user_query.format( user_query="\n".join(user_query_list), @@ -65,9 +66,9 @@ class GetObservationWithTimeWorker(GetObservationWorker): # Assemble the final message for observation retrieval obtain_obs_message = prompt_to_msg(system_prompt=system_prompt, few_shot=few_shot, user_query=user_query) - + # Log the constructed message for debugging purposes self.logger.info(f"obtain_obs_message={obtain_obs_message}") - + # Return the newly created message return obtain_obs_message diff --git a/memory_scope/memory/worker/write/get_observation_worker.py b/memory_scope/memory/worker/write/get_observation_worker.py index f04738c9..ae9dcf43 100644 --- a/memory_scope/memory/worker/write/get_observation_worker.py +++ b/memory_scope/memory/worker/write/get_observation_worker.py @@ -70,20 +70,20 @@ class GetObservationWorker(MemoryBaseWorker): # Format the system prompt with the number of observations and target name system_prompt = self.prompt_handler.get_observation_system.format(num_obs=len(user_query_list), user_name=self.target_name) - + # Incorporate few-shot examples into the prompt with the target name few_shot = self.prompt_handler.get_observation_few_shot.format(user_name=self.target_name) - + # Assemble the user query part of the prompt with the list of formatted user queries user_query = self.prompt_handler.get_observation_user_query.format(user_query="\n".join(user_query_list), user_name=self.target_name) # Combine system prompt, few-shot, and user query into a single message for obtaining observations obtain_obs_message = prompt_to_msg(system_prompt=system_prompt, few_shot=few_shot, user_query=user_query) - + # Log the constructed observation message self.logger.info(f"obtain_obs_message={obtain_obs_message}") - + # Return the processed message(s) for further steps in the observation workflow return obtain_obs_message diff --git a/memory_scope/memory/worker/write/info_filter_worker.yaml b/memory_scope/memory/worker/write/info_filter_worker.yaml index 1cf5a4e7..9b8462d6 100644 --- a/memory_scope/memory/worker/write/info_filter_worker.yaml +++ b/memory_scope/memory/worker/write/info_filter_worker.yaml @@ -14,7 +14,7 @@ info_filter_system: Please ensure to output in the following format, and the final result must be enclosed in <>: Thought: The basis and process of thinking, within 30 words. Result: - + info_filter_few_shot: cn: | diff --git a/memory_scope/models/llama_index_embedding_model.py b/memory_scope/models/llama_index_embedding_model.py index eaa51b49..ddd1e029 100644 --- a/memory_scope/models/llama_index_embedding_model.py +++ b/memory_scope/models/llama_index_embedding_model.py @@ -24,7 +24,7 @@ class LlamaIndexEmbeddingModel(BaseModel): model_class (type): The class of the model to register. """ MODEL_REGISTRY.register(model_name, model_class) - + MODEL_REGISTRY.register("dashscope_embedding", DashScopeEmbedding) def before_call(self, **kwargs): diff --git a/memory_scope/models/llama_index_generation_model.py b/memory_scope/models/llama_index_generation_model.py index cebd9f85..35e6e266 100644 --- a/memory_scope/models/llama_index_generation_model.py +++ b/memory_scope/models/llama_index_generation_model.py @@ -63,6 +63,7 @@ class LlamaIndexGenerationModel(BaseModel): model_response.message.content += response.delta model_response.delta = response.delta yield model_response + return gen() else: if isinstance(call_result, CompletionResponse): @@ -113,6 +114,6 @@ class LlamaIndexGenerationModel(BaseModel): response = await self.model.acomplete(**self.data) else: response = await self.model.achat(**self.data) - + results.raw = response return results diff --git a/memory_scope/utils/global_context.py b/memory_scope/utils/global_context.py index 807ae4f5..7c0a3b5d 100644 --- a/memory_scope/utils/global_context.py +++ b/memory_scope/utils/global_context.py @@ -5,8 +5,8 @@ from memory_scope.chat.base_memory_chat import BaseMemoryChat from memory_scope.enumeration.language_enum import LanguageEnum from memory_scope.memory.service.base_memory_service import BaseMemoryService from memory_scope.models.base_model import BaseModel -from memory_scope.storage.base_monitor import BaseMonitor from memory_scope.storage.base_memory_store import BaseMemoryStore +from memory_scope.storage.base_monitor import BaseMonitor class GlobalContext(object): diff --git a/memory_scope/utils/response_text_parser.py b/memory_scope/utils/response_text_parser.py index fb2ca139..00e4d947 100644 --- a/memory_scope/utils/response_text_parser.py +++ b/memory_scope/utils/response_text_parser.py @@ -1,8 +1,8 @@ import re -from memory_scope.utils.logger import Logger from memory_scope.constants.language_constants import NONE_WORD from memory_scope.utils.global_context import G_CONTEXT +from memory_scope.utils.logger import Logger class ResponseTextParser(object): diff --git a/memory_scope/utils/tool_functions.py b/memory_scope/utils/tool_functions.py index a3571fb5..20403ece 100644 --- a/memory_scope/utils/tool_functions.py +++ b/memory_scope/utils/tool_functions.py @@ -102,7 +102,8 @@ def prompt_to_msg(system_prompt: str, few_shot: str, user_query: str) -> List[Me return [ Message(role=MessageRoleEnum.SYSTEM.value, content=system_prompt.strip()), # System message Message(role=MessageRoleEnum.USER.value, - content="\n".join([x.strip() for x in [few_shot, system_prompt, user_query]])) # User message combining few shot, system prompt, and user query + content="\n".join([x.strip() for x in [few_shot, system_prompt, user_query]])) + # User message combining few shot, system prompt, and user query ] diff --git a/tests/llama_index_es_memory_store.py b/tests/llama_index_es_memory_store.py index de48ad29..ffec851f 100644 --- a/tests/llama_index_es_memory_store.py +++ b/tests/llama_index_es_memory_store.py @@ -1,12 +1,10 @@ -import ray - from typing import Dict, List, Any, Optional, cast +import ray from llama_index.core import VectorStoreIndex from llama_index.core.schema import TextNode, NodeWithScore from llama_index.vector_stores.elasticsearch import ElasticsearchStore, AsyncDenseVectorStrategy -from memory_scope.enumeration.action_status_enum import ActionStatusEnum from memory_scope.models.base_model import BaseModel from memory_scope.scheme.memory_node import MemoryNode from memory_scope.storage.base_memory_store import BaseMemoryStore diff --git a/tests/models/test_models_lli_rank.py b/tests/models/test_models_lli_rank.py index dc50046a..7c5efa7e 100644 --- a/tests/models/test_models_lli_rank.py +++ b/tests/models/test_models_lli_rank.py @@ -1,5 +1,6 @@ -import unittest import asyncio +import unittest + from memory_scope.models.llama_index_rank_model import LlamaIndexRankModel diff --git a/tests/storages/test_storages_lli_synces.py b/tests/storages/test_storages_lli_synces.py index 1d0aa297..d7dd124f 100644 --- a/tests/storages/test_storages_lli_synces.py +++ b/tests/storages/test_storages_lli_synces.py @@ -1,8 +1,9 @@ import unittest +from memory_scope.storage.llama_index_es_memory_store_sync import LlamaIndexEsMemoryStoreSync + from memory_scope.models.llama_index_embedding_model import LlamaIndexEmbeddingModel from memory_scope.scheme.memory_node import MemoryNode -from memory_scope.storage.llama_index_es_memory_store_sync import LlamaIndexEsMemoryStoreSync class TestLlamaIndexElasticSearchStore(unittest.TestCase):