From 49cce2234d230c5ad8c3fce3b929261a843d7858 Mon Sep 17 00:00:00 2001 From: uukelele-scratch Date: Sun, 20 Apr 2025 15:43:27 +0100 Subject: [PATCH] Added pollinations text support. --- src/models/pollinations.js | 47 ++++++++++++++++++++++++++++++++++++++ src/models/prompter.js | 5 ++++ 2 files changed, 52 insertions(+) create mode 100644 src/models/pollinations.js diff --git a/src/models/pollinations.js b/src/models/pollinations.js new file mode 100644 index 0000000..b21c8e7 --- /dev/null +++ b/src/models/pollinations.js @@ -0,0 +1,47 @@ +import { strictFormat } from "../utils/text.js"; + +export class Pollinations { + // models: https://text.pollinations.ai/models + constructor(model_name, url, params) { + this.model_name = model_name; + this.params = params; + this.url = url || "https://text.pollinations.ai/openai"; + } + + async sendRequest(turns, systemMessage) { + let messages = [{'role': 'system', 'content': systemMessage}].concat(turns); + + const payload = { + model: this.model_name || "openai-large", + messages: strictFormat(messages), + seed: Math.floor( Math.random() * (99999) ), + referrer: "mindcraft", + ...(this.params || {}) + }; + + let res = null; + + try { + console.log(`Awaiting pollinations response from model`, this.model_name); + const response = await fetch(this.url, { + method: "POST", + headers: { + "Content-Type": "application/json" + }, + body: JSON.stringify(payload) + }); + if (!response.ok) { + console.error(`Failed to receive response. Status`, response.status, response.text); + res = "My brain disconnected, try again."; + } else { + const result = await response.json(); + res = result.choices[0].message.content; + } + } catch (err) { + console.error(`Failed to receive response.`, err || err.message); + res = "My brain disconnected, try again."; + } + return res; + } +} + diff --git a/src/models/prompter.js b/src/models/prompter.js index 46f7760..445e3ba 100644 --- a/src/models/prompter.js +++ b/src/models/prompter.js @@ -21,6 +21,7 @@ import { DeepSeek } from './deepseek.js'; import { Hyperbolic } from './hyperbolic.js'; import { GLHF } from './glhf.js'; import { OpenRouter } from './openrouter.js'; +import { Pollinations } from './pollinations.js'; export class Prompter { constructor(agent, fp) { @@ -133,6 +134,8 @@ export class Prompter { profile.api = 'openrouter'; // must do first because shares names with other models else if (profile.model.includes('ollama/')) profile.api = 'ollama'; // also must do early because shares names with other models + else if (profile.model.includes('pollinations/')) + profile.api = 'pollinations'; // also shares some model names like llama else if (profile.model.includes('gemini')) profile.api = 'google'; else if (profile.model.includes('gpt') || profile.model.includes('o1')|| profile.model.includes('o3')) @@ -198,6 +201,8 @@ export class Prompter { model = new DeepSeek(profile.model, profile.url, profile.params); else if (profile.api === 'openrouter') model = new OpenRouter(profile.model.replace('openrouter/', ''), profile.url, profile.params); + else if (profile.api === 'pollinations') + model = new Pollinations(profile.model.replace('pollinations/', ''), profile.url, profile.params); else throw new Error('Unknown API:', profile.api); return model;