mirror of
https://github.com/infiniflow/ragflow.git
synced 2025-12-08 20:42:30 +08:00
SDK for session (#2354)
### What problem does this PR solve? Includes SDK for creating, updating sessions, getting sessions, listing sessions, and dialogues #1102 ### Type of change - [x] New Feature (non-breaking change which adds functionality) --------- Co-authored-by: liuhua <10215101452@stu.ecun.edu.cn>
This commit is contained in:
@ -1,27 +1,60 @@
|
||||
from ragflow import RAGFlow
|
||||
from ragflow import RAGFlow,Session
|
||||
|
||||
from common import API_KEY, HOST_ADDRESS
|
||||
|
||||
|
||||
class TestChatSession:
|
||||
class TestSession:
|
||||
def test_create_session(self):
|
||||
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
||||
kb = rag.create_dataset(name="test_create_session")
|
||||
assistant = rag.create_assistant(name="test_create_session", knowledgebases=[kb])
|
||||
session = assistant.create_session()
|
||||
assert assistant is not None, "Failed to get the assistant."
|
||||
assert session is not None, "Failed to create a session."
|
||||
assert isinstance(session,Session), "Failed to create a session."
|
||||
|
||||
def test_create_chat_with_success(self):
|
||||
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
||||
kb = rag.create_dataset(name="test_create_chat")
|
||||
assistant = rag.create_assistant(name="test_create_chat", knowledgebases=[kb])
|
||||
session = assistant.create_session()
|
||||
assert session is not None, "Failed to create a session."
|
||||
prologue = assistant.get_prologue()
|
||||
assert isinstance(prologue, str), "Prologue is not a string."
|
||||
assert len(prologue) > 0, "Prologue is empty."
|
||||
question = "What is AI"
|
||||
ans = session.chat(question, stream=True)
|
||||
response = ans[-1].content
|
||||
assert len(response) > 0, "Assistant did not return any response."
|
||||
for ans in session.chat(question, stream=True):
|
||||
pass
|
||||
assert ans.content!="\n**ERROR**", "Please check this error."
|
||||
|
||||
def test_delete_session_with_success(self):
|
||||
rag = RAGFlow(API_KEY, HOST_ADDRESS)
|
||||
kb = rag.create_dataset(name="test_delete_session")
|
||||
assistant = rag.create_assistant(name="test_delete_session",knowledgebases=[kb])
|
||||
session=assistant.create_session()
|
||||
res=session.delete()
|
||||
assert res, "Failed to delete the dataset."
|
||||
|
||||
def test_update_session_with_success(self):
|
||||
rag=RAGFlow(API_KEY,HOST_ADDRESS)
|
||||
kb=rag.create_dataset(name="test_update_session")
|
||||
assistant = rag.create_assistant(name="test_update_session",knowledgebases=[kb])
|
||||
session=assistant.create_session(name="old session")
|
||||
session.name="new session"
|
||||
res=session.save()
|
||||
assert res,"Failed to update the session"
|
||||
|
||||
def test_get_session_with_success(self):
|
||||
rag=RAGFlow(API_KEY,HOST_ADDRESS)
|
||||
kb=rag.create_dataset(name="test_get_session")
|
||||
assistant = rag.create_assistant(name="test_get_session",knowledgebases=[kb])
|
||||
session = assistant.create_session()
|
||||
session_2= assistant.get_session(id=session.id)
|
||||
assert session.to_json()==session_2.to_json(),"Failed to get the session"
|
||||
|
||||
def test_list_session_with_success(self):
|
||||
rag=RAGFlow(API_KEY,HOST_ADDRESS)
|
||||
kb=rag.create_dataset(name="test_list_session")
|
||||
assistant=rag.create_assistant(name="test_list_session",knowledgebases=[kb])
|
||||
assistant.create_session("test_1")
|
||||
assistant.create_session("test_2")
|
||||
sessions=assistant.list_session()
|
||||
if isinstance(sessions,list):
|
||||
for session in sessions:
|
||||
assert isinstance(session,Session),"Non-Session elements exist in the list"
|
||||
else :
|
||||
assert False,"Failed to retrieve the session list."
|
||||
Reference in New Issue
Block a user