MCPcopy
hub / github.com/langchain-ai/langchain / generate

Method generate

libs/core/langchain_core/language_models/llms.py:680–839  ·  view source on GitHub ↗

Pass a sequence of prompts to a model and return generations. This method should make use of batched calls for models that expose a batched API. Use this method when you want to: 1. take advantage of batched calls, 2. need more output from the model

(
        self,
        prompts: List[str],
        stop: Optional[List[str]] = None,
        callbacks: Optional[Union[Callbacks, List[Callbacks]]] = None,
        *,
        tags: Optional[Union[List[str], List[List[str]]]] = None,
        metadata: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None,
        run_name: Optional[Union[str, List[str]]] = None,
        run_id: Optional[Union[uuid.UUID, List[Optional[uuid.UUID]]]] = None,
        **kwargs: Any,
    )

Source from the content-addressed store, hash-verified

678 return output
679
680 def generate(
681 self,
682 prompts: List[str],
683 stop: Optional[List[str]] = None,
684 callbacks: Optional[Union[Callbacks, List[Callbacks]]] = None,
685 *,
686 tags: Optional[Union[List[str], List[List[str]]]] = None,
687 metadata: Optional[Union[Dict[str, Any], List[Dict[str, Any]]]] = None,
688 run_name: Optional[Union[str, List[str]]] = None,
689 run_id: Optional[Union[uuid.UUID, List[Optional[uuid.UUID]]]] = None,
690 **kwargs: Any,
691 ) -> LLMResult:
692 """Pass a sequence of prompts to a model and return generations.
693
694 This method should make use of batched calls for models that expose a batched
695 API.
696
697 Use this method when you want to:
698 1. take advantage of batched calls,
699 2. need more output from the model than just the top generated value,
700 3. are building chains that are agnostic to the underlying language model
701 type (e.g., pure text completion models vs chat models).
702
703 Args:
704 prompts: List of string prompts.
705 stop: Stop words to use when generating. Model output is cut off at the
706 first occurrence of any of these substrings.
707 callbacks: Callbacks to pass through. Used for executing additional
708 functionality, such as logging or streaming, throughout generation.
709 **kwargs: Arbitrary additional keyword arguments. These are usually passed
710 to the model provider API call.
711
712 Returns:
713 An LLMResult, which contains a list of candidate Generations for each input
714 prompt and additional model provider-specific output.
715 """
716 if not isinstance(prompts, list):
717 raise ValueError(
718 "Argument 'prompts' is expected to be of type List[str], received"
719 f" argument of type {type(prompts)}."
720 )
721 # Create callback managers
722 if (
723 isinstance(callbacks, list)
724 and callbacks
725 and (
726 isinstance(callbacks[0], (list, BaseCallbackManager))
727 or callbacks[0] is None
728 )
729 ):
730 # We've received a list of callbacks args to apply to each input
731 assert len(callbacks) == len(prompts)
732 assert tags is None or (
733 isinstance(tags, list) and len(tags) == len(prompts)
734 )
735 assert metadata is None or (
736 isinstance(metadata, list) and len(metadata) == len(prompts)
737 )

Callers 4

generate_promptMethod · 0.95
__call__Method · 0.95

Calls 12

_get_run_ids_listMethod · 0.95
dictMethod · 0.95
_generate_helperMethod · 0.95
get_llm_cacheFunction · 0.90
dumpdFunction · 0.90
RunInfoClass · 0.90
LLMResultClass · 0.90
get_promptsFunction · 0.85
update_cacheFunction · 0.85
configureMethod · 0.45
getMethod · 0.45
on_llm_startMethod · 0.45

Tested by 2