diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index 79659073..6f4f3eee 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -565,6 +565,18 @@ update_prefix_search_settings_1: |- client.index('books').update_prefix_search(PrefixSearch.DISABLED) reset_prefix_search_settings_1: |- client.index('books').reset_prefix_search() +get_chat_settings_1: |- + client.index('books').get_chat_settings() +update_chat_settings_1: |- + client.update_experimental_features({'chatCompletions': True}) + client.index('books').update_chat_settings({ + 'description': 'A collection of books with titles, authors and genres', + 'documentTemplate': 'A book titled {{ doc.title }} by {{ doc.author }}', + 'documentTemplateMaxBytes': 400, + 'searchParameters': {'limit': 5} + }) +reset_chat_settings_1: |- + client.index('books').reset_chat_settings() get_proximity_precision_settings_1: |- client.index('books').get_proximity_precision() update_proximity_precision_settings_1: |- diff --git a/meilisearch/client.py b/meilisearch/client.py index 8003f006..f6d64314 100644 --- a/meilisearch/client.py +++ b/meilisearch/client.py @@ -1360,6 +1360,82 @@ def update_chat_workspace_settings( return self.http.patch(f"chats/{workspace_uid}/settings", body=settings) + def get_chat_workspace(self, workspace_uid: str) -> dict[str, Any]: + """Get a chat workspace. + + Parameters + ---------- + workspace_uid: + Unique identifier of the chat workspace. + + Returns + ------- + workspace: + Dictionary containing the workspace uid. + + Raises + ------ + MeilisearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors + ValueError + If workspace_uid is empty or contains path separators or URL delimiters (? or #). + """ + self._validate_chat_workspace_uid(workspace_uid) + + return self.http.get(f"chats/{workspace_uid}") + + def delete_chat_workspace(self, workspace_uid: str) -> None: + """Delete a chat workspace and its settings. + + Parameters + ---------- + workspace_uid: + Unique identifier of the chat workspace. + + Raises + ------ + MeilisearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors + ValueError + If workspace_uid is empty or contains path separators or URL delimiters (? or #). + """ + self._validate_chat_workspace_uid(workspace_uid) + + self.http.delete(f"chats/{workspace_uid}") + + def reset_chat_workspace_settings(self, workspace_uid: str) -> dict[str, Any]: + """Reset the settings of a chat workspace to their default values. + + Parameters + ---------- + workspace_uid: + Unique identifier of the chat workspace. + + Returns + ------- + settings: + Dictionary containing the default workspace settings. + + Raises + ------ + MeilisearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors + ValueError + If workspace_uid is empty or contains path separators or URL delimiters (? or #). + """ + self._validate_chat_workspace_uid(workspace_uid) + + return self.http.delete(f"chats/{workspace_uid}/settings") + + @staticmethod + def _validate_chat_workspace_uid(workspace_uid: str) -> None: + if not workspace_uid: + raise ValueError("workspace_uid is required and cannot be empty") + if "/" in workspace_uid or "\\" in workspace_uid: + raise ValueError("Invalid workspace_uid: must not contain path separators") + if "?" in workspace_uid or "#" in workspace_uid: + raise ValueError("Invalid workspace_uid: must not contain URL delimiters") + @staticmethod def _base64url_encode(data: bytes) -> str: return base64.urlsafe_b64encode(data).decode("utf-8").replace("=", "") diff --git a/meilisearch/config.py b/meilisearch/config.py index 993d7d38..2b35da66 100644 --- a/meilisearch/config.py +++ b/meilisearch/config.py @@ -42,6 +42,7 @@ class Paths: embedders = "embedders" search_cutoff_ms = "search-cutoff-ms" prefix_search = "prefix-search" + chat = "chat" proximity_precision = "proximity-precision" localized_attributes = "localized-attributes" fields = "fields" diff --git a/meilisearch/index.py b/meilisearch/index.py index 82a74fed..c459cfcc 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -2515,6 +2515,73 @@ def reset_prefix_search(self) -> TaskInfo: return TaskInfo(**task) + # CHAT SETTINGS + + def get_chat_settings(self) -> dict[str, Any]: + """Get the chat settings of an index. + + These settings describe the index to the LLM used by conversational search. + https://www.meilisearch.com/docs/reference/api/settings/get-chat + + Returns + ------- + settings: + Dictionary containing the chat settings of the index. + + Raises + ------ + MeilisearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors + """ + return self.http.get(self.__settings_url_for(self.config.paths.chat)) + + def update_chat_settings(self, body: Mapping[str, Any]) -> TaskInfo: + """Update the chat settings of the index. + + Updating chat settings requires the `chatCompletions` experimental feature. + https://www.meilisearch.com/docs/reference/api/settings/update-chat + + Parameters + ---------- + body: + Dictionary containing the chat settings, for example `description`, + `documentTemplate`, `documentTemplateMaxBytes` and `searchParameters`. + + Returns + ------- + task_info: + TaskInfo instance containing information about a task to track the progress of an asynchronous process. + https://www.meilisearch.com/docs/reference/api/tasks#get-one-task + + Raises + ------ + MeilisearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors + """ + task = self.http.patch(path=self.__settings_url_for(self.config.paths.chat), body=body) + + return TaskInfo(**task) + + def reset_chat_settings(self) -> TaskInfo: + """Reset the chat settings of the index to default values. + + https://www.meilisearch.com/docs/reference/api/settings/reset-chat + + Returns + ------- + task_info: + TaskInfo instance containing information about a task to track the progress of an asynchronous process. + https://www.meilisearch.com/docs/reference/api/tasks#get-one-task + + Raises + ------ + MeilisearchApiError + An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors + """ + task = self.http.delete(self.__settings_url_for(self.config.paths.chat)) + + return TaskInfo(**task) + # PROXIMITY PRECISION SETTINGS def get_proximity_precision(self) -> ProximityPrecision: diff --git a/tests/client/test_chat_workspaces.py b/tests/client/test_chat_workspaces.py new file mode 100644 index 00000000..f6e41692 --- /dev/null +++ b/tests/client/test_chat_workspaces.py @@ -0,0 +1,56 @@ +import pytest + +from meilisearch.errors import MeilisearchApiError + +WORKSPACE = "test-workspace" + + +@pytest.fixture +def chat_workspace(client): + """Enable the chat completions feature and create a workspace.""" + client.update_experimental_features({"chatCompletions": True}) + client.update_chat_workspace_settings(WORKSPACE, {"source": "openAi", "apiKey": "test-key"}) + yield WORKSPACE + try: + client.delete_chat_workspace(WORKSPACE) + except MeilisearchApiError: + pass + client.update_experimental_features({"chatCompletions": False}) + + +def test_get_chat_workspace(client, chat_workspace): + """Test that an existing workspace can be fetched.""" + assert client.get_chat_workspace(chat_workspace) == {"uid": chat_workspace} + + +def test_get_chat_workspace_not_found(client, chat_workspace): + """Test that fetching an unknown workspace raises an API error.""" + with pytest.raises(MeilisearchApiError) as exc: + client.get_chat_workspace("does-not-exist") + assert exc.value.code == "chat_not_found" + + +def test_reset_chat_workspace_settings(client, chat_workspace): + """Test that resetting settings removes the stored API key.""" + result = client.reset_chat_workspace_settings(chat_workspace) + assert result["apiKey"] is None + assert client.get_chat_workspace_settings(chat_workspace)["apiKey"] is None + + +def test_delete_chat_workspace(client, chat_workspace): + """Test that a deleted workspace can no longer be fetched.""" + client.delete_chat_workspace(chat_workspace) + with pytest.raises(MeilisearchApiError) as exc: + client.get_chat_workspace(chat_workspace) + assert exc.value.code == "chat_not_found" + + +@pytest.mark.parametrize( + "method", + ["get_chat_workspace", "delete_chat_workspace", "reset_chat_workspace_settings"], +) +@pytest.mark.parametrize("uid", ["", "a/b", "a\\b", "a?b", "a#b"]) +def test_chat_workspace_methods_reject_invalid_uid(client, method, uid): + """Test that an empty uid or one with path separators or URL delimiters is rejected before any request.""" + with pytest.raises(ValueError): + getattr(client, method)(uid) diff --git a/tests/settings/test_settings_chat_meilisearch.py b/tests/settings/test_settings_chat_meilisearch.py new file mode 100644 index 00000000..a754d5fd --- /dev/null +++ b/tests/settings/test_settings_chat_meilisearch.py @@ -0,0 +1,68 @@ +import pytest + +DEFAULT_CHAT_SETTINGS_KEYS = { + "description", + "documentTemplate", + "documentTemplateMaxBytes", + "searchParameters", +} + +NEW_CHAT_SETTINGS = { + "description": "A collection of movies", + "documentTemplate": "A movie titled {{ doc.title }}", + "documentTemplateMaxBytes": 200, + "searchParameters": {"limit": 5}, +} + + +@pytest.fixture +def chat_completions_enabled(client): + """Updating index chat settings requires the chatCompletions experimental feature.""" + previous = client.get_experimental_features().get("chatCompletions", False) + client.update_experimental_features({"chatCompletions": True}) + yield + client.update_experimental_features({"chatCompletions": previous}) + + +def test_get_chat_settings(empty_index): + response = empty_index().get_chat_settings() + + assert DEFAULT_CHAT_SETTINGS_KEYS == set(response) + assert response["description"] == "" + assert response["documentTemplateMaxBytes"] == 400 + assert response["searchParameters"] == {} + + +def test_update_chat_settings(empty_index, chat_completions_enabled): + index = empty_index() + + response = index.update_chat_settings(NEW_CHAT_SETTINGS) + index.wait_for_task(response.task_uid) + response = index.get_chat_settings() + + assert response == NEW_CHAT_SETTINGS + + +def test_update_chat_settings_partial(empty_index, chat_completions_enabled): + index = empty_index() + index.wait_for_task(index.update_chat_settings(NEW_CHAT_SETTINGS).task_uid) + + response = index.update_chat_settings({"description": "Updated"}) + index.wait_for_task(response.task_uid) + response = index.get_chat_settings() + + assert response["description"] == "Updated" + assert response["documentTemplate"] == NEW_CHAT_SETTINGS["documentTemplate"] + assert response["searchParameters"] == NEW_CHAT_SETTINGS["searchParameters"] + + +def test_reset_chat_settings(empty_index, chat_completions_enabled): + index = empty_index() + default_settings = index.get_chat_settings() + index.wait_for_task(index.update_chat_settings(NEW_CHAT_SETTINGS).task_uid) + assert index.get_chat_settings() != default_settings + + response = index.reset_chat_settings() + index.wait_for_task(response.task_uid) + + assert index.get_chat_settings() == default_settings