ragflow/sdk/python/ragflow_sdk/modules/base.py
Mathias Panzenböck 38e7dc6246 added type annotations and __slots__ to the Python SDK
I've also added __slots__ less so to improve efficiency and more to be sure
there are no typos on assignments.

There remain a few untyped parts where I could not find documentation of the
types. These things are in particular:

- Agent.Dsl
- Agent.create_session()
- DataSet.ParserConfig - I'm not sure if the documented parameters are complete.
- Session.ask() - kwargs specific to agent/chat
2025-11-20 00:39:32 +01:00

68 lines
2.2 KiB
Python

#
# Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from typing import Any, Optional, TYPE_CHECKING
if TYPE_CHECKING:
from requests import Response
from requests.sessions import _Files, _Params
from ..ragflow import RAGFlow
class Base:
__slots__ = 'rag',
rag: "RAGFlow"
def __init__(self, rag: "RAGFlow", res_dict: dict[str, Any]) -> None:
self.rag = rag
self._update_from_dict(rag, res_dict)
def _update_from_dict(self, rag: "RAGFlow", res_dict: dict[str, Any]) -> None:
for k, v in res_dict.items():
if isinstance(v, dict):
setattr(self, k, Base(rag, v))
else:
setattr(self, k, v)
def to_json(self) -> dict[str, Any]:
pr: dict[str, Any] = {}
for name in dir(self):
value = getattr(self, name)
if not name.startswith("__") and not callable(value) and name != "rag":
if isinstance(value, Base):
pr[name] = value.to_json()
else:
pr[name] = value
return pr
def post(self, path: str, json: Any=None, stream: bool=False, files: Optional["_Files"]=None) -> "Response":
res = self.rag.post(path, json, stream=stream, files=files)
return res
def get(self, path: str, params: Optional["_Params"]=None) -> "Response":
res = self.rag.get(path, params)
return res
def rm(self, path: str, json: Any) -> "Response":
res = self.rag.delete(path, json)
return res
def put(self, path: str, json: Any) -> "Response":
res = self.rag.put(path, json)
return res
def __str__(self) -> str:
return str(self.to_json())