|
| 1 | +from __future__ import annotations |
| 2 | +import json |
| 3 | +from typing import Optional, TYPE_CHECKING |
| 4 | +from urllib.parse import quote |
| 5 | + |
| 6 | +from ravendb.documents.operations.definitions import MaintenanceOperation |
| 7 | +from ravendb.documents.conventions import DocumentConventions |
| 8 | +from ravendb.http.raven_command import RavenCommand |
| 9 | +from ravendb.http.server_node import ServerNode |
| 10 | +from ravendb.documents.starting_point_change_vector import StartingPointChangeVector |
| 11 | +from ravendb.documents.operations.ai.ai_task_operation_results import AddGenAiOperationResult |
| 12 | +import requests |
| 13 | + |
| 14 | +from ravendb.util.util import RaftIdGenerator |
| 15 | + |
| 16 | +if TYPE_CHECKING: |
| 17 | + from ravendb.documents.operations.ai.gen_ai_configuration import GenAiConfiguration |
| 18 | + |
| 19 | + |
| 20 | +class AddGenAiOperation(MaintenanceOperation[AddGenAiOperationResult]): |
| 21 | + """ |
| 22 | + Operation to add a new GenAI task to the database. |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + configuration: GenAiConfiguration, |
| 28 | + starting_point: Optional[StartingPointChangeVector] = StartingPointChangeVector.LAST_DOCUMENT, |
| 29 | + ): |
| 30 | + if configuration is None: |
| 31 | + raise ValueError("configuration cannot be None") |
| 32 | + |
| 33 | + self._configuration = configuration |
| 34 | + self._starting_point = starting_point |
| 35 | + |
| 36 | + def get_command(self, conventions: DocumentConventions) -> RavenCommand[AddGenAiOperationResult]: |
| 37 | + return AddGenAiCommand(self._configuration, self._starting_point, conventions) |
| 38 | + |
| 39 | + |
| 40 | +class AddGenAiCommand(RavenCommand[AddGenAiOperationResult]): |
| 41 | + def __init__( |
| 42 | + self, |
| 43 | + configuration: GenAiConfiguration, |
| 44 | + starting_point: StartingPointChangeVector, |
| 45 | + conventions: DocumentConventions, |
| 46 | + ): |
| 47 | + super().__init__(AddGenAiOperationResult) |
| 48 | + self._configuration = configuration |
| 49 | + self._starting_point = starting_point |
| 50 | + self._conventions = conventions |
| 51 | + |
| 52 | + def is_read_request(self) -> bool: |
| 53 | + return False |
| 54 | + |
| 55 | + def create_request(self, node: ServerNode) -> requests.Request: |
| 56 | + url = f"{node.url}/databases/{node.database}/admin/etl?changeVector={quote(self._starting_point.value)}" |
| 57 | + |
| 58 | + body_json = self._configuration.to_json() |
| 59 | + body = json.dumps(body_json) |
| 60 | + |
| 61 | + request = requests.Request("PUT", url) |
| 62 | + request.headers = {"Content-Type": "application/json"} |
| 63 | + request.data = body |
| 64 | + return request |
| 65 | + |
| 66 | + def set_response(self, response: str, from_cache: bool) -> None: |
| 67 | + if response is None: |
| 68 | + self.result = AddGenAiOperationResult() |
| 69 | + return |
| 70 | + |
| 71 | + response_json = json.loads(response) |
| 72 | + self.result = AddGenAiOperationResult.from_json(response_json) |
| 73 | + |
| 74 | + def get_raft_unique_request_id(self) -> str: |
| 75 | + return RaftIdGenerator.new_id() |
0 commit comments