mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-01-19 11:45:10 +08:00
Feat:memory sdk (#12538)
### What problem does this PR solve? Move memory and message apis to /api, and add sdk support. ### Type of change - [x] New Feature (non-breaking change which adds functionality)
This commit is contained in:
@ -26,6 +26,7 @@ from .modules.session import Session
|
||||
from .modules.document import Document
|
||||
from .modules.chunk import Chunk
|
||||
from .modules.agent import Agent
|
||||
from .modules.memory import Memory
|
||||
|
||||
__version__ = importlib.metadata.version("ragflow_sdk")
|
||||
|
||||
@ -36,5 +37,6 @@ __all__ = [
|
||||
"Session",
|
||||
"Document",
|
||||
"Chunk",
|
||||
"Agent"
|
||||
"Agent",
|
||||
"Memory"
|
||||
]
|
||||
|
||||
95
sdk/python/ragflow_sdk/modules/memory.py
Normal file
95
sdk/python/ragflow_sdk/modules/memory.py
Normal file
@ -0,0 +1,95 @@
|
||||
#
|
||||
# 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 .base import Base
|
||||
|
||||
|
||||
class Memory(Base):
|
||||
|
||||
def __init__(self, rag, res_dict):
|
||||
self.id = ""
|
||||
self.name = ""
|
||||
self.avatar = None
|
||||
self.tenant_id = None
|
||||
self.owner_name = ""
|
||||
self.memory_type = ["raw"]
|
||||
self.storage_type = "table"
|
||||
self.embd_id = ""
|
||||
self.llm_id = ""
|
||||
self.permissions = "me"
|
||||
self.description = ""
|
||||
self.memory_size = 5 * 1024 * 1024
|
||||
self.forgetting_policy = "FIFO"
|
||||
self.temperature = 0.5,
|
||||
self.system_prompt = ""
|
||||
self.user_prompt = ""
|
||||
for k in list(res_dict.keys()):
|
||||
if k not in self.__dict__:
|
||||
res_dict.pop(k)
|
||||
super().__init__(rag, res_dict)
|
||||
|
||||
def update(self, update_dict: dict):
|
||||
res = self.put(f"/memories/{self.id}", update_dict)
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
self._update_from_dict(self.rag, res.get("data", {}))
|
||||
return self
|
||||
|
||||
def get_config(self):
|
||||
res = self.get(f"/memories/{self.id}/config")
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
self._update_from_dict(self.rag, res.get("data", {}))
|
||||
return self
|
||||
|
||||
def list_memory_messages(self, agent_id: str | list[str]=None, keywords: str=None, page: int=1, page_size: int=50):
|
||||
params = {
|
||||
"agent_id": agent_id,
|
||||
"keywords": keywords,
|
||||
"page": page,
|
||||
"page_size": page_size
|
||||
}
|
||||
res = self.get(f"/memories/{self.id}", params)
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
return res["data"]
|
||||
|
||||
def forget_message(self, message_id: int):
|
||||
res = self.rm(f"/messages/{self.id}:{message_id}", {})
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
return True
|
||||
|
||||
def update_message_status(self, message_id: int, status: bool):
|
||||
update_message = {
|
||||
"status": status
|
||||
}
|
||||
res = self.put(f"/messages/{self.id}:{message_id}", update_message)
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
return True
|
||||
|
||||
def get_message_content(self, message_id: int) -> dict:
|
||||
res = self.get(f"/messages/{self.id}:{message_id}/content")
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
return res["data"]
|
||||
@ -21,6 +21,7 @@ from .modules.agent import Agent
|
||||
from .modules.chat import Chat
|
||||
from .modules.chunk import Chunk
|
||||
from .modules.dataset import DataSet
|
||||
from .modules.memory import Memory
|
||||
|
||||
|
||||
class RAGFlow:
|
||||
@ -289,3 +290,84 @@ class RAGFlow:
|
||||
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
|
||||
def create_memory(self, name: str, memory_type: list[str], embd_id: str, llm_id: str):
|
||||
payload = {"name": name, "memory_type": memory_type, "embd_id": embd_id, "llm_id": llm_id}
|
||||
res = self.post("/memories", payload)
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
return Memory(self, res["data"])
|
||||
|
||||
def list_memory(self, page: int = 1, page_size: int = 50, tenant_id: str | list[str] = None, memory_type: str | list[str] = None, storage_type: str = None, keywords: str = None) -> dict:
|
||||
res = self.get(
|
||||
"/memories",
|
||||
{
|
||||
"page": page,
|
||||
"page_size": page_size,
|
||||
"tenant_id": tenant_id,
|
||||
"memory_type": memory_type,
|
||||
"storage_type": storage_type,
|
||||
"keywords": keywords,
|
||||
}
|
||||
)
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
result_list = []
|
||||
for data in res["data"]["memory_list"]:
|
||||
result_list.append(Memory(self, data))
|
||||
return {
|
||||
"memory_list": result_list,
|
||||
"total_count": res["data"]["total_count"]
|
||||
}
|
||||
|
||||
def delete_memory(self, memory_id: str):
|
||||
res = self.delete(f"/memories/{memory_id}", {})
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
|
||||
def add_message(self, memory_id: list[str], agent_id: str, session_id: str, user_input: str, agent_response: str, user_id: str = "") -> str:
|
||||
payload = {
|
||||
"memory_id": memory_id,
|
||||
"agent_id": agent_id,
|
||||
"session_id": session_id,
|
||||
"user_input": user_input,
|
||||
"agent_response": agent_response,
|
||||
"user_id": user_id
|
||||
}
|
||||
res = self.post("/messages", payload)
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
return res["message"]
|
||||
|
||||
def search_message(self, query: str, memory_id: list[str], agent_id: str=None, session_id: str=None, similarity_threshold: float=0.2, keywords_similarity_weight: float=0.7, top_n: int=10) -> list[dict]:
|
||||
params = {
|
||||
"query": query,
|
||||
"memory_id": memory_id,
|
||||
"agent_id": agent_id,
|
||||
"session_id": session_id,
|
||||
"similarity_threshold": similarity_threshold,
|
||||
"keywords_similarity_weight": keywords_similarity_weight,
|
||||
"top_n": top_n
|
||||
}
|
||||
res = self.get("/messages/search", params)
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
return res["data"]
|
||||
|
||||
def get_recent_messages(self, memory_id: list[str], agent_id: str=None, session_id: str=None, limit: int=10) -> list[dict]:
|
||||
params = {
|
||||
"memory_id": memory_id,
|
||||
"agent_id": agent_id,
|
||||
"session_id": session_id,
|
||||
"limit": limit
|
||||
}
|
||||
res = self.get("/messages", params)
|
||||
res = res.json()
|
||||
if res.get("code") != 0:
|
||||
raise Exception(res["message"])
|
||||
return res["data"]
|
||||
|
||||
Reference in New Issue
Block a user