Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Comment thread
coderabbitai[bot] marked this conversation as resolved.
'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: |-
Expand Down
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
67 changes: 67 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
68 changes: 68 additions & 0 deletions tests/settings/test_settings_chat_meilisearch.py
Original file line number Diff line number Diff line change
@@ -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
Loading