components/llm_service/notebooks/AgentModels.ipynb (442 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "8614bb5d-bffe-4b12-9dbd-2d93b9d41eb6", "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "import json\n", "import inspect\n", "\n", "sys.path.append(\"../../common/src\")\n", "sys.path.append(\"../src\")\n", "os.chdir(\"../src\")" ] }, { "cell_type": "code", "execution_count": 2, "id": "9676ee1a-fce5-430d-94a0-af5c1f8c964b", "metadata": {}, "outputs": [], "source": [ "!export PROJECT_ID=\"lramsey-dev\"\n", "project = \"lramsey-dev\"\n", "os.environ[\"PROJECT_ID\"] = project" ] }, { "cell_type": "code", "execution_count": 3, "id": "e546519c-3e81-4c5a-ac53-711c0c3ecdb3", "metadata": {}, "outputs": [], "source": [ "from common.models import Agent\n", "from common.models.agent import AgentCapability" ] }, { "cell_type": "code", "execution_count": 5, "id": "ed87d2b6-6f09-4bbd-b2e2-c3e6efc11ed5", "metadata": {}, "outputs": [], "source": [ "from services.agents.agent_tools import agent_tool_registry\n", "from config import get_agent_config\n", "from services.agents.agent_service import get_all_agents" ] }, { "cell_type": "code", "execution_count": 6, "id": "19c6ad19-0493-4c03-b8d1-e53f8c0139ca", "metadata": {}, "outputs": [], "source": [ "agent_config = get_all_agents()" ] }, { "cell_type": "code", "execution_count": 7, "id": "798a624e-eb2e-4d33-8243-472fac01ab8d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Routing': {'llm_type': 'OpenAI-GPT4',\n", " 'agent_type': 'langchain_Conversational',\n", " 'tools': '',\n", " 'datasets': 'fqhc_medical_transactions'},\n", " 'Chat': {'llm_type': 'VertexAI-Chat-Palm2V2-Langchain',\n", " 'agent_type': 'langchain_Conversational',\n", " 'tools': 'search_tool,query_tool',\n", " 'query_engines': 'ALL'},\n", " 'Task': {'llm_type': 'OpenAI-GPT4-latest',\n", " 'agent_type': 'langchain_StructuredChatAgent',\n", " 'tools': 'ALL'},\n", " 'Plan': {'llm_type': 'OpenAI-GPT4-latest',\n", " 'agent_type': 'langchain_ZeroShot',\n", " 'query_engines': 'ALL',\n", " 'tools': 'ALL'}}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent_config" ] }, { "cell_type": "code", "execution_count": 8, "id": "bf75db66-5eee-42bc-be2b-2d11f92590df", "metadata": {}, "outputs": [], "source": [ "from services.agents.agents import BaseAgent\n", "agent_type = \"Route\"" ] }, { "cell_type": "code", "execution_count": 9, "id": "1cb0dabe-de7c-4f8c-9dbf-d3d130dba142", "metadata": {}, "outputs": [], "source": [ "routing_agents = BaseAgent.get_agents_by_capability(agent_type)" ] }, { "cell_type": "code", "execution_count": 10, "id": "c35c5f20-0eb6-4c31-8d0b-5f6b10b7f555", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Routing': {'llm_type': 'OpenAI-GPT4',\n", " 'agent_type': 'langchain_Conversational',\n", " 'tools': '',\n", " 'datasets': 'fqhc_medical_transactions'}}" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "routing_agents" ] }, { "cell_type": "code", "execution_count": 11, "id": "5aa2435a-c7e6-460b-961a-da469b3eb820", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'ruleset_input_tool': StructuredTool(name='ruleset_input_tool', description=\"ruleset_input_tool(ruleset_name: str) -> dict - Get the list of required inputs to run a set of rules (a 'ruleset').\\n The current available ruleset is a ruleset for medicaid eligibility.\\n The output of this tool is a dict of input keys and corresponding data types.\", args_schema=<class 'pydantic.main.ruleset_input_toolSchemaSchema'>, func=<function ruleset_input_tool at 0x130283430>),\n", " 'ruleset_execute_tool': StructuredTool(name='ruleset_execute_tool', description='ruleset_execute_tool(ruleset_name: str, rule_inputs: dict) -> dict - Run a business rules engine to make determinations about medicaid\\n eligibility. Takes a dict of constituent attributes as input (such as\\n income level, demographic data etc - the full set of input keys is\\n retrieved using the ruleset_input_tool). Outputs an eligibility decision.', args_schema=<class 'pydantic.main.ruleset_execute_toolSchemaSchema'>, func=<function ruleset_execute_tool at 0x1302833a0>),\n", " 'gmail_tool': StructuredTool(name='gmail_tool', description='gmail_tool(recipients: List, subject: str, message: str) -> str - Send an email to a list of recipients', args_schema=<class 'pydantic.main.gmail_toolSchemaSchema'>, func=<function gmail_tool at 0x1302834c0>),\n", " 'docs_tool': StructuredTool(name='docs_tool', description='docs_tool(recipients: List, content: str) -> Dict - Compose or create a document using Google Docs', args_schema=<class 'pydantic.main.docs_toolSchemaSchema'>, func=<function docs_tool at 0x1302835e0>),\n", " 'calendar_tool': StructuredTool(name='calendar_tool', description='calendar_tool(date: str) -> str - Create and update meetings using Google Calendar', args_schema=<class 'pydantic.main.calendar_toolSchemaSchema'>, func=<function calendar_tool at 0x130283670>),\n", " 'search_tool': StructuredTool(name='search_tool', description='search_tool(query: str) -> str - Perform an internet search.', args_schema=<class 'pydantic.main.search_toolSchemaSchema'>, func=<function search_tool at 0x130283700>),\n", " 'query_tool': StructuredTool(name='query_tool', description='query_tool(query: str) -> Dict - Perform a query and craft an answer using one of the available query engines,\\n with the name passed in as a argument.', args_schema=<class 'pydantic.main.query_toolSchemaSchema'>, func=<function query_tool at 0x130283790>),\n", " 'google_sheets_tool': StructuredTool(name='google_sheets_tool', description='google_sheets_tool(name: str, columns: list, rows: list, user_email: str = None) -> dict - Create a Google Sheet with the supplied data and return the sheet url and\\n id', args_schema=<class 'pydantic.main.google_sheets_toolSchemaSchema'>, func=<function google_sheets_tool at 0x1302838b0>),\n", " 'database_tool': StructuredTool(name='database_tool', description='database_tool(database_query_prompt: str) -> dict - Accepts a natural language question and queries a database to get an\\n answer in the form of data.', args_schema=<class 'pydantic.main.database_toolSchemaSchema'>, coroutine=<function database_tool at 0x130283550>)}" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "agent_tool_registry" ] }, { "cell_type": "code", "execution_count": null, "id": "3c30949b-8982-45df-a05a-ea7465d4944b", "metadata": {}, "outputs": [], "source": [ "from services.agents.agent_service import get_all_agents, get_agent_config" ] }, { "cell_type": "code", "execution_count": null, "id": "6aa1ffb1-c76e-43f0-bb5c-ca1ea565d627", "metadata": {}, "outputs": [], "source": [ "get_agent_config()" ] }, { "cell_type": "code", "execution_count": 12, "id": "862ac57f-858f-4fd4-8f1c-05bed5f35278", "metadata": {}, "outputs": [ { "ename": "KeyError", "evalue": "'agent_class'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[12], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m agent_name \u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mRouting\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m 2\u001b[0m agent_params \u001b[38;5;241m=\u001b[39m get_agent_config()[agent_name]\n\u001b[0;32m----> 3\u001b[0m llm_service_agent \u001b[38;5;241m=\u001b[39m \u001b[43magent_params\u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[38;5;124;43magent_class\u001b[39;49m\u001b[38;5;124;43m\"\u001b[39;49m\u001b[43m]\u001b[49m(agent_params[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mllm_type\u001b[39m\u001b[38;5;124m\"\u001b[39m], agent_name)\n", "\u001b[0;31mKeyError\u001b[0m: 'agent_class'" ] } ], "source": [ "agent_name = \"Routing\"\n", "agent_params = get_agent_config()[agent_name]\n", "llm_service_agent = agent_params[\"agent_class\"](agent_params[\"llm_type\"], agent_name)" ] }, { "cell_type": "code", "execution_count": null, "id": "9a780e25-d1d0-4882-82e6-6182c0124676", "metadata": {}, "outputs": [], "source": [ "from services.agents.agent_tools import (gmail_tool, docs_tool, database_tool,\n", " google_sheets_tool,\n", " calendar_tool, search_tool,\n", " query_tool)" ] }, { "cell_type": "code", "execution_count": null, "id": "44c2f92d-2d5e-422d-8b0a-eaba7faaddab", "metadata": {}, "outputs": [], "source": [ "query_tool" ] }, { "cell_type": "code", "execution_count": null, "id": "6b89df74-0dc4-46d5-ad34-bc69d60d2957", "metadata": {}, "outputs": [], "source": [ "query_tool.__name__" ] }, { "cell_type": "code", "execution_count": null, "id": "a32bbc50-09c0-4357-8b26-d61b78dd521d", "metadata": {}, "outputs": [], "source": [ "langchain_agent = llm_service_agent.load_langchain_agent()" ] }, { "cell_type": "code", "execution_count": null, "id": "6e41c2c0-f2a5-46ee-97d1-c0f84b189541", "metadata": {}, "outputs": [], "source": [ "llm_service_agent.get_tools()" ] }, { "cell_type": "code", "execution_count": null, "id": "7f5fab0c-6cd9-4294-ad50-38f2eccf5f1e", "metadata": {}, "outputs": [], "source": [ "from services.agents.agents import BaseAgent\n", "from config.utils import get_agent_config" ] }, { "cell_type": "code", "execution_count": null, "id": "7325b9e7-642d-4a73-88a8-be9ae688762a", "metadata": {}, "outputs": [], "source": [ "agent_config = get_agent_config()\n", "agent_config" ] }, { "cell_type": "code", "execution_count": null, "id": "844fd257-70b4-44b9-8e8c-cc61737f132a", "metadata": {}, "outputs": [], "source": [ "agent = BaseAgent.get_llm_service_agent(\"Casey\")" ] }, { "cell_type": "code", "execution_count": null, "id": "ddb1b621-5e5f-4028-9186-c16651ac64c0", "metadata": {}, "outputs": [], "source": [ "agent" ] }, { "cell_type": "code", "execution_count": null, "id": "2953187a-84bf-4387-828b-4ac820e13a44", "metadata": {}, "outputs": [], "source": [ "agent.get_tools()" ] }, { "cell_type": "code", "execution_count": null, "id": "389e75f1-1c76-43cf-ad43-fbda0efecfeb", "metadata": {}, "outputs": [], "source": [ "agent = BaseAgent.get_llm_service_agent(\"Chat\")" ] }, { "cell_type": "code", "execution_count": null, "id": "1301945a-f717-4c55-8307-ffb801966682", "metadata": {}, "outputs": [], "source": [ "agent.get_tools()" ] }, { "cell_type": "code", "execution_count": null, "id": "5cbaff9e-cfb5-4123-9b2e-33f3386bdb74", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5f5dc65d-24b2-4f89-bbee-9cc5b8c07eb9", "metadata": {}, "outputs": [], "source": [ "from services.agents.agent_service import run_agent, agent_plan" ] }, { "cell_type": "code", "execution_count": null, "id": "3fb6af63-837d-4da1-8a89-bf06d7ebc65c", "metadata": {}, "outputs": [], "source": [ "run_agent(\"Chat\", \"how is the weather today?\")" ] }, { "cell_type": "code", "execution_count": null, "id": "d2068ce5-4855-4d86-a8c0-45ce9155f9d2", "metadata": {}, "outputs": [], "source": [ "user_id = \"5nJrkPWa3D0yCKA853mD\"\n", "plan = agent_plan(\"Plan\", \"Send an email to my boss asking for a raise\", user_id)" ] }, { "cell_type": "code", "execution_count": null, "id": "7ca4a668-58fc-4216-bb90-788d4e8e883a", "metadata": {}, "outputs": [], "source": [ "plan[0]" ] }, { "cell_type": "code", "execution_count": null, "id": "60677fb7-ff35-4bbb-9dbe-e79b5ee0dd0a", "metadata": {}, "outputs": [], "source": [ "plan_steps = plan[1].plan_steps" ] }, { "cell_type": "code", "execution_count": null, "id": "d85bfa7e-20ea-4537-9b6a-4e9fbe74eead", "metadata": {}, "outputs": [], "source": [ "plan_steps[0]." ] }, { "cell_type": "code", "execution_count": null, "id": "9f3a6fb9-3915-44ab-ae8f-55ed7decb885", "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", "version": "3.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }