import { generateObject } from 'npm:ai';
import { openai } from 'npm:@ai-sdk/openai';
import { z } from 'npm:zod';
import dotenv from 'npm:dotenv';
let _ = dotenv.config();
const gpt4o = openai('gpt-4o');
function showTable(tableObject) {
const tableHTML = `
<table>
<thead>
<tr>
${Object.keys(tableObject[0]).map(column => `<th style="text-align: center">${column}</th>`).join('')}
</tr>
</thead>
<tbody>
${tableObject.map(row => `
<tr>
${Object.values(row).map(column => `<td style="text-align: left">${column}</td>`).join('')}
</tr>
`).join('')}
</tbody>
</table>
`;
//return await Deno.jupyter.display({ 'text/html': tableHTML }, { raw: true });
return Deno.jupyter.html`${tableHTML}`;
; }
LLMs: Beyond Chat - JS/TS + Vercel AI SDK port
const germanText = 'Sprachkenntnisse sind ein wichtiger Bestandteil der Kommunikation.';
const translation = await generateObject({
: gpt4o,
model: z.object({
schema: z.string().describe('The language of the original text, as 2-letter language code.'),
input_language: z.string(),
translation,
}): '',
system: germanText,
prompt;
})
console.log(translation.object);
{
input_language: "de",
translation: "Language skills are an important part of communication."
}
const englishText = 'Large Language Models are a powerful tool for natural language processing.';
const targetLanguages = ['de', 'fr', 'it', 'es', 'he'];
const translations = await generateObject({
: gpt4o,
model: z.object({
schema: z.array(z.object({
translations: z.enum(targetLanguages).describe(
language'The target language for the translation, as 2-letter language code.'
,
): z.string(),
translation
})),
}): 'Translate the user-provided text into the following languages: ' +
systemJSON.stringify(targetLanguages),
: englishText,
prompt: 0.7,
temperature;
})
showTable(translations.object.translations);
language | translation |
---|---|
de | Große Sprachmodelle sind ein leistungsstarkes Werkzeug für die Verarbeitung natürlicher Sprache. |
fr | Les modèles de langue de grande taille sont un outil puissant pour le traitement du langage naturel. |
it | I grandi modelli linguistici sono uno strumento potente per l'elaborazione del linguaggio naturale. |
es | Los modelos de lenguaje grande son una herramienta poderosa para el procesamiento del lenguaje natural. |
he | מודלים גדולים לעיבוד שפה הם כלי עוצמתי לעיבוד שפה טבעית. |
const inputText = 'Large Language Models are a powerful tool for natural language processing.';
const textStyles = ['formal', 'informal', 'casual', 'academic', 'professional', 'business'];
const normalizations = await generateObject({
: gpt4o,
model: z.object({
schema: z.array(z.object({
normalizations: z.enum(textStyles).describe(
language'The style of the text normalization.'
,
): z.string(),
translation
})),
}): 'Normalize the user-provided text into the following styles: ' +
systemJSON.stringify(textStyles),
: inputText,
prompt: 0.7,
temperature;
})
showTable(normalizations.object.normalizations);
language | translation |
---|---|
formal | Large Language Models are a highly effective instrument for natural language processing. |
informal | Large Language Models are super handy for natural language processing. |
casual | Large Language Models are pretty useful for natural language processing. |
academic | Large Language Models represent a significant advancement in the field of natural language processing, offering substantial capabilities for linguistic analysis. |
professional | Large Language Models provide robust solutions for natural language processing tasks, enhancing efficiency and accuracy. |
business | Large Language Models are a valuable asset in the realm of natural language processing, driving innovation and improving communication strategies. |
const addressStr = (
'Sherlock Holmes lives in the United Kingdom. ' +
'His residence is in at 221B Baker Street, London, NW1 6XE.'
;
)
const addressInfo = await generateObject({
: gpt4o,
model: z.object({
schema: z.string(),
first_name: z.string(),
last_name: z.string(),
street: z.string(),
house_number: z.string(),
postal_code: z.string(),
city: z.string(),
state: z.string(),
country,
}): addressStr,
prompt;
})
console.log(addressInfo.object);
{
first_name: "Sherlock",
last_name: "Holmes",
street: "Baker Street",
house_number: "221B",
postal_code: "NW1 6XE",
city: "London",
state: "",
country: "United Kingdom"
}
const addressesText = (
'During my recent travels, I had the pleasure of visiting several fascinating locations. ' +
'My journey began at the office of Dr. Elena Martinez, 142B Elm Street, San Francisco, ' +
'CA 94107, USA. Her office, nestled in the bustling heart of the city, was a hub of ' +
'innovation and creativity. Next, I made my way to the historic residence of Mr. Hans ' +
'Gruber located at 3. Stock, Goethestrasse 22, 8001 Zürich, Switzerland. The old building, ' +
'with its classic Swiss architecture, stood as a testament to the city’s rich cultural ' +
'heritage. My adventure continued at the tranquil countryside home of Satoshi Nakamoto, ' +
'2-15-5, Sakura-cho, Musashino-shi, Tokyo-to 180-0003, Japan. Their home was surrounded by ' +
'beautiful cherry blossoms, creating a picturesque scene straight out of a postcard. In ' +
'Europe, I visited the charming villa of Mme. Catherine Dubois, 15 Rue de la République, ' +
'69002 Lyon, France. The cobblestone streets and historic buildings of Lyon provided a ' +
'perfect backdrop to her elegant home. Finally, my journey concluded at the modern apartment ' +
'of Mr. David Johnson, Apt 7B, 34 Queen Street, Toronto, ON M5H 2Y4, Canada. The sleek ' +
'design of the apartment building mirrored the contemporary vibe of the city itself.'
)
const addresses = await generateObject({
: gpt4o,
model: z.object({
schema: z.array(z.object({
addresses: z.string(),
first_name: z.string(),
last_name: z.string(),
street: z.string(),
house_number: z.string(),
postal_code: z.string(),
city: z.string(),
state: z.string(),
country
})),
}): 'Return all the addresses in the user-provided text.',
system: addressesText,
prompt;
})
showTable(addresses.object.addresses);
first_name | last_name | street | house_number | postal_code | city | state | country |
---|---|---|---|---|---|---|---|
Elena | Martinez | Elm Street | 142B | 94107 | San Francisco | CA | USA |
Hans | Gruber | Goethestrasse | 22 | 8001 | Zürich | Switzerland | |
Satoshi | Nakamoto | Sakura-cho | 2-15-5 | 180-0003 | Musashino-shi | Tokyo-to | Japan |
Catherine | Dubois | Rue de la République | 15 | 69002 | Lyon | France | |
David | Johnson | Queen Street | 34 | M5H 2Y4 | Toronto | ON | Canada |
const graphInputText = (
'Some products are edible and others are inedible. Soap, newspapers, and shoes, for example, ' +
'are inedible. Of the products that are edible, some are sweet and others are savory. ' +
'Chocolate, candy, and ice cream are sweet, while pizza, burgers, and fries are savory. ' +
'Chocolate comes in different forms, such as milk chocolate, dark chocolate, and white chocolate. ' +
'The New York Times, The Wall Street Journal, and The Washington Post are newspapers.'
;
)
const graph = await generateObject({
: gpt4o,
model: z.object({
schema: z.array(z.object({
nodes: z.number(),
id: z.string(),
label,
})): z.array(z.object({
edges: z.number(),
source: z.number(),
target,
})),
}): 'Format the information in the user-provided text as a knowledge graph.',
system: graphInputText,
prompt;
})
console.log(graph.object.nodes);
console.log(graph.object.edges);
[
{ id: 1, label: "Product" },
{ id: 2, label: "Edible" },
{ id: 3, label: "Inedible" },
{ id: 4, label: "Sweet" },
{ id: 5, label: "Savory" },
{ id: 6, label: "Soap" },
{ id: 7, label: "Newspaper" },
{ id: 8, label: "Shoes" },
{ id: 9, label: "Chocolate" },
{ id: 10, label: "Candy" },
{ id: 11, label: "Ice Cream" },
{ id: 12, label: "Pizza" },
{ id: 13, label: "Burgers" },
{ id: 14, label: "Fries" },
{ id: 15, label: "Milk Chocolate" },
{ id: 16, label: "Dark Chocolate" },
{ id: 17, label: "White Chocolate" },
{ id: 18, label: "The New York Times" },
{ id: 19, label: "The Wall Street Journal" },
{ id: 20, label: "The Washington Post" }
]
[
{ source: 1, target: 2 },
{ source: 1, target: 3 },
{ source: 2, target: 4 },
{ source: 2, target: 5 },
{ source: 3, target: 6 },
{ source: 3, target: 7 },
{ source: 3, target: 8 },
{ source: 4, target: 9 },
{ source: 4, target: 10 },
{ source: 4, target: 11 },
{ source: 5, target: 12 },
{ source: 5, target: 13 },
{ source: 5, target: 14 },
{ source: 9, target: 15 },
{ source: 9, target: 16 },
{ source: 9, target: 17 },
{ source: 7, target: 18 },
{ source: 7, target: 19 },
{ source: 7, target: 20 }
]
const inputStr = 'the quick brown fox jumps over the lazy dog';
const NounPhrase = z.object({
: z.string(),
det: z.array(z.string()),
adj: z.string(),
noun;
})
const PrepPhrase = z.object({
: z.string(),
prep: NounPhrase,
noun;
})
const VerbPhrase = z.object({
: NounPhrase,
noun: z.string(),
verb: PrepPhrase,
prep;
})
const grammarTree = await generateObject({
: gpt4o,
model: VerbPhrase,
schema: 'Parse the user-provided sentence into a simple grammar tree.',
system: inputStr,
prompt;
})
console.log(JSON.stringify(grammarTree.object, null, 2));
{
"noun": {
"det": "the",
"adj": [
"quick",
"brown"
],
"noun": "fox"
},
"verb": "jumps",
"prep": {
"prep": "over",
"noun": {
"det": "the",
"adj": [
"lazy"
],
"noun": "dog"
}
}
}
const foodReview = await generateObject({
: gpt4o,
model: z.object({
schema: z.number(),
rating: z.string().describe('A review about food.'),
sentiment,
}): 'Generate food review with sentiments within a spectrum of sentiments, with rating between 1 and 5.',
prompt: 0.5,
temperature;
})
console.log(foodReview.object);
{
rating: 1,
sentiment: "The food was absolutely terrible. It was cold, tasteless, and the presentation was unappetizing. I would not recommend this place to anyone."
}
const ratings = ['*', '**', '***', '****', '*****'];
const numReviews = 10;
const foodReviews = await generateObject({
: gpt4o,
model: z.object({
schema: z.array(z.object({
reviews: z.enum(ratings).describe('rating of food review between 1 and 5'),
rating: z.string().describe('A generated review about food.'),
sentiment
})),
}): (
prompt`Generate ${numReviews} food review with sentiments within a spectrum of sentiments in ` +
`sorted order from most negative to positive, with rating: ` +
`${JSON.stringify(ratings)}`
,
): 0.7,
temperature;
})
showTable(foodReviews.object.reviews);
rating | sentiment |
---|---|
* | The food was inedible, with a taste that was both bland and unpleasant. |
* | I was thoroughly disappointed; the dish was cold and lacked any flavor. |
** | The meal was underwhelming and not worth the price. |
** | I expected more; the food was average at best. |
*** | It was decent, but nothing to write home about. |
*** | The food was okay, satisfying but lacking in excitement. |
**** | I enjoyed the meal; it was well-prepared and flavorful. |
**** | Quite good, with a nice balance of flavors. |
***** | The food was absolutely delicious with exquisite flavors. |
***** | An amazing culinary experience, everything was perfect! |
const exampleTexts = [
'I am very happy with the service provided by the company.',
'The food was terrible and the service was slow.',
'The movie was okay.',
'The weather is perfect for a day at the beach.',
'I am mostly satisfied with the product, but there are a few issues.',
'The experience was not quite what I have expected.',
'Butterflies are often colourful, and they can fly.',
]
const sentiments = await Promise.all(exampleTexts.map(async (text) => {
const response = await generateObject({
: gpt4o,
model: z.object({
schema: z.string(),
sentiment: z.number(),
confidence,
}): 'Analyze the sentiment of the user-provided text.',
system: text,
prompt;
})return response.object;
;
}))
showTable(sentiments.map((sentiment, idx) => {
return {
: exampleTexts[idx],
text: sentiment.sentiment,
sentiment: sentiment.confidence,
confidence;
}; }))
text | sentiment | confidence |
---|---|---|
I am very happy with the service provided by the company. | positive | 0.95 |
The food was terrible and the service was slow. | negative | 0.95 |
The movie was okay. | neutral | 0.7 |
The weather is perfect for a day at the beach. | positive | 0.95 |
I am mostly satisfied with the product, but there are a few issues. | neutral | 0.7 |
The experience was not quite what I have expected. | negative | 0.7 |
Butterflies are often colourful, and they can fly. | neutral | 0.95 |
const items = [
: 'The Great Gatsby', subtitle: 'A novel by F. Scott Fitzgerald'},
{title: 'The Theory of Relativity', subtitle: 'A scientific theory by Albert Einstein'},
{title: 'The Technology and Culture of Ancient Rome', subtitle: 'A cross-disciplinary study of ancient Rome'},
{title: 'Football on Television', subtitle: 'The technology and cultural impact of televising football games'},
{title: 'The Philosophy of Taylor Swift', subtitle: 'A philosophical analysis of the music and lyrics of Taylor Swift'},
{title: 'The Spanish Language in popular music', subtitle: 'A review of the use of the Spanish language in popular music'},
{title: 'The Impact of Artificial Intelligence on Healthcare', subtitle: 'Exploring the role of AI in revolutionizing healthcare'},
{title: 'The History of Jazz Music', subtitle: 'Tracing the origins and evolution of jazz music'},
{title: 'The Rise of E-commerce in the Digital Age', subtitle: 'Examining the growth and impact of online shopping'},
{title: 'The Art of Photography', subtitle: 'Exploring the creative and technical aspects of photography'},
{title: 'The Psychology of Decision Making', subtitle: 'Understanding the cognitive processes behind decision making'},
{title: 'The Role of Women in STEM Fields', subtitle: 'Highlighting the contributions of women in science, technology, engineering, and mathematics'},
{title: 'The Cultural Significance of Tattoos', subtitle: 'Exploring the history and symbolism of tattoos in different cultures'},
{title;
]
const tags = [
'literature', 'science', 'history', 'technology', 'art', 'music', 'sports',
'philosophy', 'language', 'feminism', 'health', 'media', 'physics', 'culture',
'psychology', 'artificial_intelligence',
;
]
const itemTags = await generateObject({
: gpt4o,
model: z.object({
schema: z.array(z.object({
items: z.string(),
title: z.string(),
subtitle: z.array(z.enum(tags)),
tags
})),
}): `Tag the following items with the appropriate tags. Options: ${JSON.stringify(tags)}`,
system: JSON.stringify(items),
prompt;
})
showTable(itemTags.object.items.map((item) => {
return {
: item.title,
title: item.subtitle,
subtitle: item.tags.join(' '),
tags;
}; }))
title | subtitle | tags |
---|---|---|
The Great Gatsby | A novel by F. Scott Fitzgerald | literature |
The Theory of Relativity | A scientific theory by Albert Einstein | science physics |
The Technology and Culture of Ancient Rome | A cross-disciplinary study of ancient Rome | technology culture history |
Football on Television | The technology and cultural impact of televising football games | technology culture sports media |
The Philosophy of Taylor Swift | A philosophical analysis of the music and lyrics of Taylor Swift | philosophy music |
The Spanish Language in popular music | A review of the use of the Spanish language in popular music | language music culture |
The Impact of Artificial Intelligence on Healthcare | Exploring the role of AI in revolutionizing healthcare | artificial_intelligence health technology |
The History of Jazz Music | Tracing the origins and evolution of jazz music | history music culture |
The Rise of E-commerce in the Digital Age | Examining the growth and impact of online shopping | technology culture |
The Art of Photography | Exploring the creative and technical aspects of photography | art technology |
The Psychology of Decision Making | Understanding the cognitive processes behind decision making | psychology |
The Role of Women in STEM Fields | Highlighting the contributions of women in science, technology, engineering, and mathematics | feminism science technology |
The Cultural Significance of Tattoos | Exploring the history and symbolism of tattoos in different cultures | culture history art |
const numClusters = 5;
const taggedItemsClusters = await generateObject({
: gpt4o,
model: z.object({
schema: z.array(z.object({
clusters: z.string().describe('The title of the cluster.'),
title: z.array(z.string()).describe('The titles of the items in the cluster.'),
items
})),
}): `Cluster the following items based on their tags and content. Create exactly ${numClusters} clusters.`,
system: JSON.stringify(itemTags.object.items),
prompt;
})
const clustersTable = [];
for (const cluster of taggedItemsClusters.object.clusters) {
for (const itemTitle of cluster.items) {
const item = itemTags.object.items.find(i => i.title === itemTitle);
if (item) {
.push({
clustersTable: cluster.title,
cluster: item.title,
title: item.subtitle,
subtitle: item.tags.join(' '),
tags;
})
}
}
}
showTable(clustersTable);
cluster | title | subtitle | tags |
---|---|---|---|
Literature and Philosophy | The Great Gatsby | A novel by F. Scott Fitzgerald | literature |
Literature and Philosophy | The Philosophy of Taylor Swift | A philosophical analysis of the music and lyrics of Taylor Swift | philosophy music |
Science and Psychology | The Theory of Relativity | A scientific theory by Albert Einstein | science physics |
Science and Psychology | The Psychology of Decision Making | Understanding the cognitive processes behind decision making | psychology |
Science and Psychology | The Role of Women in STEM Fields | Highlighting the contributions of women in science, technology, engineering, and mathematics | feminism science technology |
Technology and Its Impact | The Technology and Culture of Ancient Rome | A cross-disciplinary study of ancient Rome | technology culture history |
Technology and Its Impact | Football on Television | The technology and cultural impact of televising football games | technology culture sports media |
Technology and Its Impact | The Impact of Artificial Intelligence on Healthcare | Exploring the role of AI in revolutionizing healthcare | artificial_intelligence health technology |
Technology and Its Impact | The Rise of E-commerce in the Digital Age | Examining the growth and impact of online shopping | technology culture |
Technology and Its Impact | The Art of Photography | Exploring the creative and technical aspects of photography | art technology |
Music and Culture | The Spanish Language in popular music | A review of the use of the Spanish language in popular music | language music culture |
Music and Culture | The History of Jazz Music | Tracing the origins and evolution of jazz music | history music culture |
Culture and History | The Cultural Significance of Tattoos | Exploring the history and symbolism of tattoos in different cultures | culture history art |
const actions = [
'Wake up',
'Turn off the alarm',
'Stretch',
'Get out of bed',
'Use the bathroom',
'Check for movie snacks',
'Wash face in the evening',
'Change into pyjamas',
'Set alarm for the next day',
'Check phone for messages',
'Turn off lights',
'Walk or drive to the movie theatre',
'Use the bathroom in the evening',
'Dry off with a towel',
'Brush teeth',
'Wash face',
'Take a shower',
'Grab wallet or purse',
'Make sure phone is charged',
'Call a taxi or arrange transportation',
'Meet friends at designated place',
'Leave the house',
'Plan to buy tickets at the theatre',
'Decide on meeting place and time',
'Get dressed',
'Apply deodorant',
'Comb or brush hair',
'Style hair',
'Shave',
'Put on clothes',
'Apply makeup',
'Prepare breakfast',
'Eat breakfast',
'Make coffee or tea',
'Check phone for messages or emails',
'Pack lunch',
'Gather work materials',
'Put on shoes',
'Grab keys',
'Purchase tickets at the theatre',
'Lock the door',
'Finish dinner',
'Clean up dinner dishes',
'Watch TV or read a book',
'Brush teeth in the evening',
'Get into bed',
'Meditate or relax',
'Write in journal',
'Listen to calming music',
'Turn off electronic devices',
'Adjust pillows and blankets',
'Read a book',
'Close eyes and try to sleep',
'Decide on a movie to watch',
'Check movie times online',
'Purchase tickets online',
'Buy snacks at the concession stand',
'Find the correct theatre screen',
'Find seats',
'Watch the movie',
'Discuss the movie with friends',
'Say goodbye to friends',
'Return home',
;
]
const activities = [
'Waking up and going to work',
'Winding down and going to sleep',
'Going to see a movie with friends',
;
]
for (const activity of activities) {
const actionSequence = await generateObject({
: gpt4o,
model: z.object({
schema: z.array(z.enum(actions)),
actions,
}): 'Generate a sequence of actions for the user-provided activity.',
system: activity,
prompt;
})
console.log(`Activity: ${activity}`);
.object.actions.forEach((action, idx) => {
actionSequenceconsole.log(`${idx + 1}. ${action}`);
;
})console.log('\n');
}
Activity: Waking up and going to work
1. Wake up
2. Turn off the alarm
3. Stretch
4. Get out of bed
5. Use the bathroom
6. Wash face
7. Brush teeth
8. Get dressed
9. Apply deodorant
10. Comb or brush hair
11. Prepare breakfast
12. Eat breakfast
13. Make coffee or tea
14. Check phone for messages or emails
15. Pack lunch
16. Gather work materials
17. Put on shoes
18. Grab keys
19. Lock the door
20. Leave the house
Activity: Winding down and going to sleep
1. Finish dinner
2. Clean up dinner dishes
3. Watch TV or read a book
4. Brush teeth in the evening
5. Use the bathroom in the evening
6. Wash face in the evening
7. Change into pyjamas
8. Set alarm for the next day
9. Turn off electronic devices
10. Adjust pillows and blankets
11. Get into bed
12. Meditate or relax
13. Write in journal
14. Listen to calming music
15. Read a book
16. Close eyes and try to sleep
Activity: Going to see a movie with friends
1. Decide on a movie to watch
2. Check movie times online
3. Purchase tickets online
4. Call a taxi or arrange transportation
5. Grab wallet or purse
6. Make sure phone is charged
7. Leave the house
8. Meet friends at designated place
9. Walk or drive to the movie theatre
10. Purchase tickets at the theatre
11. Buy snacks at the concession stand
12. Find the correct theatre screen
13. Find seats
14. Watch the movie
15. Discuss the movie with friends
16. Say goodbye to friends
17. Return home