{
  "cells": [
    {
      "cell_type": "raw",
      "metadata": {},
      "source": [
        "---\n",
        "title: \"LLMs and data: modular and composable best practices\"\n",
        "author: \"Cody Peterson\"\n",
        "date: \"2023-10-03\"\n",
        "freeze: auto\n",
        "categories:\n",
        "    - blog\n",
        "    - ai\n",
        "    - llm\n",
        "---"
      ],
      "id": "56691f4e"
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Introduction\n"
      ],
      "id": "f55bf8d3"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "# | code-fold: true\n",
        "import ibis  # <1>\n",
        "import marvin  # <1>\n",
        "\n",
        "from dotenv import load_dotenv  # <1>\n",
        "\n",
        "load_dotenv()  # <2>\n",
        "\n",
        "con = ibis.connect(\"duckdb://penguins.ddb\")  # <3>\n",
        "t = ibis.examples.penguins.fetch()  # <3>\n",
        "t = con.create_table(\"penguins\", t.to_pyarrow(), overwrite=True)  # <3>"
      ],
      "id": "60b0d282",
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "1. Import the libraries we need.\n",
        "2. Load the environment variable to setup Marvin to call our OpenAI account.\n",
        "3. Setup the demo datain an Ibis backend.\n"
      ],
      "id": "8d246789"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "import ibis  # <1>\n",
        "import marvin  # <1>\n",
        "\n",
        "from ibis.expr.schema import Schema  # <1>\n",
        "from ibis.expr.types.relations import Table  # <1>\n",
        "\n",
        "ibis.options.interactive = True  # <2>\n",
        "marvin.settings.llm_model = \"openai/gpt-4\"  # <2>\n",
        "\n",
        "con = ibis.connect(\"duckdb://penguins.ddb\")  # <3>\n",
        "t = con.table(\"penguins\")  # <3>"
      ],
      "id": "7bf8f9ee",
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "1. Import Ibis and Marvin.\n",
        "2. Configure Ibis (interactive) and Marvin (GPT-4).\n",
        "3. Connect to the data and load a table into a variable.\n"
      ],
      "id": "d38a0eb1"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "@marvin.ai_fn  # <1>\n",
        "def _generate_sql_select(\n",
        "    text: str, table_name: str, table_schema: Schema\n",
        ") -> str:  # <1>\n",
        "    \"\"\"Generate SQL SELECT from text.\"\"\"  # <1>\n",
        "\n",
        "\n",
        "def sql_from_text(text: str, t: Table) -> Table:  # <2>\n",
        "    \"\"\"Run SQL from text.\"\"\"  # <2>\n",
        "    return t.sql(_generate_sql_select(text, t.get_name(), t.schema()).strip(\";\"))  # <2>"
      ],
      "id": "e28e6dcd",
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "1. A non-deterministic, LLM-powered AI function.\n",
        "2. A deterministic, human-authored function that calls the AI function.\n"
      ],
      "id": "989a0f14"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "t2 = sql_from_text(\"the unique combination of species and islands\", t)\n",
        "t2"
      ],
      "id": "286ac581",
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "t3 = sql_from_text(\n",
        "    \"the unique combination of species and islands, with their counts, ordered from highest to lowest, and name that column just 'count'\",\n",
        "    t,\n",
        ")\n",
        "t3"
      ],
      "id": "7acb6628",
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Summary\n",
        "\n",
        "To summarize this post:\n"
      ],
      "id": "ac4c3dd8"
    },
    {
      "cell_type": "code",
      "metadata": {},
      "source": [
        "from rich import print\n",
        "\n",
        "with open(\"index.qmd\", \"r\") as f:\n",
        "    self_text = f.read()\n",
        "\n",
        "# save some money and avoid rate limiting\n",
        "marvin.settings.llm_model = \"openai/gpt-3.5-turbo-16k\"  # <2>\n",
        "\n",
        "@marvin.ai_model\n",
        "class Summary(BaseModel):\n",
        "    \"\"\"Summary of text.\"\"\"\n",
        "\n",
        "    summary_line: str = Field(..., description=\"The one-line summary of the text.\")\n",
        "    summary_paragraph: str = Field(\n",
        "        ..., description=\"The one-paragraph summary of the text.\"\n",
        "    )\n",
        "    conclusion: str = Field(\n",
        "        ..., description=\"The conclusion the reader should draw from the text.\"\n",
        "    )\n",
        "    key_points: list[str] = Field(..., description=\"The key points of the text.\")\n",
        "    critiques: list[str] = Field(\n",
        "        ..., description=\"Professional, fair critiques of the text.\"\n",
        "    )\n",
        "    suggested_improvements: list[str] = Field(\n",
        "        ..., description=\"Suggested improvements for the text.\"\n",
        "    )\n",
        "    sentiment: float = Field(..., description=\"The sentiment of the text.\")\n",
        "    sentiment_label: str = Field(..., description=\"The sentiment label of the text.\")\n",
        "    author_bias: str = Field(..., description=\"The author bias of the text.\")\n",
        "\n",
        "\n",
        "print(Summary(self_text))"
      ],
      "id": "d1dddf9f",
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Next steps\n",
        "\n",
        "You can get involved with [Ibis\n",
        "Birdbrain](https://github.com/ibis-project/ibis-birdbrain), our open-source data\n",
        "& AI project for building next-generation natural language interfaces to data.\n",
        "\n",
        "## Discussions"
      ],
      "id": "471587d8"
    }
  ],
  "metadata": {
    "kernelspec": {
      "name": "python3",
      "language": "python",
      "display_name": "Python 3 (ipykernel)"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}