mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-01-23 11:36:38 +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:
@ -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