# This file was auto-generated by Fern from our API Definition.

import typing
import urllib.parse
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.jsonable_encoder import jsonable_encoder
from ..core.pydantic_utilities import pydantic_v1
from ..core.remove_none_from_dict import remove_none_from_dict
from ..core.request_options import RequestOptions
from .errors.unprocessable_entity_error import UnprocessableEntityError
from .types.details import Details
from .types.http_validation_error import HttpValidationError
from .types.list_tunes_response import ListTunesResponse
from .types.tune import Tune
from .types.tune_type import TuneType

# this is used as the default value for optional parameters
OMIT = typing.cast(typing.Any, ...)


class FineTuningClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._client_wrapper = client_wrapper

    def create(
        self,
        *,
        continue_on_rejection: typing.Optional[bool] = OMIT,
        description: typing.Optional[str] = OMIT,
        details: Details,
        name: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Tune:
        """
        Spawn a tune.

        Parameters:
            - continue_on_rejection: typing.Optional[bool].

            - description: typing.Optional[str].

            - details: Details. Details of the tune.

            - name: str. The name of the tune.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import OctoAI
        from octoai.fine_tuning import (
            Details_LoraTune,
            LoraTuneCheckpoint,
            LoraTuneFile,
        )

        client = OctoAI(
            api_key="YOUR_API_KEY",
        )
        client.fine_tuning.create(
            continue_on_rejection=True,
            description="string",
            details=Details_LoraTune(
                base_checkpoint=LoraTuneCheckpoint(),
                files=[LoraTuneFile()],
                resize_images=True,
                seed=1,
                steps=1,
                trigger_words=["string"],
            ),
            name="string",
        )
        """
        _request: typing.Dict[str, typing.Any] = {"details": details, "name": name}
        if continue_on_rejection is not OMIT:
            _request["continue_on_rejection"] = continue_on_rejection
        if description is not OMIT:
            _request["description"] = description
        _response = self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_environment().default}/", "v1/tune"),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            json=jsonable_encoder(_request)
            if request_options is None or request_options.get("additional_body_parameters") is None
            else {
                **jsonable_encoder(_request),
                **(jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))),
            },
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(Tune, _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def get(self, tune_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Tune:
        """
        Get the specific tune.

        Parameters:
            - tune_id: str. The ID of the tune.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import OctoAI

        client = OctoAI(
            api_key="YOUR_API_KEY",
        )
        client.fine_tuning.get(
            tune_id="string",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_environment().default}/", f"v1/tune/{jsonable_encoder(tune_id)}"
            ),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(Tune, _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def delete(
        self, tune_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.Dict[str, typing.Any]:
        """
        Delete the specified tune.

        Parameters:
            - tune_id: str. The ID of the tune.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import OctoAI

        client = OctoAI(
            api_key="YOUR_API_KEY",
        )
        client.fine_tuning.delete(
            tune_id="tune_id",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_environment().default}/", f"v1/tune/{jsonable_encoder(tune_id)}"
            ),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(typing.Dict[str, typing.Any], _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def cancel(self, tune_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Tune:
        """
        Cancel the specified tune.

        Parameters:
            - tune_id: str. The ID of the tune to cancel.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import OctoAI

        client = OctoAI(
            api_key="YOUR_API_KEY",
        )
        client.fine_tuning.cancel(
            tune_id="string",
        )
        """
        _response = self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_environment().default}/", f"v1/tune/{jsonable_encoder(tune_id)}/cancel"
            ),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            json=jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))
            if request_options is not None
            else None,
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(Tune, _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    def list(
        self,
        *,
        offset: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        name: typing.Optional[str] = None,
        tune_type: typing.Optional[TuneType] = None,
        base_checkpoint_id: typing.Optional[str] = None,
        trigger_words: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        engine: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ListTunesResponse:
        """
        List all tunes owned by the current user.

        Parameters:
            - offset: typing.Optional[int]. Offset into the results.

            - limit: typing.Optional[int]. The max number of results to be shown (limit 100).

            - name: typing.Optional[str]. The name of the tune to filter on.

            - tune_type: typing.Optional[TuneType]. The type of the tune to filter on.

            - base_checkpoint_id: typing.Optional[str]. The base checkpoint ID for the LoRA tune.

            - trigger_words: typing.Optional[typing.Union[str, typing.Sequence[str]]]. The trigger words to filter on.

            - engine: typing.Optional[str]. The engine type.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import OctoAI

        client = OctoAI(
            api_key="YOUR_API_KEY",
        )
        client.fine_tuning.list()
        """
        _response = self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_environment().default}/", "v1/tunes"),
            params=jsonable_encoder(
                remove_none_from_dict(
                    {
                        "offset": offset,
                        "limit": limit,
                        "name": name,
                        "tune_type": tune_type,
                        "base_checkpoint_id": base_checkpoint_id,
                        "trigger_words": trigger_words,
                        "engine": engine,
                        **(
                            request_options.get("additional_query_parameters", {})
                            if request_options is not None
                            else {}
                        ),
                    }
                )
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(ListTunesResponse, _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)


class AsyncFineTuningClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def create(
        self,
        *,
        continue_on_rejection: typing.Optional[bool] = OMIT,
        description: typing.Optional[str] = OMIT,
        details: Details,
        name: str,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> Tune:
        """
        Spawn a tune.

        Parameters:
            - continue_on_rejection: typing.Optional[bool].

            - description: typing.Optional[str].

            - details: Details. Details of the tune.

            - name: str. The name of the tune.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import AsyncOctoAI
        from octoai.fine_tuning import (
            Details_LoraTune,
            LoraTuneCheckpoint,
            LoraTuneFile,
        )

        client = AsyncOctoAI(
            api_key="YOUR_API_KEY",
        )
        await client.fine_tuning.create(
            continue_on_rejection=True,
            description="string",
            details=Details_LoraTune(
                base_checkpoint=LoraTuneCheckpoint(),
                files=[LoraTuneFile()],
                resize_images=True,
                seed=1,
                steps=1,
                trigger_words=["string"],
            ),
            name="string",
        )
        """
        _request: typing.Dict[str, typing.Any] = {"details": details, "name": name}
        if continue_on_rejection is not OMIT:
            _request["continue_on_rejection"] = continue_on_rejection
        if description is not OMIT:
            _request["description"] = description
        _response = await self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(f"{self._client_wrapper.get_environment().default}/", "v1/tune"),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            json=jsonable_encoder(_request)
            if request_options is None or request_options.get("additional_body_parameters") is None
            else {
                **jsonable_encoder(_request),
                **(jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))),
            },
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(Tune, _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def get(self, tune_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Tune:
        """
        Get the specific tune.

        Parameters:
            - tune_id: str. The ID of the tune.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import AsyncOctoAI

        client = AsyncOctoAI(
            api_key="YOUR_API_KEY",
        )
        await client.fine_tuning.get(
            tune_id="string",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_environment().default}/", f"v1/tune/{jsonable_encoder(tune_id)}"
            ),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(Tune, _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def delete(
        self, tune_id: str, *, request_options: typing.Optional[RequestOptions] = None
    ) -> typing.Dict[str, typing.Any]:
        """
        Delete the specified tune.

        Parameters:
            - tune_id: str. The ID of the tune.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import AsyncOctoAI

        client = AsyncOctoAI(
            api_key="YOUR_API_KEY",
        )
        await client.fine_tuning.delete(
            tune_id="tune_id",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "DELETE",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_environment().default}/", f"v1/tune/{jsonable_encoder(tune_id)}"
            ),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(typing.Dict[str, typing.Any], _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def cancel(self, tune_id: str, *, request_options: typing.Optional[RequestOptions] = None) -> Tune:
        """
        Cancel the specified tune.

        Parameters:
            - tune_id: str. The ID of the tune to cancel.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import AsyncOctoAI

        client = AsyncOctoAI(
            api_key="YOUR_API_KEY",
        )
        await client.fine_tuning.cancel(
            tune_id="string",
        )
        """
        _response = await self._client_wrapper.httpx_client.request(
            "POST",
            urllib.parse.urljoin(
                f"{self._client_wrapper.get_environment().default}/", f"v1/tune/{jsonable_encoder(tune_id)}/cancel"
            ),
            params=jsonable_encoder(
                request_options.get("additional_query_parameters") if request_options is not None else None
            ),
            json=jsonable_encoder(remove_none_from_dict(request_options.get("additional_body_parameters", {})))
            if request_options is not None
            else None,
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(Tune, _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)

    async def list(
        self,
        *,
        offset: typing.Optional[int] = None,
        limit: typing.Optional[int] = None,
        name: typing.Optional[str] = None,
        tune_type: typing.Optional[TuneType] = None,
        base_checkpoint_id: typing.Optional[str] = None,
        trigger_words: typing.Optional[typing.Union[str, typing.Sequence[str]]] = None,
        engine: typing.Optional[str] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> ListTunesResponse:
        """
        List all tunes owned by the current user.

        Parameters:
            - offset: typing.Optional[int]. Offset into the results.

            - limit: typing.Optional[int]. The max number of results to be shown (limit 100).

            - name: typing.Optional[str]. The name of the tune to filter on.

            - tune_type: typing.Optional[TuneType]. The type of the tune to filter on.

            - base_checkpoint_id: typing.Optional[str]. The base checkpoint ID for the LoRA tune.

            - trigger_words: typing.Optional[typing.Union[str, typing.Sequence[str]]]. The trigger words to filter on.

            - engine: typing.Optional[str]. The engine type.

            - request_options: typing.Optional[RequestOptions]. Request-specific configuration.
        ---
        from octoai.client import AsyncOctoAI

        client = AsyncOctoAI(
            api_key="YOUR_API_KEY",
        )
        await client.fine_tuning.list()
        """
        _response = await self._client_wrapper.httpx_client.request(
            "GET",
            urllib.parse.urljoin(f"{self._client_wrapper.get_environment().default}/", "v1/tunes"),
            params=jsonable_encoder(
                remove_none_from_dict(
                    {
                        "offset": offset,
                        "limit": limit,
                        "name": name,
                        "tune_type": tune_type,
                        "base_checkpoint_id": base_checkpoint_id,
                        "trigger_words": trigger_words,
                        "engine": engine,
                        **(
                            request_options.get("additional_query_parameters", {})
                            if request_options is not None
                            else {}
                        ),
                    }
                )
            ),
            headers=jsonable_encoder(
                remove_none_from_dict(
                    {
                        **self._client_wrapper.get_headers(),
                        **(request_options.get("additional_headers", {}) if request_options is not None else {}),
                    }
                )
            ),
            timeout=request_options.get("timeout_in_seconds")
            if request_options is not None and request_options.get("timeout_in_seconds") is not None
            else self._client_wrapper.get_timeout(),
            retries=0,
            max_retries=request_options.get("max_retries") if request_options is not None else 0,  # type: ignore
        )
        if 200 <= _response.status_code < 300:
            return pydantic_v1.parse_obj_as(ListTunesResponse, _response.json())  # type: ignore
        if _response.status_code == 422:
            raise UnprocessableEntityError(
                pydantic_v1.parse_obj_as(HttpValidationError, _response.json())  # type: ignore
            )
        try:
            _response_json = _response.json()
        except JSONDecodeError:
            raise ApiError(status_code=_response.status_code, body=_response.text)
        raise ApiError(status_code=_response.status_code, body=_response_json)
