I use openai/client-php.
require __DIR__ . '/vendor/autoload.php';
use OpenAIClient;
use OpenAIAPIRequest;
$yourApiKey = 'sk-fEpZ.......';
$client = new Client(new Request($yourApiKey));
function generateText($client, $model, $prompt, $length, $temperature = 0.5) {
$response = $client->completions()->create(
$model,
[
'prompt' => $prompt,
'max_tokens' => $length,
'temperature' => $temperature,
]
);
return $response['choices'][0]['text'];
}
$models = [
'en' => 'text-davinci-002',
'zh' => 'text-davinci-002',
'de' => 'text-davinci-002',
'fr' => 'text-davinci-002'
];
$languages = [
'en' => 'English',
'zh' => 'Chinese',
'de' => 'German',
'fr' => 'French'
];
$prompt = 'Why the sky is blue?:';
foreach ($models as $lang => $model) {
$generatedText = generateText($client, $model, $prompt, 200);
$generatedTitle = substr($generatedText, 0, 80);
echo " {$languages[$lang]} Language:n";
echo "Title: $generatedTitlen";
echo "Text: $generatedTextnn";
}
I have error.
Fatal error: Uncaught Error: Class "OpenAIAPIRequest" not found in
D:OpenServerdomainslocalhostopenaitest.php:9 Stack trace: #0
{main} thrown in D:OpenServerdomainslocalhostopenaitest.php on
line 9
2
Answers
I couldn’t find any class called
API/Request
in the library source code. It should be,OR
Check documentation: https://github.com/openai-php/client
Client is created in other way: