Skip to content
Merged
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
38 changes: 38 additions & 0 deletions cohere_compass/clients/compass.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,10 @@ class _SendRequestResult:
"POST",
"indexes/{index_name}/documents/{document_id}/_add_attributes",
),
"set_document_governance": (
"POST",
"indexes/{index_name}/documents/{document_id}/_governance",
),
"upload_documents": (
"POST",
"indexes/{index_name}/documents/upload",
Expand Down Expand Up @@ -1313,6 +1317,40 @@ def add_attributes(
timeout=timeout,
)

def set_document_governance(
self,
*,
index_name: str,
document_id: str,
governance: GovernanceWrite,
max_retries: int | None = None,
retry_wait: timedelta | None = None,
timeout: timedelta | None = None,
):
"""
Relabel an indexed document's chunks, and set when they expire, in place.

:param index_name: The name of the index containing the document.
:param document_id: The ID of the document to relabel.
:param governance: the writer's delegated token, the new label and expiry.
:param max_retries: Maximum number of retries for failed requests. If not
provided, the default from the client will be used.
:param retry_wait: Time to wait between retries. If not provided, the default
from the client will be used.
:param timeout: Request timeout duration. If not provided, the default from the
client will be used.

"""
self._send_request(
api_name="set_document_governance",
document_id=document_id,
data=governance,
index_name=index_name,
max_retries=max_retries,
retry_wait=retry_wait,
timeout=timeout,
)

def insert_doc(
self,
*,
Expand Down
34 changes: 34 additions & 0 deletions cohere_compass/clients/compass_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,40 @@ async def add_attributes(
timeout=timeout,
)

async def set_document_governance(
self,
*,
index_name: str,
document_id: str,
governance: GovernanceWrite,
max_retries: int | None = None,
retry_wait: timedelta | None = None,
timeout: timedelta | None = None,
):
"""
Relabel an indexed document's chunks, and set when they expire, in place.

:param index_name: The name of the index containing the document.
:param document_id: The ID of the document to relabel.
:param governance: the writer's delegated token, the new label and expiry.
:param max_retries: Maximum number of retries for failed requests. If not
provided, the default from the client will be used.
:param retry_wait: Time to wait between retries. If not provided, the default
from the client will be used.
:param timeout: Request timeout duration. If not provided, the default from the
client will be used.

"""
await self._send_request(
api_name="set_document_governance",
document_id=document_id,
data=governance,
index_name=index_name,
max_retries=max_retries,
retry_wait=retry_wait,
timeout=timeout,
)

async def insert_doc(
self,
*,
Expand Down
4 changes: 3 additions & 1 deletion cohere_compass/models/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Python imports
import uuid
from dataclasses import field
from datetime import datetime
from enum import Enum
from typing import Annotated, Any, TypeAlias, cast

Expand Down Expand Up @@ -332,10 +333,11 @@ def permit_query(permit: GovernancePermit | None) -> dict[str, str]:


class GovernanceWrite(BaseModel):
"""Governance for a write: the writer's delegated token and the content's label."""
"""Governance for a write: the writer's token, the label and when it expires."""

actor: str
label: dict[str, list[str]]
expires: datetime | None = None


class UploadDocumentsInput(BaseModel):
Expand Down
15 changes: 13 additions & 2 deletions tests/test_compass_client.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import uuid
from collections.abc import Callable
from datetime import timedelta
from datetime import datetime, timedelta, timezone
from typing import Any, Literal, cast

import httpx
Expand Down Expand Up @@ -1985,4 +1985,15 @@ def test_governance(client: CompassClient, respx_mock: MockRouter, call: str, go
assert query == ({"permit_id": str(_PERMIT.permit_id), "permit_rule": "visible"} if governed else {})
else:
body = json.loads(request.content)
assert body.get("governance") == (governance.model_dump(mode="json") if governance else None)
assert body.get("governance") == (governance.model_dump(mode="json", exclude_none=True) if governance else None)


@respx.mock
def test_set_document_governance(client: CompassClient, respx_mock: MockRouter):
route = respx_mock.post("http://test.com/v1/indexes/i/documents/d/_governance").mock(
return_value=httpx.Response(200, json={})
)
expires = datetime(2027, 1, 1, tzinfo=timezone.utc)
governance = _WRITE.model_copy(update={"expires": expires})
client.set_document_governance(index_name="i", document_id="d", governance=governance)
assert json.loads(route.calls.last.request.content) == {**_WRITE.model_dump(), "expires": "2027-01-01T00:00:00Z"}
Loading