Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãOPromptsLanguage modelsLLMsAsync APICustom LLMFake LLMHuman input LLMCachingSerializationStreamingTracking token usageChat modelsOutput parsersRetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreModulesModel I/‚ÄãOLanguage modelsLLMsCustom LLMCustom LLMThis notebook goes over how to create a custom LLM wrapper, in case you want to use your own LLM or a different wrapper than one that is supported in LangChain.There is only one required thing that a custom LLM needs to implement:A _call method that takes in a string, some optional stop words, and returns a stringThere is a second optional thing it can implement:An _identifying_params property that is used to help with printing of this class. Should return a dictionary.Let's implement a very simple custom LLM that just returns the first n characters of the input.from typing import Any, List, Mapping, Optionalfrom langchain.callbacks.manager import CallbackManagerForLLMRunfrom langchain.llms.base import LLMclass CustomLLM(LLM):    n: int    @property    def _llm_type(self) -> str:        return "custom"    def _call(        self,        prompt: str,        stop: Optional[List[str]] = None,        run_manager: Optional[CallbackManagerForLLMRun] = None,        **kwargs: Any,    ) -> str:        if stop is not None:            raise ValueError("stop kwargs are not permitted.")        return prompt[: self.n]    @property    def _identifying_params(self) -> Mapping[str, Any]:        """Get the identifying parameters."""        return {"n": self.n}We can now use this as an any other LLM.llm = CustomLLM(n=10)llm("This is a foobar thing")    'This is a 'We can also print the LLM and see its custom print.print(llm)    CustomLLM    Params:
This notebook goes over how to create a custom LLM wrapper, in case you want to use your own LLM or a different wrapper than one that is supported in LangChain.
This notebook goes over how to create a custom LLM wrapper, in case you want to use your own LLM or a different wrapper than one that is supported in LangChain. ->: Skip to main contentü¶úÔ∏èüîó LangChainDocsUse casesIntegrationsAPICommunityChat our docsLangSmithJS/TS DocsSearchCTRLKGet startedIntroductionInstallationQuickstartLangChain Expression LanguageInterfaceHow toCookbookLangChain Expression Language (LCEL)Why use LCEL?ModulesModel I/‚ÄãOPromptsLanguage modelsLLMsAsync APICustom LLMFake LLMHuman input LLMCachingSerializationStreamingTracking token usageChat modelsOutput parsersRetrievalChainsMemoryAgentsCallbacksModulesSecurityGuidesMoreModulesModel I/‚ÄãOLanguage modelsLLMsCustom LLMCustom LLMThis notebook goes over how to create a custom LLM wrapper, in case you want to use your own LLM or a different wrapper than one that is supported in LangChain.There is only one required thing that a custom LLM needs to implement:A _call method that takes in a string, some optional stop words, and returns a stringThere is a second optional thing it can implement:An _identifying_params property that is used to help with printing of this class. Should return a dictionary.Let's implement a very simple custom LLM that just returns the first n characters of the input.from typing import Any, List, Mapping, Optionalfrom langchain.callbacks.manager import CallbackManagerForLLMRunfrom langchain.llms.base import LLMclass CustomLLM(LLM):    n: int    @property    def _llm_type(self) -> str:        return "custom"    def _call(        self,        prompt: str,        stop: Optional[List[str]] = None,        run_manager: Optional[CallbackManagerForLLMRun] = None,        **kwargs: Any,    ) -> str:        if stop is not None:            raise ValueError("stop kwargs are not permitted.")        return prompt[: self.n]    @property    def _identifying_params(self) -> Mapping[str, Any]:        """Get the identifying parameters."""        return {"n": self.n}We can now use this as an any other LLM.llm = CustomLLM(n=10)llm("This is a foobar thing")    'This is a 'We can also print the LLM and see its custom print.print(llm)    CustomLLM    Params: