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

import typing
from json.decoder import JSONDecodeError

from ..core.api_error import ApiError
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.http_response import AsyncHttpResponse, HttpResponse
from ..core.pydantic_utilities import parse_obj_as
from ..core.request_options import RequestOptions
from ..core.serialization import convert_and_respect_annotation_metadata
from ..errors.bad_request_error import BadRequestError
from ..errors.forbidden_error import ForbiddenError
from ..errors.unprocessable_entity_error import UnprocessableEntityError
from ..types.bad_request_error_body import BadRequestErrorBody
from ..types.error import Error
from ..types.evaluate_factual_consistency_response import EvaluateFactualConsistencyResponse
from .types.evaluate_factual_consistency_request_model_parameters import (
    EvaluateFactualConsistencyRequestModelParameters,
)

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


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

    def evaluate(
        self,
        *,
        generated_text: str,
        source_texts: typing.Sequence[str],
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        model_parameters: typing.Optional[EvaluateFactualConsistencyRequestModelParameters] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HttpResponse[EvaluateFactualConsistencyResponse]:
        """
        Evaluate the factual consistency of a generated text (like a summary) against source documents.
        This determines how accurately the generated text reflects the information in the source documents,
        helping identify potential hallucinations or misrepresentations.

        Parameters
        ----------
        generated_text : str
            The generated text (e.g., summary or answer) to evaluate for factual consistency.

        source_texts : typing.Sequence[str]
            The source documents or text snippets against which to evaluate factual consistency.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

        model_parameters : typing.Optional[EvaluateFactualConsistencyRequestModelParameters]
            The model parameters for the evaluation.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        HttpResponse[EvaluateFactualConsistencyResponse]
            The factual consistency evaluation results.
        """
        _response = self._client_wrapper.httpx_client.request(
            "v2/evaluate_factual_consistency",
            base_url=self._client_wrapper.get_environment().default,
            method="POST",
            json={
                "model_parameters": convert_and_respect_annotation_metadata(
                    object_=model_parameters,
                    annotation=EvaluateFactualConsistencyRequestModelParameters,
                    direction="write",
                ),
                "generated_text": generated_text,
                "source_texts": source_texts,
            },
            headers={
                "content-type": "application/json",
                "Request-Timeout": str(request_timeout) if request_timeout is not None else None,
                "Request-Timeout-Millis": str(request_timeout_millis) if request_timeout_millis is not None else None,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    EvaluateFactualConsistencyResponse,
                    parse_obj_as(
                        type_=EvaluateFactualConsistencyResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return HttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        BadRequestErrorBody,
                        parse_obj_as(
                            type_=BadRequestErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _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 AsyncRawFactualConsistencyClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._client_wrapper = client_wrapper

    async def evaluate(
        self,
        *,
        generated_text: str,
        source_texts: typing.Sequence[str],
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        model_parameters: typing.Optional[EvaluateFactualConsistencyRequestModelParameters] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncHttpResponse[EvaluateFactualConsistencyResponse]:
        """
        Evaluate the factual consistency of a generated text (like a summary) against source documents.
        This determines how accurately the generated text reflects the information in the source documents,
        helping identify potential hallucinations or misrepresentations.

        Parameters
        ----------
        generated_text : str
            The generated text (e.g., summary or answer) to evaluate for factual consistency.

        source_texts : typing.Sequence[str]
            The source documents or text snippets against which to evaluate factual consistency.

        request_timeout : typing.Optional[int]
            The API will make a best effort to complete the request in the specified seconds or time out.

        request_timeout_millis : typing.Optional[int]
            The API will make a best effort to complete the request in the specified milliseconds or time out.

        model_parameters : typing.Optional[EvaluateFactualConsistencyRequestModelParameters]
            The model parameters for the evaluation.

        request_options : typing.Optional[RequestOptions]
            Request-specific configuration.

        Returns
        -------
        AsyncHttpResponse[EvaluateFactualConsistencyResponse]
            The factual consistency evaluation results.
        """
        _response = await self._client_wrapper.httpx_client.request(
            "v2/evaluate_factual_consistency",
            base_url=self._client_wrapper.get_environment().default,
            method="POST",
            json={
                "model_parameters": convert_and_respect_annotation_metadata(
                    object_=model_parameters,
                    annotation=EvaluateFactualConsistencyRequestModelParameters,
                    direction="write",
                ),
                "generated_text": generated_text,
                "source_texts": source_texts,
            },
            headers={
                "content-type": "application/json",
                "Request-Timeout": str(request_timeout) if request_timeout is not None else None,
                "Request-Timeout-Millis": str(request_timeout_millis) if request_timeout_millis is not None else None,
            },
            request_options=request_options,
            omit=OMIT,
        )
        try:
            if 200 <= _response.status_code < 300:
                _data = typing.cast(
                    EvaluateFactualConsistencyResponse,
                    parse_obj_as(
                        type_=EvaluateFactualConsistencyResponse,  # type: ignore
                        object_=_response.json(),
                    ),
                )
                return AsyncHttpResponse(response=_response, data=_data)
            if _response.status_code == 400:
                raise BadRequestError(
                    typing.cast(
                        BadRequestErrorBody,
                        parse_obj_as(
                            type_=BadRequestErrorBody,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 403:
                raise ForbiddenError(
                    typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            if _response.status_code == 422:
                raise UnprocessableEntityError(
                    typing.cast(
                        Error,
                        parse_obj_as(
                            type_=Error,  # type: ignore
                            object_=_response.json(),
                        ),
                    )
                )
            _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)
