MCPcopy Create free account
hub / github.com/pollinations/pollinations / postChatCompletion

Function postChatCompletion

packages/mcp/src/utils/coreUtils.js:183–210  ·  view source on GitHub ↗
(body)

Source from the content-addressed store, hash-verified

181 * @returns {Promise<Response>} - Raw fetch response (already checked for !ok)
182 */
183export async function postChatCompletion(body) {
184 const cleanedBody = {};
185 for (const [key, value] of Object.entries(body)) {
186 if (value !== undefined && value !== null) {
187 cleanedBody[key] = value;
188 }
189 }
190
191 const response = await fetchWithAuth(
192 `${API_BASE_URL}/v1/chat/completions`,
193 {
194 method: "POST",
195 headers: { "Content-Type": "application/json" },
196 body: JSON.stringify(cleanedBody),
197 timeoutMs: 30000,
198 },
199 );
200
201 if (!response.ok) {
202 const errorText = await response.text().catch(() => "Unknown error");
203 if (response.status === 429) {
204 throw new Error("Rate limited. Please wait before retrying.");
205 }
206 throw new Error(parseApiError(response.status, errorText));
207 }
208
209 return response;
210}
211
212/**
213 * @param {string} url - URL to fetch

Callers 2

generateTextFunction · 0.90
chatCompletionFunction · 0.90

Calls 4

fetchWithAuthFunction · 0.85
stringifyMethod · 0.80
parseApiErrorFunction · 0.70
textMethod · 0.65

Tested by

no test coverage detected