components/llm_service/notebooks/ChatSummary.ipynb (377 lines of code) (raw):
{
"cells": [
{
"cell_type": "code",
"execution_count": 30,
"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\")"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "f4c6b8e1-4ecd-40d3-bede-e906d1686155",
"metadata": {},
"outputs": [],
"source": [
"from langchain.agents import Tool, AgentExecutor, BaseMultiActionAgent, AgentOutputParser\n",
"from langchain.chat_models import ChatOpenAI\n",
"from langchain.agents.chat.base import ChatAgent\n",
"from langchain.prompts import PromptTemplate\n",
"from langchain.llms import OpenAI, VertexAI\n",
"from langchain.chat_models import ChatVertexAI\n",
"from langchain.utilities import SerpAPIWrapper\n",
"from langchain.chains import LLMChain\n",
"from typing import List, Union\n",
"from langchain.schema import AgentAction, AgentFinish, OutputParserException\n",
"import re"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "24c68b1d-aea4-456c-ac69-2c7854f67bf1",
"metadata": {},
"outputs": [],
"source": [
"PROJECT_ID = os.environ[\"PROJECT_ID\"]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"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",
" LLM_BACKEND_ROBOT_USERNAME,\n",
" LLM_BACKEND_ROBOT_PASSWORD)\n",
"from common.utils.token_handler import UserCredentials\n",
"from common.utils.secrets import get_secret\n",
"config.RULES_ENGINE_BASE_URL = f\"https://{PROJECT_ID}.cloudpssolutions.com/rules-engine/api/v1\"\n",
"config.auth_client = UserCredentials(LLM_BACKEND_ROBOT_USERNAME,\n",
" LLM_BACKEND_ROBOT_PASSWORD,\n",
" f\"https:/{PROJECT_ID}.cloudpssolutions.com\")"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "fb952721-b985-481b-9e7e-c50124e9ce63",
"metadata": {},
"outputs": [],
"source": [
"from services.agents.agents import ChatAgent\n",
"medikate_agent = ChatAgent(VERTEX_LLM_TYPE_BISON_CHAT)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"id": "9aa5f109-a246-4491-9c4e-dca297c748a0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO: [agents/agents.py:100 - load_agent()] Successfully loaded ChatAgent agent.\n"
]
}
],
"source": [
"tools = medikate_agent.get_tools()\n",
"agent = medikate_agent.load_agent()\n",
"\n",
"agent_executor = AgentExecutor.from_agent_and_tools(\n",
" agent=agent, tools=tools, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"id": "1ab25dc0-ecb7-4dce-a415-06bcc39bb4be",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Applying for Medicaid in Texas: Age eligibility and deadlines\n"
]
}
],
"source": [
"input = \"What pieces of information do I need to apply to Medicaid, what is the age eligibility and when is the deadline for Texas?\"\n",
"response = \"In order to apply for Medicaid, you will need to provide the following information: Proof of income, Proof of citizenship or legal residency, Proof of identity, Proof of address, Proof of disability (if applicable), Proof of pregnancy (if applicable), Proof of child support (if applicable), Proof of medical expenses (if applicable). You can find more information about Medicaid and how to apply on the Medicaid website. The deadline is 00/00/0000. See who is eligible on https://www.benefits.gov/benefit/1640\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Name the conversation topic in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"prompt = f\"{context} Conversation: {question}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "6bf9073c-d09c-4fec-895d-09f0a43b8285",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Applying for Medicaid in Texas: information needed, age eligibility, and deadline\n"
]
}
],
"source": [
"input = \"What pieces of information do I need to apply to Medicaid, what is the age eligibility and when is the deadline for Texas?\"\n",
"response = \"In order to apply for Medicaid, you will need to provide the following information: Proof of income, Proof of citizenship or legal residency, Proof of identity, Proof of address, Proof of disability (if applicable), Proof of pregnancy (if applicable), Proof of child support (if applicable), Proof of medical expenses (if applicable). You can find more information about Medicaid and how to apply on the Medicaid website. The deadline is 00/00/0000. See who is eligible on https://www.benefits.gov/benefit/1640\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Name the conversation topic in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"answer = f\"Agent:{response}\"\n",
"prompt = f\"{context} Conversation: {question}{answer}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "dfbb59c5-dcc3-4636-bf2c-88534f5ba4f6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Applying for Medicaid in Texas: Eligibility, Documents, and Deadline\n"
]
}
],
"source": [
"input = \"What pieces of information do I need to apply to Medicaid, what is the age eligibility and when is the deadline for Texas?\"\n",
"response = \"In order to apply for Medicaid, you will need to provide the following information: Proof of income, Proof of citizenship or legal residency, Proof of identity, Proof of address, Proof of disability (if applicable), Proof of pregnancy (if applicable), Proof of child support (if applicable), Proof of medical expenses (if applicable). You can find more information about Medicaid and how to apply on the Medicaid website. The deadline is 00/00/0000. See who is eligible on https://www.benefits.gov/benefit/1640\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Conversation title in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"answer = f\"Agent:{response}\"\n",
"prompt = f\"{context} Conversation: {question}{answer}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "b6078509-dd2e-4bcd-b620-b605a21f4b6e",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Applying for Medicaid in Texas: Age, Info, and Deadline\n"
]
}
],
"source": [
"input = \"What pieces of information do I need to apply to Medicaid, what is the age eligibility and when is the deadline for Texas?\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Conversation title in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"prompt = f\"{context} Conversation: {question}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "89e9d4ea-eb53-4326-9e70-387942fdb71f",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" **Medicaid Eligibility Check**\n"
]
}
],
"source": [
"input = \"Am I eligible for Medicaid?\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Conversation title in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"prompt = f\"{context} Conversation: {question}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "2ab1793d-d596-4952-8371-acc712396d07",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" User inquires about Medicaid eligibility.\n"
]
}
],
"source": [
"input = \"Am I eligible for Medicaid?\"\n",
"response = \"Based on my knowledge, it looks like you may be eligible for Medicaid. To confirm your eligibility, you should contact your state's Medicaid agency.\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Summarize the conversation in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"answer = f\"Agent:{response}\"\n",
"prompt = f\"{context} Conversation: {question}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "02262f81-44fd-4b8e-af39-6baba0c56c42",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Medicaid eligibility requirements.\n"
]
}
],
"source": [
"input = \"Am I eligible for Medicaid?\"\n",
"response = \"Based on my knowledge, it looks like you may be eligible for Medicaid. To confirm your eligibility, you should contact your state's Medicaid agency.\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Summarize the topic in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"answer = f\"Agent:{response}\"\n",
"prompt = f\"{context} Conversation: {question}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "6240afef-e367-4317-84bc-2cda8a7e856b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Medicaid eligibility requirements and application process.\n"
]
}
],
"source": [
"input = \"Am I eligible for Medicaid?\"\n",
"response = \"Based on my knowledge, it looks like you may be eligible for Medicaid. To confirm your eligibility, you should contact your state's Medicaid agency.\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Describe the topic in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"answer = f\"Agent:{response}\"\n",
"prompt = f\"{context} Conversation: {question}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "05bd1712-f06b-45fb-8f70-49ee7794e978",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Medicaid eligibility requirements\n"
]
}
],
"source": [
"input = \"Am I eligible for Medicaid?\"\n",
"response = \"Based on my knowledge, it looks like you may be eligible for Medicaid. To confirm your eligibility, you should contact your state's Medicaid agency.\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"Name the topic in 10-15 words\"\n",
"question = f\"User:{input}\"\n",
"answer = f\"Agent:{response}\"\n",
"prompt = f\"{context} Conversation: {question}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "b5de8d37-0ed3-438a-a8a8-8314f50dc918",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Applying for Medicaid in Texas\n"
]
}
],
"source": [
"input = \"What pieces of information do I need to apply to Medicaid, what is the age eligibility and when is the deadline for Texas?\"\n",
"response = \"In order to apply for Medicaid, you will need to provide the following information: Proof of income, Proof of citizenship or legal residency, Proof of identity, Proof of address, Proof of disability (if applicable), Proof of pregnancy (if applicable), Proof of child support (if applicable), Proof of medical expenses (if applicable). You can find more information about Medicaid and how to apply on the Medicaid website. The deadline is 00/00/0000. See who is eligible on https://www.benefits.gov/benefit/1640\"\n",
"llm = VertexAI(model_name=\"text-bison\", max_output_tokens=1024, temperature=0.2)\n",
"context = \"In 10-15 words, give a title to this conversation\"\n",
"question = f\"User:{input}\"\n",
"answer = f\"Agent:{response}\"\n",
"prompt = f\"{context} Conversation: {question}{answer}\"\n",
"print(llm(prompt))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "305433df-1338-4b84-91e4-2e1ec7b6e848",
"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
}