diff --git a/videodb/_constants.py b/videodb/_constants.py index 99db326..9e35a39 100644 --- a/videodb/_constants.py +++ b/videodb/_constants.py @@ -123,11 +123,13 @@ class ApiPath: identities = "identities" merge = "merge" split = "split" + async_response = "async-response" class Status: processing = "processing" in_progress = "in progress" + complete = "complete" class MeetingStatus: diff --git a/videodb/_utils/_http_client.py b/videodb/_utils/_http_client.py index ecab5de..6507e38 100644 --- a/videodb/_utils/_http_client.py +++ b/videodb/_utils/_http_client.py @@ -95,7 +95,10 @@ def _make_request( response = method(url, headers=request_headers, timeout=timeout, **kwargs) response.raise_for_status() if not wait: - return response.json().get("data") + data = response.json().get("data") + if data is not None: + return data + return response.json() return self._parse_response(response) except requests.exceptions.RequestException as e: @@ -233,12 +236,12 @@ def _apply_poll_overrides(self, kwargs): ) def get( - self, path: str, show_progress: Optional[bool] = False, **kwargs + self, path: str, show_progress: Optional[bool] = False, wait: bool = True, **kwargs ) -> requests.Response: """Make a get request""" self.show_progress = show_progress self._apply_poll_overrides(kwargs) - return self._make_request(method=self.session.get, path=path, **kwargs) + return self._make_request(method=self.session.get, path=path, wait=wait, **kwargs) def post( self, path: str, data=None, show_progress: Optional[bool] = False, diff --git a/videodb/client.py b/videodb/client.py index 7eac7bb..81bfa96 100644 --- a/videodb/client.py +++ b/videodb/client.py @@ -494,3 +494,12 @@ def generate_client_token(self, expires_in: int = 86400) -> str: data={"expires_in": expires_in}, ) return response.get("token") + + def get_async_response(self, id: str) -> dict: + """Get the details of an async response. + + :param str id: ID of the async response + :return: Details of the async response + :rtype: dict + """ + return self.get(path=f"{ApiPath.async_response}/{id}", wait=False) diff --git a/videodb/collection.py b/videodb/collection.py index 73e06e9..07f94ba 100644 --- a/videodb/collection.py +++ b/videodb/collection.py @@ -425,6 +425,8 @@ def generate_text( prompt: str, model_name: Literal["basic", "pro", "ultra"] = "basic", response_type: Literal["text", "json"] = "text", + wait: bool = True, + callback_url: Optional[str] = None, ) -> Union[str, dict]: """Generate text from a prompt using genai offering. @@ -436,13 +438,16 @@ def generate_text( :param str prompt: Prompt for the text generation :param str model_name: Model name to use ("basic", "pro" or "ultra") :param str response_type: Desired response type ("text" or "json") - :return: Generated text response + :param bool wait: Wait for the text generation to complete (default: True) + :param str callback_url: URL to receive the callback (optional) + :return: Generated text response if wait is False, otherwise job id of the text generation :rtype: Union[str, dict] """ payload = { "prompt": prompt, "model_name": model_name, "response_type": response_type, + "callback_url": callback_url, } payload_size = len(json.dumps(payload).encode("utf-8")) @@ -457,11 +462,13 @@ def generate_text( ), "model_name": model_name, "response_type": response_type, + "callback_url": callback_url, } return self._connection.post( path=f"{ApiPath.collection}/{self.id}/{ApiPath.generate}/{ApiPath.text}", data=payload, + wait=wait, ) def dub_video(