custom guard getting started

This commit is contained in:
John (Jet Token)
2023-02-06 13:06:03 -08:00
parent e8a4c88b52
commit c357355575

View File

@@ -186,7 +186,41 @@
"source": [
"## @CustomGuard\n",
"\n",
"The custom guard allows you to easily turn any function into your own guard! "
"The custom guard allows you to easily turn any function into your own guard! The custom guard takes in a function and, like other guards, a number of retries. The function should take a string as input and return True if the string violates the guard and False if not. \n",
"\n",
"One of the best use cases for this guard would be to create your own local classifier model to, for example, classify text as \"on topic\" or \"off topic.\" Or, you may have a model that determines sentiment. You could take these models and add them to a custom guard to ensure that the output of your llm, chain, or agent is exactly inline with what you want it to be.\n",
"\n",
"Let your imagination run wild!\n",
"\n",
"Here's an example of a simple guard that prevents jokes from being returned that are too long."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2acaaf18",
"metadata": {},
"outputs": [],
"source": [
"from langchain import LLMChain, OpenAI, PromptTemplate\n",
"from langchain.utilities.guards import CustomGuard\n",
"\n",
"llm = OpenAI(temperature=0.9)\n",
"\n",
"prompt_template = \"Tell me a {adjective} joke\"\n",
"prompt = PromptTemplate(\n",
" input_variables=[\"adjective\"], template=prompt_template\n",
")\n",
"chain = LLMChain(llm=OpenAI(), prompt=prompt)\n",
"\n",
"def is_long(llm_output):\n",
" return len(llm_output) > 100\n",
"\n",
"@CustomGuard(guard_function=is_long, retries=1)\n",
"def call_chain():\n",
" return chain.run(adjective=\"political\")\n",
"\n",
"call_chain()"
]
}
],