| | |
| | | json={"user_name": username, "password": password}, |
| | | headers={'Content-Type': 'application/json'} |
| | | ) |
| | | self._check_response(response) |
| | | return self._check_response(response) |
| | | |
| | | async def login(self, username: str, password: str) -> str: |
| | | public_key = await self.get_public_key_api() |
| | |
| | | data = self._check_response(response) |
| | | return data.get('public_key') |
| | | |
| | | async def get_chat_sessions(self, token: str) -> list: |
| | | url = f"{self.base_url}/api/v1/chat/list?page=1&limit=40" |
| | | async def get_chat_sessions(self, token: str, page: int = 1, limit: int=100) -> list: |
| | | url = f"{self.base_url}/api/v1/chat/list?page={page}&limit={limit}" |
| | | headers = {'cookie': f"access_token_cookie={token};"} |
| | | async with httpx.AsyncClient() as client: |
| | | response = await client.get(url, headers=headers) |
| | |
| | | "updated_time": int(datetime.strptime(item["update_time"], "%Y-%m-%dT%H:%M:%S").timestamp() * 1000) |
| | | } |
| | | for item in data |
| | | if |
| | | "latest_message" in item and "message" in item["latest_message"] and item["latest_message"]["message"] |
| | | ] |
| | | return result |
| | | |
| | | async def get_session_log(self, token: str, agent_id: str, conversation_id: str): |
| | | url = ( |
| | | f"{self.base_url}/api/v1/chat/history?" |
| | | f"flow_id={agent_id}&" |
| | | f"chat_id={conversation_id}&page_size=30&id=" |
| | | ) |
| | | headers = {'cookie': f"access_token_cookie={token};"} |
| | | async with httpx.AsyncClient() as client: |
| | | response = await client.get(url, headers=headers) |
| | | response.raise_for_status() |
| | | data = self._check_response(response) |
| | | session_log = [ |
| | | { |
| | | "message": message.get("message"), |
| | | "role": message.get("category"), |
| | | "ts": message.get("create_time") |
| | | } |
| | | for message in data |
| | | ] |
| | | |
| | | # 把session_log 按ts 升序排序 |
| | | session_log.sort(key=lambda x: x['ts']) |
| | | return session_log |
| | | |
| | | async def variable_list(self, token: str, agent_id: str) -> list: |
| | | url = f"{self.base_url}/api/v1/variable/list?flow_id={agent_id}" |
| | |
| | | } |
| | | |
| | | return result |
| | | |
| | | async def user_list(self, token: str) -> list: |
| | | url = f"{self.base_url}/api/v1/user/list" |
| | | headers = {'cookie': f"access_token_cookie={token};"} |
| | | async with httpx.AsyncClient() as client: |
| | | response = await client.get(url, headers=headers) |
| | | data = self._check_response(response) |
| | | return data |