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

import typing

from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
from ..core.pagination import AsyncPager, SyncPager
from ..core.request_options import RequestOptions
from ..types.hallucination_correction_response import HallucinationCorrectionResponse
from ..types.hallucination_corrector import HallucinationCorrector
from ..types.hcm_source_document import HcmSourceDocument
from .raw_client import AsyncRawHallucinationCorrectorsClient, RawHallucinationCorrectorsClient

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


class HallucinationCorrectorsClient:
    def __init__(self, *, client_wrapper: SyncClientWrapper):
        self._raw_client = RawHallucinationCorrectorsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> RawHallucinationCorrectorsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        RawHallucinationCorrectorsClient
        """
        return self._raw_client

    def list(
        self,
        *,
        filter: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page_key: typing.Optional[str] = None,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> SyncPager[HallucinationCorrector]:
        """
        Retrieves a list of available hallucination correctors used for detecting and correcting hallucinations in AI-generated content. This endpoint supports filtering by name or description, pagination, and metadata for navigating large result sets.

        Parameters
        ----------
        filter : typing.Optional[str]
            A regular expression applied to the name and description fields. Use this to return only hallucination correctors that match specific keywords or naming conventions.

        limit : typing.Optional[int]
            The maximum number of hallucination correctors to return in the list. Defaults to 10. Range is between 1 and 100.

        page_key : typing.Optional[str]
            Retrieves the next page of hallucination correctors after reaching the limit.

        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.

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

        Returns
        -------
        SyncPager[HallucinationCorrector]
            List of hallucination correctors.

        Examples
        --------
        from vectara import Vectara

        client = Vectara(
            api_key="YOUR_API_KEY",
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        response = client.hallucination_correctors.list()
        for item in response:
            yield item
        # alternatively, you can paginate page-by-page
        for page in response.iter_pages():
            yield page
        """
        return self._raw_client.list(
            filter=filter,
            limit=limit,
            page_key=page_key,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )

    def hallucination_correction(
        self,
        *,
        generated_text: str,
        documents: typing.Sequence[HcmSourceDocument],
        model_name: str,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        query: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HallucinationCorrectionResponse:
        """
        This endpoint identifies information in generated text that is not supported by the provided source documents and offers corrections with minimal changes. This can be used standalone or as part of a RAG workflow where the HHEM score indicates potential hallucinations.

        Parameters
        ----------
        generated_text : str
            The generated text to be evaluated. The hallucination corrector reviews this text and applies corrections based on the provided source documents.

        documents : typing.Sequence[HcmSourceDocument]
            The source documents that were used to generate the text.

        model_name : str
            The name of the LLM model to use for hallucination correction.

        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.

        query : typing.Optional[str]
            Optional query that provides context for the expected response format and factual information. When provided, enables query-aware hallucination correction that considers the specific response format and factual context expected for the query.

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

        Returns
        -------
        HallucinationCorrectionResponse
            Successfully analyzed the text for hallucinations

        Examples
        --------
        from vectara import HcmSourceDocument, Vectara

        client = Vectara(
            api_key="YOUR_API_KEY",
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )
        client.hallucination_correctors.hallucination_correction(
            generated_text="generated_text",
            documents=[
                HcmSourceDocument(
                    text="text",
                )
            ],
            model_name="vhc-large-1.0",
        )
        """
        _response = self._raw_client.hallucination_correction(
            generated_text=generated_text,
            documents=documents,
            model_name=model_name,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            query=query,
            request_options=request_options,
        )
        return _response.data


class AsyncHallucinationCorrectorsClient:
    def __init__(self, *, client_wrapper: AsyncClientWrapper):
        self._raw_client = AsyncRawHallucinationCorrectorsClient(client_wrapper=client_wrapper)

    @property
    def with_raw_response(self) -> AsyncRawHallucinationCorrectorsClient:
        """
        Retrieves a raw implementation of this client that returns raw responses.

        Returns
        -------
        AsyncRawHallucinationCorrectorsClient
        """
        return self._raw_client

    async def list(
        self,
        *,
        filter: typing.Optional[str] = None,
        limit: typing.Optional[int] = None,
        page_key: typing.Optional[str] = None,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> AsyncPager[HallucinationCorrector]:
        """
        Retrieves a list of available hallucination correctors used for detecting and correcting hallucinations in AI-generated content. This endpoint supports filtering by name or description, pagination, and metadata for navigating large result sets.

        Parameters
        ----------
        filter : typing.Optional[str]
            A regular expression applied to the name and description fields. Use this to return only hallucination correctors that match specific keywords or naming conventions.

        limit : typing.Optional[int]
            The maximum number of hallucination correctors to return in the list. Defaults to 10. Range is between 1 and 100.

        page_key : typing.Optional[str]
            Retrieves the next page of hallucination correctors after reaching the limit.

        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.

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

        Returns
        -------
        AsyncPager[HallucinationCorrector]
            List of hallucination correctors.

        Examples
        --------
        import asyncio

        from vectara import AsyncVectara

        client = AsyncVectara(
            api_key="YOUR_API_KEY",
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )


        async def main() -> None:
            response = await client.hallucination_correctors.list()
            async for item in response:
                yield item

            # alternatively, you can paginate page-by-page
            async for page in response.iter_pages():
                yield page


        asyncio.run(main())
        """
        return await self._raw_client.list(
            filter=filter,
            limit=limit,
            page_key=page_key,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            request_options=request_options,
        )

    async def hallucination_correction(
        self,
        *,
        generated_text: str,
        documents: typing.Sequence[HcmSourceDocument],
        model_name: str,
        request_timeout: typing.Optional[int] = None,
        request_timeout_millis: typing.Optional[int] = None,
        query: typing.Optional[str] = OMIT,
        request_options: typing.Optional[RequestOptions] = None,
    ) -> HallucinationCorrectionResponse:
        """
        This endpoint identifies information in generated text that is not supported by the provided source documents and offers corrections with minimal changes. This can be used standalone or as part of a RAG workflow where the HHEM score indicates potential hallucinations.

        Parameters
        ----------
        generated_text : str
            The generated text to be evaluated. The hallucination corrector reviews this text and applies corrections based on the provided source documents.

        documents : typing.Sequence[HcmSourceDocument]
            The source documents that were used to generate the text.

        model_name : str
            The name of the LLM model to use for hallucination correction.

        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.

        query : typing.Optional[str]
            Optional query that provides context for the expected response format and factual information. When provided, enables query-aware hallucination correction that considers the specific response format and factual context expected for the query.

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

        Returns
        -------
        HallucinationCorrectionResponse
            Successfully analyzed the text for hallucinations

        Examples
        --------
        import asyncio

        from vectara import AsyncVectara, HcmSourceDocument

        client = AsyncVectara(
            api_key="YOUR_API_KEY",
            client_id="YOUR_CLIENT_ID",
            client_secret="YOUR_CLIENT_SECRET",
        )


        async def main() -> None:
            await client.hallucination_correctors.hallucination_correction(
                generated_text="generated_text",
                documents=[
                    HcmSourceDocument(
                        text="text",
                    )
                ],
                model_name="vhc-large-1.0",
            )


        asyncio.run(main())
        """
        _response = await self._raw_client.hallucination_correction(
            generated_text=generated_text,
            documents=documents,
            model_name=model_name,
            request_timeout=request_timeout,
            request_timeout_millis=request_timeout_millis,
            query=query,
            request_options=request_options,
        )
        return _response.data
