mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 12:32:30 +08:00
Fix: update broken create agent session due to v0.20.0 changes (#9445)
### What problem does this PR solve? Update broken create agent session due to v0.20.0 changes. #9383 **NOTE: A session ID is no longer required to interact with the agent.** See: #9241, #9309. ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue)
This commit is contained in:
@ -22,7 +22,7 @@ from flask import Response, jsonify, request
|
||||
|
||||
from agent.canvas import Canvas
|
||||
from api.db import LLMType, StatusEnum
|
||||
from api.db.db_models import APIToken
|
||||
from api.db.db_models import API4Conversation, APIToken
|
||||
from api.db.services.api_service import API4ConversationService
|
||||
from api.db.services.canvas_service import UserCanvasService, completionOpenAI
|
||||
from api.db.services.canvas_service import completion as agent_completion
|
||||
@ -69,11 +69,7 @@ def create(tenant_id, chat_id):
|
||||
@manager.route("/agents/<agent_id>/sessions", methods=["POST"]) # noqa: F821
|
||||
@token_required
|
||||
def create_agent_session(tenant_id, agent_id):
|
||||
req = request.json
|
||||
if not request.is_json:
|
||||
req = request.form
|
||||
files = request.files
|
||||
user_id = request.args.get("user_id", "")
|
||||
user_id = request.args.get("user_id", tenant_id)
|
||||
e, cvs = UserCanvasService.get_by_id(agent_id)
|
||||
if not e:
|
||||
return get_error_data_result("Agent not found.")
|
||||
@ -82,46 +78,21 @@ def create_agent_session(tenant_id, agent_id):
|
||||
if not isinstance(cvs.dsl, str):
|
||||
cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
|
||||
|
||||
canvas = Canvas(cvs.dsl, tenant_id)
|
||||
session_id=get_uuid()
|
||||
canvas = Canvas(cvs.dsl, tenant_id, agent_id)
|
||||
canvas.reset()
|
||||
query = canvas.get_preset_param()
|
||||
if query:
|
||||
for ele in query:
|
||||
if not ele["optional"]:
|
||||
if ele["type"] == "file":
|
||||
if files is None or not files.get(ele["key"]):
|
||||
return get_error_data_result(f"`{ele['key']}` with type `{ele['type']}` is required")
|
||||
upload_file = files.get(ele["key"])
|
||||
file_content = FileService.parse_docs([upload_file], user_id)
|
||||
file_name = upload_file.filename
|
||||
ele["value"] = file_name + "\n" + file_content
|
||||
else:
|
||||
if req is None or not req.get(ele["key"]):
|
||||
return get_error_data_result(f"`{ele['key']}` with type `{ele['type']}` is required")
|
||||
ele["value"] = req[ele["key"]]
|
||||
else:
|
||||
if ele["type"] == "file":
|
||||
if files is not None and files.get(ele["key"]):
|
||||
upload_file = files.get(ele["key"])
|
||||
file_content = FileService.parse_docs([upload_file], user_id)
|
||||
file_name = upload_file.filename
|
||||
ele["value"] = file_name + "\n" + file_content
|
||||
else:
|
||||
if "value" in ele:
|
||||
ele.pop("value")
|
||||
else:
|
||||
if req is not None and req.get(ele["key"]):
|
||||
ele["value"] = req[ele["key"]]
|
||||
else:
|
||||
if "value" in ele:
|
||||
ele.pop("value")
|
||||
|
||||
for ans in canvas.run(stream=False):
|
||||
pass
|
||||
conv = {
|
||||
"id": session_id,
|
||||
"dialog_id": cvs.id,
|
||||
"user_id": user_id,
|
||||
"message": [],
|
||||
"source": "agent",
|
||||
"dsl": cvs.dsl
|
||||
}
|
||||
API4ConversationService.save(**conv)
|
||||
|
||||
cvs.dsl = json.loads(str(canvas))
|
||||
conv = {"id": get_uuid(), "dialog_id": cvs.id, "user_id": user_id, "message": [{"role": "assistant", "content": canvas.get_prologue()}], "source": "agent", "dsl": cvs.dsl}
|
||||
API4ConversationService.save(**conv)
|
||||
conv = {"id": session_id, "dialog_id": cvs.id, "user_id": user_id, "message": [{"role": "assistant", "content": canvas.get_prologue()}], "source": "agent", "dsl": cvs.dsl}
|
||||
conv["agent_id"] = conv.pop("dialog_id")
|
||||
return get_result(data=conv)
|
||||
|
||||
|
||||
@ -2684,7 +2684,7 @@ Creates a session with an agent.
|
||||
- Method: POST
|
||||
- URL: `/api/v1/agents/{agent_id}/sessions?user_id={user_id}`
|
||||
- Headers:
|
||||
- `'content-Type: application/json' or 'multipart/form-data'`
|
||||
- `'content-Type: application/json'
|
||||
- `'Authorization: Bearer <YOUR_API_KEY>'`
|
||||
- Body:
|
||||
- the required parameters:`str`
|
||||
@ -2704,29 +2704,6 @@ curl --request POST \
|
||||
}'
|
||||
```
|
||||
|
||||
If the **Begin** component in your agent takes required parameters:
|
||||
|
||||
```bash
|
||||
curl --request POST \
|
||||
--url http://{address}/api/v1/agents/{agent_id}/sessions \
|
||||
--header 'Content-Type: application/json' \
|
||||
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
||||
--data '{
|
||||
"lang":"Japanese",
|
||||
"file":"Who are you"
|
||||
}'
|
||||
```
|
||||
|
||||
If the **Begin** component in your agent takes required file parameters:
|
||||
|
||||
```bash
|
||||
curl --request POST \
|
||||
--url http://{address}/api/v1/agents/{agent_id}/sessions?user_id={user_id} \
|
||||
--header 'Content-Type: multipart/form-data' \
|
||||
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
||||
--form '<FILE_KEY>=@./test1.png'
|
||||
```
|
||||
|
||||
##### Request parameters
|
||||
|
||||
- `agent_id`: (*Path parameter*)
|
||||
@ -2742,101 +2719,190 @@ Success:
|
||||
{
|
||||
"code": 0,
|
||||
"data": {
|
||||
"agent_id": "b4a39922b76611efaa1a0242ac120006",
|
||||
"agent_id": "dbb4ed366e8611f09690a55a6daec4ef",
|
||||
"dsl": {
|
||||
"answer": [],
|
||||
"components": {
|
||||
"Answer:GreenReadersDrum": {
|
||||
"Message:EightyJobsAsk": {
|
||||
"downstream": [],
|
||||
"obj": {
|
||||
"component_name": "Answer",
|
||||
"inputs": [],
|
||||
"output": null,
|
||||
"params": {}
|
||||
"component_name": "Message",
|
||||
"params": {
|
||||
"content": [
|
||||
"{begin@var1}{begin@var2}"
|
||||
],
|
||||
"debug_inputs": {},
|
||||
"delay_after_error": 2.0,
|
||||
"description": "",
|
||||
"exception_default_value": null,
|
||||
"exception_goto": null,
|
||||
"exception_method": null,
|
||||
"inputs": {},
|
||||
"max_retries": 0,
|
||||
"message_history_window_size": 22,
|
||||
"outputs": {
|
||||
"content": {
|
||||
"type": "str",
|
||||
"value": null
|
||||
}
|
||||
},
|
||||
"stream": true
|
||||
}
|
||||
},
|
||||
"upstream": []
|
||||
"upstream": [
|
||||
"begin"
|
||||
]
|
||||
},
|
||||
"begin": {
|
||||
"downstream": [],
|
||||
"downstream": [
|
||||
"Message:EightyJobsAsk"
|
||||
],
|
||||
"obj": {
|
||||
"component_name": "Begin",
|
||||
"inputs": [],
|
||||
"output": {},
|
||||
"params": {}
|
||||
"params": {
|
||||
"debug_inputs": {},
|
||||
"delay_after_error": 2.0,
|
||||
"description": "",
|
||||
"enablePrologue": true,
|
||||
"enable_tips": true,
|
||||
"exception_default_value": null,
|
||||
"exception_goto": null,
|
||||
"exception_method": null,
|
||||
"inputs": {
|
||||
"var1": {
|
||||
"name": "var1",
|
||||
"optional": false,
|
||||
"options": [],
|
||||
"type": "line",
|
||||
"value": null
|
||||
},
|
||||
"var2": {
|
||||
"name": "var2",
|
||||
"optional": false,
|
||||
"options": [],
|
||||
"type": "line",
|
||||
"value": null
|
||||
}
|
||||
},
|
||||
"max_retries": 0,
|
||||
"message_history_window_size": 22,
|
||||
"mode": "conversational",
|
||||
"outputs": {},
|
||||
"prologue": "Hi! I'm your assistant, what can I do for you?",
|
||||
"tips": "Please fill up the form"
|
||||
}
|
||||
},
|
||||
"upstream": []
|
||||
}
|
||||
},
|
||||
"embed_id": "",
|
||||
"globals": {
|
||||
"sys.conversation_turns": 0,
|
||||
"sys.files": [],
|
||||
"sys.query": "",
|
||||
"sys.user_id": ""
|
||||
},
|
||||
"graph": {
|
||||
"edges": [],
|
||||
"edges": [
|
||||
{
|
||||
"data": {
|
||||
"isHovered": false
|
||||
},
|
||||
"id": "xy-edge__beginstart-Message:EightyJobsAskend",
|
||||
"markerEnd": "logo",
|
||||
"source": "begin",
|
||||
"sourceHandle": "start",
|
||||
"style": {
|
||||
"stroke": "rgba(151, 154, 171, 1)",
|
||||
"strokeWidth": 1
|
||||
},
|
||||
"target": "Message:EightyJobsAsk",
|
||||
"targetHandle": "end",
|
||||
"type": "buttonEdge",
|
||||
"zIndex": 1001
|
||||
}
|
||||
],
|
||||
"nodes": [
|
||||
{
|
||||
"data": {
|
||||
"form": {
|
||||
"enablePrologue": true,
|
||||
"inputs": {
|
||||
"var1": {
|
||||
"name": "var1",
|
||||
"optional": false,
|
||||
"options": [],
|
||||
"type": "line"
|
||||
},
|
||||
"var2": {
|
||||
"name": "var2",
|
||||
"optional": false,
|
||||
"options": [],
|
||||
"type": "line"
|
||||
}
|
||||
},
|
||||
"mode": "conversational",
|
||||
"prologue": "Hi! I'm your assistant, what can I do for you?"
|
||||
},
|
||||
"label": "Begin",
|
||||
"name": "begin"
|
||||
},
|
||||
"dragging": false,
|
||||
"height": 44,
|
||||
"id": "begin",
|
||||
"position": {
|
||||
"x": 53.25688640427177,
|
||||
"y": 198.37155679786412
|
||||
"measured": {
|
||||
"height": 112,
|
||||
"width": 200
|
||||
},
|
||||
"positionAbsolute": {
|
||||
"x": 53.25688640427177,
|
||||
"y": 198.37155679786412
|
||||
"position": {
|
||||
"x": 270.64098070942583,
|
||||
"y": -56.320928437811176
|
||||
},
|
||||
"selected": false,
|
||||
"sourcePosition": "left",
|
||||
"targetPosition": "right",
|
||||
"type": "beginNode",
|
||||
"width": 200
|
||||
"type": "beginNode"
|
||||
},
|
||||
{
|
||||
"data": {
|
||||
"form": {},
|
||||
"label": "Answer",
|
||||
"name": "dialog_0"
|
||||
"form": {
|
||||
"content": [
|
||||
"{begin@var1}{begin@var2}"
|
||||
]
|
||||
},
|
||||
"label": "Message",
|
||||
"name": "Message_0"
|
||||
},
|
||||
"dragging": false,
|
||||
"height": 44,
|
||||
"id": "Answer:GreenReadersDrum",
|
||||
"id": "Message:EightyJobsAsk",
|
||||
"measured": {
|
||||
"height": 57,
|
||||
"width": 200
|
||||
},
|
||||
"position": {
|
||||
"x": 360.43473114516974,
|
||||
"y": 207.29298425089348
|
||||
"x": 279.5,
|
||||
"y": 190
|
||||
},
|
||||
"positionAbsolute": {
|
||||
"x": 360.43473114516974,
|
||||
"y": 207.29298425089348
|
||||
},
|
||||
"selected": false,
|
||||
"selected": true,
|
||||
"sourcePosition": "right",
|
||||
"targetPosition": "left",
|
||||
"type": "logicNode",
|
||||
"width": 200
|
||||
"type": "messageNode"
|
||||
}
|
||||
]
|
||||
},
|
||||
"history": [],
|
||||
"memory": [],
|
||||
"messages": [],
|
||||
"path": [
|
||||
[
|
||||
"begin"
|
||||
],
|
||||
[]
|
||||
],
|
||||
"reference": []
|
||||
"path": [],
|
||||
"retrieval": [],
|
||||
"task_id": "dbb4ed366e8611f09690a55a6daec4ef"
|
||||
},
|
||||
"id": "2581031eb7a311efb5200242ac120005",
|
||||
"id": "0b02fe80780e11f084adcfdc3ed1d902",
|
||||
"message": [
|
||||
{
|
||||
"content": "Hi! I'm your smart assistant. What can I do for you?",
|
||||
"content": "Hi! I'm your assistant, what can I do for you?",
|
||||
"role": "assistant"
|
||||
}
|
||||
],
|
||||
"source": "agent",
|
||||
"user_id": "69736c5e723611efb51b0242ac120007"
|
||||
"user_id": "c3fb861af27a11efa69751e139332ced"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user