search

Home  >  Q&A  >  body text

Keep conversations flowing with the OpenAI API PHP SDK

I'm trying to use the OpenAI PHP SDK's completion() method to maintain a conversation.

But the artificial intelligence seems to have forgotten the question I asked before. It will randomly answer the second prompt.

The code I used for these two calls is as follows:

$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?',
        ]);

What did I miss? How can I keep the session active between these two calls so that the AI ​​remembers what I asked before?

P粉426906369P粉426906369395 days ago798

reply all(1)I'll reply

  • P粉316890884

    P粉3168908842023-11-02 00:08:31

    The second answer, because the first answer does not answer the OP's question.


    Based on this OpenAI Playground example, a "conversation" can only be "asked" by sending two commands to the API.

    Don’t think there’s a way to keep the conversation going after receiving a reply.


    Consider this example, we send the following text:

    The following is a conversation with an AI assistant.
    
    Human: Hello
    Human: What is 3 * 3?
    AI:
    Human: What did I just asked?
    AI:
    

    The reply I got was:

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

    Code for this purpose:

    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 = <<
    

    Note stop array, which is quoted from Documentation:

    This seems to let the AI ​​know where to "read" and where to "write"

    If you remove this parameter from the request, it will return without returning the answer:

    You asked what 3 times 3 is.
    

    reply
    0
  • Cancelreply