PHP code example of hiddenhatpress / openai-assistants
1. Go to this page and download the library: Download hiddenhatpress/openai-assistants library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
hiddenhatpress / openai-assistants example snippets
use hiddenhatpress\openai\assistants\Assistants;
use hiddenhatpress\openai\assistants\AsstComms;
$token = getenv('OPENAI_API_KEY');
// this model needed for retrieval tools
$model = "gpt-4-1106-preview";
$asstcomms = new AsstComms($model, $token);
$assistants = new Assistants($asstcomms);
// will get 20 by default
$entities = $asstservice->list();
$assistantid = null;
$name = "pliny-assistant";
foreach ($entities['data'] as $asst) {
if ($asst['name'] == $name) {
$assistantid = $asst['id'];
}
}
if (empty($assistantid)) {
// create the assistant
$asstresp = $asstservice->create(
$name,
"You are an ancient history assistant specialising in Pliny the Younger",
["retrieval"]
);
$assistantid = $asstresp['id'];
// upload file
$fileresp = $fileservice->createAndAssignAssistantFile($assistantid, "pliny.txt" );
}
// create a thread
$threadresp = $threadservice->create();
$threadid = $threadresp['id'];
// create a message and add to the thread
$content = "Discuss the ways that Pliny talks about fish in his letters.";
$msgresp = $messageservice->create($threadid, $content);
// get the files from the assistant
$files = $fileservice->listAssistantFiles($assistantid);
foreach ($files['data'] as $finfo) {
// unassign and delete
$fileservice->unassignAndDeleteAssistantFile($assistantid, $finfo['id']);
}
// delete the assistant
$del = $asstservice->del($assistantid);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.