Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // code to put in pipedream node for kindle gpt mod
- // 95% AI generated code
- import OpenAI from "openai"
- function extractArticle(evt) {
- const b = evt?.body
- if (!b) return ""
- if (typeof b === "object" && b.article != null) {
- return String(b.article)
- }
- if (typeof b === "string") {
- // Try JSON first
- try {
- const j = JSON.parse(b)
- if (j && j.article != null) return String(j.article)
- } catch {}
- // Try x-www-form-urlencoded
- const params = new URLSearchParams(b)
- if (params.has("article")) return params.get("article")
- return b
- }
- return ""
- }
- export default defineComponent({
- async run({ steps, $ }) {
- const evt = steps.trigger.event || {}
- const article = extractArticle(evt)
- const openai = new OpenAI({
- baseURL: "https://openrouter.ai/api/v1",
- apiKey: process.env.OPENROUTER_API_KEY, })
- const systemPrompt = `
- Depending on the input content provide a definition (if it is a word or phrase
- that may need one), translate to English (if the input content is in a foreign
- language) or provide further context and explanation as well as verify accuracy
- (for example for historic facts or scientific descriptions). Be straight to the
- point and concise. Always respond in English. Output HTML. Use tags such as <b>,
- <i>, <ul>, <li>, <p> as needed. Do not use header tags such as <h1>, <h2> etc.
- `.trim()
- const userPrompt = `
- I'm reading a book and encountered the following content "${article}".
- `.trim()
- try {
- const completion = await openai.chat.completions.create({
- model: "google/gemini-2.5-flash",
- models: ["openai/gpt-4.1-mini"],
- messages: [
- { role: "system", content: systemPrompt },
- { role: "user", content: userPrompt },
- ],
- })
- const msg =
- completion.choices?.[0]?.message?.content?.trim() || ""
- const usedModel = completion.model || "unknown"
- await $.respond({
- status: 200,
- headers: { "Content-Type": "application/json" },
- body: {
- status: 200,
- // Model returns HTML per the system prompt
- articleHtml: msg,
- articleUrl: "",
- licenseMessage: `Response generated via OpenRouter (model="${usedModel}")`,
- errorMessage: null,
- },
- })
- } catch (err) {
- await $.respond({
- status: 502,
- headers: { "Content-Type": "application/json" },
- body: {
- status: 502,
- articleHtml: "",
- articleUrl: "",
- licenseMessage: "",
- errorMessage:
- err?.message || "OpenRouter call failed",
- },
- })
- }
- },
- })
Advertisement
Add Comment
Please, Sign In to add comment