{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Summarize text with LLMs\n", "\n", "Generate concise summaries of long text, articles, or documents using large language models.\n", "\n", "This notebook uses `pxt.create_table()` so you can run cells without a project. An app puts the same columns on a `TableModel` in `app.py` and creates the tables with `pxt schema update`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Problem\n", "\n", "You have long text content—articles, transcripts, documents—that needs to be summarized. Processing each piece manually is time-consuming and inconsistent.\n", "\n", "| Content | Length | Need |\n", "|---------|--------|------|\n", "| News articles | 2,000 words | One-paragraph summary |\n", "| Meeting transcripts | 10,000 words | Key points and action items |\n", "| Research papers | 8,000 words | Abstract-style summary |" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solution\n", "\n", "**What's in this recipe:**\n", "\n", "- Summarize text using OpenAI GPT models\n", "- Customize summary style with prompts\n", "- Process multiple documents automatically\n", "\n", "You add a computed column that calls an LLM to generate summaries. When you insert new text, summaries are generated automatically." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[33mWARNING: Ignoring invalid distribution ~orch (/opt/miniconda3/envs/pixeltable/lib/python3.11/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution ~orch (/opt/miniconda3/envs/pixeltable/lib/python3.11/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution ~orch (/opt/miniconda3/envs/pixeltable/lib/python3.11/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution ~orch (/opt/miniconda3/envs/pixeltable/lib/python3.11/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution ~orch (/opt/miniconda3/envs/pixeltable/lib/python3.11/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0m\u001b[33mWARNING: Ignoring invalid distribution ~orch (/opt/miniconda3/envs/pixeltable/lib/python3.11/site-packages)\u001b[0m\u001b[33m\n", "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install -qU pixeltable openai" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "if 'OPENAI_API_KEY' not in os.environ:\n", " os.environ['OPENAI_API_KEY'] = getpass.getpass('OpenAI API Key: ')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Load sample text" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Connected to Pixeltable database at: postgresql+psycopg://postgres:@/pixeltable?host=/Users/pjlb/.pixeltable/pgdata\n", "Created directory 'summarize_demo'.\n" ] }, { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pixeltable as pxt\n", "from pixeltable.functions import openai\n", "\n", "# Create a fresh directory\n", "pxt.drop_dir('summarize_demo', force=True)\n", "pxt.create_dir('summarize_demo')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Created table 'articles'.\n" ] } ], "source": [ "# Create table for articles\n", "articles = pxt.create_table(\n", " 'summarize_demo/articles',\n", " {'title': pxt.String, 'content': pxt.String},\n", ")" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inserting rows into `articles`: 2 rows [00:00, 316.21 rows/s]\n", "Inserted 2 rows with 0 errors.\n" ] }, { "data": { "text/plain": [ "2 rows inserted, 4 values computed." ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Sample articles to summarize\n", "sample_articles = [\n", " {\n", " 'title': 'The Rise of Electric Vehicles',\n", " 'content': \"\"\"Electric vehicles (EVs) have seen unprecedented growth in recent years,\n", " transforming the automotive industry. Sales increased by 60% globally in 2023,\n", " with China leading the market followed by Europe and North America. Major automakers\n", " like Tesla, BYD, and traditional manufacturers have invested billions in EV technology.\n", " Battery costs have dropped significantly, making EVs more affordable for consumers.\n", " Government incentives and stricter emissions regulations continue to drive adoption.\n", " Charging infrastructure is expanding rapidly, with new fast-charging networks being\n", " deployed across major highways. Despite challenges like range anxiety and charging\n", " times, consumer acceptance is growing steadily.\"\"\",\n", " },\n", " {\n", " 'title': 'Advances in Renewable Energy',\n", " 'content': \"\"\"Solar and wind power capacity reached record levels in 2023, accounting\n", " for over 30% of global electricity generation. The cost of solar panels has fallen\n", " by 90% over the past decade, making renewable energy competitive with fossil fuels.\n", " Offshore wind farms are being built at scale, with turbines now reaching heights\n", " of over 250 meters. Energy storage solutions, particularly lithium-ion batteries,\n", " are addressing intermittency challenges. Countries like Denmark and Scotland have\n", " achieved periods of 100% renewable electricity. Corporate power purchase agreements\n", " are accelerating the transition, with tech giants committing to carbon-neutral operations.\"\"\",\n", " },\n", "]\n", "\n", "articles.insert(sample_articles)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlecontent
The Rise of Electric VehiclesElectric vehicles (EVs) have seen unprecedented growth in recent years,\n", " transforming the automotive industry. Sales increased by 60% globally in 2023,\n", " with China leading the market followed by Europe and North America. Major automakers\n", " like Tesla, BYD, and traditional manufacturers have invested billions in EV technology.\n", " Battery costs have dropped significantly, making EVs more affordable for consumers.\n", " Government incentives and stricter emissions regulations continue to drive adoption.\n", " Charging infrastructure is expanding rapidly, with new fast-charging networks being\n", " deployed across major highways. Despite challenges like range anxiety and charging\n", " times, consumer acceptance is growing steadily.
Advances in Renewable EnergySolar and wind power capacity reached record levels in 2023, accounting\n", " for over 30% of global electricity generation. The cost of solar panels has fallen\n", " by 90% over the past decade, making renewable energy competitive with fossil fuels.\n", " Offshore wind farms are being built at scale, with turbines now reaching heights\n", " of over 250 meters. Energy storage solutions, particularly lithium-ion batteries,\n", " are addressing intermittency challenges. Countries like Denmark and Scotland have\n", " achieved periods of 100% renewable electricity. Corporate power purchase agreements\n", " are accelerating the transition, with tech giants committing to carbon-neutral operations.
" ], "text/plain": [ " title \\\n", "0 The Rise of Electric Vehicles \n", "1 Advances in Renewable Energy \n", "\n", " content \n", "0 Electric vehicles (EVs) have seen unprecedente... \n", "1 Solar and wind power capacity reached record l... " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# View articles\n", "articles.select(articles.title, articles.content).collect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generate summaries\n", "\n", "Add a computed column that generates summaries using GPT:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added 2 column values with 0 errors.\n" ] }, { "data": { "text/plain": [ "2 rows updated, 2 values computed." ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Create prompt template for summarization\n", "prompt = (\n", " 'Summarize the following article in 2-3 sentences:\\n\\n'\n", " + articles.content\n", ")\n", "\n", "# Add computed column for LLM response\n", "articles.add_computed_column(\n", " response=openai.chat_completions(\n", " messages=[{'role': 'user', 'content': prompt}],\n", " model='gpt-4o-mini',\n", " )\n", ")" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added 2 column values with 0 errors.\n" ] }, { "data": { "text/plain": [ "2 rows updated, 2 values computed." ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Extract the summary text from the response\n", "articles.add_computed_column(\n", " summary=articles.response.choices[0].message.content\n", ")" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlesummary
The Rise of Electric VehiclesElectric vehicles (EVs) have experienced remarkable growth, with global sales increasing by 60% in 2023, primarily driven by China, Europe, and North America. Major automakers, including Tesla and BYD, have invested heavily in EV technology, while decreasing battery costs and government incentives are making EVs more accessible to consumers. The expansion of charging infrastructure, despite lingering issues like range anxiety, is contributing to steadily growing consumer acceptance of EVs.
Advances in Renewable EnergyIn 2023, solar and wind power reached record levels, contributing over 30% to global electricity generation, driven by a 90% drop in solar panel costs and the development of large offshore wind farms. Innovations in energy storage, particularly lithium-ion batteries, are helping to manage intermittency, while countries like Denmark and Scotland have achieved 100% renewable electricity milestones. Additionally, corporate power purchase agreements from major tech companies are facilitating faster transitions to carbon-neutral operations.
" ], "text/plain": [ " title \\\n", "0 The Rise of Electric Vehicles \n", "1 Advances in Renewable Energy \n", "\n", " summary \n", "0 Electric vehicles (EVs) have experienced remar... \n", "1 In 2023, solar and wind power reached record l... " ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# View titles and summaries\n", "articles.select(articles.title, articles.summary).collect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Custom summary styles\n", "\n", "You can customize the summary format by changing the prompt:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Added 2 column values with 0 errors.\n", "Added 2 column values with 0 errors.\n" ] }, { "data": { "text/plain": [ "2 rows updated, 2 values computed." ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Add bullet-point summary\n", "bullet_prompt = (\n", " 'List the 3 key points from this article as bullet points:\\n\\n'\n", " + articles.content\n", ")\n", "\n", "articles.add_computed_column(\n", " bullet_response=openai.chat_completions(\n", " messages=[{'role': 'user', 'content': bullet_prompt}],\n", " model='gpt-4o-mini',\n", " )\n", ")\n", "\n", "articles.add_computed_column(\n", " key_points=articles.bullet_response.choices[0].message.content\n", ")" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlekey_points
The Rise of Electric Vehicles- **Significant Sales Growth**: Electric vehicle sales increased by 60% globally in 2023, with China, Europe, and North America leading the market.\n", "- **Investment and Affordability**: Major automakers, including Tesla and traditional manufacturers, have invested billions in EV technology, while battery costs have significantly dropped, making EVs more affordable.\n", "- **Expanding Infrastructure and Support**: The expansion of charging infrastructure and government incentives, along with stricter emissions regulations, are driving consumer acceptance despite challenges like range anxiety.
Advances in Renewable Energy- Solar and wind power accounted for over 30% of global electricity generation in 2023, reaching record capacity levels.\n", "- The cost of solar panels has decreased by 90% in the past decade, making renewable energy financially competitive with fossil fuels.\n", "- Significant developments in offshore wind farms and energy storage solutions, like lithium-ion batteries, are helping to overcome intermittency challenges, while corporate power purchase agreements drive the transition towards carbon-neutral operations.
" ], "text/plain": [ " title \\\n", "0 The Rise of Electric Vehicles \n", "1 Advances in Renewable Energy \n", "\n", " key_points \n", "0 - **Significant Sales Growth**: Electric vehic... \n", "1 - Solar and wind power accounted for over 30% ... " ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# View bullet-point summaries\n", "articles.select(articles.title, articles.key_points).collect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Automatic processing\n", "\n", "New articles are automatically summarized when inserted:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Inserting rows into `articles`: 1 rows [00:00, 411.57 rows/s]\n", "Inserted 1 row with 0 errors.\n" ] }, { "data": { "text/plain": [ "1 row inserted, 6 values computed." ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Insert a new article - summaries are generated automatically\n", "articles.insert(\n", " [\n", " {\n", " 'title': 'AI in Healthcare',\n", " 'content': \"\"\"Artificial intelligence is revolutionizing healthcare diagnostics\n", " and treatment planning. Machine learning models can now detect diseases from\n", " medical images with accuracy matching or exceeding human specialists. AI-powered\n", " drug discovery is accelerating the development of new treatments. Natural language\n", " processing is being used to extract insights from clinical notes and research papers.\"\"\",\n", " }\n", " ]\n", ")" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
titlesummary
The Rise of Electric VehiclesElectric vehicles (EVs) have experienced remarkable growth, with global sales increasing by 60% in 2023, primarily driven by China, Europe, and North America. Major automakers, including Tesla and BYD, have invested heavily in EV technology, while decreasing battery costs and government incentives are making EVs more accessible to consumers. The expansion of charging infrastructure, despite lingering issues like range anxiety, is contributing to steadily growing consumer acceptance of EVs.
Advances in Renewable EnergyIn 2023, solar and wind power reached record levels, contributing over 30% to global electricity generation, driven by a 90% drop in solar panel costs and the development of large offshore wind farms. Innovations in energy storage, particularly lithium-ion batteries, are helping to manage intermittency, while countries like Denmark and Scotland have achieved 100% renewable electricity milestones. Additionally, corporate power purchase agreements from major tech companies are facilitating faster transitions to carbon-neutral operations.
AI in HealthcareArtificial intelligence is transforming healthcare by enhancing diagnostics and treatment planning, with machine learning models achieving accuracy in disease detection that rivals or surpasses human specialists. Additionally, AI is expediting drug discovery and utilizing natural language processing to derive insights from clinical notes and research literature.
" ], "text/plain": [ " title \\\n", "0 The Rise of Electric Vehicles \n", "1 Advances in Renewable Energy \n", "2 AI in Healthcare \n", "\n", " summary \n", "0 Electric vehicles (EVs) have experienced remar... \n", "1 In 2023, solar and wind power reached record l... \n", "2 Artificial intelligence is transforming health... " ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# View all summaries including the new article\n", "articles.select(articles.title, articles.summary).collect()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Explanation\n", "\n", "**Prompt engineering for summaries:**\n", "\n", "| Style | Prompt pattern |\n", "|-------|----------------|\n", "| Brief | \"Summarize in 1-2 sentences\" |\n", "| Bullet points | \"List N key points as bullets\" |\n", "| Executive | \"Write an executive summary for business leaders\" |\n", "| Technical | \"Summarize the technical details\" |\n", "\n", "**Cost optimization:**\n", "\n", "- Use `gpt-4o-mini` for most summarization tasks (fast and affordable)\n", "- Use `gpt-4o` for complex documents requiring deeper understanding\n", "- Summaries are cached—you only pay once per article\n", " and stuand toofor trL para" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## See also\n", "\n", "- [Split documents for RAG](https://docs.pixeltable.com/howto/cookbooks/text/doc-chunk-for-rag) - Process long documents\n", "- [Extract fields from JSON](https://docs.pixeltable.com/howto/cookbooks/core/workflow-json-extraction) - Parse structured LLM output\n", "- [Configure API keys](https://docs.pixeltable.com/howto/cookbooks/core/workflow-api-keys) - Set up OpenAI credentials" ] } ], "metadata": { "kernelspec": { "display_name": "pixeltable", "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.11" } }, "nbformat": 4, "nbformat_minor": 2 }