如何使用服務(wù)器發(fā)送事件 (SSE) 將數(shù)據(jù)從上述 API 流式傳輸?shù)绞褂?JavaScript 和 PHP 的瀏覽器客戶端?我已經(jīng)研究這個(gè)問題好幾個(gè)小時(shí)了,但我似乎無法弄清楚出了什么問題。作為參考,我嘗試在這里調(diào)整解決方案:Stream DATA From openai GPT-3 API using PHP
我的代碼的其余部分或多或少與上面問題中的代碼相同。我修改的唯一不起作用的部分是:
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($curl, $data) { # str_repeat(' ',1024*8) is needed to fill the buffer and will make streaming the data possible $data = json_decode($data, true); $text = $data['choices'][0]['text']; echo $text . str_repeat(' ', 1024 * 8); return strlen($data); });
首先,我嘗試僅返回“choices”數(shù)組中的“text”屬性(請(qǐng)參閱下面的示例 API 響應(yīng))。
這是我收到的回復(fù):
注意:嘗試訪問 C:FILE_PATHsse.php 中 null 類型值的數(shù)組偏移量。
其次,如何將“文本”實(shí)時(shí)傳輸?shù)娇蛻舳松系脑??這是我到目前為止的實(shí)現(xiàn)。
JavaScript
$.ajax({ type: "POST", url: "sse.php", data: JSON.stringify({ prompt: "What is the best way to", num_completions: 1, temperature: 0.5, }), contentType: "application/json", success: function (response) { const source = new EventSource("sse.php"); source.onmessage = function (event) { const div = document.getElementById("response"); div.innerHTML += event.data + "<br>"; console.log(event); }; }, });
API 流式傳輸?shù)臄?shù)據(jù)樣本塊如下所示。我試圖僅將“文本”部分流回瀏覽器。
data: {"id": "cmpl-XXXXXXXXXXXXXXXXXXXXXXX", "object": "text_completion", "created": 1671700494, "choices": [{"text": " Best", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"} data: {"id": "cmpl-XXXXXXXXXXXXXXXXXXXXXXX", "object": "text_completion", "created": 1671700494, "choices": [{"text": " way", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"} data: {"id": "cmpl-XXXXXXXXXXXXXXXXXXXXXXX", "object": "text_completion", "created": 1671700494, "choices": [{"text": " to", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"} data: [DONE]
我應(yīng)該如何實(shí)現(xiàn)這個(gè)?我已經(jīng)無計(jì)可施了。提前致謝。
我使用以下代碼找到了解決方法:
//Placed at the beginning of the script @ini_set('zlib.output_compression', 0); ob_implicit_flush(true); ob_end_flush(); header("Content-Type: text/event-stream"); header("Cache-Control: no-cache"); //Initialize cURL and set the necessary headers and request parameters ... ... curl_setopt($curl, CURLOPT_WRITEFUNCTION, function ($curl, $data) { echo $data; return strlen($data); }); $curl_response = curl_exec($curl); echo $curl_response;
然后我使用 JavaScript 來提取文本,如下所示:
source.onmessage = function (event) { const div = document.getElementById("response"); text = JSON.parse(event.data).choices[0].text; div.innerHTML += text; };