Fix some issues in API and test (#3001)

### What problem does this PR solve?

Fix some issues in API and test

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

Co-authored-by: liuhua <10215101452@stu.ecun.edu.cn>
This commit is contained in:
liuhua
2024-10-24 20:05:21 +08:00
committed by GitHub
parent e997b42504
commit 161c7a231b
6 changed files with 64 additions and 76 deletions

View File

@ -9,7 +9,7 @@ class Chat(Base):
self.id = ""
self.name = "assistant"
self.avatar = "path/to/avatar"
self.datasets = ["kb1"]
self.dataset_ids = ["kb1"]
self.llm = Chat.LLM(rag, {})
self.prompt = Chat.Prompt(rag, {})
super().__init__(rag, res_dict)

View File

@ -64,8 +64,8 @@ class RAGFlow:
return DataSet(self, res["data"])
raise Exception(res["message"])
def delete_datasets(self, ids: List[str] = None, names: List[str] = None):
res = self.delete("/dataset",{"ids": ids, "names": names})
def delete_datasets(self, ids: List[str]):
res = self.delete("/dataset",{"ids": ids})
res=res.json()
if res.get("code") != 0:
raise Exception(res["message"])
@ -89,11 +89,11 @@ class RAGFlow:
return result_list
raise Exception(res["message"])
def create_chat(self, name: str, avatar: str = "", datasets: List[DataSet] = [],
def create_chat(self, name: str, avatar: str = "", dataset_ids: List[str] = [],
llm: Chat.LLM = None, prompt: Chat.Prompt = None) -> Chat:
dataset_list = []
for dataset in datasets:
dataset_list.append(dataset.id)
for id in dataset_ids:
dataset_list.append(id)
if llm is None:
llm = Chat.LLM(self, {"model_name": None,
@ -126,7 +126,7 @@ class RAGFlow:
temp_dict = {"name": name,
"avatar": avatar,
"datasets": dataset_list,
"dataset_ids": dataset_list,
"llm": llm.to_json(),
"prompt": prompt.to_json()}
res = self.post("/chat", temp_dict)
@ -154,7 +154,9 @@ class RAGFlow:
raise Exception(res["message"])
def retrieve(self, datasets,documents,question="", offset=1, limit=1024, similarity_threshold=0.2,vector_similarity_weight=0.3,top_k=1024,rerank_id:str=None,keyword:bool=False,):
def retrieve(self, dataset_ids, document_ids=None, question="", offset=1, limit=1024, similarity_threshold=0.2, vector_similarity_weight=0.3, top_k=1024, rerank_id:str=None, keyword:bool=False, ):
if document_ids is None:
document_ids = []
data_json ={
"offset": offset,
"limit": limit,
@ -164,10 +166,9 @@ class RAGFlow:
"rerank_id": rerank_id,
"keyword": keyword,
"question": question,
"datasets": datasets,
"documents": documents
"datasets": dataset_ids,
"documents": document_ids
}
# Send a POST request to the backend service (using requests library as an example, actual implementation may vary)
res = self.post(f'/retrieval',json=data_json)
res = res.json()

View File

@ -1,5 +1,4 @@
from ragflow import RAGFlow, Chat
import time
HOST_ADDRESS = 'http://127.0.0.1:9380'
def test_create_chat_with_name(get_api_key_fixture):
@ -12,13 +11,10 @@ def test_create_chat_with_name(get_api_key_fixture):
document = {"displayed_name":displayed_name,"blob":blob}
documents = []
documents.append(document)
doc_ids = []
docs= kb.upload_documents(documents)
for doc in docs:
doc_ids.append(doc.id)
kb.async_parse_documents(doc_ids)
time.sleep(60)
rag.create_chat("test_create", datasets=[kb])
doc.add_chunk("This is a test to add chunk")
rag.create_chat("test_create", dataset_ids=[kb.id])
def test_update_chat_with_name(get_api_key_fixture):
@ -31,13 +27,10 @@ def test_update_chat_with_name(get_api_key_fixture):
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
documents.append(document)
doc_ids = []
docs = kb.upload_documents(documents)
for doc in docs:
doc_ids.append(doc.id)
kb.async_parse_documents(doc_ids)
time.sleep(60)
chat = rag.create_chat("test_update", datasets=[kb])
doc.add_chunk("This is a test to add chunk")
chat = rag.create_chat("test_update", dataset_ids=[kb.id])
chat.update({"name": "new_chat"})
@ -51,17 +44,27 @@ def test_delete_chats_with_success(get_api_key_fixture):
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
documents.append(document)
doc_ids = []
docs = kb.upload_documents(documents)
for doc in docs:
doc_ids.append(doc.id)
kb.async_parse_documents(doc_ids)
time.sleep(60)
chat = rag.create_chat("test_delete", datasets=[kb])
doc.add_chunk("This is a test to add chunk")
chat = rag.create_chat("test_delete", dataset_ids=[kb.id])
rag.delete_chats(ids=[chat.id])
def test_list_chats_with_success(get_api_key_fixture):
API_KEY = get_api_key_fixture
rag = RAGFlow(API_KEY, HOST_ADDRESS)
kb = rag.create_dataset(name="test_delete_chat")
displayed_name = "ragflow.txt"
with open("./ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
documents.append(document)
docs = kb.upload_documents(documents)
for doc in docs:
doc.add_chunk("This is a test to add chunk")
rag.create_chat("test_list_1", dataset_ids=[kb.id])
rag.create_chat("test_list_2", dataset_ids=[kb.id])
rag.list_chats()

View File

@ -10,16 +10,13 @@ def test_create_session_with_success(get_api_key_fixture):
displayed_name = "ragflow.txt"
with open("./ragflow.txt", "rb") as file:
blob = file.read()
document = {"displayed_name": displayed_name, "blob": blob}
document = {"displayed_name":displayed_name,"blob":blob}
documents = []
documents.append(document)
doc_ids = []
docs = kb.upload_documents(documents)
docs= kb.upload_documents(documents)
for doc in docs:
doc_ids.append(doc.id)
kb.async_parse_documents(doc_ids)
time.sleep(60)
assistant = rag.create_chat(name="test_create_session", datasets=[kb])
doc.add_chunk("This is a test to add chunk")
assistant=rag.create_chat("test_create", dataset_ids=[kb.id])
assistant.create_session()
@ -30,16 +27,13 @@ def test_create_conversation_with_success(get_api_key_fixture):
displayed_name = "ragflow.txt"
with open("./ragflow.txt","rb") as file:
blob = file.read()
document = {"displayed_name":displayed_name,"blob":blob}
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
documents.append(document)
doc_ids = []
docs= kb.upload_documents(documents)
docs = kb.upload_documents(documents)
for doc in docs:
doc_ids.append(doc.id)
kb.async_parse_documents(doc_ids)
time.sleep(60)
assistant = rag.create_chat(name="test_create_conversation", datasets=[kb])
doc.add_chunk("This is a test to add chunk")
assistant = rag.create_chat("test_create", dataset_ids=[kb.id])
session = assistant.create_session()
question = "What is AI"
for ans in session.ask(question, stream=True):
@ -57,13 +51,10 @@ def test_delete_sessions_with_success(get_api_key_fixture):
document = {"displayed_name":displayed_name,"blob":blob}
documents = []
documents.append(document)
doc_ids = []
docs= kb.upload_documents(documents)
for doc in docs:
doc_ids.append(doc.id)
kb.async_parse_documents(doc_ids)
time.sleep(60)
assistant = rag.create_chat(name="test_delete_session", datasets=[kb])
doc.add_chunk("This is a test to add chunk")
assistant=rag.create_chat("test_create", dataset_ids=[kb.id])
session = assistant.create_session()
assistant.delete_sessions(ids=[session.id])
@ -74,16 +65,13 @@ def test_update_session_with_name(get_api_key_fixture):
displayed_name = "ragflow.txt"
with open("./ragflow.txt","rb") as file:
blob = file.read()
document = {"displayed_name":displayed_name,"blob":blob}
document = {"displayed_name": displayed_name, "blob": blob}
documents = []
documents.append(document)
doc_ids = []
docs= kb.upload_documents(documents)
docs = kb.upload_documents(documents)
for doc in docs:
doc_ids.append(doc.id)
kb.async_parse_documents(doc_ids)
time.sleep(60)
assistant = rag.create_chat(name="test_update_session", datasets=[kb])
doc.add_chunk("This is a test to add chunk")
assistant = rag.create_chat("test_create", dataset_ids=[kb.id])
session = assistant.create_session(name="old session")
session.update({"name": "new session"})
@ -98,13 +86,10 @@ def test_list_sessions_with_success(get_api_key_fixture):
document = {"displayed_name":displayed_name,"blob":blob}
documents = []
documents.append(document)
doc_ids = []
docs= kb.upload_documents(documents)
for doc in docs:
doc_ids.append(doc.id)
kb.async_parse_documents(doc_ids)
time.sleep(60)
assistant = rag.create_chat(name="test_list_session", datasets=[kb])
doc.add_chunk("This is a test to add chunk")
assistant=rag.create_chat("test_create", dataset_ids=[kb.id])
assistant.create_session("test_1")
assistant.create_session("test_2")
assistant.list_sessions()