Guest User

kindle_gpt

a guest
Sep 11th, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 2.70 KB | Source Code | 0 0
  1. // code to put in pipedream node for kindle gpt mod
  2. // 95% AI generated code
  3. import OpenAI from "openai"
  4.  
  5. function extractArticle(evt) {
  6.   const b = evt?.body
  7.   if (!b) return ""
  8.  
  9.   if (typeof b === "object" && b.article != null) {
  10.     return String(b.article)
  11.   }
  12.  
  13.   if (typeof b === "string") {
  14.     // Try JSON first
  15.     try {
  16.       const j = JSON.parse(b)
  17.       if (j && j.article != null) return String(j.article)
  18.     } catch {}
  19.  
  20.     // Try x-www-form-urlencoded
  21.     const params = new URLSearchParams(b)
  22.     if (params.has("article")) return params.get("article")
  23.  
  24.     return b
  25.   }
  26.  
  27.   return ""
  28. }
  29.  
  30. export default defineComponent({
  31.   async run({ steps, $ }) {
  32.     const evt = steps.trigger.event || {}
  33.     const article = extractArticle(evt)
  34.  
  35.     const openai = new OpenAI({
  36.       baseURL: "https://openrouter.ai/api/v1",
  37.       apiKey: process.env.OPENROUTER_API_KEY,    })
  38.  
  39.     const systemPrompt = `
  40. Depending on the input content provide a definition (if it is a word or phrase
  41. that may need one), translate to English (if the input content is in a foreign
  42. language) or provide further context and explanation as well as verify accuracy
  43. (for example for historic facts or scientific descriptions). Be straight to the
  44. point and concise. Always respond in English. Output HTML. Use tags such as <b>,
  45. <i>, <ul>, <li>, <p> as needed. Do not use header tags such as <h1>, <h2> etc.
  46. `.trim()
  47.  
  48.     const userPrompt = `
  49. I'm reading a book and encountered the following content "${article}".
  50. `.trim()
  51.  
  52.    try {
  53.      const completion = await openai.chat.completions.create({
  54.        model: "google/gemini-2.5-flash",
  55.        models: ["openai/gpt-4.1-mini"],
  56.        messages: [
  57.          { role: "system", content: systemPrompt },
  58.          { role: "user", content: userPrompt },
  59.        ],
  60.      })
  61.  
  62.      const msg =
  63.        completion.choices?.[0]?.message?.content?.trim() || ""
  64.      const usedModel = completion.model || "unknown"
  65.  
  66.      await $.respond({
  67.        status: 200,
  68.        headers: { "Content-Type": "application/json" },
  69.        body: {
  70.          status: 200,
  71.          // Model returns HTML per the system prompt
  72.          articleHtml: msg,
  73.          articleUrl: "",
  74.          licenseMessage: `Response generated via OpenRouter (model="${usedModel}")`,
  75.          errorMessage: null,
  76.        },
  77.      })
  78.    } catch (err) {
  79.      await $.respond({
  80.        status: 502,
  81.        headers: { "Content-Type": "application/json" },
  82.        body: {
  83.          status: 502,
  84.          articleHtml: "",
  85.          articleUrl: "",
  86.          licenseMessage: "",
  87.          errorMessage:
  88.            err?.message || "OpenRouter call failed",
  89.        },
  90.      })
  91.    }
  92.  },
  93. })
Advertisement
Add Comment
Please, Sign In to add comment