"""Base interface for large language models to expose."""
import inspect
import json
import warnings
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union

import yaml
from pydantic import Extra, Field, root_validator, validator

import langchain
from langchain.base_language import BaseLanguageModel
from langchain.callbacks.base import BaseCallbackManager
from langchain.callbacks.manager import (
    AsyncCallbackManager,
    AsyncCallbackManagerForLLMRun,
    CallbackManager,
    CallbackManagerForLLMRun,
    Callbacks,
)
from langchain.load.dump import dumpd
from langchain.schema import (
    AIMessage,
    BaseMessage,
    Generation,
    LLMResult,
    PromptValue,
    RunInfo,
    get_buffer_string,
)


def _get_verbosity() -> bool:
    return langchain.verbose


def get_prompts(
    params: Dict[str, Any], prompts: List[str]
) -> Tuple[Dict[int, List], str, List[int], List[str]]:
    """Get prompts that are already cached."""
    llm_string = str(sorted([(k, v) for k, v in params.items()]))
    missing_prompts = []
    missing_prompt_idxs = []
    existing_prompts = {}
    for i, prompt in enumerate(prompts):
        if langchain.llm_cache is not None:
            cache_val = langchain.llm_cache.lookup(prompt, llm_string)
            if isinstance(cache_val, list):
                existing_prompts[i] = cache_val
            else:
                missing_prompts.append(prompt)
                missing_prompt_idxs.append(i)
    return existing_prompts, llm_string, missing_prompt_idxs, missing_prompts


def update_cache(
    existing_prompts: Dict[int, List],
    llm_string: str,
    missing_prompt_idxs: List[int],
    new_results: LLMResult,
    prompts: List[str],
) -> Optional[dict]:
    """Update the cache and get the LLM output."""
    for i, result in enumerate(new_results.generations):
        existing_prompts[missing_prompt_idxs[i]] = result
        prompt = prompts[missing_prompt_idxs[i]]
        if langchain.llm_cache is not None:
            langchain.llm_cache.update(prompt, llm_string, result)
    llm_output = new_results.llm_output
    return llm_output


