liuhua
liuhua
Kevin Hu
writinwaters
commited on
Commit
·
792f830
1
Parent(s):
aa2e210
Update the component of the agent API with parameters. (#4131)
Browse files### What problem does this PR solve?
Update the component of the agent API with parameters.
### Type of change
- [x] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [x] Refactoring
---------
Co-authored-by: liuhua <[email protected]>
Co-authored-by: Kevin Hu <[email protected]>
Co-authored-by: writinwaters <[email protected]>
- api/apps/sdk/chat.py +4 -3
- api/apps/sdk/session.py +22 -3
- api/db/services/canvas_service.py +20 -9
- api/utils/api_utils.py +4 -1
- docs/references/http_api_reference.md +49 -106
- docs/references/python_api_reference.md +25 -7
- sdk/python/ragflow_sdk/modules/agent.py +3 -31
- sdk/python/ragflow_sdk/modules/session.py +19 -19
api/apps/sdk/chat.py
CHANGED
@@ -267,9 +267,10 @@ def delete(tenant_id):
|
|
267 |
def list_chat(tenant_id):
|
268 |
id = request.args.get("id")
|
269 |
name = request.args.get("name")
|
270 |
-
|
271 |
-
|
272 |
-
|
|
|
273 |
page_number = int(request.args.get("page", 1))
|
274 |
items_per_page = int(request.args.get("page_size", 30))
|
275 |
orderby = request.args.get("orderby", "create_time")
|
|
|
267 |
def list_chat(tenant_id):
|
268 |
id = request.args.get("id")
|
269 |
name = request.args.get("name")
|
270 |
+
if id or name:
|
271 |
+
chat = DialogService.query(id=id, name=name, status=StatusEnum.VALID.value, tenant_id=tenant_id)
|
272 |
+
if not chat:
|
273 |
+
return get_error_data_result(message="The chat doesn't exist")
|
274 |
page_number = int(request.args.get("page", 1))
|
275 |
items_per_page = int(request.args.get("page_size", 30))
|
276 |
orderby = request.args.get("orderby", "create_time")
|
api/apps/sdk/session.py
CHANGED
@@ -77,15 +77,28 @@ def create_agent_session(tenant_id, agent_id):
|
|
77 |
cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
|
78 |
|
79 |
canvas = Canvas(cvs.dsl, tenant_id)
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
conv = {
|
83 |
"id": get_uuid(),
|
84 |
"dialog_id": cvs.id,
|
85 |
"user_id": req.get("usr_id","") if isinstance(req, dict) else "",
|
86 |
"message": [{"role": "assistant", "content": canvas.get_prologue()}],
|
87 |
"source": "agent",
|
88 |
-
"dsl":
|
89 |
}
|
90 |
API4ConversationService.save(**conv)
|
91 |
conv["agent_id"] = conv.pop("dialog_id")
|
@@ -149,6 +162,12 @@ def agent_completions(tenant_id, agent_id):
|
|
149 |
if not cvs:
|
150 |
return get_error_data_result(f"You don't own the agent {agent_id}")
|
151 |
if req.get("session_id"):
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
conv = API4ConversationService.query(id=req["session_id"], dialog_id=agent_id)
|
153 |
if not conv:
|
154 |
return get_error_data_result(f"You don't own the session {req['session_id']}")
|
|
|
77 |
cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)
|
78 |
|
79 |
canvas = Canvas(cvs.dsl, tenant_id)
|
80 |
+
canvas.reset()
|
81 |
+
query = canvas.get_preset_param()
|
82 |
+
if query:
|
83 |
+
for ele in query:
|
84 |
+
if not ele["optional"]:
|
85 |
+
if not req.get(ele["key"]):
|
86 |
+
return get_error_data_result(f"`{ele['key']}` is required")
|
87 |
+
ele["value"] = req[ele["key"]]
|
88 |
+
if ele["optional"]:
|
89 |
+
if req.get(ele["key"]):
|
90 |
+
ele["value"] = req[ele['key']]
|
91 |
+
else:
|
92 |
+
if "value" in ele:
|
93 |
+
ele.pop("value")
|
94 |
+
cvs.dsl = json.loads(str(canvas))
|
95 |
conv = {
|
96 |
"id": get_uuid(),
|
97 |
"dialog_id": cvs.id,
|
98 |
"user_id": req.get("usr_id","") if isinstance(req, dict) else "",
|
99 |
"message": [{"role": "assistant", "content": canvas.get_prologue()}],
|
100 |
"source": "agent",
|
101 |
+
"dsl": cvs.dsl
|
102 |
}
|
103 |
API4ConversationService.save(**conv)
|
104 |
conv["agent_id"] = conv.pop("dialog_id")
|
|
|
162 |
if not cvs:
|
163 |
return get_error_data_result(f"You don't own the agent {agent_id}")
|
164 |
if req.get("session_id"):
|
165 |
+
dsl = cvs[0].dsl
|
166 |
+
if not isinstance(dsl,str):
|
167 |
+
dsl = json.dumps(dsl)
|
168 |
+
canvas=Canvas(dsl,tenant_id)
|
169 |
+
if canvas.get_preset_param():
|
170 |
+
req["question"]=""
|
171 |
conv = API4ConversationService.query(id=req["session_id"], dialog_id=agent_id)
|
172 |
if not conv:
|
173 |
return get_error_data_result(f"You don't own the session {req['session_id']}")
|
api/db/services/canvas_service.py
CHANGED
@@ -74,21 +74,32 @@ def completion(tenant_id, agent_id, question, session_id=None, stream=True, **kw
|
|
74 |
else:
|
75 |
if "value" in ele:
|
76 |
ele.pop("value")
|
77 |
-
|
78 |
-
|
79 |
-
UserCanvasService.update_by_id(agent_id, cvs.to_dict())
|
80 |
-
else:
|
81 |
-
temp_dsl = json.loads(cvs.dsl)
|
82 |
-
session_id = get_uuid()
|
83 |
conv = {
|
84 |
"id": session_id,
|
85 |
"dialog_id": cvs.id,
|
86 |
-
"user_id": kwargs.get("
|
|
|
87 |
"source": "agent",
|
88 |
-
"dsl":
|
89 |
}
|
90 |
API4ConversationService.save(**conv)
|
91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
else:
|
93 |
e, conv = API4ConversationService.get_by_id(session_id)
|
94 |
assert e, "Session not found!"
|
|
|
74 |
else:
|
75 |
if "value" in ele:
|
76 |
ele.pop("value")
|
77 |
+
cvs.dsl = json.loads(str(canvas))
|
78 |
+
session_id=get_uuid()
|
|
|
|
|
|
|
|
|
79 |
conv = {
|
80 |
"id": session_id,
|
81 |
"dialog_id": cvs.id,
|
82 |
+
"user_id": kwargs.get("usr_id", "") if isinstance(kwargs, dict) else "",
|
83 |
+
"message": [{"role": "assistant", "content": canvas.get_prologue()}],
|
84 |
"source": "agent",
|
85 |
+
"dsl": cvs.dsl
|
86 |
}
|
87 |
API4ConversationService.save(**conv)
|
88 |
+
if query:
|
89 |
+
yield "data:" + json.dumps({"code": 0,
|
90 |
+
"message": "",
|
91 |
+
"data": {
|
92 |
+
"session_id": session_id,
|
93 |
+
"answer": canvas.get_prologue(),
|
94 |
+
"reference": [],
|
95 |
+
"param": canvas.get_preset_param()
|
96 |
+
}
|
97 |
+
},
|
98 |
+
ensure_ascii=False) + "\n\n"
|
99 |
+
yield "data:" + json.dumps({"code": 0, "message": "", "data": True}, ensure_ascii=False) + "\n\n"
|
100 |
+
return
|
101 |
+
else:
|
102 |
+
conv = API4Conversation(**conv)
|
103 |
else:
|
104 |
e, conv = API4ConversationService.get_by_id(session_id)
|
105 |
assert e, "Session not found!"
|
api/utils/api_utils.py
CHANGED
@@ -283,7 +283,10 @@ def construct_error_response(e):
|
|
283 |
def token_required(func):
|
284 |
@wraps(func)
|
285 |
def decorated_function(*args, **kwargs):
|
286 |
-
|
|
|
|
|
|
|
287 |
if len(authorization_list) < 2:
|
288 |
return get_json_result(data=False,message="Please check your authorization format.")
|
289 |
token = authorization_list[1]
|
|
|
283 |
def token_required(func):
|
284 |
@wraps(func)
|
285 |
def decorated_function(*args, **kwargs):
|
286 |
+
authorization_str=flask_request.headers.get('Authorization')
|
287 |
+
if not authorization_str:
|
288 |
+
return get_json_result(data=False,message="`Authorization` can't be empty")
|
289 |
+
authorization_list=authorization_str.split()
|
290 |
if len(authorization_list) < 2:
|
291 |
return get_json_result(data=False,message="Please check your authorization format.")
|
292 |
token = authorization_list[1]
|
docs/references/http_api_reference.md
CHANGED
@@ -1346,7 +1346,7 @@ Creates a chat assistant.
|
|
1346 |
curl --request POST \
|
1347 |
--url http://{address}/api/v1/chats \
|
1348 |
--header 'Content-Type: application/json' \
|
1349 |
-
--header 'Authorization: Bearer <YOUR_API_KEY>'
|
1350 |
--data '{
|
1351 |
"dataset_ids": ["0b2cbc8c877f11ef89070242ac120005"],
|
1352 |
"name":"new_chat_1"
|
@@ -2160,15 +2160,28 @@ Creates a session with an agent.
|
|
2160 |
- `'content-Type: application/json'`
|
2161 |
- `'Authorization: Bearer <YOUR_API_KEY>'`
|
2162 |
- Body:
|
|
|
|
|
2163 |
|
2164 |
##### Request example
|
2165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2166 |
```bash
|
2167 |
curl --request POST \
|
2168 |
--url http://{address}/api/v1/agents/{agent_id}/sessions \
|
2169 |
--header 'Content-Type: application/json' \
|
2170 |
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
2171 |
--data '{
|
|
|
|
|
2172 |
}'
|
2173 |
```
|
2174 |
|
@@ -2326,8 +2339,8 @@ Asks a specified agent a question to start an AI-powered conversation.
|
|
2326 |
- `"session_id"`: `string`
|
2327 |
- other parameters: `string`
|
2328 |
##### Request example
|
2329 |
-
|
2330 |
-
```bash
|
2331 |
curl --request POST \
|
2332 |
--url http://{address}/api/v1/agents/{agent_id}/completions \
|
2333 |
--header 'Content-Type: application/json' \
|
@@ -2336,6 +2349,7 @@ curl --request POST \
|
|
2336 |
{
|
2337 |
}'
|
2338 |
```
|
|
|
2339 |
```bash
|
2340 |
curl --request POST \
|
2341 |
--url http://{address}/api/v1/agents/{agent_id}/completions \
|
@@ -2343,11 +2357,11 @@ curl --request POST \
|
|
2343 |
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
2344 |
--data-binary '
|
2345 |
{
|
2346 |
-
"
|
2347 |
-
"
|
2348 |
-
"session_id": "cb2f385cb86211efa36e0242ac120005"
|
2349 |
}'
|
2350 |
```
|
|
|
2351 |
```bash
|
2352 |
curl --request POST \
|
2353 |
--url http://{address}/api/v1/agents/{agent_id}/completions \
|
@@ -2355,8 +2369,9 @@ curl --request POST \
|
|
2355 |
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
2356 |
--data-binary '
|
2357 |
{
|
2358 |
-
"
|
2359 |
-
"
|
|
|
2360 |
}'
|
2361 |
```
|
2362 |
|
@@ -2394,113 +2409,41 @@ data:{
|
|
2394 |
"data": true
|
2395 |
}
|
2396 |
```
|
2397 |
-
Success
|
2398 |
|
2399 |
```json
|
2400 |
data:{
|
2401 |
"code": 0,
|
2402 |
"message": "",
|
2403 |
"data": {
|
|
|
2404 |
"answer": "",
|
2405 |
"reference": [],
|
2406 |
-
"
|
2407 |
-
|
2408 |
-
|
2409 |
-
|
2410 |
-
|
2411 |
-
|
2412 |
-
|
2413 |
-
|
2414 |
-
|
2415 |
-
|
2416 |
-
|
2417 |
-
|
2418 |
-
|
2419 |
-
|
2420 |
-
|
2421 |
-
|
2422 |
-
|
2423 |
-
|
2424 |
-
|
2425 |
-
|
2426 |
-
|
2427 |
-
|
2428 |
-
data:{
|
2429 |
-
"code": 0,
|
2430 |
-
"data": {
|
2431 |
-
"answer": "Hello! How",
|
2432 |
-
"reference": [],
|
2433 |
-
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
|
2434 |
-
"session_id": "ce1b4fa89c1811ef85720242ac120006"
|
2435 |
-
}
|
2436 |
-
}
|
2437 |
-
data:{
|
2438 |
-
"code": 0,
|
2439 |
-
"data": {
|
2440 |
-
"answer": "Hello! How can",
|
2441 |
-
"reference": [],
|
2442 |
-
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
|
2443 |
-
"session_id": "ce1b4fa89c1811ef85720242ac120006"
|
2444 |
-
}
|
2445 |
-
}
|
2446 |
-
data:{
|
2447 |
-
"code": 0,
|
2448 |
-
"data": {
|
2449 |
-
"answer": "Hello! How can I",
|
2450 |
-
"reference": [],
|
2451 |
-
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
|
2452 |
-
"session_id": "ce1b4fa89c1811ef85720242ac120006"
|
2453 |
-
}
|
2454 |
-
}
|
2455 |
-
data:{
|
2456 |
-
"code": 0,
|
2457 |
-
"data": {
|
2458 |
-
"answer": "Hello! How can I assist",
|
2459 |
-
"reference": [],
|
2460 |
-
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
|
2461 |
-
"session_id": "ce1b4fa89c1811ef85720242ac120006"
|
2462 |
-
}
|
2463 |
-
}
|
2464 |
-
data:{
|
2465 |
-
"code": 0,
|
2466 |
-
"data": {
|
2467 |
-
"answer": "Hello! How can I assist you",
|
2468 |
-
"reference": [],
|
2469 |
-
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
|
2470 |
-
"session_id": "ce1b4fa89c1811ef85720242ac120006"
|
2471 |
-
}
|
2472 |
-
}
|
2473 |
-
data:{
|
2474 |
-
"code": 0,
|
2475 |
-
"data": {
|
2476 |
-
"answer": "Hello! How can I assist you today",
|
2477 |
-
"reference": [],
|
2478 |
-
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
|
2479 |
-
"session_id": "ce1b4fa89c1811ef85720242ac120006"
|
2480 |
-
}
|
2481 |
-
}
|
2482 |
-
data:{
|
2483 |
-
"code": 0,
|
2484 |
-
"data": {
|
2485 |
-
"answer": "Hello! How can I assist you today?",
|
2486 |
-
"reference": [],
|
2487 |
-
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
|
2488 |
-
"session_id": "ce1b4fa89c1811ef85720242ac120006"
|
2489 |
-
}
|
2490 |
-
}
|
2491 |
-
data:{
|
2492 |
-
"code": 0,
|
2493 |
-
"data": {
|
2494 |
-
"answer": "Hello! How can I assist you today?",
|
2495 |
-
"reference": [],
|
2496 |
-
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
|
2497 |
-
"session_id": "ce1b4fa89c1811ef85720242ac120006"
|
2498 |
}
|
2499 |
}
|
2500 |
-
data:
|
2501 |
-
"code": 0,
|
2502 |
-
"data": true
|
2503 |
-
}
|
2504 |
```
|
2505 |
Success with parameters in the `begin` component:
|
2506 |
```json
|
|
|
1346 |
curl --request POST \
|
1347 |
--url http://{address}/api/v1/chats \
|
1348 |
--header 'Content-Type: application/json' \
|
1349 |
+
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
1350 |
--data '{
|
1351 |
"dataset_ids": ["0b2cbc8c877f11ef89070242ac120005"],
|
1352 |
"name":"new_chat_1"
|
|
|
2160 |
- `'content-Type: application/json'`
|
2161 |
- `'Authorization: Bearer <YOUR_API_KEY>'`
|
2162 |
- Body:
|
2163 |
+
- the required parameters:`str`
|
2164 |
+
- the optional parameters:`str`
|
2165 |
|
2166 |
##### Request example
|
2167 |
+
If `begin` component in the agent doesn't have required parameters:
|
2168 |
+
```bash
|
2169 |
+
curl --request POST \
|
2170 |
+
--url http://{address}/api/v1/agents/{agent_id}/sessions \
|
2171 |
+
--header 'Content-Type: application/json' \
|
2172 |
+
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
2173 |
+
--data '{
|
2174 |
+
}'
|
2175 |
+
```
|
2176 |
+
If `begin` component in the agent has required parameters:
|
2177 |
```bash
|
2178 |
curl --request POST \
|
2179 |
--url http://{address}/api/v1/agents/{agent_id}/sessions \
|
2180 |
--header 'Content-Type: application/json' \
|
2181 |
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
2182 |
--data '{
|
2183 |
+
"lang":"Japanese",
|
2184 |
+
"file":"Who are you"
|
2185 |
}'
|
2186 |
```
|
2187 |
|
|
|
2339 |
- `"session_id"`: `string`
|
2340 |
- other parameters: `string`
|
2341 |
##### Request example
|
2342 |
+
If the `begin` component doesn't have parameters, the following code will create a session.
|
2343 |
+
```bash
|
2344 |
curl --request POST \
|
2345 |
--url http://{address}/api/v1/agents/{agent_id}/completions \
|
2346 |
--header 'Content-Type: application/json' \
|
|
|
2349 |
{
|
2350 |
}'
|
2351 |
```
|
2352 |
+
If the `begin` component have parameters, the following code will create a session.
|
2353 |
```bash
|
2354 |
curl --request POST \
|
2355 |
--url http://{address}/api/v1/agents/{agent_id}/completions \
|
|
|
2357 |
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
2358 |
--data-binary '
|
2359 |
{
|
2360 |
+
"lang":"English",
|
2361 |
+
"file":"How is the weather tomorrow?"
|
|
|
2362 |
}'
|
2363 |
```
|
2364 |
+
The following code will execute the completion process
|
2365 |
```bash
|
2366 |
curl --request POST \
|
2367 |
--url http://{address}/api/v1/agents/{agent_id}/completions \
|
|
|
2369 |
--header 'Authorization: Bearer <YOUR_API_KEY>' \
|
2370 |
--data-binary '
|
2371 |
{
|
2372 |
+
"question": "Hello",
|
2373 |
+
"stream": true,
|
2374 |
+
"session_id": "cb2f385cb86211efa36e0242ac120005"
|
2375 |
}'
|
2376 |
```
|
2377 |
|
|
|
2409 |
"data": true
|
2410 |
}
|
2411 |
```
|
2412 |
+
Success without `session_id` provided and with parameters in the `begin` component:
|
2413 |
|
2414 |
```json
|
2415 |
data:{
|
2416 |
"code": 0,
|
2417 |
"message": "",
|
2418 |
"data": {
|
2419 |
+
"session_id": "eacb36a0bdff11ef97120242ac120006",
|
2420 |
"answer": "",
|
2421 |
"reference": [],
|
2422 |
+
"param": [
|
2423 |
+
{
|
2424 |
+
"key": "lang",
|
2425 |
+
"name": "Target Language",
|
2426 |
+
"optional": false,
|
2427 |
+
"type": "line",
|
2428 |
+
"value": "English"
|
2429 |
+
},
|
2430 |
+
{
|
2431 |
+
"key": "file",
|
2432 |
+
"name": "Files",
|
2433 |
+
"optional": false,
|
2434 |
+
"type": "file",
|
2435 |
+
"value": "How is the weather tomorrow?"
|
2436 |
+
},
|
2437 |
+
{
|
2438 |
+
"key": "hhyt",
|
2439 |
+
"name": "hhty",
|
2440 |
+
"optional": true,
|
2441 |
+
"type": "line"
|
2442 |
+
}
|
2443 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2444 |
}
|
2445 |
}
|
2446 |
+
data:
|
|
|
|
|
|
|
2447 |
```
|
2448 |
Success with parameters in the `begin` component:
|
2449 |
```json
|
docs/references/python_api_reference.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
---
|
2 |
sidebar_position: 2
|
3 |
slug: /python_api_reference
|
4 |
---
|
@@ -1312,7 +1312,7 @@ assistant.delete_sessions(ids=["id_1","id_2"])
|
|
1312 |
### Converse with chat assistant
|
1313 |
|
1314 |
```python
|
1315 |
-
Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
|
1316 |
```
|
1317 |
|
1318 |
Asks a specified chat assistant a question to start an AI-powered conversation.
|
@@ -1325,7 +1325,7 @@ In streaming mode, not all responses include a reference, as this depends on the
|
|
1325 |
|
1326 |
##### question: `str`, *Required*
|
1327 |
|
1328 |
-
The question to start an AI-powered conversation.
|
1329 |
|
1330 |
##### stream: `bool`
|
1331 |
|
@@ -1334,6 +1334,10 @@ Indicates whether to output responses in a streaming way:
|
|
1334 |
- `True`: Enable streaming (default).
|
1335 |
- `False`: Disable streaming.
|
1336 |
|
|
|
|
|
|
|
|
|
1337 |
#### Returns
|
1338 |
|
1339 |
- A `Message` object containing the response to the question if `stream` is set to `False`.
|
@@ -1402,11 +1406,25 @@ while True:
|
|
1402 |
### Create session with agent
|
1403 |
|
1404 |
```python
|
1405 |
-
Agent.create_session(id,rag) -> Session
|
1406 |
```
|
1407 |
|
1408 |
Creates a session with the current agent.
|
1409 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1410 |
#### Returns
|
1411 |
|
1412 |
- Success: A `Session` object containing the following attributes:
|
@@ -1430,7 +1448,7 @@ session = create_session(AGENT_ID,rag_object)
|
|
1430 |
### Converse with agent
|
1431 |
|
1432 |
```python
|
1433 |
-
Session.ask(question: str, stream: bool = False) -> Optional[Message, iter[Message]]
|
1434 |
```
|
1435 |
|
1436 |
Asks a specified agent a question to start an AI-powered conversation.
|
@@ -1441,9 +1459,9 @@ In streaming mode, not all responses include a reference, as this depends on the
|
|
1441 |
|
1442 |
#### Parameters
|
1443 |
|
1444 |
-
##### question: `str
|
1445 |
|
1446 |
-
The question to start an AI-powered conversation.
|
1447 |
|
1448 |
##### stream: `bool`
|
1449 |
|
|
|
1 |
+
from scipy.special import kwargs---
|
2 |
sidebar_position: 2
|
3 |
slug: /python_api_reference
|
4 |
---
|
|
|
1312 |
### Converse with chat assistant
|
1313 |
|
1314 |
```python
|
1315 |
+
Session.ask(question: str = "", stream: bool = False, **kwargs) -> Optional[Message, iter[Message]]
|
1316 |
```
|
1317 |
|
1318 |
Asks a specified chat assistant a question to start an AI-powered conversation.
|
|
|
1325 |
|
1326 |
##### question: `str`, *Required*
|
1327 |
|
1328 |
+
The question to start an AI-powered conversation. Defalut to `""`
|
1329 |
|
1330 |
##### stream: `bool`
|
1331 |
|
|
|
1334 |
- `True`: Enable streaming (default).
|
1335 |
- `False`: Disable streaming.
|
1336 |
|
1337 |
+
##### **kwargs
|
1338 |
+
|
1339 |
+
The parameters in prompt(system).
|
1340 |
+
|
1341 |
#### Returns
|
1342 |
|
1343 |
- A `Message` object containing the response to the question if `stream` is set to `False`.
|
|
|
1406 |
### Create session with agent
|
1407 |
|
1408 |
```python
|
1409 |
+
Agent.create_session(id,rag, **kwargs) -> Session
|
1410 |
```
|
1411 |
|
1412 |
Creates a session with the current agent.
|
1413 |
|
1414 |
+
#### Parameters
|
1415 |
+
|
1416 |
+
##### id: `str`, *Required*
|
1417 |
+
|
1418 |
+
The id of agent
|
1419 |
+
|
1420 |
+
##### rag:`RAGFlow object`
|
1421 |
+
|
1422 |
+
The RAGFlow object
|
1423 |
+
|
1424 |
+
##### **kwargs
|
1425 |
+
|
1426 |
+
The parameters in `begin` component.
|
1427 |
+
|
1428 |
#### Returns
|
1429 |
|
1430 |
- Success: A `Session` object containing the following attributes:
|
|
|
1448 |
### Converse with agent
|
1449 |
|
1450 |
```python
|
1451 |
+
Session.ask(question: str="", stream: bool = False) -> Optional[Message, iter[Message]]
|
1452 |
```
|
1453 |
|
1454 |
Asks a specified agent a question to start an AI-powered conversation.
|
|
|
1459 |
|
1460 |
#### Parameters
|
1461 |
|
1462 |
+
##### question: `str`
|
1463 |
|
1464 |
+
The question to start an AI-powered conversation. If the `begin` component takes parameters, a question is not required.
|
1465 |
|
1466 |
##### stream: `bool`
|
1467 |
|
sdk/python/ragflow_sdk/modules/agent.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
from .base import Base
|
2 |
-
from .session import Session
|
3 |
import requests
|
4 |
-
import json
|
5 |
|
6 |
|
7 |
class Agent(Base):
|
@@ -52,8 +51,8 @@ class Agent(Base):
|
|
52 |
super().__init__(rag,res_dict)
|
53 |
|
54 |
@staticmethod
|
55 |
-
def create_session(id,rag) -> Session:
|
56 |
-
res = requests.post(f"{rag.api_url}/agents/{id}/sessions",headers={"Authorization": f"Bearer {rag.user_key}"},json=
|
57 |
res = res.json()
|
58 |
if res.get("code") == 0:
|
59 |
return Session(rag,res.get("data"))
|
@@ -74,30 +73,3 @@ class Agent(Base):
|
|
74 |
result_list.append(temp_agent)
|
75 |
return result_list
|
76 |
raise Exception(res.get("message"))
|
77 |
-
|
78 |
-
@staticmethod
|
79 |
-
def ask(agent_id,rag,stream=True,**kwargs):
|
80 |
-
url = f"{rag.api_url}/agents/{agent_id}/completions"
|
81 |
-
headers = {"Authorization": f"Bearer {rag.user_key}"}
|
82 |
-
res = requests.post(url=url, headers=headers, json=kwargs,stream=stream)
|
83 |
-
for line in res.iter_lines():
|
84 |
-
line = line.decode("utf-8")
|
85 |
-
if line.startswith("{"):
|
86 |
-
json_data = json.loads(line)
|
87 |
-
raise Exception(json_data["message"])
|
88 |
-
if line.startswith("data:"):
|
89 |
-
json_data = json.loads(line[5:])
|
90 |
-
if json_data["data"] is not True:
|
91 |
-
if json_data["data"].get("running_status"):
|
92 |
-
continue
|
93 |
-
answer = json_data["data"]["answer"]
|
94 |
-
reference = json_data["data"]["reference"]
|
95 |
-
temp_dict = {
|
96 |
-
"content": answer,
|
97 |
-
"role": "assistant"
|
98 |
-
}
|
99 |
-
if "chunks" in reference:
|
100 |
-
chunks = reference["chunks"]
|
101 |
-
temp_dict["reference"] = chunks
|
102 |
-
message = Message(rag, temp_dict)
|
103 |
-
yield message
|
|
|
1 |
from .base import Base
|
2 |
+
from .session import Session
|
3 |
import requests
|
|
|
4 |
|
5 |
|
6 |
class Agent(Base):
|
|
|
51 |
super().__init__(rag,res_dict)
|
52 |
|
53 |
@staticmethod
|
54 |
+
def create_session(id,rag,**kwargs) -> Session:
|
55 |
+
res = requests.post(f"{rag.api_url}/agents/{id}/sessions",headers={"Authorization": f"Bearer {rag.user_key}"},json=kwargs)
|
56 |
res = res.json()
|
57 |
if res.get("code") == 0:
|
58 |
return Session(rag,res.get("data"))
|
|
|
73 |
result_list.append(temp_agent)
|
74 |
return result_list
|
75 |
raise Exception(res.get("message"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
sdk/python/ragflow_sdk/modules/session.py
CHANGED
@@ -17,7 +17,7 @@ class Session(Base):
|
|
17 |
self.__session_type = "agent"
|
18 |
super().__init__(rag, res_dict)
|
19 |
|
20 |
-
def ask(self, question,stream=True,**kwargs):
|
21 |
if self.__session_type == "agent":
|
22 |
res=self._ask_agent(question,stream)
|
23 |
elif self.__session_type == "chat":
|
@@ -27,23 +27,22 @@ class Session(Base):
|
|
27 |
if line.startswith("{"):
|
28 |
json_data = json.loads(line)
|
29 |
raise Exception(json_data["message"])
|
30 |
-
if line.startswith("data:"):
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
|
48 |
def _ask_chat(self, question: str, stream: bool,**kwargs):
|
49 |
json_data={"question": question, "stream": True,"session_id":self.id}
|
@@ -51,6 +50,7 @@ class Session(Base):
|
|
51 |
res = self.post(f"/chats/{self.chat_id}/completions",
|
52 |
json_data, stream=stream)
|
53 |
return res
|
|
|
54 |
def _ask_agent(self,question:str,stream:bool):
|
55 |
res = self.post(f"/agents/{self.agent_id}/completions",
|
56 |
{"question": question, "stream": True,"session_id":self.id}, stream=stream)
|
@@ -70,4 +70,4 @@ class Message(Base):
|
|
70 |
self.role = "assistant"
|
71 |
self.prompt = None
|
72 |
self.id = None
|
73 |
-
super().__init__(rag, res_dict)
|
|
|
17 |
self.__session_type = "agent"
|
18 |
super().__init__(rag, res_dict)
|
19 |
|
20 |
+
def ask(self, question="",stream=True,**kwargs):
|
21 |
if self.__session_type == "agent":
|
22 |
res=self._ask_agent(question,stream)
|
23 |
elif self.__session_type == "chat":
|
|
|
27 |
if line.startswith("{"):
|
28 |
json_data = json.loads(line)
|
29 |
raise Exception(json_data["message"])
|
30 |
+
if not line.startswith("data:"):
|
31 |
+
continue
|
32 |
+
json_data = json.loads(line[5:])
|
33 |
+
if json_data["data"] is True or json_data["data"].get("running_status"):
|
34 |
+
continue
|
35 |
+
answer = json_data["data"]["answer"]
|
36 |
+
reference = json_data["data"].get("reference", {})
|
37 |
+
temp_dict = {
|
38 |
+
"content": answer,
|
39 |
+
"role": "assistant"
|
40 |
+
}
|
41 |
+
if reference and "chunks" in reference:
|
42 |
+
chunks = reference["chunks"]
|
43 |
+
temp_dict["reference"] = chunks
|
44 |
+
message = Message(self.rag, temp_dict)
|
45 |
+
yield message
|
|
|
46 |
|
47 |
def _ask_chat(self, question: str, stream: bool,**kwargs):
|
48 |
json_data={"question": question, "stream": True,"session_id":self.id}
|
|
|
50 |
res = self.post(f"/chats/{self.chat_id}/completions",
|
51 |
json_data, stream=stream)
|
52 |
return res
|
53 |
+
|
54 |
def _ask_agent(self,question:str,stream:bool):
|
55 |
res = self.post(f"/agents/{self.agent_id}/completions",
|
56 |
{"question": question, "stream": True,"session_id":self.id}, stream=stream)
|
|
|
70 |
self.role = "assistant"
|
71 |
self.prompt = None
|
72 |
self.id = None
|
73 |
+
super().__init__(rag, res_dict)
|