components/llm_service/notebooks/EmailTool.ipynb (335 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": 1, "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": 2, "id": "f4c6b8e1-4ecd-40d3-bede-e906d1686155", "metadata": {}, "outputs": [], "source": [ "from langchain.tools.gmail.utils import build_resource_service, get_gmail_credentials\n", "from langchain.agents.agent_toolkits import GmailToolkit\n", "from langchain.prompts import ChatPromptTemplate\n", "from langchain.llms import OpenAI\n", "from langchain.chains import LLMChain" ] }, { "cell_type": "code", "execution_count": 3, "id": "24c68b1d-aea4-456c-ac69-2c7854f67bf1", "metadata": {}, "outputs": [], "source": [ "!export PROJECT_ID=\"gcp-mira-demo\"\n", "project = \"gcp-mira-demo\"\n", "os.environ[\"PROJECT_ID\"] = project" ] }, { "cell_type": "code", "execution_count": 4, "id": "8fa76111-f1f8-4789-b202-4ca10a69abfd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO: [src/config.py:49 - <module>()] Namespace File not found, setting job namespace as default\n", "INFO: [src/config.py:84 - get_environ_flag()] ENABLE_GOOGLE_LLM = True\n", "INFO: [src/config.py:84 - get_environ_flag()] ENABLE_GOOGLE_MODEL_GARDEN = True\n", "INFO: [src/config.py:84 - get_environ_flag()] ENABLE_OPENAI_LLM = True\n", "INFO: [src/config.py:84 - get_environ_flag()] ENABLE_COHERE_LLM = True\n", "INFO: [src/config.py:196 - <module>()] LLM types loaded ['OpenAI-GPT3.5', 'OpenAI-GPT4', 'Cohere', 'VertexAI-Text', 'VertexAI-Chat', 'VertexAI-Chat', 'VertexAI-ModelGarden-LLAMA2-Chat']\n" ] } ], "source": [ "from config import (OPENAI_API_KEY)" ] }, { "cell_type": "code", "execution_count": 5, "id": "4b1b4f19-3bfa-4bb9-89e9-b7fe8aa16445", "metadata": {}, "outputs": [], "source": [ "llm = OpenAI(temperature=0, openai_api_key=OPENAI_API_KEY)" ] }, { "cell_type": "code", "execution_count": 6, "id": "042b170e-2add-4f82-9eb8-4a4ff8eef191", "metadata": {}, "outputs": [], "source": [ "email_template = \"\"\"\n", " You are working for {state} State Medicaid Agency. Create only the email message body for recipient: {recipient} \\n\\n\n", " \\n\\n Use text delimited by triple backticks to create the email body text:'''{email_body}'''\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 7, "id": "b88c087e-19f3-48ba-a666-691e05fba83a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "ChatPromptTemplate(input_variables=['email_body', 'recipient', 'state'], messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['email_body', 'recipient', 'state'], template=\"\\n You are working for {state} State Medicaid Agency. Create only the email message body for recipient: {recipient} \\n\\n\\n \\n\\n Use text delimited by triple backticks to create the email body text:'''{email_body}'''\\n\"))])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "email_body_prompt = ChatPromptTemplate.from_template(email_template)\n", "email_body_prompt" ] }, { "cell_type": "code", "execution_count": 8, "id": "066f6d2e-ae7f-493f-905c-d293b9967b67", "metadata": {}, "outputs": [], "source": [ "llm_chain = LLMChain(prompt=email_body_prompt, llm=llm)" ] }, { "cell_type": "code", "execution_count": 9, "id": "c9114680-0e05-47f2-a5b4-1e4ad1cee35e", "metadata": {}, "outputs": [], "source": [ "prompt_string = \"\"\"Create an email to this medicaid applicant that is missing income verification asking them to email a pay stub from their employers\"\"\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "dc0f769d-e23f-4111-a695-61752bd144d0", "metadata": {}, "outputs": [], "source": [ "sender='mira-admin@lavjain.altostrat.com'" ] }, { "cell_type": "code", "execution_count": 11, "id": "3ccbd29d-0166-46ab-b5ef-97788193f62a", "metadata": {}, "outputs": [], "source": [ "recipient='hello@google.com'" ] }, { "cell_type": "code", "execution_count": 12, "id": "403094af-e16f-4ce9-aa85-adb12ebe506b", "metadata": {}, "outputs": [], "source": [ "state='NYS'" ] }, { "cell_type": "code", "execution_count": 13, "id": "3a41a24c-e196-443c-837a-0d6359953c3c", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Dear Applicant, \n", "\n", "We are writing to you regarding your application for New York State Medicaid. We have noticed that we are missing income verification from your application. \n", "\n", "In order to complete your application, we need you to email a pay stub from your employer to us at evekhm@google.com. \n", "\n", "We appreciate your cooperation in this matter and thank you for your time. \n", "\n", "Sincerely, \n", "NYS State Medicaid Agency\n" ] } ], "source": [ "message = llm_chain.run(state=state,recipient=recipient,email_body=prompt_string)\n", "print(f\"{message}\")" ] }, { "cell_type": "code", "execution_count": 14, "id": "8e455e9a-1a19-4b61-b979-8360e63250bf", "metadata": {}, "outputs": [], "source": [ "subject_template = \"\"\"\n", " Create a subject for the following email: {email_body} \\n\\n \n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 15, "id": "7bbd3138-0423-43a4-a8cd-9289f0ae549d", "metadata": {}, "outputs": [], "source": [ "subject_prompt = ChatPromptTemplate.from_template(subject_template)" ] }, { "cell_type": "code", "execution_count": 16, "id": "6535d449-cc9c-46ed-bcfc-bdf4839c8efc", "metadata": {}, "outputs": [], "source": [ "llm_chain = LLMChain(prompt=subject_prompt, llm=llm)" ] }, { "cell_type": "code", "execution_count": 17, "id": "27ddf7de-e5f7-4201-aaef-669982018dde", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Subject: Income Verification Request for New York State Medicaid Application\n" ] } ], "source": [ "subject = llm_chain.run(email_body=message)\n", "print(f\"{subject}\")" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "For the steps below, you will need to locally generate token and credentials:\n", "```shell\n", "cd components/llm_service/src\n", "gcloud secrets versions access latest --secret=tools-gmail-oauth-token > data/token.json\n", "gcloud secrets versions access latest --secret=tools-gmail-client-secret > data/credentials.json\n", "```\n" ] }, { "cell_type": "code", "execution_count": 18, "id": "af444481-e28f-4a3c-9a7c-5e52e7b519b4", "metadata": {}, "outputs": [], "source": [ "credentials = get_gmail_credentials(\n", " token_file=\"./data/token.json\",\n", " scopes=[\"https://mail.google.com/\"],\n", " client_secrets_file=\"./data/credentials.json\",\n", ")\n", "api_resource = build_resource_service(credentials=credentials)\n", "toolkit = GmailToolkit(api_resource=api_resource)" ] }, { "cell_type": "code", "execution_count": 19, "id": "82c56c42-d03a-4c67-bbf6-d274001d164d", "metadata": {}, "outputs": [], "source": [ "tools = toolkit.get_tools()" ] }, { "cell_type": "code", "execution_count": 20, "id": "d900448f-c91f-4567-9f6d-99ebbc66dd6b", "metadata": {}, "outputs": [], "source": [ "input = {'to':recipient, 'subject':subject,'message':message}" ] }, { "cell_type": "code", "execution_count": 21, "id": "b73cb230-dbe9-4f14-a1c0-2511bffd4da9", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'Message sent. Message Id: 18bf302f7174cd27'" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "tools[1].invoke(input=input)" ] } ], "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.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }