From b02c7b271f0d846b7655818f55a7be1a46db343e Mon Sep 17 00:00:00 2001 From: "panxuchen.pxc" Date: Tue, 21 May 2024 20:22:25 +0800 Subject: [PATCH] add more fields into message --- src/agentscope/agents/agent.py | 10 ++++++++-- src/agentscope/agents/user_agent.py | 2 ++ src/agentscope/web/_app.py | 12 ++++++++++++ src/agentscope/web/client.py | 10 ++++++++-- 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/agentscope/agents/agent.py b/src/agentscope/agents/agent.py index 080632da0..9e60538b7 100644 --- a/src/agentscope/agents/agent.py +++ b/src/agentscope/agents/agent.py @@ -310,14 +310,20 @@ def speak( if isinstance(content, dict): _runtime.studio_client.send_message( name=content.get("name", ""), + role=content.get("role", "assistant"), content=content.get("content", ""), - url=content.get("url", ""), + metadata=content.get("metadata", {}), + url=content.get("url", None), + timestamp=content.get("timestamp", None), ) else: _runtime.studio_client.send_message( name="", + role="assistant", content=str(content), - url="", + metadata={}, + url=None, + timestamp=None, ) def observe(self, x: Union[dict, Sequence[dict]]) -> None: diff --git a/src/agentscope/agents/user_agent.py b/src/agentscope/agents/user_agent.py index fef577df9..171766ef0 100644 --- a/src/agentscope/agents/user_agent.py +++ b/src/agentscope/agents/user_agent.py @@ -82,6 +82,8 @@ def reply( ) raw_input = self.input_client.get_user_input() content = raw_input["content"] + url = None + kwargs = {} else: time.sleep(0.5) content = user_input(timeout=timeout) diff --git a/src/agentscope/web/_app.py b/src/agentscope/web/_app.py index d5384826b..82b580bd5 100644 --- a/src/agentscope/web/_app.py +++ b/src/agentscope/web/_app.py @@ -45,8 +45,11 @@ class Message(db.Model): # type: ignore[name-defined] id = db.Column(db.Integer, primary_key=True) run_id = db.Column(db.String, db.ForeignKey("run.id"), nullable=False) name = db.Column(db.String) + role = db.Column(db.String) content = db.Column(db.String) url = db.Column(db.String) + meta = db.Column(db.String) + timestamp = db.Column(db.String) def get_history_messages(run_id: str) -> list: @@ -55,8 +58,11 @@ def get_history_messages(run_id: str) -> list: return [ { "name": message.name, + "role": message.role, "content": message.content, "url": message.url, + "metadata": json.loads(message.meta), + "timestamp": message.timestamp, } for message in messages ] @@ -140,14 +146,20 @@ def put_message() -> Response: data = request.json run_id = data["run_id"] name = data["name"] + role = data["role"] content = data["content"] + metadata = json.dumps(data["metadata"]) + timestamp = data["timestamp"] url = data.get("url", None) try: new_message = Message( run_id=run_id, name=name, + role=role, content=content, + meta=metadata, url=url, + timestamp=timestamp, ) db.session.add(new_message) db.session.commit() diff --git a/src/agentscope/web/client.py b/src/agentscope/web/client.py index 6ecf1a92f..308687896 100644 --- a/src/agentscope/web/client.py +++ b/src/agentscope/web/client.py @@ -104,7 +104,7 @@ def register_run( }, timeout=10, # todo: configurable timeout ) - if resp == 200: + if resp.status_code == 200: return True else: logger.warning(f"Fail to register to studio: {resp}") @@ -113,7 +113,10 @@ def register_run( def send_message( self, name: str, + role: str, content: str, + timestamp: str = None, + metadata: dict = None, url: str = None, ) -> bool: """Send a message to the studio.""" @@ -123,8 +126,11 @@ def send_message( json={ "run_id": self.run_id, "name": name, + "role": role, "content": content, - "url": None, + "timestamp": timestamp, + "metadata": metadata, + "url": url, }, timeout=10, )