A sample Javascript code snippet can be found below:
/**
* Sends the current message to the chat service, then processes and displays the results.
* Streamed responses provide a more interactive user experience by showing message replies as they arrive.
*/
const bearerToken = "<your auth token>"; // Replace with your actual bearer token.
const hostName = "<the chat api host name>"; // Replace with the chat API's host name.
const useStreaming = true; // Set to false for a complete response instead of a streamed one.
// Construct the URL for the chat API endpoint.
// If streaming is disabled, append '?stream=false' to the URL.
const hostUrl = `${hostName}/api/v1/chat${useStreaming ? '' : '/?stream=false'}`;
// The session ID maintains the context of the conversation. It starts as null and is set when the server responds.
let sessionId = null;
/**
* Sends a message to the chat API and handles the response.
* @param {string} message - The message to send to the chat service.
*/
function sendMessage(message) {
// Configuration for the POST request to the chat service.
const options = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${bearerToken}`
},
body: JSON.stringify({ messages: [{ content: message }], sessionId })
};
// Make the POST request to the chat service.
fetch(hostUrl, options)
.then(async response => {
// Check for non-200 OK responses and throw an error if found.
if (!response.ok) {
const errorData = await response.json();
throw new Error(`${errorData.message || 'Bad request error'} Status: ${response.status}`);
}
// Determine if the response is streamed based on the 'content-type'.
const isChunkedStream = response.headers.get('content-type') === 'application/x-ndjson';
const reader = response.body.getReader();
const decoder = new TextDecoder('utf-8');
// Buffer to hold incomplete data fragments from the stream.
let buffer = '';
// Process one chunk of data from the reader at a time.
async function processChunk() {
while (true) {
const { done, value } = await reader.read();
if (done) {
console.log('Stream completed.');
break;
}
// Decode the chunk and accumulate it in the buffer.
buffer += decoder.decode(value, { stream: isChunkedStream });
// Process complete JSON objects from the buffer.
let position;
while ((position = buffer.indexOf('\n')) !== -1 || !isChunkedStream) {
const line = isChunkedStream ? buffer.substring(0, position) : buffer;
buffer = buffer.substring(position + 1);
if (line.trim()) {
try {
// Parse the JSON from the line.
const json = JSON.parse(line);
// Extract the message content.
const content = isChunkedStream
? json.message_fragment && json.message_fragment.content
: json.message.content;
// Display the message content if it's not null.
if (content) {
// Here you could update the UI with the chat response.
console.log('Chat Response:', content);
};
// Update the session ID for continuity in conversation.
if (json.session_id != null) {
sessionId = json.session_id;
}
// Break the loop if not streaming since there will only be one response.
if (!isChunkedStream) {
break;
}
} catch (error) {
throw new Error(`Failed to parse JSON: ${error}\n${line}`);
}
}
}
}
}
// Begin processing data chunks from the response.
await processChunk();
})
.catch(err => {
// Handle any errors that occurred during the fetch or processing.
console.error('Chat service error:', err);
});
}