Build an AI App

Extraction

Import generateText from AI SDK and call it.

app/(1-extraction)/extraction.ts
import "dotenv/config";
import fs from "fs";
import { generateText } from "ai"; 
 
// import essay
const essay = fs.readFileSync("app/(1-extraction)/essay.txt", "utf-8");
 
async function main() {
  const result = await generateText({}); 
}
 
main().catch(console.error);

Import the OpenAI provider and define your model.

app/(1-extraction)/extraction.ts
import "dotenv/config";
import fs from "fs";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai"; 
 
// import essay
const essay = fs.readFileSync("app/(1-extraction)/essay.txt", "utf-8");
 
async function main() {
  const result = await generateText({
    model: openai("gpt-4o-mini"), 
  });
}
 
main().catch(console.error);

Pass in a prompt. In this case, we want to extract all the names mentioned in the essay.

app/(1-extraction)/extraction.ts
import "dotenv/config";
import fs from "fs";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
 
// import essay
const essay = fs.readFileSync("app/(1-extraction)/essay.txt", "utf-8");
 
async function main() {
  const result = await generateText({
    model: openai("gpt-4o-mini"),
    prompt: "Extract all the names mentioned in this essay." + "\n\n" + essay, 
  });
}
 
main().catch(console.error);

Now, let's log the resulting text generation to the console.

app/(1-extraction)/extraction.ts
import "dotenv/config";
import fs from "fs";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
 
// import essay
const essay = fs.readFileSync("app/(1-extraction)/essay.txt", "utf-8");
 
async function main() {
  const result = await generateText({
    model: openai("gpt-4o-mini"),
    prompt: "Extract all the names mentioned in this essay." + "\n\n" + essay,
  });
 
  console.log(result.text); 
}
 
main().catch(console.error);

Run the script

pnpm extraction

Play around with the prompt to ask for different things like, "What is the key takeaway of this piece in 50 words?"

app/(1-extraction)/extraction.ts
import "dotenv/config";
import fs from "fs";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
 
// import essay
const essay = fs.readFileSync("app/(1-extraction)/essay.txt", "utf-8");
 
async function main() {
  const result = await generateText({
    model: openai("gpt-4o-mini"),
    prompt: "What is the key takeaway of this piece in 50 words?" + "\n\n" + essay, 
  });
 
  console.log(result.text);
}
 
main().catch(console.error);

Play around with a different model to see how the response changes.

app/(1-extraction)/extraction.ts
import "dotenv/config";
import fs from "fs";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";
 
// import essay
const essay = fs.readFileSync("app/(1-extraction)/essay.txt", "utf-8");
 
async function main() {
  const result = await generateText({
    model: openai("gpt-4o"), 
    prompt: "What is the key takeaway of this piece in 50 words?" + "\n\n" + essay,
  });
 
  console.log(result.text);
}
 
main().catch(console.error);

Keep playing around with the prompt. Ask for different things and see how the model responds. You can also try different models to see how the response changes.