You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
langchain/docs/extras/modules/callbacks/custom_callbacks.ipynb

103 lines
2.7 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "0d9af580",
"metadata": {},
"source": [
"# Custom callback handlers\n",
"\n",
"You can create a custom handler to set on the object as well. In the example below, we'll implement streaming with a custom handler."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "ed9e8756",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"My custom handler, token: \n",
"My custom handler, token: Why\n",
"My custom handler, token: don\n",
"My custom handler, token: 't\n",
"My custom handler, token: scientists\n",
"My custom handler, token: trust\n",
"My custom handler, token: atoms\n",
"My custom handler, token: ?\n",
"My custom handler, token: \n",
"\n",
"\n",
"My custom handler, token: Because\n",
"My custom handler, token: they\n",
"My custom handler, token: make\n",
"My custom handler, token: up\n",
"My custom handler, token: everything\n",
"My custom handler, token: .\n",
"My custom handler, token: \n"
]
},
{
"data": {
"text/plain": [
"AIMessage(content=\"Why don't scientists trust atoms? \\n\\nBecause they make up everything.\", additional_kwargs={}, example=False)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from langchain.callbacks.base import BaseCallbackHandler\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.schema import HumanMessage\n",
"\n",
"\n",
"class MyCustomHandler(BaseCallbackHandler):\n",
" def on_llm_new_token(self, token: str, **kwargs) -> None:\n",
" print(f\"My custom handler, token: {token}\")\n",
"\n",
"\n",
"# To enable streaming, we pass in `streaming=True` to the ChatModel constructor\n",
"# Additionally, we pass in a list with our custom handler\n",
"chat = ChatOpenAI(max_tokens=25, streaming=True, callbacks=[MyCustomHandler()])\n",
"\n",
"chat([HumanMessage(content=\"Tell me a joke\")])"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "67ef5548",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"language": "python",
"name": "venv"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.3"
}
},
"nbformat": 4,
"nbformat_minor": 5
}