Kevin Hu
commited on
Commit
·
94d909f
1
Parent(s):
4d88c77
Fix misspell. (#4219)
Browse files### What problem does this PR solve?
#4216
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- api/apps/sdk/session.py +33 -35
api/apps/sdk/session.py
CHANGED
@@ -71,7 +71,7 @@ def create_agent_session(tenant_id, agent_id):
|
|
71 |
if not e:
|
72 |
return get_error_data_result("Agent not found.")
|
73 |
|
74 |
-
if not UserCanvasService.query(user_id=tenant_id,id=agent_id):
|
75 |
return get_error_data_result("You cannot access the agent.")
|
76 |
|
77 |
if not isinstance(cvs.dsl, str):
|
@@ -96,7 +96,7 @@ def create_agent_session(tenant_id, agent_id):
|
|
96 |
conv = {
|
97 |
"id": get_uuid(),
|
98 |
"dialog_id": cvs.id,
|
99 |
-
"user_id": req.get("
|
100 |
"message": [{"role": "assistant", "content": canvas.get_prologue()}],
|
101 |
"source": "agent",
|
102 |
"dsl": cvs.dsl
|
@@ -133,11 +133,11 @@ def update(tenant_id, chat_id, session_id):
|
|
133 |
def chat_completion(tenant_id, chat_id):
|
134 |
req = request.json
|
135 |
if not req or not req.get("session_id"):
|
136 |
-
req = {"question":""}
|
137 |
-
if not DialogService.query(tenant_id=tenant_id,id=chat_id,status=StatusEnum.VALID.value):
|
138 |
return get_error_data_result(f"You don't own the chat {chat_id}")
|
139 |
if req.get("session_id"):
|
140 |
-
if not ConversationService.query(id=req["session_id"],dialog_id=chat_id):
|
141 |
return get_error_data_result(f"You don't own the session {req['session_id']}")
|
142 |
if req.get("stream", True):
|
143 |
resp = Response(rag_completion(tenant_id, chat_id, **req), mimetype="text/event-stream")
|
@@ -158,34 +158,34 @@ def chat_completion(tenant_id, chat_id):
|
|
158 |
@manager.route('/agents/<agent_id>/completions', methods=['POST']) # noqa: F821
|
159 |
@token_required
|
160 |
def agent_completions(tenant_id, agent_id):
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
|
190 |
|
191 |
@manager.route('/chats/<chat_id>/sessions', methods=['GET']) # noqa: F821
|
@@ -447,5 +447,3 @@ def agent_bot_completions(agent_id):
|
|
447 |
|
448 |
for answer in agent_completion(objs[0].tenant_id, agent_id, **req):
|
449 |
return get_result(data=answer)
|
450 |
-
|
451 |
-
|
|
|
71 |
if not e:
|
72 |
return get_error_data_result("Agent not found.")
|
73 |
|
74 |
+
if not UserCanvasService.query(user_id=tenant_id, id=agent_id):
|
75 |
return get_error_data_result("You cannot access the agent.")
|
76 |
|
77 |
if not isinstance(cvs.dsl, str):
|
|
|
96 |
conv = {
|
97 |
"id": get_uuid(),
|
98 |
"dialog_id": cvs.id,
|
99 |
+
"user_id": req.get("user_id", "") if isinstance(req, dict) else "",
|
100 |
"message": [{"role": "assistant", "content": canvas.get_prologue()}],
|
101 |
"source": "agent",
|
102 |
"dsl": cvs.dsl
|
|
|
133 |
def chat_completion(tenant_id, chat_id):
|
134 |
req = request.json
|
135 |
if not req or not req.get("session_id"):
|
136 |
+
req = {"question": ""}
|
137 |
+
if not DialogService.query(tenant_id=tenant_id, id=chat_id, status=StatusEnum.VALID.value):
|
138 |
return get_error_data_result(f"You don't own the chat {chat_id}")
|
139 |
if req.get("session_id"):
|
140 |
+
if not ConversationService.query(id=req["session_id"], dialog_id=chat_id):
|
141 |
return get_error_data_result(f"You don't own the session {req['session_id']}")
|
142 |
if req.get("stream", True):
|
143 |
resp = Response(rag_completion(tenant_id, chat_id, **req), mimetype="text/event-stream")
|
|
|
158 |
@manager.route('/agents/<agent_id>/completions', methods=['POST']) # noqa: F821
|
159 |
@token_required
|
160 |
def agent_completions(tenant_id, agent_id):
|
161 |
+
req = request.json
|
162 |
+
cvs = UserCanvasService.query(user_id=tenant_id, id=agent_id)
|
163 |
+
if not cvs:
|
164 |
+
return get_error_data_result(f"You don't own the agent {agent_id}")
|
165 |
+
if req.get("session_id"):
|
166 |
+
dsl = cvs[0].dsl
|
167 |
+
if not isinstance(dsl, str):
|
168 |
+
dsl = json.dumps(dsl)
|
169 |
+
canvas = Canvas(dsl, tenant_id)
|
170 |
+
if canvas.get_preset_param():
|
171 |
+
req["question"] = ""
|
172 |
+
conv = API4ConversationService.query(id=req["session_id"], dialog_id=agent_id)
|
173 |
+
if not conv:
|
174 |
+
return get_error_data_result(f"You don't own the session {req['session_id']}")
|
175 |
+
else:
|
176 |
+
req["question"] = ""
|
177 |
+
if req.get("stream", True):
|
178 |
+
resp = Response(agent_completion(tenant_id, agent_id, **req), mimetype="text/event-stream")
|
179 |
+
resp.headers.add_header("Cache-control", "no-cache")
|
180 |
+
resp.headers.add_header("Connection", "keep-alive")
|
181 |
+
resp.headers.add_header("X-Accel-Buffering", "no")
|
182 |
+
resp.headers.add_header("Content-Type", "text/event-stream; charset=utf-8")
|
183 |
+
return resp
|
184 |
+
try:
|
185 |
+
for answer in agent_completion(tenant_id, agent_id, **req):
|
186 |
+
return get_result(data=answer)
|
187 |
+
except Exception as e:
|
188 |
+
return get_error_data_result(str(e))
|
189 |
|
190 |
|
191 |
@manager.route('/chats/<chat_id>/sessions', methods=['GET']) # noqa: F821
|
|
|
447 |
|
448 |
for answer in agent_completion(objs[0].tenant_id, agent_id, **req):
|
449 |
return get_result(data=answer)
|
|
|
|