skip to Main Content

I’m trying to keep a conversation going using the completion() method with OpenAI PHP SDK.

  • Prompt #1: "How Are You?"
  • Prompt #2: "What I asked you before?"

but the AI seems to forget what i asked before. and it reply with random answers to the second prompt.

The code i’m using for the 2 calls are these:


   $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 am I missing? How can i keep the session alive between these two calls in order to make the AI remember what I asked before?

2

Answers


  1. Second answer, as the first one did not answer OP’s question.


    Based on this OpenAI Playground Example, a ‘conversation’ can only be ‘asked’ by sending both the command to the API.

    Don’t think there is a way of keep the conversation going after retreiving a response.


    Consider this example, were 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 response I get is:

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

    Code used for this:

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use OrhanerdayOpenAiOpenAi;
    
    $open_ai_key = getenv('OPENAI_API_KEY');
    $open_ai = new OpenAi($open_ai_key);
    
    function ask($ai, $question, $model = 'text-davinci-003') {
        $res = $ai->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 = <<<EOL
    The following is a conversation with an AI assistant.
    
    Human: Hello
    Human: What is 3 * 3?
    AI:
    Human: What did I just asked?
    AI:
    EOL;
    
    $res = ask($open_ai, $text);
    

    Note the stop array that, quoted from the documentation:

    Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.

    This seems to let the AI know where to ‘read’ and where to ‘write’

    If you remove that param from the request, it returns without the answer:

    You asked what 3 times 3 is.
    
    Login or Signup to reply.
  2. The same problem is here 🙁 Kind of a dream to develop a relationship with AI to keep a conversation but apparently, every request triggers a new session, according to my experience you have to send a full text to AI (davinci for me now)to get what you want.

    openai.api_key = key
    
    response = openai.Completion.create(
      model="text-davinci-003",
      prompt="Human: Hello AI, how can we save our session to talk future? nAI:",
      temperature=0.9,
      max_tokens=150,
      top_p=1,
      frequency_penalty=0.0,
      presence_penalty=0.6,
      stop=[" Human:", " AI:"]
    )
    

    Rep:

    "text": " You can save the session by saving the conversation logs to a file or capturing screenshots of the conversation. Additionally, you can create an account with a chatbot service provider in order to save and continue conversations at any time."}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search