{ "cells": [ { "cell_type": "markdown", "id": "dfe37963-1af6-44fc-a841-8e462443f5e6", "metadata": {}, "source": [ "## Expert Knowledge Worker\n", "\n", "### A question answering agent that is an expert knowledge worker\n", "### To be used by employees of Insurellm, an Insurance Tech company\n", "### The agent needs to be accurate and the solution should be low cost.\n", "\n", "This project will use RAG (Retrieval Augmented Generation) to ensure our question/answering assistant has high accuracy.\n", "\n", "This first implementation will use a simple, brute-force type of RAG.." ] }, { "cell_type": "code", "execution_count": 1, "id": "ba2779af-84ef-4227-9e9e-6eaf0df87e77", "metadata": {}, "outputs": [], "source": [ "# imports\n", "\n", "import os\n", "import glob\n", "from dotenv import load_dotenv\n", "import gradio as gr\n", "from openai import OpenAI" ] }, { "cell_type": "code", "execution_count": 2, "id": "58c85082-e417-4708-9efe-81a5d55d1424", "metadata": {}, "outputs": [], "source": [ "# price is a factor for our company, so we're going to use a low cost model\n", "\n", "MODEL = \"gpt-4o-mini\"" ] }, { "cell_type": "code", "execution_count": 3, "id": "ee78efcb-60fe-449e-a944-40bab26261af", "metadata": {}, "outputs": [], "source": [ "# Load environment variables in a file called .env\n", "\n", "load_dotenv()\n", "os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY', 'your-key-if-not-using-env')\n", "openai = OpenAI()" ] }, { "cell_type": "code", "execution_count": 4, "id": "9e0652c2-3d76-40c7-8313-9dc1895155a8", "metadata": {}, "outputs": [], "source": [ "context = {}\n", "\n", "employees = glob.glob(\"knowledge-base/employees/*\")\n", "\n", "for employee in employees:\n", " name = employee.split(' ')[-1][:-3]\n", " doc = \"\"\n", " with open(employee, \"r\") as f:\n", " doc = f.read()\n", " context[name]=doc" ] }, { "cell_type": "code", "execution_count": 6, "id": "2c85a11b-b04d-4066-b243-f96139ca106f", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "\"# Avery Lancaster\\n\\n## Summary\\n- **Date of Birth**: March 15, 1985 \\n- **Job Title**: Co-Founder & Chief Executive Officer (CEO) \\n- **Location**: San Francisco, California \\n\\n## Insurellm Career Progression\\n- **2015 - Present**: Co-Founder & CEO \\n Avery Lancaster co-founded Insurellm in 2015 and has since guided the company to its current position as a leading Insurance Tech provider. Avery is known for her innovative leadership strategies and risk management expertise that have catapulted the company into the mainstream insurance market. \\n\\n- **2013 - 2015**: Senior Product Manager at Innovate Insurance Solutions \\n Before launching Insurellm, Avery was a leading Senior Product Manager at Innovate Insurance Solutions, where she developed groundbreaking insurance products aimed at the tech sector. \\n\\n- **2010 - 2013**: Business Analyst at Edge Analytics \\n Prior to joining Innovate, Avery worked as a Business Analyst, focusing on market trends and consumer preferences in the insurance space. This position laid the groundwork for Avery’s future entrepreneurial endeavors.\\n\\n## Annual Performance History\\n- **2015**: **Exceeds Expectations** \\n Avery’s leadership during Insurellm's foundational year led to successful product launches and securing initial funding. \\n\\n- **2016**: **Meets Expectations** \\n Growth continued, though challenges arose in operational efficiency that required Avery's attention. \\n\\n- **2017**: **Developing** \\n Market competition intensified, and monthly sales metrics were below targets. Avery implemented new strategies which required a steep learning curve. \\n\\n- **2018**: **Exceeds Expectations** \\n Under Avery’s pivoted vision, Insurellm launched two new successful products that significantly increased market share. \\n\\n- **2019**: **Meets Expectations** \\n Steady growth, however, some team tensions led to a minor drop in employee morale. Avery recognized the need to enhance company culture. \\n\\n- **2020**: **Below Expectations** \\n The COVID-19 pandemic posed unforeseen operational difficulties. Avery faced criticism for delayed strategy shifts, although efforts were eventually made to stabilize the company. \\n\\n- **2021**: **Exceptional** \\n Avery's decisive transition to remote work and rapid adoption of digital tools led to record-high customer satisfaction levels and increased sales. \\n\\n- **2022**: **Satisfactory** \\n Avery focused on rebuilding team dynamics and addressing employee concerns, leading to overall improvement despite a saturated market. \\n\\n- **2023**: **Exceeds Expectations** \\n Market leadership was regained with innovative approaches to personalized insurance solutions. Avery is now recognized in industry publications as a leading voice in Insurance Tech innovation.\\n\\n## Compensation History\\n- **2015**: $150,000 base salary + Significant equity stake \\n- **2016**: $160,000 base salary + Equity increase \\n- **2017**: $150,000 base salary + Decrease in bonus due to performance \\n- **2018**: $180,000 base salary + performance bonus of $30,000 \\n- **2019**: $185,000 base salary + market adjustment + $5,000 bonus \\n- **2020**: $170,000 base salary (temporary reduction due to COVID-19) \\n- **2021**: $200,000 base salary + performance bonus of $50,000 \\n- **2022**: $210,000 base salary + retention bonus \\n- **2023**: $225,000 base salary + $75,000 performance bonus \\n\\n## Other HR Notes\\n- **Professional Development**: Avery has actively participated in leadership training programs and industry conferences, representing Insurellm and fostering partnerships. \\n- **Diversity & Inclusion Initiatives**: Avery has championed a commitment to diversity in hiring practices, seeing visible improvements in team representation since 2021. \\n- **Work-Life Balance**: Feedback revealed concerns regarding work-life balance, which Avery has approached by implementing flexible working conditions and ensuring regular check-ins with the team.\\n- **Community Engagement**: Avery led community outreach efforts, focusing on financial literacy programs, particularly aimed at underserved populations, improving Insurellm's corporate social responsibility image. \\n\\nAvery Lancaster has demonstrated resilience and adaptability throughout her career at Insurellm, positioning the company as a key player in the insurance technology landscape.\"" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "context[\"Lancaster\"]" ] }, { "cell_type": "code", "execution_count": 7, "id": "a1d231f9-091e-4c72-b0f8-6af578a74e22", "metadata": {}, "outputs": [], "source": [ "products = glob.glob(\"knowledge-base/products/*\")\n", "\n", "for product in products:\n", " name = product.split('/')[-1][:-3]\n", " doc = \"\"\n", " with open(product, \"r\") as f:\n", " doc = f.read()\n", " context[name]=doc" ] }, { "cell_type": "code", "execution_count": 8, "id": "aba46a57-d973-4195-8fe3-70fc60687192", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['Chen', 'Spencer', 'Tran', 'Blake', 'Lancaster', 'Thompson', 'Greene', 'Thomson', 'Trenton', 'Harper', 'Bishop', 'Carter', 'Rellm', 'Markellm', 'Homellm', 'Carllm'])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "context.keys()" ] }, { "cell_type": "code", "execution_count": 9, "id": "129c7d1e-0094-4479-9459-f9360b95f244", "metadata": {}, "outputs": [], "source": [ "system_message = \"You are an expert in answering accurate questions about Insurellm, the Insurance Tech company. Give brief, accurate answers. If you don't know the answer, say so. Do not make anything up if you haven't been provided with relevant context.\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "d40e390b-c110-42d5-8d80-daf3295b9862", "metadata": {}, "outputs": [], "source": [ "def get_relevant_context(message):\n", " relevant_context = []\n", " for context_title, context_details in context.items():\n", " if context_title in message:\n", " relevant_context.append(context_details)\n", " return relevant_context " ] }, { "cell_type": "code", "execution_count": 14, "id": "d94c768d-c47a-4c34-85e9-7b786da96507", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "get_relevant_context(\"Who is Avery and what is carllm?\")" ] }, { "cell_type": "code", "execution_count": 16, "id": "5a7cef7f-f214-4bac-8217-3f9ab9ba1bf0", "metadata": {}, "outputs": [], "source": [ "def add_context(message):\n", " relevant_context = get_relevant_context(message)\n", " if relevant_context:\n", " message += \"\\n\\nThe following additional context might be relevant in answering this question:\\n\\n\"\n", " for relevant in relevant_context:\n", " message += relevant + \"\\n\\n\"\n", " return message" ] }, { "cell_type": "code", "execution_count": 18, "id": "2b36399c-440b-4049-9d39-68d208283c71", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Who is Alex Lancaster?\n", "\n", "The following additional context might be relevant in answering this question:\n", "\n", "# Avery Lancaster\n", "\n", "## Summary\n", "- **Date of Birth**: March 15, 1985 \n", "- **Job Title**: Co-Founder & Chief Executive Officer (CEO) \n", "- **Location**: San Francisco, California \n", "\n", "## Insurellm Career Progression\n", "- **2015 - Present**: Co-Founder & CEO \n", " Avery Lancaster co-founded Insurellm in 2015 and has since guided the company to its current position as a leading Insurance Tech provider. Avery is known for her innovative leadership strategies and risk management expertise that have catapulted the company into the mainstream insurance market. \n", "\n", "- **2013 - 2015**: Senior Product Manager at Innovate Insurance Solutions \n", " Before launching Insurellm, Avery was a leading Senior Product Manager at Innovate Insurance Solutions, where she developed groundbreaking insurance products aimed at the tech sector. \n", "\n", "- **2010 - 2013**: Business Analyst at Edge Analytics \n", " Prior to joining Innovate, Avery worked as a Business Analyst, focusing on market trends and consumer preferences in the insurance space. This position laid the groundwork for Avery’s future entrepreneurial endeavors.\n", "\n", "## Annual Performance History\n", "- **2015**: **Exceeds Expectations** \n", " Avery’s leadership during Insurellm's foundational year led to successful product launches and securing initial funding. \n", "\n", "- **2016**: **Meets Expectations** \n", " Growth continued, though challenges arose in operational efficiency that required Avery's attention. \n", "\n", "- **2017**: **Developing** \n", " Market competition intensified, and monthly sales metrics were below targets. Avery implemented new strategies which required a steep learning curve. \n", "\n", "- **2018**: **Exceeds Expectations** \n", " Under Avery’s pivoted vision, Insurellm launched two new successful products that significantly increased market share. \n", "\n", "- **2019**: **Meets Expectations** \n", " Steady growth, however, some team tensions led to a minor drop in employee morale. Avery recognized the need to enhance company culture. \n", "\n", "- **2020**: **Below Expectations** \n", " The COVID-19 pandemic posed unforeseen operational difficulties. Avery faced criticism for delayed strategy shifts, although efforts were eventually made to stabilize the company. \n", "\n", "- **2021**: **Exceptional** \n", " Avery's decisive transition to remote work and rapid adoption of digital tools led to record-high customer satisfaction levels and increased sales. \n", "\n", "- **2022**: **Satisfactory** \n", " Avery focused on rebuilding team dynamics and addressing employee concerns, leading to overall improvement despite a saturated market. \n", "\n", "- **2023**: **Exceeds Expectations** \n", " Market leadership was regained with innovative approaches to personalized insurance solutions. Avery is now recognized in industry publications as a leading voice in Insurance Tech innovation.\n", "\n", "## Compensation History\n", "- **2015**: $150,000 base salary + Significant equity stake \n", "- **2016**: $160,000 base salary + Equity increase \n", "- **2017**: $150,000 base salary + Decrease in bonus due to performance \n", "- **2018**: $180,000 base salary + performance bonus of $30,000 \n", "- **2019**: $185,000 base salary + market adjustment + $5,000 bonus \n", "- **2020**: $170,000 base salary (temporary reduction due to COVID-19) \n", "- **2021**: $200,000 base salary + performance bonus of $50,000 \n", "- **2022**: $210,000 base salary + retention bonus \n", "- **2023**: $225,000 base salary + $75,000 performance bonus \n", "\n", "## Other HR Notes\n", "- **Professional Development**: Avery has actively participated in leadership training programs and industry conferences, representing Insurellm and fostering partnerships. \n", "- **Diversity & Inclusion Initiatives**: Avery has championed a commitment to diversity in hiring practices, seeing visible improvements in team representation since 2021. \n", "- **Work-Life Balance**: Feedback revealed concerns regarding work-life balance, which Avery has approached by implementing flexible working conditions and ensuring regular check-ins with the team.\n", "- **Community Engagement**: Avery led community outreach efforts, focusing on financial literacy programs, particularly aimed at underserved populations, improving Insurellm's corporate social responsibility image. \n", "\n", "Avery Lancaster has demonstrated resilience and adaptability throughout her career at Insurellm, positioning the company as a key player in the insurance technology landscape.\n", "\n", "\n" ] } ], "source": [ "print(add_context(\"Who is Alex Lancaster?\"))" ] }, { "cell_type": "code", "execution_count": 21, "id": "968e7bf2-e862-4679-a11f-6c1efb6ec8ca", "metadata": {}, "outputs": [], "source": [ "def chat(message, history):\n", " messages = [{\"role\": \"system\", \"content\": system_message}]\n", " for user_message, assistant_message in history:\n", " messages.append({\"role\": \"user\", \"content\": user_message})\n", " messages.append({\"role\": \"assistant\", \"content\": assistant_message})\n", " \n", " message = add_context(message)\n", " messages.append({\"role\": \"user\", \"content\": message})\n", "\n", " stream = openai.chat.completions.create(model=MODEL, messages=messages, stream=True)\n", "\n", " response = \"\"\n", " for chunk in stream:\n", " response += chunk.choices[0].delta.content or ''\n", " yield response" ] }, { "cell_type": "markdown", "id": "bbbcb659-13ce-47ab-8a5e-01b930494964", "metadata": {}, "source": [ "## Now we will bring this up in Gradio using the Chat interface -\n", "\n", "A quick and easy way to prototype a chat with an LLM" ] }, { "cell_type": "code", "execution_count": 22, "id": "c3536590-85c7-4155-bd87-ae78a1467670", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7865\n", "\n", "To create a public link, set `share=True` in `launch()`.\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "view = gr.ChatInterface(chat).launch()" ] }, { "cell_type": "code", "execution_count": null, "id": "48873d11-2fbd-4329-af27-46c781788561", "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.11.10" } }, "nbformat": 4, "nbformat_minor": 5 }