GPT-4o with token streaming. Works with any OpenAI-compatible API (OpenAI, Mistral, Anthropic, local proxies). Your API key stays in your browser.
const chat = new quikchat('#chat', handleAIRequest);
async function handleAIRequest(chatInstance, userInput) {
chatInstance.messageAddNew(userInput, 'user', 'right');
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + apiKey
},
body: JSON.stringify({
model: 'gpt-4o-mini',
messages: [
{ role: 'system', content: systemPrompt },
...chatInstance.historyGet() // full conversation context
],
stream: true
})
});
const reader = response.body.getReader();
let start = true, id;
// Read SSE stream token-by-token
while (true) {
const { value, done } = await reader.read();
if (done) break;
// parse chunks, extract content, append to message
if (start) {
id = chatInstance.messageAddNew(content, 'bot', 'left');
start = false;
} else {
chatInstance.messageAppendContent(id, content);
}
}
}