{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Runtime Logging with AutoGen \n", "\n", "AutoGen offers utilities to log data for debugging and performance analysis. This notebook demonstrates how to use them. \n", "\n", "we log data in different modes:\n", "- SQlite Database\n", "- File \n", "\n", "In general, users can initiate logging by calling `autogen.runtime_logging.start()` and stop logging by calling `autogen.runtime_logging.stop()`" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Logging session ID: 6e08f3e0-392b-434e-8b69-4ab36c4fcf99\n", "\u001B[33muser_proxy\u001B[0m (to assistant):\n", "\n", "What is the height of the Eiffel Tower? Only respond with the answer and terminate\n", "\n", "--------------------------------------------------------------------------------\n", "\u001B[33massistant\u001B[0m (to user_proxy):\n", "\n", "The height of the Eiffel Tower is approximately 330 meters.\n", "\n", "TERMINATE\n", "\n", "--------------------------------------------------------------------------------\n" ] } ], "source": [ "import json\n", "\n", "import pandas as pd\n", "\n", "import autogen\n", "from autogen import AssistantAgent, UserProxyAgent\n", "\n", "# Setup API key. Add your own API key to config file or environment variable\n", "llm_config = {\n", " \"config_list\": autogen.config_list_from_json(\n", " env_or_file=\"OAI_CONFIG_LIST\",\n", " ),\n", " \"temperature\": 0.9,\n", "}\n", "\n", "# Start logging\n", "logging_session_id = autogen.runtime_logging.start(config={\"dbname\": \"logs.db\"})\n", "print(\"Logging session ID: \" + str(logging_session_id))\n", "\n", "# Create an agent workflow and run it\n", "assistant = AssistantAgent(name=\"assistant\", llm_config=llm_config)\n", "user_proxy = UserProxyAgent(\n", " name=\"user_proxy\",\n", " code_execution_config=False,\n", " human_input_mode=\"NEVER\",\n", " is_termination_msg=lambda msg: \"TERMINATE\" in msg[\"content\"],\n", ")\n", "\n", "user_proxy.initiate_chat(\n", " assistant, message=\"What is the height of the Eiffel Tower? Only respond with the answer and terminate\"\n", ")\n", "autogen.runtime_logging.stop()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Getting Data from the SQLite Database \n", "\n", "`logs.db` should be generated, by default it's using SQLite database. You can view the data with GUI tool like `sqlitebrowser`, using SQLite command line shell or using python script:\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "def get_log(dbname=\"logs.db\", table=\"chat_completions\"):\n", " import sqlite3\n", "\n", " con = sqlite3.connect(dbname)\n", " query = f\"SELECT * from {table}\"\n", " cursor = con.execute(query)\n", " rows = cursor.fetchall()\n", " column_names = [description[0] for description in cursor.description]\n", " data = [dict(zip(column_names, row)) for row in rows]\n", " con.close()\n", " return data" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | id | \n", "invocation_id | \n", "client_id | \n", "wrapper_id | \n", "session_id | \n", "request | \n", "response | \n", "is_cached | \n", "cost | \n", "start_time | \n", "end_time | \n", "total_tokens | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "e8bb00d7-6da5-4407-a949-e19b55d53da8 | \n", "139819167322784 | \n", "139823225568704 | \n", "8821a150-8c78-4d05-a858-8a64f1d18648 | \n", "You are a helpful AI assistant.\\nSolve tasks u... | \n", "The height of the Eiffel Tower is approximatel... | \n", "1 | \n", "0.01572 | \n", "2024-02-13 15:06:22.082896 | \n", "2024-02-13 15:06:22.083169 | \n", "507 | \n", "
1 | \n", "2 | \n", "c8522790-0067-484b-bb37-d39ae80db98b | \n", "139823225568656 | \n", "139823225563040 | \n", "fb0ef547-a2ac-428b-8c20-a5e63263b8e1 | \n", "You are a helpful AI assistant.\\nSolve tasks u... | \n", "The height of the Eiffel Tower is approximatel... | \n", "1 | \n", "0.01572 | \n", "2024-02-13 15:06:23.498758 | \n", "2024-02-13 15:06:23.499045 | \n", "507 | \n", "
2 | \n", "3 | \n", "91c3f6c0-c6f7-4306-89cd-f304c9556de4 | \n", "139823225449024 | \n", "139819166072448 | \n", "6e08f3e0-392b-434e-8b69-4ab36c4fcf99 | \n", "You are a helpful AI assistant.\\nSolve tasks u... | \n", "The height of the Eiffel Tower is approximatel... | \n", "1 | \n", "0.01572 | \n", "2024-02-13 15:06:24.688990 | \n", "2024-02-13 15:06:24.689238 | \n", "507 | \n", "