Add sdk for list agents and sessions (#3848)

### What problem does this PR solve?

Add sdk for list agents and sessions

### Type of change

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

Co-authored-by: liuhua <10215101452@stu.ecun.edu.cn>
This commit is contained in:
liuhua
2024-12-04 16:23:22 +08:00
committed by GitHub
parent 289f4f1916
commit 1b589609a4
8 changed files with 1332 additions and 7 deletions

View File

@ -1,6 +1,7 @@
from .base import Base
from .session import Session
import requests
from typing import List
class Agent(Base):
def __init__(self,rag,res_dict):
@ -57,3 +58,18 @@ class Agent(Base):
return Session(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,
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 = res.json()
if res.get("code") == 0:
result_list = []
for data in res.get("data"):
temp_agent = Session(rag,data)
result_list.append(temp_agent)
return result_list
raise Exception(res.get("message"))

View File

@ -8,12 +8,12 @@ class Session(Base):
self.id = None
self.name = "New session"
self.messages = [{"role": "assistant", "content": "Hi! I am your assistantcan I help you?"}]
self.chat_id = None
self.agent_id = None
for key,value in res_dict.items():
if key =="chat_id" and value is not None:
self.chat_id = None
self.__session_type = "chat"
if key == "agent_id" and value is not None:
self.agent_id = None
self.__session_type = "agent"
super().__init__(rag, res_dict)