Skip to content

Commit a4ee39d

Browse files
committed
[features][add utils]
1 parent 3901a71 commit a4ee39d

File tree

5 files changed

+126
-8
lines changed

5 files changed

+126
-8
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exclude tests/*

docker-compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ services:
129129
- 11434:11434
130130
volumes:
131131
# - //d/models/ollama:/root/.ollama # windows path
132-
- /Users/yunjiu/ant/models:/root/.ollama # linux/mac path
132+
- /Users/wangyunpeng/Downloads/models:/root/.ollama # linux/mac path
133133
networks:
134134
- ekg-net
135135
restart: on-failure
@@ -170,4 +170,4 @@ services:
170170
networks:
171171
ekg-net:
172172
# driver: bridge
173-
external: true
173+
# external: true

muagent/service/ekg_construct/ekg_construct_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ def update_edges(self, edges: List[GEdge], teamid: str):
608608
gb_result = []
609609
for edge in edges:
610610
# if node.id not in update_tbase_nodeids: continue
611-
SRCID = edge.attributes.pop("SRCID", None) or double_hashing(edge.start_id)
611+
SRCID = edge.attributes.pop("SRCID", None) or double_hashing(edge.start_id) # todo bug,数据不一致问题
612612
DSTID = edge.attributes.pop("DSTID", None) or double_hashing(edge.end_id)
613613
resp = self.gb.update_edge(
614614
SRCID, DSTID,
@@ -699,7 +699,7 @@ def search_nodes_by_text(self, text: str, node_type: str = None, teamid: str = N
699699
# tmp iead to filter by teamid
700700
nodes = [node for node in nodes if str(teamid) in str(node.attributes)]
701701
# select the node which can connect the rootid
702-
nodes = [node for node in nodes if len(self.search_rootpath_by_nodeid(node.id, node.type, f"ekg_team:{teamid}").paths)>0]
702+
nodes = [node for node in nodes if len(self.search_rootpath_by_nodeid(node.id, node.type, f"ekg_team_{teamid}").paths)>0]
703703
return nodes
704704

705705
def search_rootpath_by_nodeid(self, nodeid: str, node_type: str, rootid: str) -> Graph:

muagent/utils/common_utils.py

Lines changed: 118 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import textwrap, time, copy, random, hashlib, json, os
1+
import time, hashlib, json, os
22
from datetime import datetime, timedelta
33
from functools import wraps
44
from loguru import logger
55
from typing import *
66
from pathlib import Path
77
from io import BytesIO
8-
from fastapi import Body, File, Form, Body, Query, UploadFile
8+
from fastapi import UploadFile
99
from tempfile import SpooledTemporaryFile
1010
import json
11+
import signal
12+
import contextlib
13+
import sys
14+
import socket
1115

1216

1317
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
@@ -124,4 +128,115 @@ def string_to_long_sha256(s: str) -> int:
124128
def double_hashing(s: str, modulus: int = 10e12) -> int:
125129
hash1 = string_to_long_sha256(s)
126130
hash2 = string_to_long_sha256(s[::-1]) # 用字符串的反序进行第二次hash
127-
return int((hash1 + hash2) % modulus)
131+
return int((hash1 + hash2) % modulus)
132+
133+
134+
@contextlib.contextmanager
135+
def timer(seconds: Optional[Union[int, float]] = None) -> Generator:
136+
"""
137+
A context manager that limits the execution time of a code block to a
138+
given number of seconds.
139+
The implementation of this contextmanager are borrowed from
140+
https://github.com/openai/human-eval/blob/master/human_eval/execution.py
141+
142+
Note:
143+
This function only works in Unix and MainThread,
144+
since `signal.setitimer` is only available in Unix.
145+
146+
"""
147+
if (
148+
seconds is None
149+
or sys.platform == "win32"
150+
or threading.currentThread().name # pylint: disable=W4902
151+
!= "MainThread"
152+
):
153+
yield
154+
return
155+
156+
def signal_handler(*args: Any, **kwargs: Any) -> None:
157+
raise TimeoutError("timed out")
158+
159+
signal.setitimer(signal.ITIMER_REAL, seconds)
160+
signal.signal(signal.SIGALRM, signal_handler)
161+
162+
try:
163+
# Enter the context and execute the code block.
164+
yield
165+
finally:
166+
signal.setitimer(signal.ITIMER_REAL, 0)
167+
168+
169+
class ImportErrorReporter:
170+
"""Used as a placeholder for missing packages.
171+
When called, an ImportError will be raised, prompting the user to install
172+
the specified extras requirement.
173+
The implementation of this ImportErrorReporter are borrowed from
174+
https://github.com/modelscope/agentscope/src/agentscope/utils/common.py
175+
"""
176+
177+
def __init__(self, error: ImportError, extras_require: str = None) -> None:
178+
"""Init the ImportErrorReporter.
179+
180+
Args:
181+
error (`ImportError`): the original ImportError.
182+
extras_require (`str`): the extras requirement.
183+
"""
184+
self.error = error
185+
self.extras_require = extras_require
186+
187+
def __call__(self, *args: Any, **kwds: Any) -> Any:
188+
return self._raise_import_error()
189+
190+
def __getattr__(self, name: str) -> Any:
191+
return self._raise_import_error()
192+
193+
def __getitem__(self, __key: Any) -> Any:
194+
return self._raise_import_error()
195+
196+
def _raise_import_error(self) -> Any:
197+
"""Raise the ImportError"""
198+
err_msg = f"ImportError occorred: [{self.error.msg}]."
199+
if self.extras_require is not None:
200+
err_msg += (
201+
f" Please install [{self.extras_require}] version"
202+
" of agentscope."
203+
)
204+
raise ImportError(err_msg)
205+
206+
207+
def _find_available_port() -> int:
208+
"""
209+
Get an unoccupied socket port number.
210+
The implementation of this _find_available_port are borrowed from
211+
https://github.com/modelscope/agentscope/src/agentscope/utils/common.py
212+
"""
213+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
214+
s.bind(("", 0))
215+
return s.getsockname()[1]
216+
217+
218+
def _check_port(port: Optional[int] = None) -> int:
219+
"""Check if the port is available.
220+
The implementation of this _check_port are borrowed from
221+
https://github.com/modelscope/agentscope/src/agentscope/utils/common.py
222+
223+
Args:
224+
port (`int`):
225+
the port number being checked.
226+
227+
Returns:
228+
`int`: the port number that passed the check. If the port is found
229+
to be occupied, an available port number will be automatically
230+
returned.
231+
"""
232+
if port is None:
233+
new_port = _find_available_port()
234+
return new_port
235+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
236+
try:
237+
if s.connect_ex(("localhost", port)) == 0:
238+
raise RuntimeError("Port is occupied.")
239+
except Exception:
240+
new_port = _find_available_port()
241+
return new_port
242+
return port

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="codefuse-muagent",
8-
version="0.0.5",
8+
version="0.1.0",
99
author="shanshi",
1010
author_email="wyp311395@antgroup.com",
1111
description="A multi-agent framework that facilitates the rapid construction of collaborative teams of agents.",
@@ -35,6 +35,8 @@
3535
"notebook",
3636
"docker",
3737
"sseclient",
38+
"Levenshtein",
39+
"urllib3==1.26.6",
3840
#
3941
"chromadb==0.4.17",
4042
"javalang==0.13.0",

0 commit comments

Comments
 (0)