{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import sys\n",
    "sys.path.append(\"../\")\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"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\"\n",
    "os.environ[\"OPENAI_ORGANIZATION\"] = \"org-lnKDQT1GAiB9UWYLNPsvnKJz\"\n",
    "os.environ[\"sql_dialect\"] = \"mysql+pymysql://4HgsvrQ16HUnrUW.root:vlgyZWUwh4JIV511@gateway01.eu-central-1.prod.aws.tidbcloud.com:4000/tpch?ssl_ca%2Fetc%2Fssl%2Fcerts%2Fca-certificates.crt&ssl_verify_identity=true\"\n",
    "%load_ext jupytee"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "\n",
       "### Domain\n",
       "Sales\n",
       "### Knowledge\n",
       "* **Sales Process**  \n",
       "A series of steps that a salesperson follows to convert a prospect into a customer, including prospecting, qualifying, presenting, handling objections, and closing the deal.\n",
       "* **Prospecting**  \n",
       "The process of identifying and reaching out to potential customers who may be interested in a product or service.\n",
       "* **Lead Generation**  \n",
       "The process of attracting and converting prospects into leads, which are potential customers who have shown interest in a product or service.\n",
       "* **Qualifying Leads**  \n",
       "The process of determining whether a lead is a good fit for a product or service based on factors such as budget, need, and decision-making authority.\n",
       "* **Sales Presentation**  \n",
       "A formal presentation of a product or service to a prospect, highlighting its features, benefits, and value proposition.\n",
       "* **Handling Objections**  \n",
       "The process of addressing and overcoming concerns or objections raised by a prospect during the sales process.\n",
       "* **Closing Techniques**  \n",
       "Strategies and tactics used by salespeople to secure a commitment from a prospect to purchase a product or service.\n",
       "* **Sales Metrics**  \n",
       "Quantitative measures used to track and evaluate the performance of a sales team, individual salespeople, or sales campaigns.\n",
       "* **Customer Relationship Management (CRM)**  \n",
       "A system or software used to manage interactions with current and potential customers, including tracking leads, sales activities, and customer communication.\n",
       "* **Sales Forecasting**  \n",
       "The process of estimating future sales based on historical data, market trends, and other factors, used to inform business planning and resource allocation.\n",
       "* **Account Management**  \n",
       "The process of managing relationships with existing customers, ensuring their needs are met, and identifying opportunities for upselling or cross-selling.\n",
       "* **Sales Negotiation**  \n",
       "The process of discussing and reaching an agreement on the terms of a sale, including price, payment terms, and delivery.\n",
       "* **Sales Training**  \n",
       "The process of providing salespeople with the knowledge, skills, and tools they need to be successful in their roles.\n",
       "* **Sales Enablement**  \n",
       "The process of providing sales teams with the resources, tools, and support they need to sell more effectively, including training, content, and technology."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%domain_knowledge"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "\n",
       "# Main Task\n",
       "&nbsp;How to reduce cost ?        \n",
       "# Candidate Actions\n",
       "> ### Reduce supplier costs\n",
       "> &nbsp;Negotiate better prices with suppliers or find alternative suppliers with lower costs.\n",
       "> \n",
       "\n",
       "> ### Improve inventory management\n",
       "> &nbsp;Optimize inventory levels to reduce carrying costs and minimize stockouts.\n",
       "> \n",
       "\n",
       "> ### Increase sales efficiency\n",
       "> &nbsp;Improve the sales process to close deals faster and generate more revenue.\n",
       "> \n",
       "\n",
       "> ### Optimize shipping and logistics\n",
       "> &nbsp;Find more cost-effective shipping methods and streamline logistics processes.\n",
       "> \n",
       "\n",
       "# Choosed Action\n",
       "&nbsp;Reduce supplier costs\n",
       "# Solution Plans\n",
       "&nbsp;**Analyze supplier data to identify opportunities for cost reduction.**\n",
       "\n",
       "> ### Identify high-cost suppliers\n",
       "> &nbsp;Analyze the partsupp table to find suppliers with the highest supply costs.  \n",
       "> &nbsp;Generated Step Question: Which suppliers have the highest supply costs?\n",
       "> \n",
       "\n",
       "> ### Evaluate supplier performance\n",
       "> &nbsp;Compare high-cost suppliers to others in terms of factors such as delivery times, quality, and reliability using the lineitem and supplier tables.  \n",
       "> &nbsp;Generated Step Question: How do high-cost suppliers compare to others in terms of performance?\n",
       "> \n",
       "\n",
       "> ### Find alternative suppliers\n",
       "> &nbsp;Search the supplier and partsupp tables for alternative suppliers with lower costs and similar performance metrics.  \n",
       "> &nbsp;Generated Step Question: Are there alternative suppliers with lower costs and similar performance?\n",
       "> \n",
       "\n",
       "> ### Negotiate better prices\n",
       "> &nbsp;Use the supplier and partsupp data to prepare for negotiations with high-cost suppliers, focusing on areas where they can improve or offer better pricing.  \n",
       "> &nbsp;Generated Step Question: Can we negotiate better prices with our current high-cost suppliers?\n",
       "> \n",
       "\n"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/javascript": [
       "\n",
       "    var output_area = this;\n",
       "    setTimeout(function(){\n",
       "    var cell_element = output_area.element.parents('.cell');\n",
       "    var cell_idx = Jupyter.notebook.get_cell_elements().index(cell_element);\n",
       "    Jupyter.notebook.execute_cells([cell_idx+1,cell_idx+2,cell_idx+3,cell_idx+4]);\n",
       "    }, 1000);\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%main_task How to reduce cost ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "To find the suppliers with the highest supply costs, you can analyze the `partsupp` table. Here's a sample SQL query to get the top 10 suppliers with the highest supply costs:\n",
       "\n",
       "```sql\n",
       "SELECT s.s_suppkey, s.s_name, AVG(ps.ps_supplycost) as avg_supply_cost\n",
       "FROM partsupp AS ps\n",
       "JOIN supplier AS s ON ps.ps_suppkey = s.s_suppkey\n",
       "GROUP BY s.s_suppkey, s.s_name\n",
       "ORDER BY avg_supply_cost DESC\n",
       "LIMIT 10;\n",
       "```\n",
       "\n",
       "This query will return a table with the following columns:\n",
       "\n",
       "| s_suppkey | s_name | avg_supply_cost |\n",
       "|-----------|--------|-----------------|\n",
       "| ...       | ...    | ...             |\n",
       "\n",
       "The table will display the top 10 suppliers with the highest average supply costs. You can then use this information to evaluate their performance and explore alternative suppliers with lower costs."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sub_task Identify high-cost suppliers:Which suppliers have the highest supply costs ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "To compare high-cost suppliers to others in terms of performance, you can analyze factors such as delivery times, quality, and reliability using the `lineitem` and `supplier` tables. Here's a sample SQL query to compare the average delivery times of the top 10 high-cost suppliers to the overall average delivery time:\n",
       "\n",
       "```sql\n",
       "WITH high_cost_suppliers AS (\n",
       "  SELECT s.s_suppkey, s.s_name, AVG(ps.ps_supplycost) as avg_supply_cost\n",
       "  FROM partsupp AS ps\n",
       "  JOIN supplier AS s ON ps.ps_suppkey = s.s_suppkey\n",
       "  GROUP BY s.s_suppkey, s.s_name\n",
       "  ORDER BY avg_supply_cost DESC\n",
       "  LIMIT 10\n",
       "),\n",
       "supplier_delivery_times AS (\n",
       "  SELECT s.s_suppkey, s.s_name, AVG(DATE_PART('day', l.l_receiptdate - l.l_shipdate)) as avg_delivery_time\n",
       "  FROM lineitem AS l\n",
       "  JOIN supplier AS s ON l.l_suppkey = s.s_suppkey\n",
       "  GROUP BY s.s_suppkey, s.s_name\n",
       ")\n",
       "SELECT hcs.s_suppkey, hcs.s_name, hcs.avg_supply_cost, sdt.avg_delivery_time, overall_avg_delivery_time\n",
       "FROM high_cost_suppliers AS hcs\n",
       "JOIN supplier_delivery_times AS sdt ON hcs.s_suppkey = sdt.s_suppkey\n",
       "JOIN (SELECT AVG(avg_delivery_time) as overall_avg_delivery_time FROM supplier_delivery_times) AS overall\n",
       "ORDER BY hcs.avg_supply_cost DESC;\n",
       "```\n",
       "\n",
       "This query will return a table with the following columns:\n",
       "\n",
       "| s_suppkey | s_name | avg_supply_cost | avg_delivery_time | overall_avg_delivery_time |\n",
       "|-----------|--------|-----------------|-------------------|---------------------------|\n",
       "| ...       | ...    | ...             | ...               | ...                       |\n",
       "\n",
       "The table will display the top 10 high-cost suppliers along with their average delivery times and the overall average delivery time for all suppliers. You can use this information to evaluate their performance and identify areas for improvement."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sub_task Evaluate supplier performance:How do high-cost suppliers compare to others in terms of performance ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "To find alternative suppliers with lower costs and similar performance, you can analyze the `supplier`, `partsupp`, and `lineitem` tables. Here's a sample SQL query to find suppliers with lower costs and similar average delivery times compared to the top 10 high-cost suppliers:\n",
       "\n",
       "```sql\n",
       "WITH high_cost_suppliers AS (\n",
       "  SELECT s.s_suppkey, s.s_name, AVG(ps.ps_supplycost) as avg_supply_cost\n",
       "  FROM partsupp AS ps\n",
       "  JOIN supplier AS s ON ps.ps_suppkey = s.s_suppkey\n",
       "  GROUP BY s.s_suppkey, s.s_name\n",
       "  ORDER BY avg_supply_cost DESC\n",
       "  LIMIT 10\n",
       "),\n",
       "supplier_delivery_times AS (\n",
       "  SELECT s.s_suppkey, s.s_name, AVG(DATE_PART('day', l.l_receiptdate - l.l_shipdate)) as avg_delivery_time\n",
       "  FROM lineitem AS l\n",
       "  JOIN supplier AS s ON l.l_suppkey = s.s_suppkey\n",
       "  GROUP BY s.s_suppkey, s.s_name\n",
       "),\n",
       "alternative_suppliers AS (\n",
       "  SELECT s.s_suppkey, s.s_name, AVG(ps.ps_supplycost) as avg_supply_cost, sdt.avg_delivery_time\n",
       "  FROM partsupp AS ps\n",
       "  JOIN supplier AS s ON ps.ps_suppkey = s.s_suppkey\n",
       "  JOIN supplier_delivery_times AS sdt ON s.s_suppkey = sdt.s_suppkey\n",
       "  WHERE s.s_suppkey NOT IN (SELECT s_suppkey FROM high_cost_suppliers)\n",
       "  GROUP BY s.s_suppkey, s.s_name, sdt.avg_delivery_time\n",
       "  HAVING AVG(ps.ps_supplycost) < (SELECT AVG(avg_supply_cost) FROM high_cost_suppliers)\n",
       ")\n",
       "SELECT alt.s_suppkey, alt.s_name, alt.avg_supply_cost, alt.avg_delivery_time, hcs.avg_delivery_time as high_cost_supplier_avg_delivery_time\n",
       "FROM alternative_suppliers AS alt\n",
       "JOIN (SELECT AVG(avg_delivery_time) as high_cost_supplier_avg_delivery_time FROM high_cost_suppliers JOIN supplier_delivery_times ON high_cost_suppliers.s_suppkey = supplier_delivery_times.s_suppkey) AS hcs\n",
       "WHERE alt.avg_delivery_time <= hcs.high_cost_supplier_avg_delivery_time\n",
       "ORDER BY alt.avg_supply_cost ASC;\n",
       "```\n",
       "\n",
       "This query will return a table with the following columns:\n",
       "\n",
       "| s_suppkey | s_name | avg_supply_cost | avg_delivery_time | high_cost_supplier_avg_delivery_time |\n",
       "|-----------|--------|-----------------|-------------------|---------------------------------------|\n",
       "| ...       | ...    | ...             | ...               | ...                                   |\n",
       "\n",
       "The table will display alternative suppliers with lower average supply costs and similar or better average delivery times compared to the top 10 high-cost suppliers. You can use this information to explore potential partnerships with these alternative suppliers to reduce costs while maintaining similar performance levels."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sub_task Find alternative suppliers:Are there alternative suppliers with lower costs and similar performance ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/markdown": [
       "To negotiate better prices with your current high-cost suppliers, you can use the data from the `supplier` and `partsupp` tables to identify areas where they can improve or offer better pricing. Here's a plan to prepare for negotiations:\n",
       "\n",
       "1. **Identify high-cost suppliers**: Analyze the `partsupp` table to find suppliers with the highest supply costs. You can use the SQL query provided in the previous steps to get the top 10 suppliers with the highest supply costs.\n",
       "\n",
       "2. **Evaluate supplier performance**: Compare high-cost suppliers to others in terms of factors such as delivery times, quality, and reliability using the `lineitem` and `supplier` tables. Use the SQL query provided in the previous steps to compare the average delivery times of the top 10 high-cost suppliers to the overall average delivery time.\n",
       "\n",
       "3. **Identify areas for improvement**: Based on the performance evaluation, identify areas where high-cost suppliers can improve, such as delivery times, product quality, or reliability. This information can be used as leverage during negotiations.\n",
       "\n",
       "4. **Research alternative suppliers**: As a backup plan, research alternative suppliers with lower costs and similar performance metrics. Use the SQL query provided in the previous steps to find suppliers with lower costs and similar average delivery times compared to the top 10 high-cost suppliers.\n",
       "\n",
       "5. **Prepare negotiation points**: Based on the performance evaluation and research on alternative suppliers, prepare a list of negotiation points to discuss with high-cost suppliers. These points can include areas for improvement, better pricing, or additional services that can add value to your business.\n",
       "\n",
       "6. **Schedule meetings with high-cost suppliers**: Reach out to the high-cost suppliers and schedule meetings to discuss the negotiation points. During the meetings, present your findings and discuss potential improvements or better pricing options.\n",
       "\n",
       "By following this plan, you can effectively negotiate better prices with your current high-cost suppliers, potentially reducing costs and improving overall performance."
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%sub_task Negotiate better prices:Can we negotiate better prices with our current high-cost suppliers ?"
   ]
  }
 ],
 "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.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
