Feat: Agent api (#14157)

### What problem does this PR solve?

1. **List agents**  
   **Prev API**:  
   - `/v1/canvas/list GET`  
   - `/api/v1/agents GET`  
   **Current API**: `/api/v2/agents GET`

2. **Get canvas template**  
   **Prev API**: `/v1/canvas/templates GET`  
   **Current API**: `/api/v2/agents/templates GET`

3. **Delete an agent**  
   **Prev API**: 
    - `/v1/canvas/rm POST`  
    - `/api/v1/agents/<agent_id> DELETE`
   **Current API**: `/api/v2/agents/<agent_id> DELETE`

4. **Update an agent**  
   **Prev API**: 
    - `/api/v1/agents/<agent_id> PUT`   
    - `/v1/canvas/setting POST `
   **Current API**: `/api/v2/agents/<agent_id> PATCH`


5. **Create an agent**  
   **Prev API**: 
    - `/v1/canvas/set POST`  
    - `/api/v1/agents POST`
   **Current API**: `/api/v2/agents POST`


6. **Get an agent**  
   **Prev API**: 
    - `/v1/canvas/get/<canvas_id> GET `  
   **Current API**: `/api/v2/agents/<agent_id> GET`


7. **Reset an agent**  
   **Prev API**: 
    - `/v1/canvas/reset POST`  
   **Current API**: `/api/v2/agents/<agent_id>/reset POST`


8. **Upload a file to an agent**  
   **Prev API**: 
    - `/v1/canvas/upload/<canvas_id> POST`  
   **Current API**: `/api/v2/agents/<agent_id>/upload POST`


9. **Input form**  
   **Prev API**: 
    - `/v1/canvas/input_form GET`  
**Current API**:
`/api/v2/agents/<agent_id>/components/<component_id>/input-form GET`


10. **Debug an agent**  
   **Prev API**: 
    - `/v1/canvas/debug POST`  
**Current API**:
`/api/v2/agents/<agent_id>/components/<component_id>/debug POST`


11. **Trace an agent**  
   **Prev API**: 
    - `/v1/canvas/trace GET`  
   **Current API**: `/api/v2/agents/<agent_id>/logs/<message_id> GET`


12. **Get an agent version list**  
   **Prev API**: 
    - `/v1/canvas/getlistversion/<canvas_id>`  
   **Current API**: `/api/v2/agents/<agent_id>/versions GET`


13. **Get a version of agent**  
   **Prev API**: 
    - `/v1/canvas/getversion/<version_id>`  
**Current API**: `/api/v2/agents/<agent_id>/versions/<version_id> GET`


14. **Test db connection**  
   **Prev API**: 
    - `/v1/canvas/test_db_connect POST`  
   **Current API**: `/api/v2/agents/test_db_connection`


15. **Rerun the agent**  
   **Prev API**: 
    - `/v1/canvas/rerun POST`  
   **Current API**: `/api/v2/agents/rerun POST`


16. **Get prompts**  
   **Prev API**: 
    - `/v1/canvas/prompts GET`  
   **Current API**: `/api/v2/agents/prompts GET`

### Type of change
- [x] New Feature (non-breaking change which adds functionality)

---------

Co-authored-by: chanx <1243304602@qq.com>
This commit is contained in:
Magicbook1108
2026-04-24 10:02:22 +08:00
committed by GitHub
parent d84438fd53
commit c74aece63c
34 changed files with 1819 additions and 4671 deletions

View File

@ -1710,7 +1710,7 @@ from ragflow_sdk import RAGFlow, Agent
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
agent_id = "AGENT_ID"
agent = rag_object.list_agents(id = agent_id)[0]
agent = rag_object.get_agent(agent_id)
session = agent.create_session()
# Or create in release mode:
# session = agent.create_session(release=True)
@ -1721,10 +1721,10 @@ session = agent.create_session()
### Converse with agent
```python
Session.ask(question: str="", stream: bool = False) -> Optional[Message, iter[Message]]
Session.ask(question: str = "", stream: bool = False, **kwargs) -> Optional[Message | iter[Message]]
```
Asks a specified agent a question to start an AI-powered conversation.
Asks a specified agent through the unified completion endpoint.
:::tip NOTE
In streaming mode, not all responses include a reference, as this depends on the system's judgement.
@ -1734,15 +1734,25 @@ In streaming mode, not all responses include a reference, as this depends on the
##### question: `string`
The question to start an AI-powered conversation. If the **Begin** component takes parameters, a question is not required.
The user message sent to the agent. If the **Begin** component takes parameters, `question` can be an empty string.
##### stream: `bool`
Indicates whether to output responses in a streaming way:
- `True`: Enable streaming (default).
- `True`: Enable streaming.
- `False`: Disable streaming.
##### kwargs: `dict`
Additional request parameters forwarded to the completion API. Common options:
- `inputs`: Variables defined in the **Begin** component.
- `session_id`: Continue an existing session instead of creating a new one.
- `release`: Use the latest published version of the agent.
- `return_trace`: Include execution trace information in the response.
- Other custom Begin component parameters supported by the current workflow.
#### Returns
- A `Message` object containing the response to the question if `stream` is set to `False`
@ -1792,8 +1802,8 @@ from ragflow_sdk import RAGFlow, Agent
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
AGENT_id = "AGENT_ID"
agent = rag_object.list_agents(id = AGENT_id)[0]
session = agent.create_session()
agent = rag_object.get_agent(AGENT_id)
session = agent.create_session()
print("\n===== Miss R ====\n")
print("Hello. What can I do for you?")
@ -1808,6 +1818,31 @@ while True:
cont = ans.content
```
Use Begin inputs and request trace output:
```python
from ragflow_sdk import RAGFlow, Agent
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
agent = rag_object.get_agent("AGENT_ID")
session = agent.create_session()
message = session.ask(
"",
stream=False,
inputs={
"line_var": {
"type": "line",
"value": "I am line_var",
}
},
return_trace=True,
)
print(message.content)
print(message.reference)
```
---
### List agent sessions
@ -1861,7 +1896,7 @@ from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
AGENT_id = "AGENT_ID"
agent = rag_object.list_agents(id = AGENT_id)[0]
agent = rag_object.get_agent(AGENT_id)
sessons = agent.list_sessions()
for session in sessions:
print(session)
@ -1900,7 +1935,7 @@ from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
AGENT_id = "AGENT_ID"
agent = rag_object.list_agents(id = AGENT_id)[0]
agent = rag_object.get_agent(AGENT_id)
agent.delete_sessions(ids=["id_1","id_2"])
agent.delete_sessions(delete_all=True)
```
@ -1917,14 +1952,12 @@ agent.delete_sessions(delete_all=True)
RAGFlow.list_agents(
page: int = 1,
page_size: int = 30,
orderby: str = "create_time",
desc: bool = True,
id: str = None,
title: str = None
orderby: str = "update_time",
desc: bool = True
) -> List[Agent]
```
Lists agents.
Lists agents. This is a collection API and always returns a list.
#### Parameters
@ -1940,21 +1973,13 @@ The number of agents on each page. Defaults to `30`.
The attribute by which the results are sorted. Available options:
- `"create_time"` (default)
- `"update_time"`
- `"create_time"`
- `"update_time"` (default)
##### desc: `bool`
Indicates whether the retrieved agents should be sorted in descending order. Defaults to `True`.
##### id: `string`
The ID of the agent to retrieve. Defaults to `None`.
##### name: `string`
The name of the agent to retrieve. Defaults to `None`.
#### Returns
- Success: A list of `Agent` objects.
@ -1971,6 +1996,37 @@ for agent in rag_object.list_agents():
---
### Get agent
```python
RAGFlow.get_agent(agent_id: str) -> Agent
```
Gets a single agent by ID and returns the detailed agent payload.
#### Parameters
##### agent_id: `string`
The ID of the agent to retrieve.
#### Returns
- Success: An `Agent` object.
- Failure: `Exception`.
#### Examples
```python
from ragflow_sdk import RAGFlow
rag_object = RAGFlow(api_key="<YOUR_API_KEY>", base_url="http://<YOUR_BASE_URL>:9380")
agent = rag_object.get_agent("AGENT_ID")
print(agent)
```
---
### Create agent
```python