Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the component of the agent API with parameters. #4131

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions api/apps/sdk/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,10 @@ def delete(tenant_id):
def list_chat(tenant_id):
id = request.args.get("id")
name = request.args.get("name")
chat = DialogService.query(id=id,name=name,status=StatusEnum.VALID.value,tenant_id=tenant_id)
if not chat:
return get_error_data_result(message="The chat doesn't exist")
if id or name:
chat = DialogService.query(id=id, name=name, status=StatusEnum.VALID.value, tenant_id=tenant_id)
if not chat:
return get_error_data_result(message="The chat doesn't exist")
page_number = int(request.args.get("page", 1))
items_per_page = int(request.args.get("page_size", 30))
orderby = request.args.get("orderby", "create_time")
Expand Down
25 changes: 22 additions & 3 deletions api/apps/sdk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,28 @@ def create_agent_session(tenant_id, agent_id):
cvs.dsl = json.dumps(cvs.dsl, ensure_ascii=False)

canvas = Canvas(cvs.dsl, tenant_id)
if canvas.get_preset_param():
return get_error_data_result("The agent cannot create a session directly")
canvas.reset()
query = canvas.get_preset_param()
if query:
for ele in query:
if not ele["optional"]:
if not req.get(ele["key"]):
return get_error_data_result(f"`{ele['key']}` is required")
ele["value"] = req[ele["key"]]
if ele["optional"]:
if req.get(ele["key"]):
ele["value"] = req[ele['key']]
else:
if "value" in ele:
ele.pop("value")
cvs.dsl = json.loads(str(canvas))
conv = {
"id": get_uuid(),
"dialog_id": cvs.id,
"user_id": req.get("usr_id","") if isinstance(req, dict) else "",
"message": [{"role": "assistant", "content": canvas.get_prologue()}],
"source": "agent",
"dsl": json.loads(cvs.dsl)
"dsl": cvs.dsl
}
API4ConversationService.save(**conv)
conv["agent_id"] = conv.pop("dialog_id")
Expand Down Expand Up @@ -149,6 +162,12 @@ def agent_completions(tenant_id, agent_id):
if not cvs:
return get_error_data_result(f"You don't own the agent {agent_id}")
if req.get("session_id"):
dsl = cvs[0].dsl
if not isinstance(dsl,str):
dsl = json.dumps(dsl)
canvas=Canvas(dsl,tenant_id)
if canvas.get_preset_param():
req["question"]=""
conv = API4ConversationService.query(id=req["session_id"], dialog_id=agent_id)
if not conv:
return get_error_data_result(f"You don't own the session {req['session_id']}")
Expand Down
29 changes: 20 additions & 9 deletions api/db/services/canvas_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,32 @@ def completion(tenant_id, agent_id, question, session_id=None, stream=True, **kw
else:
if "value" in ele:
ele.pop("value")
cvs.dsl = json.loads(str(canvas))
temp_dsl = cvs.dsl
UserCanvasService.update_by_id(agent_id, cvs.to_dict())
else:
temp_dsl = json.loads(cvs.dsl)
session_id = get_uuid()
cvs.dsl = json.loads(str(canvas))
session_id=get_uuid()
conv = {
"id": session_id,
"dialog_id": cvs.id,
"user_id": kwargs.get("user_id", ""),
"user_id": kwargs.get("usr_id", "") if isinstance(kwargs, dict) else "",
"message": [{"role": "assistant", "content": canvas.get_prologue()}],
"source": "agent",
"dsl": temp_dsl
"dsl": cvs.dsl
}
API4ConversationService.save(**conv)
conv = API4Conversation(**conv)
if query:
yield "data:" + json.dumps({"code": 0,
"message": "",
"data": {
"session_id": session_id,
"answer": canvas.get_prologue(),
"reference": [],
"param": canvas.get_preset_param()
}
},
ensure_ascii=False) + "\n\n"
yield "data:" + json.dumps({"code": 0, "message": "", "data": True}, ensure_ascii=False) + "\n\n"
return
else:
conv = API4Conversation(**conv)
else:
e, conv = API4ConversationService.get_by_id(session_id)
assert e, "Session not found!"
Expand Down
5 changes: 4 additions & 1 deletion api/utils/api_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,10 @@ def construct_error_response(e):
def token_required(func):
@wraps(func)
def decorated_function(*args, **kwargs):
authorization_list=flask_request.headers.get('Authorization').split()
authorization_str=flask_request.headers.get('Authorization')
if not authorization_str:
return get_json_result(data=False,message="`Authorization` can't be empty")
authorization_list=authorization_str.split()
if len(authorization_list) < 2:
return get_json_result(data=False,message="Please check your authorization format.")
token = authorization_list[1]
Expand Down
157 changes: 50 additions & 107 deletions docs/references/http_api_reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ Uploads documents to a specified dataset.
curl --request POST \
--url http://{address}/api/v1/datasets/{dataset_id}/documents \
--header 'Content-Type: multipart/form-data' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--form 'file=@./test1.txt' \
--form 'file=@./test2.pdf'
```
Expand Down Expand Up @@ -1346,7 +1346,7 @@ Creates a chat assistant.
curl --request POST \
--url http://{address}/api/v1/chats \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <YOUR_API_KEY>'
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--data '{
"dataset_ids": ["0b2cbc8c877f11ef89070242ac120005"],
"name":"new_chat_1"
Expand Down Expand Up @@ -2160,15 +2160,28 @@ Creates a session with an agent.
- `'content-Type: application/json'`
- `'Authorization: Bearer <YOUR_API_KEY>'`
- Body:
- the required parameters:`str`
- the optional parameters:`str`

##### Request example

If `begin` component in the agent doesn't have 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 '{
}'
```
If `begin` component in the agent has 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"
}'
```

Expand Down Expand Up @@ -2326,8 +2339,8 @@ Asks a specified agent a question to start an AI-powered conversation.
- `"session_id"`: `string`
- other parameters: `string`
##### Request example

```bash
If the `begin` component doesn't have parameters, the following code will create a session.
```bash
curl --request POST \
--url http://{address}/api/v1/agents/{agent_id}/completions \
--header 'Content-Type: application/json' \
Expand All @@ -2336,27 +2349,29 @@ curl --request POST \
{
}'
```
If the `begin` component have parameters, the following code will create a session.
```bash
curl --request POST \
--url http://{address}/api/v1/agents/{agent_id}/completions \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--data-binary '
{
"question": "Hello",
"stream": true,
"session_id": "cb2f385cb86211efa36e0242ac120005"
"lang":"English",
"file":"How is the weather tomorrow?"
}'
```
The following code will execute the completion process
```bash
curl --request POST \
--url http://{address}/api/v1/agents/{agent_id}/completions \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <YOUR_API_KEY>' \
--data-binary '
{
"lang":"English"
"file":"How is the weather tomorrow?"
"question": "Hello",
"stream": true,
"session_id": "cb2f385cb86211efa36e0242ac120005"
}'
```

Expand Down Expand Up @@ -2394,113 +2409,41 @@ data:{
"data": true
}
```
Success with `session_id` provided and with no parameters in the `begin` component:
Success without `session_id` provided and with parameters in the `begin` component:

```json
data:{
"code": 0,
"message": "",
"data": {
"session_id": "eacb36a0bdff11ef97120242ac120006",
"answer": "",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello!",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello! How",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello! How can",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello! How can I",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello! How can I assist",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello! How can I assist you",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello! How can I assist you today",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello! How can I assist you today?",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
}
}
data:{
"code": 0,
"data": {
"answer": "Hello! How can I assist you today?",
"reference": [],
"id": "7ed5c2e4-aa28-4397-bbed-59664a332aa0",
"session_id": "ce1b4fa89c1811ef85720242ac120006"
"param": [
{
"key": "lang",
"name": "Target Language",
"optional": false,
"type": "line",
"value": "English"
},
{
"key": "file",
"name": "Files",
"optional": false,
"type": "file",
"value": "How is the weather tomorrow?"
},
{
"key": "hhyt",
"name": "hhty",
"optional": true,
"type": "line"
}
]
}
}
data:{
"code": 0,
"data": true
}
data:
```
Success with parameters in the `begin` component:
```json
Expand Down
Loading