web.config 配置文件。
在调用 ob_flush() 和 flush() 不生效(页面总是一直等,等 Load 到 100% 一次性渲染出来,而不似乎 Stream 流方式输出到浏览器),请检查 IIS 中是否有 web.config 里加入类似配置:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<handlers>
<remove name="PHP_FastCGI" />
<add name="PHP_FastCGI" path="*.php" verb="*" modules="FastCgiModule" scriptProcessor="[path .. ]\php-cgi.exe" responseBufferLimit="0"/>
</handlers>
...
</system.webServer>
</configuration>
不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。配置错误 不能在此路径中使用此配置节。如果在父级别上锁定了该节,便会出现这种情况。锁定是默认设置的(overrideModeDefault="Deny"),或者是通过包含 overrideMode="Deny" 或旧有的 allowOverride="false" 的位置标记明确设置的。 配置文件 \\?\X(盘符):\目录名\目录名\web.config
全新 IIS 采用了更安全的 web.config 管理机制,默认情况下会锁住配置项不允许更改。要取消锁定可以以管理员身份运行命令行:
# 如果是 handlers 被 IIS 锁定 %windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/handlers # 如果 modules 被 IIS 锁定 %windir%\system32\inetsrv\appcmd.exe unlock config -section:system.webServer/modules
推荐一个 OpenAI PHP 异步客户端,适用于 workerman 和 webman:
# Install composer create-project workerman/webman cd webman composer require webman/openai
Chat with stream - 流式输出
<?php
namespace app\controller;
use support\Request;
use Webman\Openai\Chat;
use Workerman\Protocols\Http\Chunk;
class ChatController
{
public function completions(Request $request)
{
$connection = $request->connection;
$chat = new Chat(['apikey' => 'sk-xx', 'api' => 'https://api.openai.com']);
$chat->completions(
[
'model' => 'gpt-3.5-turbo',
'stream' => true,
'messages' => [['role' => 'user', 'content' => 'hello']],
], [
'stream' => function($data) use ($connection) {
$connection->send(new Chunk(json_encode($data, JSON_UNESCAPED_UNICODE) . "\n"));
},
'complete' => function($result, $response) use ($connection) {
if (isset($result['error'])) {
$connection->send(new Chunk(json_encode($result, JSON_UNESCAPED_UNICODE) . "\n"));
}
$connection->send(new Chunk(''));
},
]);
return response()->withHeaders([
"Transfer-Encoding" => "chunked",
]);
}
}