2022-11-26 14:07:02 +00:00
{
"cells": [
{
"cell_type": "markdown",
"id": "9e9b7651",
"metadata": {},
"source": [
2023-03-27 02:49:46 +00:00
"# How to write a custom LLM wrapper\n",
2022-11-26 14:07:02 +00:00
"\n",
"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.\n",
"\n",
"There is only one required thing that a custom LLM needs to implement:\n",
"\n",
2022-12-15 15:53:32 +00:00
"1. A `_call` method that takes in a string, some optional stop words, and returns a string\n",
2022-11-26 14:07:02 +00:00
"\n",
"There is a second optional thing it can implement:\n",
"\n",
"1. An `_identifying_params` property that is used to help with printing of this class. Should return a dictionary.\n",
"\n",
"Let's implement a very simple custom LLM that just returns the first N characters of the input."
]
},
{
"cell_type": "code",
2023-04-30 18:14:09 +00:00
"execution_count": 6,
2022-11-26 14:07:02 +00:00
"id": "a65696a0",
"metadata": {},
"outputs": [],
"source": [
2023-04-30 18:14:09 +00:00
"from typing import Any, List, Mapping, Optional\n",
"\n",
"from langchain.callbacks.manager import CallbackManagerForLLMRun\n",
"from langchain.llms.base import LLM"
2022-11-26 14:07:02 +00:00
]
},
{
"cell_type": "code",
2023-04-30 18:14:09 +00:00
"execution_count": 7,
2022-11-26 14:07:02 +00:00
"id": "d5ceff02",
"metadata": {},
"outputs": [],
"source": [
"class CustomLLM(LLM):\n",
" \n",
2022-12-15 15:53:32 +00:00
" n: int\n",
" \n",
" @property\n",
" def _llm_type(self) -> str:\n",
" return \"custom\"\n",
2022-11-26 14:07:02 +00:00
" \n",
2023-04-30 18:14:09 +00:00
" def _call(\n",
" self,\n",
" prompt: str,\n",
" stop: Optional[List[str]] = None,\n",
" run_manager: Optional[CallbackManagerForLLMRun] = None,\n",
" ) -> str:\n",
2022-11-26 14:07:02 +00:00
" if stop is not None:\n",
" raise ValueError(\"stop kwargs are not permitted.\")\n",
" return prompt[:self.n]\n",
" \n",
" @property\n",
" def _identifying_params(self) -> Mapping[str, Any]:\n",
" \"\"\"Get the identifying parameters.\"\"\"\n",
" return {\"n\": self.n}"
]
},
{
"cell_type": "markdown",
"id": "714dede0",
"metadata": {},
"source": [
"We can now use this as an any other LLM."
]
},
{
"cell_type": "code",
2023-04-30 18:14:09 +00:00
"execution_count": 8,
2022-11-26 14:07:02 +00:00
"id": "10e5ece6",
"metadata": {},
"outputs": [],
"source": [
"llm = CustomLLM(n=10)"
]
},
{
"cell_type": "code",
2023-04-30 18:14:09 +00:00
"execution_count": 9,
2022-11-26 14:07:02 +00:00
"id": "8cd49199",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'This is a '"
]
},
2023-04-30 18:14:09 +00:00
"execution_count": 9,
2022-11-26 14:07:02 +00:00
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"llm(\"This is a foobar thing\")"
]
},
{
"cell_type": "markdown",
"id": "bbfebea1",
"metadata": {},
"source": [
"We can also print the LLM and see its custom print."
]
},
{
"cell_type": "code",
2023-04-30 18:14:09 +00:00
"execution_count": 10,
2022-11-26 14:07:02 +00:00
"id": "9c33fa19",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1mCustomLLM\u001b[0m\n",
"Params: {'n': 10}\n"
]
}
],
"source": [
"print(llm)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6dac3f47",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
2023-03-27 02:49:46 +00:00
"version": "3.9.1"
2022-11-26 14:07:02 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 5
}