class BaseLLM(BaseLanguageModel, ABC):
    """LLM wrapper should take in a prompt and return a string."""

    cache: Optional[bool] = None
    verbose: bool = Field(default_factory=_get_verbosity)
    """Whether to print out response text."""
    callbacks: Callbacks = Field(default=None, exclude=True)
    callback_manager: Optional[BaseCallbackManager] = Field(default=None, exclude=True)
    tags: Optional[List[str]] = Field(default=None, exclude=True)
    """Tags to add to the run trace."""

    class Config:
        """Configuration for this pydantic object."""

        extra = Extra.forbid
        arbitrary_types_allowed = True

    @root_validator()
    def raise_deprecation(cls, values: Dict) -> Dict:
        """Raise deprecation warning if callback_manager is used."""
        if values.get("callback_manager") is not None:
            warnings.warn(
                "callback_manager is deprecated. Please use callbacks instead.",
                DeprecationWarning,
            )
            values["callbacks"] = values.pop("callback_manager", None)
        return values

    @validator("verbose", pre=True, always=True)
    def set_verbose(cls, verbose: Optional[bool]) -> bool:
        """If verbose is None, set it.

        This allows users to pass in None as verbose to access the global setting.
        """
        if verbose is None:
            return _get_verbosity()
        else:
            return verbose

    @abstractmethod
    def _generate(
        self,
        prompts: List[str],
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> LLMResult:
        """Run the LLM on the given prompts."""

    @abstractmethod
    async def _agenerate(
        self,
        prompts: List[str],
        stop: Optional[List[str]] = None,
        run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> LLMResult:
        """Run the LLM on the given prompts."""

    def generate_prompt(
        self,
        prompts: List[PromptValue],
        stop: Optional[List[str]] = None,
        callbacks: Callbacks = None,
        **kwargs: Any,
    ) -> LLMResult:
        prompt_strings = [p.to_string() for p in prompts]
        return self.generate(prompt_strings, stop=stop, callbacks=callbacks, **kwargs)

    async def agenerate_prompt(
        self,
        prompts: List[PromptValue],
        stop: Optional[List[str]] = None,
        callbacks: Callbacks = None,
        **kwargs: Any,
    ) -> LLMResult:
        prompt_strings = [p.to_string() for p in prompts]
        return await self.agenerate(
            prompt_strings, stop=stop, callbacks=callbacks, **kwargs
        )

    def generate(
        self,
        prompts: List[str],
        stop: Optional[List[str]] = None,
        callbacks: Callbacks = None,
        *,
        tags: Optional[List[str]] = None,
        **kwargs: Any,
    ) -> LLMResult:
        """Run the LLM on the given prompt and input."""
        # If string is passed in directly no errors will be raised but outputs will
        # not make sense.
        if not isinstance(prompts, list):
            raise ValueError(
                "Argument 'prompts' is expected to be of type List[str], received"
                f" argument of type {type(prompts)}."
            )
        params = self.dict()
        params["stop"] = stop
        options = {"stop": stop}
        (
            existing_prompts,
            llm_string,
            missing_prompt_idxs,
            missing_prompts,
        ) = get_prompts(params, prompts)
        disregard_cache = self.cache is not None and not self.cache
        callback_manager = CallbackManager.configure(
            callbacks, self.callbacks, self.verbose, tags, self.tags
        )
        new_arg_supported = inspect.signature(self._generate).parameters.get(
            "run_manager"
        )
        if langchain.llm_cache is None or disregard_cache:
            # This happens when langchain.cache is None, but self.cache is True
            if self.cache is not None and self.cache:
                raise ValueError(
                    "Asked to cache, but no cache found at `langchain.cache`."
                )
            run_manager = callback_manager.on_llm_start(
                dumpd(self), prompts, invocation_params=params, options=options
            )
            try:
                output = (
                    self._generate(
                        prompts, stop=stop, run_manager=run_manager, **kwargs
                    )
                    if new_arg_supported
                    else self._generate(prompts, stop=stop, **kwargs)
                )
            except (KeyboardInterrupt, Exception) as e:
                run_manager.on_llm_error(e)
                raise e
            run_manager.on_llm_end(output)
            if run_manager:
                output.run = RunInfo(run_id=run_manager.run_id)
            return output
        if len(missing_prompts) > 0:
            run_manager = callback_manager.on_llm_start(
                dumpd(self),
                missing_prompts,
                invocation_params=params,
                options=options,
            )
            try:
                new_results = (
                    self._generate(
                        missing_prompts, stop=stop, run_manager=run_manager, **kwargs
                    )
                    if new_arg_supported
                    else self._generate(missing_prompts, stop=stop, **kwargs)
                )
            except (KeyboardInterrupt, Exception) as e:
                run_manager.on_llm_error(e)
                raise e
            run_manager.on_llm_end(new_results)
            llm_output = update_cache(
                existing_prompts, llm_string, missing_prompt_idxs, new_results, prompts
            )
            run_info = None
            if run_manager:
                run_info = RunInfo(run_id=run_manager.run_id)
        else:
            llm_output = {}
            run_info = None
        generations = [existing_prompts[i] for i in range(len(prompts))]
        return LLMResult(generations=generations, llm_output=llm_output, run=run_info)

    async def agenerate(
        self,
        prompts: List[str],
        stop: Optional[List[str]] = None,
        callbacks: Callbacks = None,
        *,
        tags: Optional[List[str]] = None,
        **kwargs: Any,
    ) -> LLMResult:
        """Run the LLM on the given prompt and input."""
        params = self.dict()
        params["stop"] = stop
        options = {"stop": stop}
        (
            existing_prompts,
            llm_string,
            missing_prompt_idxs,
            missing_prompts,
        ) = get_prompts(params, prompts)
        disregard_cache = self.cache is not None and not self.cache
        callback_manager = AsyncCallbackManager.configure(
            callbacks, self.callbacks, self.verbose, tags, self.tags
        )
        new_arg_supported = inspect.signature(self._agenerate).parameters.get(
            "run_manager"
        )
        if langchain.llm_cache is None or disregard_cache:
            # This happens when langchain.cache is None, but self.cache is True
            if self.cache is not None and self.cache:
                raise ValueError(
                    "Asked to cache, but no cache found at `langchain.cache`."
                )
            run_manager = await callback_manager.on_llm_start(
                dumpd(self), prompts, invocation_params=params, options=options
            )
            try:
                output = (
                    await self._agenerate(
                        prompts, stop=stop, run_manager=run_manager, **kwargs
                    )
                    if new_arg_supported
                    else await self._agenerate(prompts, stop=stop, **kwargs)
                )
            except (KeyboardInterrupt, Exception) as e:
                await run_manager.on_llm_error(e, verbose=self.verbose)
                raise e
            await run_manager.on_llm_end(output, verbose=self.verbose)
            if run_manager:
                output.run = RunInfo(run_id=run_manager.run_id)
            return output
        if len(missing_prompts) > 0:
            run_manager = await callback_manager.on_llm_start(
                dumpd(self),
                missing_prompts,
                invocation_params=params,
                options=options,
            )
            try:
                new_results = (
                    await self._agenerate(
                        missing_prompts, stop=stop, run_manager=run_manager, **kwargs
                    )
                    if new_arg_supported
                    else await self._agenerate(missing_prompts, stop=stop, **kwargs)
                )
            except (KeyboardInterrupt, Exception) as e:
                await run_manager.on_llm_error(e)
                raise e
            await run_manager.on_llm_end(new_results)
            llm_output = update_cache(
                existing_prompts, llm_string, missing_prompt_idxs, new_results, prompts
            )
            run_info = None
            if run_manager:
                run_info = RunInfo(run_id=run_manager.run_id)
        else:
            llm_output = {}
            run_info = None
        generations = [existing_prompts[i] for i in range(len(prompts))]
        return LLMResult(generations=generations, llm_output=llm_output, run=run_info)

    def __call__(
        self,
        prompt: str,
        stop: Optional[List[str]] = None,
        callbacks: Callbacks = None,
        **kwargs: Any,
    ) -> str:
        """Check Cache and run the LLM on the given prompt and input."""
        if not isinstance(prompt, str):
            raise ValueError(
                "Argument `prompt` is expected to be a string. Instead found "
                f"{type(prompt)}. If you want to run the LLM on multiple prompts, use "
                "`generate` instead."
            )
        return (
            self.generate([prompt], stop=stop, callbacks=callbacks, **kwargs)
            .generations[0][0]
            .text
        )

    async def _call_async(
        self,
        prompt: str,
        stop: Optional[List[str]] = None,
        callbacks: Callbacks = None,
        **kwargs: Any,
    ) -> str:
        """Check Cache and run the LLM on the given prompt and input."""
        result = await self.agenerate(
            [prompt], stop=stop, callbacks=callbacks, **kwargs
        )
        return result.generations[0][0].text

    def predict(
        self, text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any
    ) -> str:
        if stop is None:
            _stop = None
        else:
            _stop = list(stop)
        return self(text, stop=_stop, **kwargs)

    def predict_messages(
        self,
        messages: List[BaseMessage],
        *,
        stop: Optional[Sequence[str]] = None,
        **kwargs: Any,
    ) -> BaseMessage:
        text = get_buffer_string(messages)
        if stop is None:
            _stop = None
        else:
            _stop = list(stop)
        content = self(text, stop=_stop, **kwargs)
        return AIMessage(content=content)

    async def apredict(
        self, text: str, *, stop: Optional[Sequence[str]] = None, **kwargs: Any
    ) -> str:
        if stop is None:
            _stop = None
        else:
            _stop = list(stop)
        return await self._call_async(text, stop=_stop, **kwargs)

    async def apredict_messages(
        self,
        messages: List[BaseMessage],
        *,
        stop: Optional[Sequence[str]] = None,
        **kwargs: Any,
    ) -> BaseMessage:
        text = get_buffer_string(messages)
        if stop is None:
            _stop = None
        else:
            _stop = list(stop)
        content = await self._call_async(text, stop=_stop, **kwargs)
        return AIMessage(content=content)

    @property
    def _identifying_params(self) -> Mapping[str, Any]:
        """Get the identifying parameters."""
        return {}

    def __str__(self) -> str:
        """Get a string representation of the object for printing."""
        cls_name = f"\033[1m{self.__class__.__name__}\033[0m"
        return f"{cls_name}\nParams: {self._identifying_params}"

    @property
    @abstractmethod
    def _llm_type(self) -> str:
        """Return type of llm."""

    def dict(self, **kwargs: Any) -> Dict:
        """Return a dictionary of the LLM."""
        starter_dict = dict(self._identifying_params)
        starter_dict["_type"] = self._llm_type
        return starter_dict

    def save(self, file_path: Union[Path, str]) -> None:
        """Save the LLM.

        Args:
            file_path: Path to file to save the LLM to.

        Example:
        .. code-block:: python

            llm.save(file_path="path/llm.yaml")
        """
        # Convert file to Path object.
        if isinstance(file_path, str):
            save_path = Path(file_path)
        else:
            save_path = file_path

        directory_path = save_path.parent
        directory_path.mkdir(parents=True, exist_ok=True)

        # Fetch dictionary to save
        prompt_dict = self.dict()

        if save_path.suffix == ".json":
            with open(file_path, "w") as f:
                json.dump(prompt_dict, f, indent=4)
        elif save_path.suffix == ".yaml":
            with open(file_path, "w") as f:
                yaml.dump(prompt_dict, f, default_flow_style=False)
        else:
            raise ValueError(f"{save_path} must be json or yaml")


class LLM(BaseLLM):
    """LLM class that expect subclasses to implement a simpler call method.

    The purpose of this class is to expose a simpler interface for working
    with LLMs, rather than expect the user to implement the full _generate method.
    """

    @abstractmethod
    def _call(
        self,
        prompt: str,
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> str:
        """Run the LLM on the given prompt and input."""

    async def _acall(
        self,
        prompt: str,
        stop: Optional[List[str]] = None,
        run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> str:
        """Run the LLM on the given prompt and input."""
        raise NotImplementedError("Async generation not implemented for this LLM.")

    def _generate(
        self,
        prompts: List[str],
        stop: Optional[List[str]] = None,
        run_manager: Optional[CallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> LLMResult:
        """Run the LLM on the given prompt and input."""
        # TODO: add caching here.
        generations = []
        new_arg_supported = inspect.signature(self._call).parameters.get("run_manager")
        for prompt in prompts:
            text = (
                self._call(prompt, stop=stop, run_manager=run_manager, **kwargs)
                if new_arg_supported
                else self._call(prompt, stop=stop, **kwargs)
            )
            generations.append([Generation(text=text)])
        return LLMResult(generations=generations)

    async def _agenerate(
        self,
        prompts: List[str],
        stop: Optional[List[str]] = None,
        run_manager: Optional[AsyncCallbackManagerForLLMRun] = None,
        **kwargs: Any,
    ) -> LLMResult:
        """Run the LLM on the given prompt and input."""
        generations = []
        new_arg_supported = inspect.signature(self._acall).parameters.get("run_manager")
        for prompt in prompts:
            text = (
                await self._acall(prompt, stop=stop, run_manager=run_manager, **kwargs)
                if new_arg_supported
                else await self._acall(prompt, stop=stop, **kwargs)
            )
            generations.append([Generation(text=text)])
        return LLMResult(generations=generations)

langchain.callbacks.manager.AsyncCallbackManager.configure langchain.schema.Generation langchain.load.dump.dumpd langchain.schema.get_buffer_string langchain.callbacks.manager.CallbackManager.configure langchain.schema.RunInfo langchain.schema.AIMessage langchain.llm_cache.lookup langchain.llm_cache.update langchain.schema.LLMResult
[((2353, 2390), 'pydantic.Field', 'Field', ([], {'default_factory': '_get_verbosity'}), '(default_factory=_get_verbosity)\n', (2358, 2390), False, 'from pydantic import Extra, Field, root_validator, validator\n'), ((2464, 2497), 'pydantic.Field', 'Field', ([], {'default': 'None', 'exclude': '(True)'}), '(default=None, exclude=True)\n', (2469, 2497), False, 'from pydantic import Extra, Field, root_validator, validator\n'), ((2552, 2585), 'pydantic.Field', 'Field', ([], {'default': 'None', 'exclude': '(True)'}), '(default=None, exclude=True)\n', (2557, 2585), False, 'from pydantic import Extra, Field, root_validator, validator\n'), ((2618, 2651), 'pydantic.Field', 'Field', ([], {'default': 'None', 'exclude': '(True)'}), '(default=None, exclude=True)\n', (2623, 2651), False, 'from pydantic import Extra, Field, root_validator, validator\n'), ((2840, 2856), 'pydantic.root_validator', 'root_validator', ([], {}), '()\n', (2854, 2856), False, 'from pydantic import Extra, Field, root_validator, validator\n'), ((3292, 3335), 'pydantic.validator', 'validator', (['"""verbose"""'], {'pre': '(True)', 'always': '(True)'}), "('verbose', pre=True, always=True)\n", (3301, 3335), False, 'from pydantic import Extra, Field, root_validator, validator\n'), ((5907, 5994), 'langchain.callbacks.manager.CallbackManager.configure', 'CallbackManager.configure', (['callbacks', 'self.callbacks', 'self.verbose', 'tags', 'self.tags'], {}), '(callbacks, self.callbacks, self.verbose, tags,\n    self.tags)\n', (5932, 5994), False, 'from langchain.callbacks.manager import AsyncCallbackManager, AsyncCallbackManagerForLLMRun, CallbackManager, CallbackManagerForLLMRun, Callbacks\n'), ((8360, 8431), 'langchain.schema.LLMResult', 'LLMResult', ([], {'generations': 'generations', 'llm_output': 'llm_output', 'run': 'run_info'}), '(generations=generations, llm_output=llm_output, run=run_info)\n', (8369, 8431), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((9086, 9178), 'langchain.callbacks.manager.AsyncCallbackManager.configure', 'AsyncCallbackManager.configure', (['callbacks', 'self.callbacks', 'self.verbose', 'tags', 'self.tags'], {}), '(callbacks, self.callbacks, self.verbose,\n    tags, self.tags)\n', (9116, 9178), False, 'from langchain.callbacks.manager import AsyncCallbackManager, AsyncCallbackManagerForLLMRun, CallbackManager, CallbackManagerForLLMRun, Callbacks\n'), ((11653, 11724), 'langchain.schema.LLMResult', 'LLMResult', ([], {'generations': 'generations', 'llm_output': 'llm_output', 'run': 'run_info'}), '(generations=generations, llm_output=llm_output, run=run_info)\n', (11662, 11724), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((13270, 13297), 'langchain.schema.get_buffer_string', 'get_buffer_string', (['messages'], {}), '(messages)\n', (13287, 13297), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((13459, 13485), 'langchain.schema.AIMessage', 'AIMessage', ([], {'content': 'content'}), '(content=content)\n', (13468, 13485), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((13968, 13995), 'langchain.schema.get_buffer_string', 'get_buffer_string', (['messages'], {}), '(messages)\n', (13985, 13995), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((14175, 14201), 'langchain.schema.AIMessage', 'AIMessage', ([], {'content': 'content'}), '(content=content)\n', (14184, 14201), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((17471, 17505), 'langchain.schema.LLMResult', 'LLMResult', ([], {'generations': 'generations'}), '(generations=generations)\n', (17480, 17505), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((18230, 18264), 'langchain.schema.LLMResult', 'LLMResult', ([], {'generations': 'generations'}), '(generations=generations)\n', (18239, 18264), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((1286, 1332), 'langchain.llm_cache.lookup', 'langchain.llm_cache.lookup', (['prompt', 'llm_string'], {}), '(prompt, llm_string)\n', (1312, 1332), False, 'import langchain\n'), ((2074, 2128), 'langchain.llm_cache.update', 'langchain.llm_cache.update', (['prompt', 'llm_string', 'result'], {}), '(prompt, llm_string, result)\n', (2100, 2128), False, 'import langchain\n'), ((3047, 3149), 'warnings.warn', 'warnings.warn', (['"""callback_manager is deprecated. Please use callbacks instead."""', 'DeprecationWarning'], {}), "('callback_manager is deprecated. Please use callbacks instead.',\n    DeprecationWarning)\n", (3060, 3149), False, 'import warnings\n'), ((15251, 15266), 'pathlib.Path', 'Path', (['file_path'], {}), '(file_path)\n', (15255, 15266), False, 'from pathlib import Path\n'), ((6524, 6535), 'langchain.load.dump.dumpd', 'dumpd', (['self'], {}), '(self)\n', (6529, 6535), False, 'from langchain.load.dump import dumpd\n'), ((7135, 7169), 'langchain.schema.RunInfo', 'RunInfo', ([], {'run_id': 'run_manager.run_id'}), '(run_id=run_manager.run_id)\n', (7142, 7169), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((7306, 7317), 'langchain.load.dump.dumpd', 'dumpd', (['self'], {}), '(self)\n', (7311, 7317), False, 'from langchain.load.dump import dumpd\n'), ((8167, 8201), 'langchain.schema.RunInfo', 'RunInfo', ([], {'run_id': 'run_manager.run_id'}), '(run_id=run_manager.run_id)\n', (8174, 8201), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((10396, 10430), 'langchain.schema.RunInfo', 'RunInfo', ([], {'run_id': 'run_manager.run_id'}), '(run_id=run_manager.run_id)\n', (10403, 10430), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((11460, 11494), 'langchain.schema.RunInfo', 'RunInfo', ([], {'run_id': 'run_manager.run_id'}), '(run_id=run_manager.run_id)\n', (11467, 11494), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((15587, 15622), 'json.dump', 'json.dump', (['prompt_dict', 'f'], {'indent': '(4)'}), '(prompt_dict, f, indent=4)\n', (15596, 15622), False, 'import json\n'), ((6041, 6074), 'inspect.signature', 'inspect.signature', (['self._generate'], {}), '(self._generate)\n', (6058, 6074), False, 'import inspect\n'), ((9225, 9259), 'inspect.signature', 'inspect.signature', (['self._agenerate'], {}), '(self._agenerate)\n', (9242, 9259), False, 'import inspect\n'), ((9715, 9726), 'langchain.load.dump.dumpd', 'dumpd', (['self'], {}), '(self)\n', (9720, 9726), False, 'from langchain.load.dump import dumpd\n'), ((10573, 10584), 'langchain.load.dump.dumpd', 'dumpd', (['self'], {}), '(self)\n', (10578, 10584), False, 'from langchain.load.dump import dumpd\n'), ((15725, 15776), 'yaml.dump', 'yaml.dump', (['prompt_dict', 'f'], {'default_flow_style': '(False)'}), '(prompt_dict, f, default_flow_style=False)\n', (15734, 15776), False, 'import yaml\n'), ((17095, 17124), 'inspect.signature', 'inspect.signature', (['self._call'], {}), '(self._call)\n', (17112, 17124), False, 'import inspect\n'), ((17432, 17453), 'langchain.schema.Generation', 'Generation', ([], {'text': 'text'}), '(text=text)\n', (17442, 17453), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n'), ((17839, 17869), 'inspect.signature', 'inspect.signature', (['self._acall'], {}), '(self._acall)\n', (17856, 17869), False, 'import inspect\n'), ((18191, 18212), 'langchain.schema.Generation', 'Generation', ([], {'text': 'text'}), '(text=text)\n', (18201, 18212), False, 'from langchain.schema import AIMessage, BaseMessage, Generation, LLMResult, PromptValue, RunInfo, get_buffer_string\n')]