mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-15 05:04:27 +08:00
### What problem does this PR solve?
This reverts commit 1a608ac411.
### Type of change
- [x] Other (please describe):
This commit is contained in:
@@ -14,8 +14,11 @@
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
from operator import attrgetter
|
||||
|
||||
import pytest
|
||||
from configs import CHAT_ASSISTANT_NAME_LIMIT
|
||||
from ragflow_sdk import Chat
|
||||
from utils import encode_avatar
|
||||
from utils.file_utils import create_image_file
|
||||
|
||||
@@ -73,16 +76,18 @@ class TestChatAssistantCreate:
|
||||
assert chat_assistant.name == "ragflow test"
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_icon(self, client, tmp_path):
|
||||
def test_avatar(self, client, tmp_path):
|
||||
fn = create_image_file(tmp_path / "ragflow_test.png")
|
||||
chat_assistant = client.create_chat(name="icon_test", icon=encode_avatar(fn), dataset_ids=[])
|
||||
assert chat_assistant.name == "icon_test"
|
||||
chat_assistant = client.create_chat(name="avatar_test", avatar=encode_avatar(fn), dataset_ids=[])
|
||||
assert chat_assistant.name == "avatar_test"
|
||||
|
||||
@pytest.mark.p3
|
||||
@pytest.mark.parametrize(
|
||||
"llm_setting, expected_message",
|
||||
"llm, expected_message",
|
||||
[
|
||||
({}, ""),
|
||||
({"model_name": "glm-4"}, ""),
|
||||
({"model_name": "unknown"}, "`model_name` unknown doesn't exist"),
|
||||
({"temperature": 0}, ""),
|
||||
({"temperature": 1}, ""),
|
||||
pytest.param({"temperature": -1}, "", marks=pytest.mark.skip),
|
||||
@@ -111,41 +116,47 @@ class TestChatAssistantCreate:
|
||||
pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
|
||||
],
|
||||
)
|
||||
def test_llm_setting(self, client, add_chunks, llm_setting, expected_message):
|
||||
def test_llm(self, client, add_chunks, llm, expected_message):
|
||||
dataset, _, _ = add_chunks
|
||||
llm_o = Chat.LLM(client, llm)
|
||||
|
||||
if expected_message:
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_setting=llm_setting or None)
|
||||
client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm=llm_o)
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
chat_assistant = client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_setting=llm_setting or None)
|
||||
for k, v in llm_setting.items():
|
||||
assert getattr(chat_assistant.llm_setting, k) == v
|
||||
chat_assistant = client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm=llm_o)
|
||||
if llm:
|
||||
for k, v in llm.items():
|
||||
assert attrgetter(k)(chat_assistant.llm) == v
|
||||
else:
|
||||
assert attrgetter("model_name")(chat_assistant.llm) == "glm-4-flash@ZHIPU-AI"
|
||||
assert attrgetter("temperature")(chat_assistant.llm) == 0.1
|
||||
assert attrgetter("top_p")(chat_assistant.llm) == 0.3
|
||||
assert attrgetter("presence_penalty")(chat_assistant.llm) == 0.4
|
||||
assert attrgetter("frequency_penalty")(chat_assistant.llm) == 0.7
|
||||
assert attrgetter("max_tokens")(chat_assistant.llm) == 512
|
||||
|
||||
@pytest.mark.p3
|
||||
@pytest.mark.parametrize(
|
||||
"llm_id, expected_message",
|
||||
[
|
||||
("glm-4", ""),
|
||||
("unknown", "`llm_id` unknown doesn't exist"),
|
||||
],
|
||||
)
|
||||
def test_llm_id(self, client, add_chunks, llm_id, expected_message):
|
||||
dataset, _, _ = add_chunks
|
||||
|
||||
if expected_message:
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_id=llm_id)
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
chat_assistant = client.create_chat(name="llm_test", dataset_ids=[dataset.id], llm_id=llm_id)
|
||||
assert chat_assistant.llm_id == llm_id
|
||||
|
||||
@pytest.mark.p3
|
||||
@pytest.mark.parametrize(
|
||||
"prompt_config, expected_message",
|
||||
"prompt, expected_message",
|
||||
[
|
||||
({"similarity_threshold": 0}, ""),
|
||||
({"similarity_threshold": 1}, ""),
|
||||
pytest.param({"similarity_threshold": -1}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"similarity_threshold": 10}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"similarity_threshold": "a"}, "", marks=pytest.mark.skip),
|
||||
({"keywords_similarity_weight": 0}, ""),
|
||||
({"keywords_similarity_weight": 1}, ""),
|
||||
pytest.param({"keywords_similarity_weight": -1}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"keywords_similarity_weight": 10}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"keywords_similarity_weight": "a"}, "", marks=pytest.mark.skip),
|
||||
({"variables": []}, ""),
|
||||
({"top_n": 0}, ""),
|
||||
({"top_n": 1}, ""),
|
||||
pytest.param({"top_n": -1}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"top_n": 10}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"top_n": "a"}, "", marks=pytest.mark.skip),
|
||||
({"empty_response": "Hello World"}, ""),
|
||||
({"empty_response": ""}, ""),
|
||||
({"empty_response": "!@#$%^&*()"}, ""),
|
||||
@@ -153,36 +164,55 @@ class TestChatAssistantCreate:
|
||||
pytest.param({"empty_response": 123}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"empty_response": True}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"empty_response": " "}, "", marks=pytest.mark.skip),
|
||||
({"prologue": "Hello World"}, ""),
|
||||
({"prologue": ""}, ""),
|
||||
({"prologue": "!@#$%^&*()"}, ""),
|
||||
({"prologue": "中文测试"}, ""),
|
||||
pytest.param({"prologue": 123}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"prologue": True}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"prologue": " "}, "", marks=pytest.mark.skip),
|
||||
({"quote": True}, ""),
|
||||
({"quote": False}, ""),
|
||||
({"system": "Hello World {knowledge}"}, ""),
|
||||
({"system": "{knowledge}"}, ""),
|
||||
({"system": "!@#$%^&*() {knowledge}"}, ""),
|
||||
({"system": "中文测试 {knowledge}"}, ""),
|
||||
({"system": "Hello World"}, ""),
|
||||
({"system": "Hello World", "parameters": []}, ""),
|
||||
pytest.param({"system": 123}, "", marks=pytest.mark.skip),
|
||||
({"opener": "Hello World"}, ""),
|
||||
({"opener": ""}, ""),
|
||||
({"opener": "!@#$%^&*()"}, ""),
|
||||
({"opener": "中文测试"}, ""),
|
||||
pytest.param({"opener": 123}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"opener": True}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"opener": " "}, "", marks=pytest.mark.skip),
|
||||
({"show_quote": True}, ""),
|
||||
({"show_quote": False}, ""),
|
||||
({"prompt": "Hello World {knowledge}"}, ""),
|
||||
({"prompt": "{knowledge}"}, ""),
|
||||
({"prompt": "!@#$%^&*() {knowledge}"}, ""),
|
||||
({"prompt": "中文测试 {knowledge}"}, ""),
|
||||
({"prompt": "Hello World"}, ""),
|
||||
({"prompt": "Hello World", "variables": []}, ""),
|
||||
pytest.param({"prompt": 123}, """AttributeError("\'int\' object has no attribute \'find\'")""", marks=pytest.mark.skip),
|
||||
pytest.param({"prompt": True}, """AttributeError("\'int\' object has no attribute \'find\'")""", marks=pytest.mark.skip),
|
||||
pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
|
||||
],
|
||||
)
|
||||
def test_prompt_config(self, client, add_chunks, prompt_config, expected_message):
|
||||
def test_prompt(self, client, add_chunks, prompt, expected_message):
|
||||
dataset, _, _ = add_chunks
|
||||
prompt_o = Chat.Prompt(client, prompt)
|
||||
|
||||
if expected_message:
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
client.create_chat(name="prompt_test", dataset_ids=[dataset.id], prompt_config=prompt_config)
|
||||
client.create_chat(name="prompt_test", dataset_ids=[dataset.id], prompt=prompt_o)
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
chat_assistant = client.create_chat(name="prompt_test", dataset_ids=[dataset.id], prompt_config=prompt_config)
|
||||
for k, v in prompt_config.items():
|
||||
assert getattr(chat_assistant.prompt_config, k) == v
|
||||
chat_assistant = client.create_chat(name="prompt_test", dataset_ids=[dataset.id], prompt=prompt_o)
|
||||
if prompt:
|
||||
for k, v in prompt.items():
|
||||
if k == "keywords_similarity_weight":
|
||||
assert attrgetter(k)(chat_assistant.prompt) == 1 - v
|
||||
else:
|
||||
assert attrgetter(k)(chat_assistant.prompt) == v
|
||||
else:
|
||||
assert attrgetter("similarity_threshold")(chat_assistant.prompt) == 0.2
|
||||
assert attrgetter("keywords_similarity_weight")(chat_assistant.prompt) == 0.7
|
||||
assert attrgetter("top_n")(chat_assistant.prompt) == 6
|
||||
assert attrgetter("variables")(chat_assistant.prompt) == [{"key": "knowledge", "optional": False}]
|
||||
assert attrgetter("rerank_model")(chat_assistant.prompt) == ""
|
||||
assert attrgetter("empty_response")(chat_assistant.prompt) == "Sorry! No relevant content was found in the knowledge base!"
|
||||
assert attrgetter("opener")(chat_assistant.prompt) == "Hi! I'm your assistant. What can I do for you?"
|
||||
assert attrgetter("show_quote")(chat_assistant.prompt) is True
|
||||
assert (
|
||||
attrgetter("prompt")(chat_assistant.prompt)
|
||||
== 'You are an intelligent assistant. Please summarize the content of the dataset to answer the question. Please list the data in the dataset and answer in detail. When all dataset content is irrelevant to the question, your answer must include the sentence "The answer you are looking for is not found in the dataset!" Answers need to consider chat history.\n Here is the knowledge base:\n {knowledge}\n The above is the knowledge base.'
|
||||
)
|
||||
|
||||
|
||||
class TestChatAssistantCreate2:
|
||||
|
||||
@@ -136,83 +136,75 @@ class TestChatAssistantsList:
|
||||
@pytest.mark.parametrize(
|
||||
"params, expected_num, expected_message",
|
||||
[
|
||||
({"keywords": None}, 5, ""),
|
||||
({"keywords": ""}, 5, ""),
|
||||
({"keywords": "test_chat_assistant_1"}, 1, ""),
|
||||
({"keywords": "unknown"}, 0, ""),
|
||||
({"name": None}, 5, ""),
|
||||
({"name": ""}, 5, ""),
|
||||
({"name": "test_chat_assistant_1"}, 1, ""),
|
||||
({"name": "unknown"}, 0, "The chat doesn't exist"),
|
||||
],
|
||||
)
|
||||
def test_keywords(self, client, params, expected_num, expected_message):
|
||||
def test_name(self, client, params, expected_num, expected_message):
|
||||
if expected_message:
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
client.list_chats(**params)
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
assistants = client.list_chats(**params)
|
||||
if params["keywords"] in [None, ""]:
|
||||
if params["name"] in [None, ""]:
|
||||
assert len(assistants) == expected_num
|
||||
else:
|
||||
assert len(assistants) == expected_num
|
||||
if expected_num:
|
||||
assert assistants[0].name == params["keywords"]
|
||||
|
||||
@pytest.mark.p1
|
||||
def test_exact_id_and_name_filters(self, client, add_chat_assistants):
|
||||
_, _, chat_assistants = add_chat_assistants
|
||||
target = chat_assistants[1]
|
||||
|
||||
assistants = client.list_chats(id=target.id)
|
||||
assert len(assistants) == 1
|
||||
assert assistants[0].id == target.id
|
||||
|
||||
assistants = client.list_chats(name=target.name)
|
||||
assert len(assistants) == 1
|
||||
assert assistants[0].name == target.name
|
||||
|
||||
assistants = client.list_chats(name=target.name, keywords="unknown")
|
||||
assert len(assistants) == 1
|
||||
assert assistants[0].name == target.name
|
||||
assert assistants[0].name == params["name"]
|
||||
|
||||
@pytest.mark.p1
|
||||
@pytest.mark.parametrize(
|
||||
"chat_assistant_id, expected_message",
|
||||
"chat_assistant_id, expected_num, expected_message",
|
||||
[
|
||||
(lambda r: r[0], ""),
|
||||
("unknown", "No authorization."),
|
||||
(None, 5, ""),
|
||||
("", 5, ""),
|
||||
(lambda r: r[0], 1, ""),
|
||||
("unknown", 0, "The chat doesn't exist"),
|
||||
],
|
||||
)
|
||||
def test_get_chat(self, client, add_chat_assistants, chat_assistant_id, expected_message):
|
||||
def test_id(self, client, add_chat_assistants, chat_assistant_id, expected_num, expected_message):
|
||||
_, _, chat_assistants = add_chat_assistants
|
||||
chat_id = chat_assistant_id([chat.id for chat in chat_assistants]) if callable(chat_assistant_id) else chat_assistant_id
|
||||
if callable(chat_assistant_id):
|
||||
params = {"id": chat_assistant_id([chat.id for chat in chat_assistants])}
|
||||
else:
|
||||
params = {"id": chat_assistant_id}
|
||||
|
||||
if expected_message:
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
client.get_chat(chat_id)
|
||||
client.list_chats(**params)
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
assistant = client.get_chat(chat_id)
|
||||
assert assistant.id == chat_id
|
||||
assistants = client.list_chats(**params)
|
||||
if params["id"] in [None, ""]:
|
||||
assert len(assistants) == expected_num
|
||||
else:
|
||||
assert assistants[0].id == params["id"]
|
||||
|
||||
@pytest.mark.p3
|
||||
@pytest.mark.parametrize(
|
||||
"chat_assistant_id, keywords, expected_num, expected_message",
|
||||
"chat_assistant_id, name, expected_num, expected_message",
|
||||
[
|
||||
(lambda r: r[0], "test_chat_assistant_0", 1, ""),
|
||||
(lambda r: r[0], "test_chat_assistant_1", 0, ""),
|
||||
(lambda r: r[0], "unknown", 0, ""),
|
||||
(lambda r: r[0], "test_chat_assistant_1", 0, "The chat doesn't exist"),
|
||||
(lambda r: r[0], "unknown", 0, "The chat doesn't exist"),
|
||||
("id", "chat_assistant_0", 0, "The chat doesn't exist"),
|
||||
],
|
||||
)
|
||||
def test_get_and_keywords_are_separate_lookups(self, client, add_chat_assistants, chat_assistant_id, keywords, expected_num, expected_message):
|
||||
def test_name_and_id(self, client, add_chat_assistants, chat_assistant_id, name, expected_num, expected_message):
|
||||
_, _, chat_assistants = add_chat_assistants
|
||||
chat_id = chat_assistant_id([chat.id for chat in chat_assistants]) if callable(chat_assistant_id) else chat_assistant_id
|
||||
if callable(chat_assistant_id):
|
||||
params = {"id": chat_assistant_id([chat.id for chat in chat_assistants]), "name": name}
|
||||
else:
|
||||
params = {"id": chat_assistant_id, "name": name}
|
||||
|
||||
if expected_message:
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
client.get_chat(chat_id)
|
||||
client.list_chats(**params)
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
client.get_chat(chat_id)
|
||||
assistants = client.list_chats(keywords=keywords)
|
||||
assistants = client.list_chats(**params)
|
||||
assert len(assistants) == expected_num
|
||||
|
||||
@pytest.mark.p3
|
||||
|
||||
@@ -13,16 +13,18 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
from operator import attrgetter
|
||||
|
||||
import pytest
|
||||
from configs import CHAT_ASSISTANT_NAME_LIMIT
|
||||
from ragflow_sdk import Chat
|
||||
from utils import encode_avatar
|
||||
from utils.file_utils import create_image_file
|
||||
|
||||
|
||||
class TestChatAssistantUpdate:
|
||||
@pytest.mark.p2
|
||||
def test_update_rejects_non_dict(self, add_chat_assistants_func):
|
||||
def test_update_rejects_non_dict_and_empty_llm_prompt(self, add_chat_assistants_func):
|
||||
_, _, chat_assistants = add_chat_assistants_func
|
||||
chat_assistant = chat_assistants[0]
|
||||
|
||||
@@ -30,6 +32,14 @@ class TestChatAssistantUpdate:
|
||||
chat_assistant.update.__wrapped__(chat_assistant, "bad")
|
||||
assert "`update_message` must be a dict" in str(exception_info.value)
|
||||
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
chat_assistant.update({"llm": {}})
|
||||
assert "`llm` cannot be empty" in str(exception_info.value)
|
||||
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
chat_assistant.update({"prompt": {}})
|
||||
assert "`prompt` cannot be empty" in str(exception_info.value)
|
||||
|
||||
@pytest.mark.p2
|
||||
def test_update_raises_on_nonzero_response(self, add_chat_assistants_func, monkeypatch):
|
||||
_, _, chat_assistants = add_chat_assistants_func
|
||||
@@ -39,35 +49,12 @@ class TestChatAssistantUpdate:
|
||||
def json(self):
|
||||
return {"code": 1, "message": "boom"}
|
||||
|
||||
monkeypatch.setattr(chat_assistant, "patch", lambda *_args, **_kwargs: _DummyResponse())
|
||||
monkeypatch.setattr(chat_assistant, "put", lambda *_args, **_kwargs: _DummyResponse())
|
||||
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
chat_assistant.update({"name": "error-case"})
|
||||
assert "boom" in str(exception_info.value)
|
||||
|
||||
@pytest.mark.p1
|
||||
def test_update_uses_patch_for_partial_payload(self, add_chat_assistants_func, monkeypatch):
|
||||
_, _, chat_assistants = add_chat_assistants_func
|
||||
chat_assistant = chat_assistants[0]
|
||||
captured = {}
|
||||
|
||||
class _DummyResponse:
|
||||
def json(self):
|
||||
return {"code": 0, "message": "ok"}
|
||||
|
||||
def _patch(path, payload):
|
||||
captured["path"] = path
|
||||
captured["payload"] = payload
|
||||
return _DummyResponse()
|
||||
|
||||
monkeypatch.setattr(chat_assistant, "patch", _patch)
|
||||
monkeypatch.setattr(chat_assistant, "put", lambda *_args, **_kwargs: pytest.fail("update() should not use PUT"))
|
||||
|
||||
chat_assistant.update({"name": "renamed"})
|
||||
|
||||
assert captured["path"] == f"/chats/{chat_assistant.id}"
|
||||
assert captured["payload"] == {"name": "renamed"}
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"payload, expected_message",
|
||||
[
|
||||
@@ -89,28 +76,29 @@ class TestChatAssistantUpdate:
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
chat_assistant.update(payload)
|
||||
updated_chat = client.get_chat(chat_assistant.id)
|
||||
updated_chat = client.list_chats(id=chat_assistant.id)[0]
|
||||
assert updated_chat.name == payload["name"], str(updated_chat)
|
||||
|
||||
@pytest.mark.p3
|
||||
def test_icon(self, client, add_chat_assistants_func, tmp_path):
|
||||
def test_avatar(self, client, add_chat_assistants_func, tmp_path):
|
||||
dataset, _, chat_assistants = add_chat_assistants_func
|
||||
chat_assistant = chat_assistants[0]
|
||||
|
||||
fn = create_image_file(tmp_path / "ragflow_test.png")
|
||||
payload = {"name": "icon_test", "icon": encode_avatar(fn), "dataset_ids": [dataset.id]}
|
||||
payload = {"name": "avatar_test", "avatar": encode_avatar(fn), "dataset_ids": [dataset.id]}
|
||||
|
||||
chat_assistant.update(payload)
|
||||
updated_chat = client.get_chat(chat_assistant.id)
|
||||
updated_chat = client.list_chats(id=chat_assistant.id)[0]
|
||||
assert updated_chat.name == payload["name"], str(updated_chat)
|
||||
assert updated_chat.icon is not None, str(updated_chat)
|
||||
assert updated_chat.avatar is not None, str(updated_chat)
|
||||
|
||||
@pytest.mark.p3
|
||||
@pytest.mark.parametrize(
|
||||
"llm_setting, expected_message",
|
||||
"llm, expected_message",
|
||||
[
|
||||
({}, "ValueError"),
|
||||
({"model_name": "glm-4"}, ""),
|
||||
({"model_name": "unknown"}, "`llm_id` unknown doesn't exist"),
|
||||
({"model_name": "unknown"}, "`model_name` unknown doesn't exist"),
|
||||
({"temperature": 0}, ""),
|
||||
({"temperature": 1}, ""),
|
||||
pytest.param({"temperature": -1}, "", marks=pytest.mark.skip),
|
||||
@@ -139,13 +127,10 @@ class TestChatAssistantUpdate:
|
||||
pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
|
||||
],
|
||||
)
|
||||
def test_llm_setting(self, client, add_chat_assistants_func, llm_setting, expected_message):
|
||||
def test_llm(self, client, add_chat_assistants_func, llm, expected_message):
|
||||
dataset, _, chat_assistants = add_chat_assistants_func
|
||||
chat_assistant = chat_assistants[0]
|
||||
llm_id = llm_setting.pop("model_name", None)
|
||||
payload = {"name": "llm_test", "dataset_ids": [dataset.id], "llm_setting": llm_setting}
|
||||
if llm_id is not None:
|
||||
payload["llm_id"] = llm_id
|
||||
payload = {"name": "llm_test", "llm": llm, "dataset_ids": [dataset.id]}
|
||||
|
||||
if expected_message:
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
@@ -153,16 +138,45 @@ class TestChatAssistantUpdate:
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
chat_assistant.update(payload)
|
||||
updated_chat = client.get_chat(chat_assistant.id)
|
||||
if llm_id:
|
||||
assert updated_chat.llm_id == llm_id, str(updated_chat)
|
||||
for k, v in llm_setting.items():
|
||||
assert getattr(updated_chat.llm_setting, k) == v, str(updated_chat)
|
||||
updated_chat = client.list_chats(id=chat_assistant.id)[0]
|
||||
if llm:
|
||||
for k, v in llm.items():
|
||||
assert attrgetter(k)(updated_chat.llm) == v, str(updated_chat)
|
||||
else:
|
||||
excepted_value = Chat.LLM(
|
||||
client,
|
||||
{
|
||||
"model_name": "glm-4-flash@ZHIPU-AI",
|
||||
"temperature": 0.1,
|
||||
"top_p": 0.3,
|
||||
"presence_penalty": 0.4,
|
||||
"frequency_penalty": 0.7,
|
||||
"max_tokens": 512,
|
||||
},
|
||||
)
|
||||
assert str(updated_chat.llm) == str(excepted_value), str(updated_chat)
|
||||
|
||||
@pytest.mark.p3
|
||||
@pytest.mark.parametrize(
|
||||
"prompt_config, expected_message",
|
||||
"prompt, expected_message",
|
||||
[
|
||||
({}, "ValueError"),
|
||||
({"similarity_threshold": 0}, ""),
|
||||
({"similarity_threshold": 1}, ""),
|
||||
pytest.param({"similarity_threshold": -1}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"similarity_threshold": 10}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"similarity_threshold": "a"}, "", marks=pytest.mark.skip),
|
||||
({"keywords_similarity_weight": 0}, ""),
|
||||
({"keywords_similarity_weight": 1}, ""),
|
||||
pytest.param({"keywords_similarity_weight": -1}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"keywords_similarity_weight": 10}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"keywords_similarity_weight": "a"}, "", marks=pytest.mark.skip),
|
||||
({"variables": []}, ""),
|
||||
({"top_n": 0}, ""),
|
||||
({"top_n": 1}, ""),
|
||||
pytest.param({"top_n": -1}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"top_n": 10}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"top_n": "a"}, "", marks=pytest.mark.skip),
|
||||
({"empty_response": "Hello World"}, ""),
|
||||
({"empty_response": ""}, ""),
|
||||
({"empty_response": "!@#$%^&*()"}, ""),
|
||||
@@ -170,29 +184,30 @@ class TestChatAssistantUpdate:
|
||||
pytest.param({"empty_response": 123}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"empty_response": True}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"empty_response": " "}, "", marks=pytest.mark.skip),
|
||||
({"prologue": "Hello World"}, ""),
|
||||
({"prologue": ""}, ""),
|
||||
({"prologue": "!@#$%^&*()"}, ""),
|
||||
({"prologue": "中文测试"}, ""),
|
||||
pytest.param({"prologue": 123}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"prologue": True}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"prologue": " "}, "", marks=pytest.mark.skip),
|
||||
({"quote": True}, ""),
|
||||
({"quote": False}, ""),
|
||||
({"system": "Hello World {knowledge}"}, ""),
|
||||
({"system": "{knowledge}"}, ""),
|
||||
({"system": "!@#$%^&*() {knowledge}"}, ""),
|
||||
({"system": "中文测试 {knowledge}"}, ""),
|
||||
({"system": "Hello World"}, ""),
|
||||
({"system": "Hello World", "parameters": []}, ""),
|
||||
pytest.param({"system": 123}, "", marks=pytest.mark.skip),
|
||||
({"opener": "Hello World"}, ""),
|
||||
({"opener": ""}, ""),
|
||||
({"opener": "!@#$%^&*()"}, ""),
|
||||
({"opener": "中文测试"}, ""),
|
||||
pytest.param({"opener": 123}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"opener": True}, "", marks=pytest.mark.skip),
|
||||
pytest.param({"opener": " "}, "", marks=pytest.mark.skip),
|
||||
({"show_quote": True}, ""),
|
||||
({"show_quote": False}, ""),
|
||||
({"prompt": "Hello World {knowledge}"}, ""),
|
||||
({"prompt": "{knowledge}"}, ""),
|
||||
({"prompt": "!@#$%^&*() {knowledge}"}, ""),
|
||||
({"prompt": "中文测试 {knowledge}"}, ""),
|
||||
({"prompt": "Hello World"}, ""),
|
||||
({"prompt": "Hello World", "variables": []}, ""),
|
||||
pytest.param({"prompt": 123}, """AttributeError("\'int\' object has no attribute \'find\'")""", marks=pytest.mark.skip),
|
||||
pytest.param({"prompt": True}, """AttributeError("\'int\' object has no attribute \'find\'")""", marks=pytest.mark.skip),
|
||||
pytest.param({"unknown": "unknown"}, "", marks=pytest.mark.skip),
|
||||
],
|
||||
)
|
||||
def test_prompt_config(self, client, add_chat_assistants_func, prompt_config, expected_message):
|
||||
def test_prompt(self, client, add_chat_assistants_func, prompt, expected_message):
|
||||
dataset, _, chat_assistants = add_chat_assistants_func
|
||||
chat_assistant = chat_assistants[0]
|
||||
payload = {"name": "prompt_test", "prompt_config": prompt_config, "dataset_ids": [dataset.id]}
|
||||
payload = {"name": "prompt_test", "prompt": prompt, "dataset_ids": [dataset.id]}
|
||||
|
||||
if expected_message:
|
||||
with pytest.raises(Exception) as exception_info:
|
||||
@@ -200,6 +215,26 @@ class TestChatAssistantUpdate:
|
||||
assert expected_message in str(exception_info.value)
|
||||
else:
|
||||
chat_assistant.update(payload)
|
||||
updated_chat = client.get_chat(chat_assistant.id)
|
||||
for k, v in prompt_config.items():
|
||||
assert getattr(updated_chat.prompt_config, k) == v, str(updated_chat)
|
||||
updated_chat = client.list_chats(id=chat_assistant.id)[0]
|
||||
if prompt:
|
||||
for k, v in prompt.items():
|
||||
if k == "keywords_similarity_weight":
|
||||
assert attrgetter(k)(updated_chat.prompt) == 1 - v, str(updated_chat)
|
||||
else:
|
||||
assert attrgetter(k)(updated_chat.prompt) == v, str(updated_chat)
|
||||
else:
|
||||
excepted_value = Chat.LLM(
|
||||
client,
|
||||
{
|
||||
"similarity_threshold": 0.2,
|
||||
"keywords_similarity_weight": 0.7,
|
||||
"top_n": 6,
|
||||
"variables": [{"key": "knowledge", "optional": False}],
|
||||
"rerank_model": "",
|
||||
"empty_response": "Sorry! No relevant content was found in the knowledge base!",
|
||||
"opener": "Hi! I'm your assistant. What can I do for you?",
|
||||
"show_quote": True,
|
||||
"prompt": 'You are an intelligent assistant. Please summarize the content of the dataset to answer the question. Please list the data in the dataset and answer in detail. When all dataset content is irrelevant to the question, your answer must include the sentence "The answer you are looking for is not found in the dataset!" Answers need to consider chat history.\n Here is the knowledge base:\n {knowledge}\n The above is the knowledge base.',
|
||||
},
|
||||
)
|
||||
assert str(updated_chat.prompt) == str(excepted_value), str(updated_chat)
|
||||
|
||||
Reference in New Issue
Block a user