Feat: add 'delete' for agent's sessions api and unify apis of agent sdk (#5525)

### What problem does this PR solve?

Add sessions deletion support for agent in http and python api

### Type of change

- [ ] Bug Fix (non-breaking change which fixes an issue)
- [x] New Feature (non-breaking change which adds functionality)
- [ ] Documentation Update
- [x] Refactoring
- [ ] Performance Improvement
- [ ] Other (please describe):

---------

Co-authored-by: Kevin Hu <kevinhu.sh@gmail.com>
This commit is contained in:
Debug Doctor
2025-03-03 17:15:16 +08:00
committed by GitHub
parent 65d7c19979
commit 76cb4cd174
5 changed files with 158 additions and 35 deletions

View File

@ -16,11 +16,10 @@
from .base import Base
from .session import Session
import requests
class Agent(Base):
def __init__(self,rag,res_dict):
def __init__(self, rag, res_dict):
self.id = None
self.avatar = None
self.canvas_type = None
@ -29,7 +28,7 @@ class Agent(Base):
super().__init__(rag, res_dict)
class Dsl(Base):
def __init__(self,rag,res_dict):
def __init__(self, rag, res_dict):
self.answer = []
self.components = {
"begin": {
@ -64,28 +63,32 @@ class Agent(Base):
self.messages = []
self.path = []
self.reference = []
super().__init__(rag,res_dict)
super().__init__(rag, res_dict)
@staticmethod
def create_session(id,rag,**kwargs) -> Session:
res = requests.post(f"{rag.api_url}/agents/{id}/sessions",headers={"Authorization": f"Bearer {rag.user_key}"},json=kwargs)
def create_session(self, **kwargs) -> Session:
res = self.post(f"/agents/{self.id}/sessions", json=kwargs)
res = res.json()
if res.get("code") == 0:
return Session(rag,res.get("data"))
return Session(self.rag, res.get("data"))
raise Exception(res.get("message"))
@staticmethod
def list_sessions(agent_id,rag,page: int = 1, page_size: int = 30, orderby: str = "create_time", desc: bool = True,
def list_sessions(self, page: int = 1, page_size: int = 30, orderby: str = "create_time", desc: bool = True,
id: str = None) -> list[Session]:
url = f"{rag.api_url}/agents/{agent_id}/sessions"
headers = {"Authorization": f"Bearer {rag.user_key}"}
params = {"page": page, "page_size": page_size, "orderby": orderby, "desc": desc, "id": id}
res = requests.get(url=url,headers=headers,params=params)
res = self.get(f"/agents/{self.id}/sessions",
{"page": page, "page_size": page_size, "orderby": orderby, "desc": desc, "id": id})
res = res.json()
if res.get("code") == 0:
result_list = []
for data in res.get("data"):
temp_agent = Session(rag,data)
temp_agent = Session(self.rag, data)
result_list.append(temp_agent)
return result_list
raise Exception(res.get("message"))
def delete_sessions(self, ids: list[str] | None = None):
res = self.rm(f"/agents/{self.id}/sessions", {"ids": ids})
res = res.json()
if res.get("code") != 0:
raise Exception(res.get("message"))

View File

@ -14,7 +14,7 @@
# limitations under the License.
#
from ragflow_sdk import RAGFlow, Agent
from ragflow_sdk import RAGFlow
from common import HOST_ADDRESS
import pytest
@ -117,20 +117,29 @@ def test_list_sessions_with_success(get_api_key_fixture):
def test_create_agent_session_with_success(get_api_key_fixture):
API_KEY = "ragflow-BkOGNhYjIyN2JiODExZWY5MzVhMDI0Mm"
rag = RAGFlow(API_KEY, HOST_ADDRESS)
Agent.create_session("2e45b5209c1011efa3e90242ac120006", rag)
agent = rag.list_agents(id="2e45b5209c1011efa3e90242ac120006")[0]
agent.create_session()
@pytest.mark.skip(reason="")
def test_create_agent_conversation_with_success(get_api_key_fixture):
API_KEY = "ragflow-BkOGNhYjIyN2JiODExZWY5MzVhMDI0Mm"
rag = RAGFlow(API_KEY, HOST_ADDRESS)
session = Agent.create_session("2e45b5209c1011efa3e90242ac120006", rag)
agent = rag.list_agents(id="2e45b5209c1011efa3e90242ac120006")[0]
session = agent.create_session()
session.ask("What is this job")
@pytest.mark.skip(reason="")
def test_list_agent_sessions_with_success(get_api_key_fixture):
API_KEY = "ragflow-BkOGNhYjIyN2JiODExZWY5MzVhMDI0Mm"
agent_id = "2710f2269b4611ef8fdf0242ac120006"
rag = RAGFlow(API_KEY, HOST_ADDRESS)
Agent.list_sessions(agent_id, rag)
agent = rag.list_agents(id="2e45b5209c1011efa3e90242ac120006")[0]
agent.list_sessions()
@pytest.mark.skip(reason="")
def test_delete_session_of_agent_with_success(get_api_key_fixture):
API_KEY = "ragflow-BkOGNhYjIyN2JiODExZWY5MzVhMDI0Mm"
rag = RAGFlow(API_KEY, HOST_ADDRESS)
agent = rag.list_agents(id="2e45b5209c1011efa3e90242ac120006")[0]
agent.delete_sessions(ids=["test_1"])