Created
May 19, 2026 18:28
-
-
Save SaurusAraAra/e2d84f6cca1c5a51580fd2335a110b3d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const axios = require("axios") | |
| const crypto = require("crypto") | |
| const BASE = "https://www.perplexity.ai" | |
| const header_dasar = { | |
| "User-Agent": | |
| "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36", | |
| Accept: "text/event-stream", | |
| "Accept-Language": "id-ID,id;q=0.9,en-US;q=0.8,en;q=0.7", | |
| "Content-Type": "application/json", | |
| Origin: BASE, | |
| Referer: BASE + "/", | |
| } | |
| function buat_cookie() { | |
| const cookies = { | |
| "pplx.visitor-id": crypto.randomUUID(), | |
| "pplx.session-id": crypto.randomUUID(), | |
| "pplx.edge-vid": crypto.randomUUID(), | |
| "pplx.edge-sid": crypto.randomUUID(), | |
| } | |
| return Object.entries(cookies) | |
| .map(([k, v]) => `${k}=${v}`) | |
| .join("; ") | |
| } | |
| function buat_payload(query) { | |
| return { | |
| params: { | |
| last_backend_uuid: crypto.randomUUID(), | |
| read_write_token: crypto.randomUUID(), | |
| attachments: [], | |
| language: "id-ID", | |
| timezone: "Asia/Jakarta", | |
| search_focus: "internet", | |
| sources: ["web"], | |
| frontend_uuid: crypto.randomUUID(), | |
| mode: "copilot", | |
| model_preference: "turbo", | |
| query_source: "followup", | |
| version: "2.18", | |
| }, | |
| query_str: query, | |
| } | |
| } | |
| function parse_sse(raw) { | |
| const lines = raw.split("\n") | |
| for (let i = lines.length - 1; i >= 0; i--) { | |
| if (!lines[i].startsWith("data: ")) continue | |
| try { | |
| const d = JSON.parse(lines[i].slice(6)) | |
| if (d.text && d.final) { | |
| const parsed = JSON.parse(d.text) | |
| for (const item of parsed) { | |
| if (item.step_type === "FINAL" && item.content?.answer) { | |
| return JSON.parse(item.content.answer).answer | |
| .replace(/\[\d+\]/g, "") | |
| .trim() | |
| } | |
| } | |
| } | |
| } catch {} | |
| } | |
| return null | |
| } | |
| async function perplexity(query) { | |
| const { data } = await axios.post( | |
| `${BASE}/rest/sse/perplexity_ask`, | |
| JSON.stringify(buat_payload(query)), | |
| { | |
| headers: { | |
| ...header_dasar, | |
| Cookie: buat_cookie(), | |
| "x-request-id": crypto.randomUUID(), | |
| }, | |
| timeout: 30000, | |
| responseType: "text", | |
| } | |
| ) | |
| const hasil = parse_sse(data) | |
| if (!hasil) throw new Error("gagal dapet jawaban") | |
| return hasil | |
| } | |
| ;(async () => { | |
| const query = "apa kabar kamu?" | |
| try { | |
| const result = await perplexity(query) | |
| console.log({ | |
| status: true, | |
| query, | |
| answer: result, | |
| }) | |
| } catch (e) { | |
| console.log({ | |
| status: false, | |
| message: e.message, | |
| }) | |
| } | |
| })() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment