使用 OpenAI API PHP SDK 保持對話流暢
P粉426906369
P粉426906369 2023-11-01 09:04:15
0
1
706

我正在嘗試使用 OpenAI PHP SDK 的completion() 方法來保持對話。

  • 提示#1:「你好嗎?」
  • 提示#2:「我之前問過你什麼?」

但是人工智慧似乎忘記了我之前問過的問題。它會隨機回答第二個提示。

我用於這兩個呼叫的程式碼如下:

$call1 = $open_ai->completion([
            'model' => 'text-davinci-003', 
            'prompt' => 'How Are You?',

        ]);


        $call2 = $open_ai->completion([
            'model' => 'text-davinci-003', 
            'prompt' => 'What i asked you before?',
        ]);

我錯過了什麼?我怎麼能在這兩個呼叫之間保持會話處於活動狀態,以便讓人工智慧記住我之前問過的內容?

P粉426906369
P粉426906369

全部回覆(1)
P粉316890884

第二個答案,因為第一個答案沒有回答OP的問題。


基於此 OpenAI Playground 範例,只能透過將兩個命令傳送到 API 來「詢問」「對話」。

不要認為在收到回覆後有辦法讓對話繼續進行。


考慮這個例子,我們發送以下文字:

The following is a conversation with an AI assistant.

Human: Hello
Human: What is 3 * 3?
AI:
Human: What did I just asked?
AI:

我得到的回覆是:

You asked me what 3 * 3 is. The answer is 9.

用於此目的的程式碼:

completion([
        'model' => $model,
        'prompt' => $question,
        'temperature' => 0.9,
        'max_tokens' => 150,
        'frequency_penalty' => 0,
        'presence_penalty' => 0.6,
        'stop' => ["\nHuman:", "\nAI:"]
    ]);
    try {
        $json = @json_decode($res);
        foreach ($json->choices as $choice) {
            echo $choice->text . PHP_EOL;
        }
    } catch (Exception $e) {
        var_dump($e);
        return NULL;
    }
}

$text = 

注意 stop 數組,該數組引用自 文檔

這似乎讓人工智慧知道在哪裡「讀」和在哪裡「寫」

如果您從請求中刪除該參數,它將返回而不返回答案:

You asked what 3 times 3 is.
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!