import { z } from "zod";
import { generateText, tool } from "ai";
import { openai } from "@ai-sdk/openai";
import { smartScraper } from "scrapegraph-js";
const apiKey = process.env.SGAI_APIKEY;
const ArticleSchema = z.object({
title: z.string().describe("The article title"),
author: z.string().describe("The author's name"),
publishDate: z.string().describe("Article publication date"),
content: z.string().describe("Main article content"),
category: z.string().describe("Article category"),
});
const ArticlesArraySchema = z
.array(ArticleSchema)
.describe("Array of articles");
const result = await generateText({
model: openai("gpt-4.1-mini"),
tools: {
scrape: tool({
description: "Get articles information for a given url.",
parameters: z.object({
url: z.string().describe("The exact url."),
}),
execute: async ({ url }) => {
const response = await smartScraper(
apiKey,
url,
"Extract the article information",
ArticlesArraySchema
);
const result: z.infer<typeof ArticleSchema> = response;
return result;
},
}),
},
prompt: "Can you find me the articles on https://scrapegraphai.com/blog?",
});
console.log(result);