Maintaining context in consecutive API calls with OpenAI PHP SDK

I’m working on a project using the OpenAI PHP SDK and I’m having trouble keeping the conversation context between API calls. Here’s what I’ve tried:

$firstCall = $aiClient->generateResponse([
    'engine' => 'text-davinci-003',
    'prompt' => 'How's the weather today?'
]);

$secondCall = $aiClient->generateResponse([
    'engine' => 'text-davinci-003',
    'prompt' => 'What was my previous question?'
]);

The AI doesn’t seem to remember my first question when I make the second call. It just gives random answers.

I’m pretty sure I’m missing something important here. How can I make the AI remember what I asked before? Is there a way to keep the conversation going between these separate API calls?

Any help would be awesome! I’m kind of stuck and not sure what to do next.

I’ve dealt with this exact problem before. The key is to use the Chat Completion API and maintain a conversation array. Here’s what worked for me:

Store each interaction (both user and AI responses) in an array. Then, pass this entire array to each subsequent API call. This way, the AI has the full context of the conversation.

Something like this:

$conversation = [];
$conversation[] = ['role' => 'user', 'content' => 'How's the weather today?'];

$response = $aiClient->chat([
    'model' => 'gpt-3.5-turbo',
    'messages' => $conversation
]);

$conversation[] = ['role' => 'assistant', 'content' => $response->choices[0]->message->content];

// Next question
$conversation[] = ['role' => 'user', 'content' => 'What was my previous question?'];

$response = $aiClient->chat([
    'model' => 'gpt-3.5-turbo',
    'messages' => $conversation
]);

This approach has consistently worked for me in maintaining context across multiple API calls. Give it a shot and let me know if you need any clarification!

The issue you’re encountering is due to the stateless nature of API calls. Each request is treated independently, so the AI doesn’t inherently retain context from previous interactions. To maintain conversation history, you need to implement a system to store and pass previous messages.

One approach is to use the Chat Completion API instead of the Completion API. With this method, you can send an array of messages, including both the user’s inputs and the AI’s responses. This allows you to build up a conversation over multiple API calls.

Here’s a basic implementation:

$conversation = [];
$conversation[] = ['role' => 'user', 'content' => 'How's the weather today?'];

$response = $aiClient->chat([
    'model' => 'gpt-3.5-turbo',
    'messages' => $conversation
]);

$conversation[] = ['role' => 'assistant', 'content' => $response->choices[0]->message->content];

// For the next question
$conversation[] = ['role' => 'user', 'content' => 'What was my previous question?'];

$response = $aiClient->chat([
    'model' => 'gpt-3.5-turbo',
    'messages' => $conversation
]);

This method should solve your context retention problem.

hey, i had this issue. use ‘messages’ param instead of ‘prompt’. pass previous msgs in an array. ex:

$messages=[
  ['role'=>'user','content'=>'How's the weather?'],
  // add prior conversation here
];
$secondCall=$aiClient->chat(['model'=>'gpt-3.5-turbo','messages'=>$messages]);

should fix it!