{ "cells": [ { "cell_type": "markdown", "id": "6da76396", "metadata": {}, "source": [ "### An application to generate epigrams\n", "\n", "For our next assignment we are going to pretend that we have been hired by a company that is working on a blogging platform. One of the features that they would like to add is an automated epigram generator. An epigram is a short quote that many authors attach to works like blog posts or chapters in a book that is meant to introduce the content in the work." ] }, { "cell_type": "markdown", "id": "2815bd70", "metadata": {}, "source": [ "One way we could solve this problem would be to use a large LLM. Since large LLMs have a lot of knowledge about the world, they know a large number of quotes and can find a quote to match a block of text. For example, if I ask the larger LLM we have access to produce an epigram for this text:\n", "\n", ">Americans are reading less, sleeping less and partying less. We have fewer marriages, fewer children and fewer friends than we used to. Our children are doing worse in school.\n", ">\n", ">These are complicated phenomena on some level, but on another level it’s pretty simple: Smartphones, social media and the internet are transforming our lives and our culture in ways that are not all for the best. Streaming video is ubiquitous, and some of the smartest people in the world have developed powerful recommendation engines to ensure that we always have an unlimited supply of customized content. Technological progress has, in effect, supercharged television, and as a result people are passively watching more and actively doing less.\n", ">\n", ">Yet rather than saying straightforwardly that this is an unhealthy trend and the US should enlist public policy to reverse it, many critics are focusing on a bank-shot idea: This is a competition policy problem related to the bigness of the largest technology companies. Others have gone full anti-capitalist and have slogans about the evils of tech billionaires.\n", ">\n", ">None of this makes very much sense. Some of the biggest culprits for transforming the information superhighway into a giant time suck are companies such as TikTok and X, which are not particularly large. Some of the biggest technology companies, such as Nvidia, Oracle and Broadcom, are only incidentally involved in the content glut.\n", ">\n", ">More to the point, the issue here is not the classic antitrust problem of a monopolist restricting output to raise prices. There is too much content, and the content is too cheap — in fact, it’s generally free. Tech skeptics who acknowledge this point have, like Lina Khan in her star-making law review article “Amazon’s Antitrust Paradox,” tended to retreat to convoluted theories about corporate power that manifests in non-price ways.\n", ">\n", ">There is a better way to think about the issue of overconsumption of digital content: It is a vice.\n", ">\n", ">The US has a long tradition of public policies designed to reduce the availability of vice products, from alcohol and tobacco to pornography. These industries are regulated not to create a better value proposition for consumers, but to make it worse. What motivates the regulation is not hostility to market capitalism or entrepreneurs, but respect for the power of free markets to create abundance, even of vice products.\n", ">\n", ">Granted, the US has stepped away from vice regulation in recent years. More than half of Americans live in states where marijuana is legal, and there is easy nationwide access to online sports gambling. At the same time, there has been a huge reduction in smoking in the last few decades, a reminder that public policy can constrain the consumption of undesirable goods.\n", "\n", "It will respond with \n", "\n", ">“A wealth of information creates a poverty of attention.” — Herbert A. Simon" ] }, { "cell_type": "markdown", "id": "72dce4c3", "metadata": {}, "source": [ "The problem with using large LLMs is that they are expensive. To bypass this expense we can use a combination of a RAG system and a smaller, cheaper LLM. The smaller LLM doesn't need to know any quotes itself, it just needs to be able to perform simple tasks such as labeling, summarization, and evaluation of potential quotes. A further benefit of using a RAG system is that we can control the pool of potential quotes, which is something that we can't do when we simply ask an LLM to come up with a quote itself." ] }, { "cell_type": "markdown", "id": "573ae56c", "metadata": {}, "source": [ "The first thing we are going to need is a database of quotations.\n", "\n", "We can download one such database from Hugging Face." ] }, { "cell_type": "code", "execution_count": null, "id": "7c72f893", "metadata": {}, "outputs": [], "source": [ "!pip install pandas" ] }, { "cell_type": "code", "execution_count": 2, "id": "c1b71876", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "\n", "df = pd.read_json(\"hf://datasets/Abirate/english_quotes/quotes.jsonl\", lines=True)" ] }, { "cell_type": "code", "execution_count": 6, "id": "590b1fb2", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | quote | \n", "author | \n", "tags | \n", "
|---|---|---|---|
| 0 | \n", "“Be yourself; everyone else is already taken.” | \n", "Oscar Wilde | \n", "[be-yourself, gilbert-perreira, honesty, inspi... | \n", "
| 1 | \n", "“I'm selfish, impatient and a little insecure.... | \n", "Marilyn Monroe | \n", "[best, life, love, mistakes, out-of-control, t... | \n", "
| 2 | \n", "“Two things are infinite: the universe and hum... | \n", "Albert Einstein | \n", "[human-nature, humor, infinity, philosophy, sc... | \n", "
| 3 | \n", "“So many books, so little time.” | \n", "Frank Zappa | \n", "[books, humor] | \n", "
| 4 | \n", "“A room without books is like a body without a... | \n", "Marcus Tullius Cicero | \n", "[books, simile, soul] | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 2503 | \n", "“Morality is simply the attitude we adopt towa... | \n", "Oscar Wilde, | \n", "[morality, philosophy] | \n", "
| 2504 | \n", "“Don't aim at success. The more you aim at it ... | \n", "Viktor E. Frankl, | \n", "[happiness, success] | \n", "
| 2505 | \n", "“In life, finding a voice is speaking and livi... | \n", "John Grisham | \n", "[inspirational-life] | \n", "
| 2506 | \n", "“Winter is the time for comfort, for good food... | \n", "Edith Sitwell | \n", "[comfort, home, winter] | \n", "
| 2507 | \n", "“Silence is so freaking loud” | \n", "Sarah Dessen, | \n", "[just-listen, loud, owen, sara-dessen, silence] | \n", "
2508 rows × 3 columns
\n", "| \n", " | quote | \n", "author | \n", "tags | \n", "
|---|---|---|---|
| 3 | \n", "“So many books, so little time.” | \n", "Frank Zappa | \n", "[books, humor] | \n", "
| 4 | \n", "“A room without books is like a body without a... | \n", "Marcus Tullius Cicero | \n", "[books, simile, soul] | \n", "
| 32 | \n", "“The person, be it gentleman or lady, who has ... | \n", "Jane Austen, | \n", "[aliteracy, books, classic, humor] | \n", "
| 36 | \n", "“Good friends, good books, and a sleepy consci... | \n", "Mark Twain | \n", "[books, contentment, friends, friendship, life] | \n", "
| 39 | \n", "“Fairy tales are more than true: not because t... | \n", "Neil Gaiman, | \n", "[books, dragons, fairy-tales, inspirational, p... | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 2326 | \n", "“Five exclamation marks, the sure sign of an i... | \n", "Terry Pratchett, | \n", "[books, humor] | \n", "
| 2330 | \n", "“Reading is like thinking, like praying, like ... | \n", "Roberto Bolaño, | \n", "[books, reading] | \n", "
| 2345 | \n", "“Literature is a luxury; fiction is a necessity.” | \n", "G.K. Chesterton | \n", "[books, literature, on-fiction] | \n", "
| 2484 | \n", "“The worst thing about new books is that they ... | \n", "Joseph Joubert | \n", "[books, reading] | \n", "
| 2488 | \n", "“The VoiceThere is a voice inside of youThat w... | \n", "Shel Silverstein | \n", "[books, inner-voice, leaders, parents, right, ... | \n", "
141 rows × 3 columns
\n", "| \n", " | quote | \n", "author | \n", "tags | \n", "
|---|---|---|---|
| 2 | \n", "“Two things are infinite: the universe and hum... | \n", "Albert Einstein | \n", "[human-nature, humor, infinity, philosophy, sc... | \n", "
| 3 | \n", "“So many books, so little time.” | \n", "Frank Zappa | \n", "[books, humor] | \n", "
| 8 | \n", "“You only live once, but if you do it right, o... | \n", "Mae West | \n", "[humor, life] | \n", "
| 25 | \n", "“Insanity is doing the same thing, over and ov... | \n", "Narcotics Anonymous | \n", "[humor, insanity, life, misattributed-ben-fran... | \n", "
| 32 | \n", "“The person, be it gentleman or lady, who has ... | \n", "Jane Austen, | \n", "[aliteracy, books, classic, humor] | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 2443 | \n", "“Sebastian just smiled. “I could hear your h... | \n", "Cassandra Clare, | \n", "[humor, jace-wayland, realization, sarcasm, se... | \n", "
| 2453 | \n", "“Moths,\" repeats Will. \"You're afraid of moths... | \n", "Veronica Roth, | \n", "[humor] | \n", "
| 2470 | \n", "“If you try to fail, and succeed, which have y... | \n", "George Carlin | \n", "[humor, humour, paradox] | \n", "
| 2476 | \n", "“If my doctor told me I had only six minutes t... | \n", "Isaac Asimov | \n", "[humor, writing] | \n", "
| 2482 | \n", "“See, the problem is that God gives men a brai... | \n", "Robin Williams | \n", "[humor, men] | \n", "
254 rows × 3 columns
\n", "