Skip to content

Commit

Permalink
odevzdané
Browse files Browse the repository at this point in the history
  • Loading branch information
JosefPRajmon committed Mar 4, 2024
1 parent ebfb7af commit 38ca615
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 27 deletions.
21 changes: 9 additions & 12 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

?>
<?php
/*
session_start();
//pod jmenem chatbota
$chatBotID ="asst_qwNsFaZ9AQVNHOKNqC8wRDoz";
Expand All @@ -24,7 +23,6 @@
}

}
*/
?>

<!DOCTYPE html>
Expand All @@ -41,13 +39,11 @@
if (isset($_POST['message'])) {
if (strlen($_POST['message'])>1) {
$sendMessage = $_POST["message"];
//$test->AddInstruction($sendMessage,isset($_POST["table"]));
$test->AddInstruction($sendMessage,isset($_POST["table"]));

//$test->NewMessage($sendMessage);
//$a= $test->CheckReply();
//var_dump($a[0]->content[0]->text);
//var_dump($a);
$a=array(
$test->NewMessage($sendMessage);
$a= $test->CheckReply();
/*$a=array(
new Message("zprava jedna","user"),
new Message("zprava dva","assistent"),
new Message("zprava tři","user"),
Expand All @@ -58,14 +54,14 @@
new Message("zprava osm","assistent"),
new Message("zprava devet","user"),
new Message("zprava deset","assistent")
);
);*/
foreach (array_reverse($a) as $key => $value) {
$role = $value->role;
//$message =$value->content[0]->text->value;
//$message = $test->ReplaceInResponse($message,$role);
$message =$value->content[0]->text->value;
$message = $test->ReplaceInResponse($message,$role);
// $message=preg_replace('/\[(https:\/\/.*?)]\((https:\/\/.*?)\)/i', '<a href="\1">\2</a>', $message);

$message =$value->message;
//$message =$value->message;
echo("<p class=\"$role\"><strong> $role<br></strong> $message</p>");
}

Expand All @@ -86,6 +82,7 @@

<script src="chat.js"></script>
<script>
//automatická aktualizace (zatím není třeba)
/* var userId = 'x'; // Nahraďte 'x' ID uživatele, kterého chcete sledovat

setInterval(function() {
Expand Down
126 changes: 111 additions & 15 deletions openAiObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,38 +221,134 @@ function __construct($message,$user){

//uplaud a mazání souboru ŕozdeláno
$test = new FilesManagment($open_Api_Key);
//$test->DeleteFile("asst_qbRrJdnZw3W2z5uvKq9qww1W","file-Atm4doWzBG9IXk9QSEiA5TeN");
$test->FileUplaud();
//ve funkci FileUplaud je třeba upravit cestu k souboru $file = new CURLFile(__DIR__ . '/' . $fileName, 'application/octet-stream',$fileName);
//funkce se postará o smazaní puvodních souboru a nahraní +napojení nových
$test->DataFunctionCaller("asst_qwNsFaZ9AQVNHOKNqC8wRDoz","prostejov-novinky.jsonl");

class FilesManagment{
private $OPENAI_API_KEY;
public function __construct($id){
$this->OPENAI_API_KEY = $id;
}

function FileUplaud(){
$ch = curl_init();
$file = new CURLFile('prostejov-novinky.jsonl', 'application/octet-stream');
$data = array(
'purpose' => 'assistants',
'file' => $file
function DataFunctionCaller($assistID,$fileName){
//najití a smazání souboru se stejným jménem pokud není tak to chytně try catch
$arrayAllFiles =json_decode($this->GetFiles())->data;
foreach ($arrayAllFiles as $key => $value) {
if ($value->filename == $fileName) {
try {
$this->RemoveFilesFromAssist($assistID,$value->id);
} catch (Throwable $th) {
//throw $th;
}
try {
$this->DeleteFile($assistID,$value->id);
} catch (\Throwable $th) {
//throw $th;
}
}
}

$this->FileUplaud($assistID,$fileName);

}

function GetFiles(){
$url = "https://api.openai.com/v1/files";

$headers = array(
"Authorization: Bearer " . $this->OPENAI_API_KEY
);
var_dump($data);
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/files/upload');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}

curl_close($ch);

return $result;

}

function GetAssistFiles(){
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer " . $this->OPENAI_API_KEY,
"OpenAI-Beta: assistants=v1"
);

$ch = curl_init( "https://api.openai.com/v1/assistants?order=desc&limit=20");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);

if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}

curl_close($ch);

var_dump($result);
}

function FileUplaud($assistID,$fileName){
$ch = curl_init();
$file = new CURLFile(__DIR__ . '/' . $fileName, 'application/octet-stream',$fileName);
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/files');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'purpose' => 'assistants',
'file' => $file
));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$headers = array();
$headers[] = "Content-Type: multipart/form-data";
$headers[] = 'User-Agent: Apidog/1.0.0 (https://apidog.com)';
$headers[] = 'Authorization: Bearer ' . $this->OPENAI_API_KEY;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
var_dump(curl_errno($ch));
var_dump( 'Error:' . curl_error($ch));
}
curl_close($ch);
var_dump($result);
$this->CreateAssistFile($assistID,json_decode($result)->id);
}

function CreateAssistFile($assistID,$fileID){
$url = "https://api.openai.com/v1/assistants/$assistID/files";

$headers = array(
"Authorization: Bearer " . $this->OPENAI_API_KEY,
"Content-Type: application/json",
"OpenAI-Beta: assistants=v1"
);

$data = array(
"file_id" => $fileID
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

$result = curl_exec($ch);

if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}

curl_close($ch);

//var_dump($result);
}

function DeleteFile($assistID,$fileID){
Expand Down

0 comments on commit 38ca615

Please sign in to comment.