code format

This commit is contained in:
jinli.yl 2024-07-14 18:27:49 +08:00
commit 0d3463742b
20 changed files with 54 additions and 62 deletions

View file

@ -1,12 +1,6 @@
[flake8]
exclude =
scripts/*
src/agentscope/rpc/*
max-line-length = 120
inline-quotes = "
avoid-escape = no
ignore =
F401
F403
W503
E731
ignore =

View file

@ -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, . ]

View file

@ -2,10 +2,8 @@
# ModelScope
## 概念解释
- service: 在顶层的交互对象用于定义operation的使用范围
- operation: 读写记忆等对于记忆的操作方法是worker的有序组合workflow

View file

@ -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 = ""):

View file

@ -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

View file

@ -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: |

View file

@ -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"]}, "

View file

@ -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}")

View file

@ -199,4 +199,3 @@ class UpdateInsightWorker(MemoryBaseWorker):
for node in not_reflected_nodes:
node.obs_updated = 1
node.action_status = ActionStatusEnum.MODIFIED

View file

@ -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

View file

@ -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

View file

@ -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: <Score: 0 or 1 or 2 or 3>
info_filter_few_shot:
cn: |

View file

@ -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):

View file

@ -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

View file

@ -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):

View file

@ -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):

View file

@ -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
]

View file

@ -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

View file

@ -1,5 +1,6 @@
import unittest
import asyncio
import unittest
from memory_scope.models.llama_index_rank_model import LlamaIndexRankModel

View file

@ -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):