Skip to content

Commit

Permalink
Lecture 14 - Writing code to save and restore a conversation
Browse files Browse the repository at this point in the history
  • Loading branch information
nibblebits committed Dec 28, 2023
1 parent 3303e8f commit fa31480
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions chat.php
@@ -1,8 +1,10 @@
<?php
use Dragonzap\OpenAI\ChatGPT\APIConfiguration;
use Dragonzap\OpenAI\ChatGPT\Assistant;
use Dragonzap\OpenAI\ChatGPT\ConversationIdentificationData;
use Dragonzap\OpenAI\ChatGPT\Exceptions\ThreadRunResponseLastError;
use Dragonzap\OpenAI\ChatGPT\Exceptions\TimeoutException;
use Dragonzap\OpenAI\ChatGPT\RunState;
use Dragonzap\OpenAI\ChatGPT\UnknownAssistant;

require './vendor/autoload.php';
Expand All @@ -14,7 +16,7 @@ public function getAssistantId(): string
return 'asst_43dJhZ11hFgjynmsq9GxqUJV';
}

public function handleFunctionFillCalendar(array $arguments) : array
public function handleFunctionFillCalendar(array $arguments): array
{
$response = [
'success' => true,
Expand All @@ -39,10 +41,10 @@ public function handleFunction($function, array $arguments): array|string
'message' => 'No such function'
];

switch($function) {
switch ($function) {
case 'fill_calendar':
$response = $this->handleFunctionFillCalendar($arguments);
break;
break;
}

return $response;
Expand All @@ -52,26 +54,37 @@ public function handleFunction($function, array $arguments): array|string

public function saveConversationIdentificationData(Dragonzap\OpenAI\ChatGPT\ConversationIdentificationData $conversation_id_data): void
{

echo 'FUNCTION HIT' . "\n";
}

}

$betty = new BettyAssistant(new APIConfiguration('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'));
$conversation = $betty->newConversation();
$conversation = null;
$save_data_string = $argv[1];
$message = $argv[2];

while(true)
if ($save_data_string == 'na')
{
$input_line = fgets(STDIN);
$conversation->sendMessage($input_line);
try {
$conversation->blockUntilResponded(1);
} catch(TimeoutException $ex) {
echo "Timed out\n";
} catch (ThreadRunResponseLastError $ex) {
throw $ex;
}
$conversation = $betty->newConversation();
}
else
{
$conversation = $betty->loadConversation(ConversationIdentificationData::fromSaveData($save_data_string));
}

$run_state = $conversation->getRunState();
echo 'RunState: ' . $run_state->value . "\n";

if ($run_state == RunState::COMPLETED)
{
echo 'Betty: ' . $conversation->getResponseData()->getResponse() . "\n";

}
}

if ($message != 'na')
{
$conversation->sendMessage($message);
}

echo 'Saved data string: ' . $conversation->getIdentificationData()->getSaveDataString() . "\n";

0 comments on commit fa31480

Please sign in to comment.