├── LICENSE ├── README.md ├── index.js └── package.json /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Parking Master 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Perplexity.AI Unofficial Node API 2 | > An unofficial API for Node.js based off of the [Perplexity.AI](https://perplexity.ai) website and mobile app 3 | 4 | ## Intro 5 | This project is based off of an idea to make unofficial wrappers for AI sites that don't have an official API yet, and this idea is from the [`node_characterai`]() package. 6 | 7 | __This project is not affiliated with Perplexity.AI in any way! It is a community project.__ The purpose of this project is to bring and build projects powered by Perplexity.AI. 8 | 9 | If you like this project, please check their [website](https://perplexity.ai). 10 | 11 | ## Features 12 | - Fully written in Javascript and CommonJS 13 | - Asynchronous requests 14 | - Easy to use 15 | - Active development 16 | - Send messages to Perplexity.AI 17 | - Make the AI forget your message 18 | - Customize your answers with categories 19 | - Ask it basically anything 20 | 21 | ## Installation 22 | ```bash 23 | $ npm install node_perplexityai 24 | ``` 25 | 26 | And if you want to save it to your env: 27 | ```bash 28 | $ npm install node_perplexityai --save 29 | ``` 30 | 31 | ## Usage 32 | 33 | Here is a basic script that asks the AI a question: 34 | ```javascript 35 | const perplexity = require("node_perplexityai"); 36 | 37 | (async () => { 38 | // Ask the AI a question 39 | const answer = await perplexity.send("Can dogs eat chocolate?"); 40 | console.log(answer); 41 | })(); 42 | ``` 43 | 44 | All of the functions are listed here: 45 | ```javascript 46 | const perplexity = require("node_perplexityai"); 47 | 48 | (async () => { 49 | // Ask the AI a question 50 | const answer1 = await perplexity.send("Who was the first person to step foot on The Moon?"); 51 | 52 | console.log(answer1); // This logs the answer to your first question 53 | 54 | // List the available categories for asking questions (all, youtube, etc.) 55 | console.log(perplexity.categories); 56 | 57 | // Change the category of your next message 58 | await perplexity.category("wikipedia"); 59 | 60 | // Ask the AI another question with the category "wikipedia" 61 | const answer2 = await perplexity.send("How tall is the One World Trade Center?"); 62 | 63 | // Forgets your previous messages and resets the API 64 | await perplexity.forget(); 65 | })(); 66 | ``` 67 | 68 | ## FAQ 69 | | Question | Answer | 70 | | --- | --- | 71 | | Do I need an account? | Nope! You can ask unlimited questions to the AI for free. Account login features may be added in the future for better access to Perplexity. | 72 | | Error: No category found called "..." | You haven't selected the right category or have a typo. List the categories via `perplexity.categories`. | 73 | | Is this an official API? | No, this is a __community-based__ project for building amazing projects using Perplexity.AI. | 74 | | Are there any limits to using this? | No, you can use this package for anything without any limits or signing up. Just make sure you don't violate the [Perplexity.AI ToS](https://www.perplexity.ai/tos). | 75 | | What does this package depend on? | The only dependency required is Puppeteer. It uses puppeteer to fetch the answer from the question sent to Perplexity.AI. It is installed automatically when installing this package. | 76 | | Other issue? | Report it in the [Issues](https://github.com/Parking-Master/node_perplexityai/issues) tab | 77 | 78 | ### About Puppeteer 79 | Any errors puppeteer related in this package can be reported in the Issues section, but please make note that if it is a full-on puppeteer related issue, I will close it. 80 | 81 | __TL;DR__ if the issue is puppeteer related, please post it in the Issues section but I may close it if it's not related to `node_perplexityai` 82 | 83 | ### Support and Contribution 84 | This project is updated frequently, always check for the latest version for new features or bug fixes. 85 | 86 | If you have an issue or idea, let me know in the Issues tab. If you use this API, you also bound to the terms of usage of the Perplexity.AI website. 87 | 88 | # License 89 | MIT 90 | ###### © 2021-2023 Parking Master 91 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const puppeteer = require("puppeteer"); 2 | 3 | const perplexity = { 4 | isInitialized: false, 5 | page: null, 6 | categories: [ 7 | "all", 8 | "academic", 9 | "writing", 10 | "wolfram", 11 | "youtube", 12 | "reddit", 13 | "wikipedia" 14 | ], 15 | currentCategory: "all", 16 | categoryChange: false, 17 | send: async (message) => { 18 | return new Promise(function(resolve, reject) { 19 | async function sendMessage(i) { 20 | if (!perplexity.categoryChange) { 21 | await perplexity.page.goto("https://www.perplexity.ai/"); 22 | } else perplexity.categoryChange = false; 23 | await perplexity.page.waitForSelector("textarea"); 24 | await type("textarea", i.toString().trim()); 25 | await click("button.bg-super.text-sm"); 26 | let wait = setInterval(async () => { 27 | let content = await perplexity.page.content(); 28 | if (content.includes("Answer")) { 29 | let response = await perplexity.page.evaluate(`Object.values(document.querySelector("div.break-words.min-w-0").children[0].children[0].children).map(x => x).filter(x => x.className == "" && x.textContent.length > 5).map(x => x.textContent).join(" ")`); 30 | resolve(response.trim()); 31 | clearInterval(wait); 32 | } 33 | }, 1000); 34 | } 35 | if (!perplexity.isInitialized) { 36 | let wait = setInterval(async () => { 37 | if (perplexity.isInitialized) { 38 | sendMessage(message); 39 | clearInterval(wait); 40 | } 41 | }) 42 | } else sendMessage(message); 43 | }); 44 | }, 45 | forget: async function() { 46 | perplexity.isInitialized = false; 47 | await perplexity.page.evaluate(` 48 | (() => { 49 | const cookies = document.cookie.split(";"); 50 | for (let i = 0; i < cookies.length; i++) { 51 | const cookie = cookies[i]; 52 | const eqPos = cookie.indexOf("="); 53 | const name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie; 54 | document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT"; 55 | } 56 | })(); 57 | localStorage.clear(); 58 | sessionStorage.clear(); 59 | `); 60 | await perplexity.page.deleteCookie(); 61 | await perplexity.page.goto("https://www.perplexity.ai/"); 62 | perplexity.isInitialized = true; 63 | }, 64 | category: async function(type) { 65 | perplexity.categoryChange = true; 66 | perplexity.isInitialized = false; 67 | type = type.toString().toLowerCase().trim(); 68 | await perplexity.page.goto("https://www.perplexity.ai/"); 69 | if (perplexity.categories.indexOf(type) > -1) { 70 | await perplexity.page.evaluate(`Object.values(document.querySelectorAll("button")).filter(x => x.textContent.includes("Focus"))[0].click(), setTimeout(() => Object.values(document.querySelectorAll(".mt-one ml-two light font-sans text-xs font-medium text-textOff".replace(/ /gi, "."))).map(x => x.parentElement).filter(x => x.textContent.toLowerCase().includes("${type}"))[0].click(), 500)`); 71 | setTimeout(() => perplexity.isInitialized = true, 500); 72 | } else throw new Error(`Perplexity.AI: No category found called "${type}"`); 73 | } 74 | }; 75 | 76 | async function type(element, text) { 77 | await perplexity.page.type(element, text); 78 | } 79 | async function press(element, key) { 80 | await (await perplexity.page.$(element)).press(key); 81 | } 82 | async function click(element) { 83 | await perplexity.page.waitForSelector(element); 84 | await perplexity.page.click(element); 85 | } 86 | 87 | (async () => { 88 | const browser = await puppeteer.launch({ 89 | headless: "new" 90 | }); 91 | const page = await browser.newPage(); 92 | perplexity.page = page; 93 | await page.goto("https://perplexity.ai/"); 94 | 95 | perplexity.isInitialized = true; 96 | })(); 97 | 98 | module.exports = perplexity; 99 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "node_perplexityai", 3 | "version": "1.0.4", 4 | "description": "An unofficial API for Node.js based off of the Perplexity.AI website and mobile app", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/Parking-Master/node_perplexityai.git" 12 | }, 13 | "keywords": [ 14 | "ai", 15 | "nodejs", 16 | "perplexityai", 17 | "puppeteer" 18 | ], 19 | "author": "Parking Master (https://parkingmaster.w3spaces.com/)", 20 | "license": "MIT", 21 | "bugs": { 22 | "url": "https://github.com/Parking-Master/node_perplexityai/issues" 23 | }, 24 | "homepage": "https://github.com/Parking-Master/node_perplexityai#readme" 25 | } 26 | --------------------------------------------------------------------------------