Created
August 12, 2023 01:59
-
-
Save ambiorixg12/257f7af3c18d3ad62c2d56ea20b228e7 to your computer and use it in GitHub Desktop.
chatgpt working model with asterisk
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions'); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, [ | |
'Authorization: Bearer sk-dLwvCvDVK', | |
'Content-Type: application/json', | |
]); | |
$model = "gpt-3.5-turbo-16k"; | |
// Load previous messages from files | |
$userMessages = file('/var/www/html/gpt/user_messages.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | |
$assistantMessages = file('/var/www/html/gpt/assistant_messages.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); | |
// Construct messages array | |
$messages = []; | |
foreach ($userMessages as $userMessage) { | |
$messages[] = ["role" => "user", "content" => $userMessage]; | |
} | |
foreach ($assistantMessages as $assistantMessage) { | |
$messages[] = ["role" => "assistant", "content" => $assistantMessage]; | |
} | |
// Add new user message | |
$newUserMessage = "$argv[1]"; // Replace with user input or other source | |
$messages[] = ["role" => "user", "content" => $newUserMessage]; | |
// Prepare data for API request | |
$data = [ | |
"model" => $model, | |
"messages" => $messages | |
]; | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); | |
$response = curl_exec($ch); | |
// Check for API errors | |
if (curl_errno($ch)) { | |
echo 'Curl error: ' . curl_error($ch); | |
} else { | |
$object = json_decode($response); | |
if (isset($object->choices[0]->message->content)) { | |
$assistantReply = $object->choices[0]->message->content; | |
// Update assistant_messages.txt | |
file_put_contents("/var/www/html/gpt/assistant_messages.txt", "\nassistant: " . $assistantReply, FILE_APPEND); | |
echo $assistantReply . "\n"; | |
} else { | |
echo 'Error in API response: ' . $response; | |
} | |
} | |
curl_close($ch); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment