mirror of
https://github.com/hwchase17/langchain
synced 2024-10-29 17:07:25 +00:00
200 lines
7.2 KiB
Plaintext
200 lines
7.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Arthur"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"[Arthur](https://arthur.ai) is a model monitoring and observability platform.\n",
|
|
"\n",
|
|
"The following guide shows how to run a registered chat LLM with the Arthur callback handler to automatically log model inferences to Arthur.\n",
|
|
"\n",
|
|
"If you do not have a model currently onboarded to Arthur, visit our [onboarding guide for generative text models](https://docs.arthur.ai/user-guide/walkthroughs/model-onboarding/generative_text_onboarding.html). For more information about how to use the Arthur SDK, visit our [docs](https://docs.arthur.ai/)."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {
|
|
"id": "y8ku6X96sebl"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"from langchain.callbacks import ArthurCallbackHandler\n",
|
|
"from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler\n",
|
|
"from langchain.chat_models import ChatOpenAI\n",
|
|
"from langchain.schema import HumanMessage"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Place Arthur credentials here"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {
|
|
"id": "Me3prhqjsoqz"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"arthur_url = \"https://app.arthur.ai\"\n",
|
|
"arthur_login = \"your-arthur-login-username-here\"\n",
|
|
"arthur_model_id = \"your-arthur-model-id-here\""
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Create Langchain LLM with Arthur callback handler"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"id": "9Hq9snQasynA"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def make_langchain_chat_llm(chat_model=):\n",
|
|
" return ChatOpenAI(\n",
|
|
" streaming=True,\n",
|
|
" temperature=0.1,\n",
|
|
" callbacks=[\n",
|
|
" StreamingStdOutCallbackHandler(),\n",
|
|
" ArthurCallbackHandler.from_credentials(\n",
|
|
" arthur_model_id, \n",
|
|
" arthur_url=arthur_url, \n",
|
|
" arthur_login=arthur_login)\n",
|
|
" ])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Please enter password for admin: ········\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"chatgpt = make_langchain_chat_llm()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "aXRyj50Ls8eP"
|
|
},
|
|
"source": [
|
|
"Running the chat LLM with this `run` function will save the chat history in an ongoing list so that the conversation can reference earlier messages and log each response to the Arthur platform. You can view the history of this model's inferences on your [model dashboard page](https://app.arthur.ai/).\n",
|
|
"\n",
|
|
"Enter `q` to quit the run loop"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {
|
|
"id": "4taWSbN-s31Y"
|
|
},
|
|
"outputs": [],
|
|
"source": [
|
|
"def run(llm):\n",
|
|
" history = []\n",
|
|
" while True:\n",
|
|
" user_input = input(\"\\n>>> input >>>\\n>>>: \")\n",
|
|
" if user_input == \"q\":\n",
|
|
" break\n",
|
|
" history.append(HumanMessage(content=user_input))\n",
|
|
" history.append(llm(history))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {
|
|
"id": "MEx8nWJps-EG"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"\n",
|
|
">>> input >>>\n",
|
|
">>>: What is a callback handler?\n",
|
|
"A callback handler, also known as a callback function or callback method, is a piece of code that is executed in response to a specific event or condition. It is commonly used in programming languages that support event-driven or asynchronous programming paradigms.\n",
|
|
"\n",
|
|
"The purpose of a callback handler is to provide a way for developers to define custom behavior that should be executed when a certain event occurs. Instead of waiting for a result or blocking the execution, the program registers a callback function and continues with other tasks. When the event is triggered, the callback function is invoked, allowing the program to respond accordingly.\n",
|
|
"\n",
|
|
"Callback handlers are commonly used in various scenarios, such as handling user input, responding to network requests, processing asynchronous operations, and implementing event-driven architectures. They provide a flexible and modular way to handle events and decouple different components of a system.\n",
|
|
">>> input >>>\n",
|
|
">>>: What do I need to do to get the full benefits of this\n",
|
|
"To get the full benefits of using a callback handler, you should consider the following:\n",
|
|
"\n",
|
|
"1. Understand the event or condition: Identify the specific event or condition that you want to respond to with a callback handler. This could be user input, network requests, or any other asynchronous operation.\n",
|
|
"\n",
|
|
"2. Define the callback function: Create a function that will be executed when the event or condition occurs. This function should contain the desired behavior or actions you want to take in response to the event.\n",
|
|
"\n",
|
|
"3. Register the callback function: Depending on the programming language or framework you are using, you may need to register or attach the callback function to the appropriate event or condition. This ensures that the callback function is invoked when the event occurs.\n",
|
|
"\n",
|
|
"4. Handle the callback: Implement the necessary logic within the callback function to handle the event or condition. This could involve updating the user interface, processing data, making further requests, or triggering other actions.\n",
|
|
"\n",
|
|
"5. Consider error handling: It's important to handle any potential errors or exceptions that may occur within the callback function. This ensures that your program can gracefully handle unexpected situations and prevent crashes or undesired behavior.\n",
|
|
"\n",
|
|
"6. Maintain code readability and modularity: As your codebase grows, it's crucial to keep your callback handlers organized and maintainable. Consider using design patterns or architectural principles to structure your code in a modular and scalable way.\n",
|
|
"\n",
|
|
"By following these steps, you can leverage the benefits of callback handlers, such as asynchronous and event-driven programming, improved responsiveness, and modular code design.\n",
|
|
">>> input >>>\n",
|
|
">>>: q\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"run(chatgpt)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"colab": {
|
|
"provenance": []
|
|
},
|
|
"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",
|
|
"version": "3.10.11"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 1
|
|
}
|