我使用openai/client-php。
require __DIR__ . '/vendor/autoload.php';
use OpenAI\Client;
use OpenAI\API\Request;
$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: $generatedTitle\n";
echo "Text: $generatedText\n\n";
}
我有错误。
致命错误:未捕获的错误:在D:\OpenServer\domains\localhost\openai\test中找不到Class“OpenAI\API\Request”。#0 {main}抛出D:\OpenServer\domains\localhost\openai\test.php上线9
2条答案
按热度按时间knpiaxh11#
我在库源代码中找不到任何名为
API/Request
的类。它应该是,或
ct2axkht2#
检查文件:https://github.com/openai-php/client客户端的创建方式为: