const llmResponse = await openai.chat.completions.create({ ... });
// Run ChatAds and your other post-processing in parallel
const [chatadsResult, formattedResponse] = await Promise.all([
fetch("https://api.getchatads.com/v1/chatads/messages", {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": "YOUR_API_KEY",
},
body: JSON.stringify({ message: llmResponse.choices[0].message.content }),
}).then(r => r.json()),
formatAndSanitize(llmResponse),
]);
// Insert affiliate link if available
if (chatadsResult.data?.offers?.length) {
const offer = chatadsResult.data.offers[0];
formattedResponse = formattedResponse.replace(
offer.link_text,
`<a href="${offer.url}">${offer.link_text}</a>`
);
}