forked from pollinations/pollinations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelDonationWrapper.js
More file actions
115 lines (96 loc) · 3.77 KB
/
modelDonationWrapper.js
File metadata and controls
115 lines (96 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* modelDonationWrapper.js
*
* This module provides a wrapper for model handlers to catch errors and return
* a donation message when models run out of credits.
*/
import debug from "debug";
const log = debug("pollinations:donation-wrapper");
const errorLog = debug("pollinations:donation-wrapper:error");
/**
* Creates a wrapped version of a model handler that catches errors
* and returns a donation message.
*
* @param {Function} modelHandler - The original model handler function
* @param {string} modelName - The name of the model being wrapped
* @param {Object} options - Options for the wrapper
* @returns {Function} A wrapped handler function
*/
export function wrapModelWithDonationMessage(
modelHandler,
modelName,
options = {},
) {
// Donation message configuration
const donationConfig = {
threshold: options.threshold || 50,
currentDonations: options.currentDonations || 47,
kofiLink: options.kofiLink || "https://ko-fi.com/pollinationsai",
...options,
};
// Return the wrapped handler function
return async function wrappedHandler(messages, handlerOptions = {}) {
try {
// Attempt to run the original model handler
const result = await modelHandler(messages, handlerOptions);
// console.log("rrresult", result)
if (result?.error) throw result.error;
return result;
} catch (error) {
// Log the original error
errorLog(`Error in ${modelName} model:`, error);
// Create a user-friendly message about the error
const errorMessage = {
id: `donation-${Date.now()}`,
object: "chat.completion",
created: Math.floor(Date.now() / 1000),
model: modelName,
choices: [
{
index: 0,
message: {
role: "assistant",
content: formatDonationMessage(
modelName,
donationConfig,
),
},
finish_reason: "stop",
},
],
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0,
},
donation_request: true,
};
log(`Returning donation message for ${modelName}`);
return errorMessage;
}
};
}
/**
* Formats a donation message for the specified model
*
* @param {string} modelName - The model that needs donations
* @param {Object} config - Donation configuration
* @returns {string} Formatted donation message
*/
function formatDonationMessage(modelName, config) {
const remainingNeeded = config.threshold - config.currentDonations;
return `
## Claude is powered by Pollinations.ai
We're currently experiencing high demand for ${modelName}. Our AI models rely on external APIs that require credits.
### Current Status
We're aiming to reach **$${config.threshold}** to keep ${modelName} available for everyone.
**$${config.currentDonations}** has been donated so far - we're **$${remainingNeeded}** away from our goal.
If you'd like to support this service, a contribution of any size would be appreciated.
### Our Commitment
When we reach our goal, ${modelName} will be available for a full week for all users.
Donate at: ${config.kofiLink}
**Note:** The other models are still available if you'd like to continue using Pollinations.ai without donating.
Thank you for your understanding and support in keeping Pollinations.ai accessible! 🙏
`;
}
export default wrapModelWithDonationMessage;