Kevin Hu
commited on
Commit
·
85d3c0e
1
Parent(s):
0bb19dd
Add timestamp to messages (#4624)
Browse files### What problem does this PR solve?
### Type of change
- [x] New Feature (non-breaking change which adds functionality)
api/db/services/canvas_service.py
CHANGED
|
@@ -14,6 +14,7 @@
|
|
| 14 |
# limitations under the License.
|
| 15 |
#
|
| 16 |
import json
|
|
|
|
| 17 |
import traceback
|
| 18 |
from uuid import uuid4
|
| 19 |
from agent.canvas import Canvas
|
|
@@ -80,7 +81,7 @@ def completion(tenant_id, agent_id, question, session_id=None, stream=True, **kw
|
|
| 80 |
"id": session_id,
|
| 81 |
"dialog_id": cvs.id,
|
| 82 |
"user_id": kwargs.get("user_id", "") if isinstance(kwargs, dict) else "",
|
| 83 |
-
"message": [{"role": "assistant", "content": canvas.get_prologue()}],
|
| 84 |
"source": "agent",
|
| 85 |
"dsl": cvs.dsl
|
| 86 |
}
|
|
@@ -134,7 +135,7 @@ def completion(tenant_id, agent_id, question, session_id=None, stream=True, **kw
|
|
| 134 |
yield "data:" + json.dumps({"code": 0, "message": "", "data": ans},
|
| 135 |
ensure_ascii=False) + "\n\n"
|
| 136 |
|
| 137 |
-
canvas.messages.append({"role": "assistant", "content": final_ans["content"], "id": message_id})
|
| 138 |
canvas.history.append(("assistant", final_ans["content"]))
|
| 139 |
if final_ans.get("reference"):
|
| 140 |
canvas.reference.append(final_ans["reference"])
|
|
|
|
| 14 |
# limitations under the License.
|
| 15 |
#
|
| 16 |
import json
|
| 17 |
+
import time
|
| 18 |
import traceback
|
| 19 |
from uuid import uuid4
|
| 20 |
from agent.canvas import Canvas
|
|
|
|
| 81 |
"id": session_id,
|
| 82 |
"dialog_id": cvs.id,
|
| 83 |
"user_id": kwargs.get("user_id", "") if isinstance(kwargs, dict) else "",
|
| 84 |
+
"message": [{"role": "assistant", "content": canvas.get_prologue(), "created_at": time.time()}],
|
| 85 |
"source": "agent",
|
| 86 |
"dsl": cvs.dsl
|
| 87 |
}
|
|
|
|
| 135 |
yield "data:" + json.dumps({"code": 0, "message": "", "data": ans},
|
| 136 |
ensure_ascii=False) + "\n\n"
|
| 137 |
|
| 138 |
+
canvas.messages.append({"role": "assistant", "content": final_ans["content"], "created_at": time.time(), "id": message_id})
|
| 139 |
canvas.history.append(("assistant", final_ans["content"]))
|
| 140 |
if final_ans.get("reference"):
|
| 141 |
canvas.reference.append(final_ans["reference"])
|
api/db/services/conversation_service.py
CHANGED
|
@@ -13,6 +13,7 @@
|
|
| 13 |
# See the License for the specific language governing permissions and
|
| 14 |
# limitations under the License.
|
| 15 |
#
|
|
|
|
| 16 |
from uuid import uuid4
|
| 17 |
from api.db import StatusEnum
|
| 18 |
from api.db.db_models import Conversation, DB
|
|
@@ -75,9 +76,9 @@ def structure_answer(conv, ans, message_id, session_id):
|
|
| 75 |
if not conv.message:
|
| 76 |
conv.message = []
|
| 77 |
if not conv.message or conv.message[-1].get("role", "") != "assistant":
|
| 78 |
-
conv.message.append({"role": "assistant", "content": ans["answer"], "id": message_id})
|
| 79 |
else:
|
| 80 |
-
conv.message[-1] = {"role": "assistant", "content": ans["answer"], "id": message_id}
|
| 81 |
if conv.reference:
|
| 82 |
conv.reference[-1] = reference
|
| 83 |
return ans
|
|
@@ -94,7 +95,7 @@ def completion(tenant_id, chat_id, question, name="New session", session_id=None
|
|
| 94 |
"id": session_id,
|
| 95 |
"dialog_id": chat_id,
|
| 96 |
"name": name,
|
| 97 |
-
"message": [{"role": "assistant", "content": dia[0].prompt_config.get("prologue")}],
|
| 98 |
"user_id": kwargs.get("user_id", "")
|
| 99 |
}
|
| 100 |
ConversationService.save(**conv)
|
|
@@ -166,7 +167,7 @@ def iframe_completion(dialog_id, question, session_id=None, stream=True, **kwarg
|
|
| 166 |
"id": session_id,
|
| 167 |
"dialog_id": dialog_id,
|
| 168 |
"user_id": kwargs.get("user_id", ""),
|
| 169 |
-
"message": [{"role": "assistant", "content": dia.prompt_config["prologue"]}]
|
| 170 |
}
|
| 171 |
API4ConversationService.save(**conv)
|
| 172 |
yield "data:" + json.dumps({"code": 0, "message": "",
|
|
|
|
| 13 |
# See the License for the specific language governing permissions and
|
| 14 |
# limitations under the License.
|
| 15 |
#
|
| 16 |
+
import time
|
| 17 |
from uuid import uuid4
|
| 18 |
from api.db import StatusEnum
|
| 19 |
from api.db.db_models import Conversation, DB
|
|
|
|
| 76 |
if not conv.message:
|
| 77 |
conv.message = []
|
| 78 |
if not conv.message or conv.message[-1].get("role", "") != "assistant":
|
| 79 |
+
conv.message.append({"role": "assistant", "content": ans["answer"], "created_at": time.time(), "id": message_id})
|
| 80 |
else:
|
| 81 |
+
conv.message[-1] = {"role": "assistant", "content": ans["answer"], "created_at": time.time(), "id": message_id}
|
| 82 |
if conv.reference:
|
| 83 |
conv.reference[-1] = reference
|
| 84 |
return ans
|
|
|
|
| 95 |
"id": session_id,
|
| 96 |
"dialog_id": chat_id,
|
| 97 |
"name": name,
|
| 98 |
+
"message": [{"role": "assistant", "content": dia[0].prompt_config.get("prologue"), "created_at": time.time()}],
|
| 99 |
"user_id": kwargs.get("user_id", "")
|
| 100 |
}
|
| 101 |
ConversationService.save(**conv)
|
|
|
|
| 167 |
"id": session_id,
|
| 168 |
"dialog_id": dialog_id,
|
| 169 |
"user_id": kwargs.get("user_id", ""),
|
| 170 |
+
"message": [{"role": "assistant", "content": dia.prompt_config["prologue"], "created_at": time.time()}]
|
| 171 |
}
|
| 172 |
API4ConversationService.save(**conv)
|
| 173 |
yield "data:" + json.dumps({"code": 0, "message": "",
|