components/llm_service/notebooks/TaskAgent.ipynb (359 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "61f9ba3c-8703-4b16-8af2-9d5d4d327b2d", "metadata": {}, "outputs": [], "source": [ "import os\n", "import sys\n", "\n", "sys.path.append(\"../../common/src\")\n", "sys.path.append(\"../src\")\n", "os.chdir(\"../src\")" ] }, { "cell_type": "code", "execution_count": null, "id": "f4c6b8e1-4ecd-40d3-bede-e906d1686155", "metadata": {}, "outputs": [], "source": [ "from langchain.agents import Tool, AgentExecutor, BaseMultiActionAgent, AgentOutputParser\n", "from langchain.agents.chat.base import ChatAgent\n", "from langchain.prompts import StringPromptTemplate\n", "from langchain.llms import OpenAI, VertexAI\n", "from langchain.chat_models import ChatVertexAI, ChatOpenAI\n", "from langchain.utilities import SerpAPIWrapper\n", "from langchain.chains import LLMChain\n", "from typing import List, Union, Optional\n", "from langchain.schema import AgentAction, AgentFinish, OutputParserException\n", "import re" ] }, { "cell_type": "code", "execution_count": null, "id": "24c68b1d-aea4-456c-ac69-2c7854f67bf1", "metadata": {}, "outputs": [], "source": [ "!export PROJECT_ID=\"gcp-mira-develop\"\n", "project = \"gcp-mira-develop\"\n", "os.environ[\"PROJECT_ID\"] = project\n", "PROJECT_ID = project" ] }, { "cell_type": "code", "execution_count": null, "id": "8fa76111-f1f8-4789-b202-4ca10a69abfd", "metadata": {}, "outputs": [], "source": [ "import config\n", "from config import (VERTEX_LLM_TYPE_BISON_CHAT,\n", " VERTEX_LLM_TYPE_BISON_TEXT,\n", " OPENAI_LLM_TYPE_GPT3_5,\n", " OPENAI_LLM_TYPE_GPT4,\n", " COHERE_LLM_TYPE,\n", " LLM_BACKEND_ROBOT_USERNAME,\n", " LLM_BACKEND_ROBOT_PASSWORD)\n", "from common.utils.token_handler import UserCredentials\n", "config.TOOLS_SERVICE_BASE_URL = \"https://gcp-mira-develop.cloudpssolutions.com/tools-service/api/v1\"\n", "config.auth_client = UserCredentials(LLM_BACKEND_ROBOT_USERNAME,\n", " LLM_BACKEND_ROBOT_PASSWORD,\n", " \"https://gcp-mira-develop.cloudpssolutions.com\")" ] }, { "cell_type": "code", "execution_count": null, "id": "9a4ce13c-baf4-44e2-b48e-6ffd283ee08e", "metadata": {}, "outputs": [], "source": [ "config.SERVICES['tools-service']['host']='https://gcp-mira-develop.cloudpssolutions.com'" ] }, { "cell_type": "code", "execution_count": null, "id": "ca92a118-985d-4815-8dda-b19d73599b56", "metadata": {}, "outputs": [], "source": [ "config.SERVICES['tools-service']['api_url_prefix']='https://gcp-mira-develop.cloudpssolutions.com/tools-service/api/v1'" ] }, { "cell_type": "code", "execution_count": null, "id": "36729e2c-cf64-4862-81c0-dfca35191c85", "metadata": {}, "outputs": [], "source": [ "config.SERVICES['tools-service']" ] }, { "cell_type": "code", "execution_count": null, "id": "67079e21-816e-418f-9152-f0f056c44f17", "metadata": {}, "outputs": [], "source": [ "from common.models import Agent, User\n", "from common.models.agent import AgentType, UserPlan, PlanStep\n", "from services.agents.agent_service import get_agent_config, get_plan_agent_config,agent_execute_plan, run_agent\n", "from services.agents.agent_tools import database_tool, google_sheets_tool" ] }, { "cell_type": "code", "execution_count": null, "id": "fb952721-b985-481b-9e7e-c50124e9ce63", "metadata": {}, "outputs": [], "source": [ "from services.agents.agents import TaskAgent, PlanAgent\n", "#casey_plan_agent = TaskAgent(VERTEX_LLM_TYPE_BISON_TEXT)\n", "#casey_plan_agent = TaskAgent(VERTEX_LLM_TYPE_BISON_CHAT)\n", "#casey_plan_agent = TaskPlanAgent(OPENAI_LLM_TYPE_GPT3_5)\n", "#casey_plan_agent = TaskAgent(OPENAI_LLM_TYPE_GPT4)\n", "#casey_plan_agent = TaskAgent(OPENAI_LLM_TYPE_GPT4)\n", "#casey_plan_agent = TaskPlanAgent(COHERE_LLM_TYPE)" ] }, { "cell_type": "code", "execution_count": null, "id": "f1925b32-48f0-4d92-a8f6-c2a501ce1c6f", "metadata": {}, "outputs": [], "source": [ "#prompt = \"\"\"Compose and send an email to all the medicaid applicants that are missing income verification asking them to provide a pay stub from their employers\"\"\"\n", "prompt = \"\"\" Find all FQHCs that have billed an incorrect CDT code and save the results in a Google spreadsheet called FQHC_with_Incorrect_CDTs\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "id": "9aa5f109-a246-4491-9c4e-dca297c748a0", "metadata": {}, "outputs": [], "source": [ "#user id for Jon Chen\n", "user_id = \"7XqXArrDvknCNWaAOlmS\"" ] }, { "cell_type": "code", "execution_count": null, "id": "d695b8e8-393c-4bf4-96e0-44292d3905ce", "metadata": {}, "outputs": [], "source": [ "user = User.find_by_email(\"jonchen@google.com\")" ] }, { "cell_type": "code", "execution_count": null, "id": "d0b3a5a1-ad41-42c6-8f74-b23bedfc836c", "metadata": {}, "outputs": [], "source": [ "#agent_executor.run(agent_inputs)" ] }, { "cell_type": "code", "execution_count": null, "id": "8b4096d3-5cc3-4372-922a-b9a0e88e4244", "metadata": {}, "outputs": [], "source": [ " agent_params = get_agent_config()[\"Task\"]" ] }, { "cell_type": "code", "execution_count": null, "id": "a211cb3f-5c0a-463f-b74b-a67871e97b92", "metadata": {}, "outputs": [], "source": [ "agent_params" ] }, { "cell_type": "code", "execution_count": null, "id": "4bde7693-3b11-4a1b-a16e-ebc89b17a030", "metadata": {}, "outputs": [], "source": [ "llm_service_agent = agent_params[\"agent_class\"](agent_params[\"llm_type\"])" ] }, { "cell_type": "code", "execution_count": null, "id": "a7fa185c-4904-4c1d-b746-d126e2d96289", "metadata": {}, "outputs": [], "source": [ "langchain_agent = llm_service_agent.load_agent()" ] }, { "cell_type": "code", "execution_count": null, "id": "1f316e14-feb9-410a-bbf3-c2258c296246", "metadata": {}, "outputs": [], "source": [ "type(langchain_agent)" ] }, { "cell_type": "code", "execution_count": null, "id": "692a4d14-dba7-4e83-92b0-f38c3c767eeb", "metadata": {}, "outputs": [], "source": [ "from services.agents.agent_service import agent_plan, parse_plan" ] }, { "cell_type": "code", "execution_count": null, "id": "95e38ee6-46e7-421a-a441-d60bee40e4a0", "metadata": {}, "outputs": [], "source": [ "plan = agent_plan(\"Plan\", prompt, user_id)\n", "\n", "user_plan = plan[1]\n", "plan_steps=user_plan.plan_steps\n", "chat_history = []\n", "\n", "for step in plan_steps:\n", " description = PlanStep.find_by_id(step).description\n", " chat_history.append(description)" ] }, { "cell_type": "code", "execution_count": null, "id": "c7ed7efe-b7f4-45a0-9974-f1476a42b864", "metadata": { "scrolled": true }, "outputs": [], "source": [ "#tools = casey_plan_agent.get_tools()\n", "#print(tools)\n", "\n", "#agent = casey_plan_agent.load_agent()\n", "\n", "prompt = \"\"\"You are an AI assistant that can execute steps by calling upon the right tools. You have access to all the information required to execute the plan by from the values returned by the tools .\"\"\"\n", "#prompt = \"\"\"You are an AI assistant that can execute steps by calling upon the right tools. You have access to all the information required to execute the plan by from the values returned by the tools . Execute the steps as outlined in the {task_plan} variable \"\"\"\n", "#agent_executor = AgentExecutor.from_agent_and_tools(\n", "# agent=agent, \n", "# tools=tools,\n", "# verbose=True)\n", "\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1c28d4bb-e125-4b54-af09-d4ffcc88dd87", "metadata": {}, "outputs": [], "source": [ "#chat_history[0]= '1. Use [query_tool] to [ find Medicaid applicants missing a pay stub ]'\n", "#chat_history[2] = '3. Use [gmail_tool] to [send the composed email to the identified applicants]' \n", "#chat_history.insert(1,'Use [docs_tool] to [compose an email asking for a pay stub from their employers]')" ] }, { "cell_type": "code", "execution_count": null, "id": "9d73f802-8fb6-49d7-a833-165c2d9ffe38", "metadata": {}, "outputs": [], "source": [ "#result = agent_executor.run(agent_inputs)" ] }, { "cell_type": "code", "execution_count": null, "id": "b47f8a31-4ed1-4c6d-9347-ccc30c9b2484", "metadata": {}, "outputs": [], "source": [ "\n", "#call agent_service that looks up the plan steps for a given User Plan\n", "#agent_execute_plan(agent_name='Task', prompt=agent_inputs,user_plan=user_plan,user=user)\n", "agent_execute_plan(agent_name='Task', prompt=prompt,user_plan=user_plan,user=user)" ] }, { "cell_type": "code", "execution_count": null, "id": "581158f4-2e2b-4dfb-b0be-311f9f45a325", "metadata": {}, "outputs": [], "source": [ "\n", "#run_agent(agent_name='Task', prompt=agent_inputs['input'])" ] }, { "cell_type": "code", "execution_count": null, "id": "1141436c-e0fa-43f3-bdca-a187590c702e", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "7e135d1c-6c35-4341-acbd-b671fd5b69e9", "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.6" } }, "nbformat": 4, "nbformat_minor": 5 }