PHPStan shows parameter error
ronaldrupp opened this issue · comments
Describe the bug
PHPStan shows error:
Parameter #2 $stream of method Orhanerday\OpenAi\OpenAi::chat() expects null, Closure given.
Secondary parameter $stream of chat method is typed null.
To Reproduce
- use PHPStan Level 5
- run it
Code snippets
$open_ai->chat($options, function ($curl_info, $data) {
echo $data . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
return strlen($data);
});
OS
macOS
PHP version
PHP 8.0
Library version
openai V4.7.1
You should handle the error messages, but no worries, I already add that handling please check; https://github.com/orhanerday/ChatGPT/blob/b326bbf82299c1267e21cd182c02ea21660fb57f/event-stream.php#L51,
$complete = $open_ai->chat($opts, function ($curl_info, $data) use (&$txt) {
if ($obj = json_decode($data) and $obj->error->message != "") {
error_log(json_encode($obj->error->message)); // <- you can bind this message to toastr etc.
} else {
echo $data;
$clean = str_replace("data: ", "", $data);
$arr = json_decode($clean, true);
if ($data != "data: [DONE]\n\n" and isset($arr["choices"][0]["delta"]["content"])) {
$txt .= $arr["choices"][0]["delta"]["content"];
}
}
echo PHP_EOL;
ob_flush();
flush();
return strlen($data);
});
Hey 👋
It's not about an error coming in from the API, it's about the type that the method expects as second parameter. The chat method expects a null type as second parameter, but we are giving it a function (closure).
Did you append ‘"stream" => true,’ to the opts var?
$opts = [
'prompt' => "Hello",
'temperature' => 0.9,
"max_tokens" => 150,
"frequency_penalty" => 0,
"presence_penalty" => 0.6,
"stream" => true,
];
Yes, I did.
$options = [
'model' => 'gpt-3.5-turbo',
'messages' => [
[
'role' => 'user',
'content' => 'Hi',
],
],
'temperature' => 1.0,
'max_tokens' => 4000,
'frequency_penalty' => 0,
'presence_penalty' => 0,
'stream' => true,
];
/** @phpstan-ignore-next-line */
$open_ai->chat($options, function ($curl_info, $data) {
echo $data . PHP_EOL;
echo PHP_EOL;
ob_flush();
flush();
return strlen($data);
});
The method chat
expects the second parameter to be null
/**
* @param $opts
* @param null $stream
* @return bool|string
* @throws Exception
*/
public function chat($opts, $stream = null)
{
if ($stream != null && array_key_exists('stream', $opts)) {
if (!$opts['stream']) {
throw new Exception(
'Please provide a stream function. Check https://github.com/orhanerday/open-ai#stream-example for an example.'
);
}
$this->stream_method = $stream;
}
$opts['model'] = $opts['model'] ?? $this->chatModel;
$url = Url::chatUrl();
$this->baseUrl($url);
return $this->sendRequest($url, 'POST', $opts);
}