AI-pipe:使用自定义脚本从网站抓取数据,生成/存储从 AI 模型到数据库的嵌入的管道

我建造了什么

一个网页,用于快速创建管道,为 AI 模型提供从提供的网页上抓取的数据。

特征

自定义脚本

通过提供的模板自定义脚本,可以完全控制从网页抓取的数据的种类、类型和形式。

嵌入生成

该 Web 服务支持从 OpenAI 和 Ollama AI 模型生成嵌入。它还为无法访问远程服务器上运行的 AI 模型的用户提供了后备方案,方法是

演示

虽然来得有点晚,但下面是已部署的 Web 应用程序演示的链接

https://ai-pipe.vercel.app/

ogbotemi-2000 / ai-pipe

一个 Web 应用程序,它可以从互联网上抓取你指定的数据,然后让你清理和格式化它,然后将其输入到 AI 模型中以生成嵌入

艾管

一个 Web 应用程序,它可以从互联网上抓取你指定的数据,然后让你清理和格式化它,然后将其输入到 AI 模型中以生成嵌入

AI模型提供商

欧拉玛

通过 Koyeb 等远程部署

开放人工智能

支持随请求主体添加 API 密钥

PostgresML

用作后备

抓取数据

您提供一个 URL 并指定要定位的节点以及从它们中提取什么样的数据,然后将这些数据发送到后端。

响应已发送,您可以针对目标节点的每个响应进行处理,通过编写脚本来格式化、清理数据并预览结果,然后通过您选择的 AI 模型为其生成嵌入。

嵌入

生成的嵌入可供复制

此外

该网页提供有关人工智能和……的有用资源链接。

在 GitHub 上查看

如何使用 Bright Data

抓取浏览器

我使用“Puppeteer”以及指向**BrightData**提供的浏览器的 Web 套接字 URL 来访问网站、变异 DOM 并遍历 DOM,同时应用自定义脚本从中抓取数据。

以下是处理上述问题的代码

const puppeteer = require('puppeteer-core'),
      path   = require('path'),
      fs     = require('fs'),
      both = require('../js/both'),
      file   = path.join(require('../utils/rootDir')(__dirname), './config.json'),
      config = fs.existsSync(file)&&require(file)||{...process.env};

module.exports = function(request, response) {
  let { data } = request.body, result;
  let { nodes, url } = data = JSON.parse(data),
  /**serialize the needed function in the imported object for usage in puppeteer */
      _both = { asText: both.asText.toString() };

  new Promise(async (res, rej) => {  
    puppeteer.connect({
      headless: false,
      browserWSEndpoint: config.BROWSER_WS,
    }).then(browser=>browser.newPage().then(async page=>{

      await page.setUserAgent('5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36');
      await page.goto(url, { waitUntil:'load', timeout:1e3*60 });
      // await page.waitForFunction(() => document.readyState === 'complete');
      // await page.screenshot({path: 'example.png'});


      const result = await page.evaluate((nodes, both) => {
        /** convert serialized function string back into a function to execute it */
        both.asText = new Function(`return ${both.asText}`)()
        /**remove needless nodes from the DOM */
        document.head.remove(), ['link', 'script', 'style', 'svg'].forEach(tag=>document.body.querySelectorAll(tag).forEach(el=>el.remove()))
        /**defined "node" - the variable present in the dynamic scripts locally to make it available in the 
          custom function context when created with new Function */
        let page = {}, node, fxns = Object.keys(nodes).map(key=>
          /**slip in the local variable - page and prepend a return keyword to make the function string work 
           * as expected when made into a function
          */
          nodes[key] = new Function(`return ${nodes[key].replace(',', ', page, ')}`)()
        );
        /** apply the functions for the nodes to retrieve data as the DOM is being traversed */
        both.asText(document.body, (_node, res)=>fxns.find(fxn=>res=fxn(node=_node, page)) && /*handle fetching media assets later here*/res || '');
        return page
      }, nodes, _both);
      res(result), await browser.close();
    }).catch(rej))
    .catch(rej)
  }).then(page=>result = page)
  .catch((err, str, name)=>{
    str = err.toString(), name = err.constructor.name, result = {
      error: /^\[/.test(str) ? `${name}: A sudden network disconnect occured` : str
    }
  }).finally( ()=> {
    response.json(result)
  })
}

网页解锁器

对于使用 Cloudflare Trunstile 防止抓取的顽固网站,我使用 BrightData 的代理 API 测试了一些代码,并且成功了!

将来,我将实施一种解决方法,即根据人们对这项服务的有用程度,将下载的顽固网站的 HTML 发送到客户端,然后通过脚本进行抓取。

合格提示

人工智能管道

我的意见主要集中在这个提示上,但它恰好提供了解决方案

感谢阅读

我为 BrightData 挑战赛构建了这个,但如果它有用的话,我会对其进行改进

DEV 挑战现已开始!

DEV Challenges Hub

查看所有参与方式、证明您的技能并赢取奖品。

访问挑战中心