Added pollinations text support.

This commit is contained in:
uukelele-scratch 2025-04-20 15:43:27 +01:00
parent ca25142959
commit 49cce2234d
2 changed files with 52 additions and 0 deletions

View file

@ -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;
}
}

View file

@ -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;