php 为什么我找不到类“OpenAI\API\Request”?

aydmsdu9  于 2023-03-22  发布在  PHP
关注(0)|答案(2)|浏览(150)

我使用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

knpiaxh1

knpiaxh11#

我在库源代码中找不到任何名为API/Request的类。它应该是,

use OpenAI\Client;

$yourApiKey = 'sk-fEpZ.......';
$client = new Client($yourApiKey);

$yourApiKey = 'sk-fEpZ.......';
$client = OpenAI::client($yourApiKey);
ct2axkht

ct2axkht2#

检查文件:https://github.com/openai-php/client客户端的创建方式为:

$yourApiKey = getenv('YOUR_API_KEY');
$client = OpenAI::client($yourApiKey);

相关问题