zhaoqingang
2024-11-13 0d11657e07dff84cfe118eb446efdb645a3c735f
app/service/ragflow.py
@@ -1,6 +1,5 @@
import httpx
from typing import Union, Dict, List
from fastapi import HTTPException
from starlette import status
@@ -40,10 +39,11 @@
            response = await client.post(
                f"{self.base_url}/v1/user/register",
                headers={'Content-Type': 'application/json'},
                json={"nickname": username, "email": f"{username}@example.com", "password": password}
                json={"nickname": username, "email":  f"{username}@example.com", "password": password}
            )
            if response.status_code != 200:
                raise Exception(f"Ragflow registration failed: {response.text}")
            return self._handle_response(response)
    async def login(self, username: str, password: str) -> str:
        password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password)
@@ -102,6 +102,24 @@
            ]
            return result
    async def get_session_log(self, token: str, conversation_id: str) -> dict:
        url = f"{self.base_url}/v1/conversation/get?conversation_id={conversation_id}"
        headers = {"Authorization": token}
        async with httpx.AsyncClient() as client:
            response = await client.get(url, headers=headers)
            data = self._handle_response(response)
            session_log = {
                "session_log": [
                    {
                        "message": message.get("content"),
                        "role": message.get("role"),
                    }
                    for message in data.get("message", [])
                ],
                "reference": data.get("reference"),
            }
        return session_log
    async def set_session(self, token: str, dialog_id: str, message: dict, chat_id: str, is_new: bool) -> list:
        url = f"{self.base_url}/v1/conversation/set?dialog_id={dialog_id}"
        headers = {"Authorization": token}
@@ -145,3 +163,12 @@
            response = await client.post(url, headers=headers, files=files, data=data)
            data = self._handle_response(response)
            return data
    async def add_user_tenant(self, token: str, tenant_id: str, email: str, user_id: str) -> str:
        url = f"{self.base_url}/v1/tenant/{tenant_id}/user"
        headers = {"Authorization": token}
        data = {"email": email, "user_id": user_id}
        async with httpx.AsyncClient(timeout=60) as client:
            response = await client.post(url, headers=headers, json=data)
            if response.status_code != 200:
                raise Exception(f"Ragflow add user to tenant failed: {response.text}")