Код: Выделить всё
SmartQuest is an intelligent search assistant that helps users locate information quickly and effectively. It leverages advanced language understanding to interpret queries and deliver relevant, context-sensitive answers.
__PADDING__
__PADDING__
. The assistant is built to be intuitive and works seamlessly across multiple platforms, including desktop and mobile devices.
Key Features:
Intelligent Search: Uses AI-driven algorithms to provide accurate and meaningful results
__PADDING__
__PADDING__
.
2. Cross-Platform Access: Works on web browsers and mobile apps.
3. Instant Responses: Delivers answers in real-time.
4
__PADDING__
__PADDING__
. Customizable Options: Users can tailor search preferences, including depth and response format.
Frontend JavaScript (потоковой обработчик):
Код: Выделить всё
function readStream() {
return reader.read().then(({ done, value }) => {
if (done) {
console.log('Streaming complete!');
isStreaming = false;
$('#chatgpt-submit').removeClass('loading').prop('disabled', false);
$('.streaming-cursor').remove();
const finalFormatted = formatResponseText(accumulatedText);
responseElement.html(finalFormatted);
addCopyButton(responseElement);
return;
}
chunkCount++;
// Process new chunk and remove padding markers
let chunk = decoder.decode(value);
// Strip out padding sections
chunk = chunk.replace(/\n__PADDING__\n[\s]+\n__PADDING__\n/g, '');
console.log(`Chunk #${chunkCount} received:`, chunk);
accumulatedText += chunk;
// Update display with formatted text and streaming cursor
const formatted = formatResponseText(accumulatedText);
responseElement.html(formatted + '');
return readStream();
});
}
< /code>
Backend Php (реализация потоковой передачи): < /p>
private function stream_external_api($url, $data) {
// ... setup code ...
curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HTTPHEADER => [
'Content-Type: 'application/json',
'Content-Length: ' . strlen($post_data),
'Accept: text/plain',
'Cache-Control: no-cache'
],
CURLOPT_RETURNTRANSFER => false,
CURLOPT_HEADER => false,
CURLOPT_TIMEOUT => 300,
CURLOPT_BUFFERSIZE => 128,
CURLOPT_WRITEFUNCTION => function($curl, $data) {
// Output the actual chunk
echo $data;
// Use a unique delimiter + padding that client can strip
echo "\n__PADDING__\n" . str_repeat(' ', 2048) . "\n__PADDING__\n";
if (ob_get_level() > 0) {
ob_flush();
}
flush();
if (connection_aborted()) {
return 0;
}
return strlen($data);
}
]);
// ... execute request ...
}
Пладация добавляется сервером для поддержания потока соединения, но она не полностью отфильтрована на стороне клиента. Иногда заполнение появляется в середине предложений, разбивая ответ. Есть ли лучший подход для обработки этой серверной накладки?
Подробнее здесь: https://stackoverflow.com/questions/797 ... -in-answer
Мобильная версия