我目前正在尝试使用OPENAI gpt-4模型测试大型文本。我试图重构下面的代码,使模型可以生成连贯的,可读的和流动的文档。下面的代码确实返回5000个单词,不幸的是,它根据主题返回不同的变体。我的期望是它写一个完整的5000字的文档。任何帮助都将非常感激。检查生成的文档:https://docs.google.com/document/d/16zzm4w4uegng6BbNdErtaqItwGtLpHH0uEt0qLW59aE/edit?usp=sharing
代码:
public function writePropmt(Request $request)
{
$this->authCheck();
$this->validate($request, [
'content' => 'required',
'maxWords' => 'required|integer|min:1',
]);
ini_set('max_execution_time', 1800); // Set the maximum execution time to 10 minutes
$content = $request->input('content');
$maxWords = intval($request->input('maxWords'));
$maxWordsPerRequest = 2000; // Set the maximum words to generate in each request
$result = '';
if (!empty($this->super_settings['openai_api_key']) && $content) {
if ($this->isDemo() || $this->super_settings['openai_api_key'] == 'demo') {
$result = '';
} else {
// Set OpenAI API Key
$client = OpenAI::client($this->super_settings['openai_api_key']);
try {
$response = $client->chat()->create([
'model' => 'gpt-4',
'messages' => [
[
'role' => 'user',
'content' => $content,
],
],
'max_tokens' => $maxWordsPerRequest,
]);
if (!empty($response->choices)) {
foreach ($response->choices as $data) {
$result .= $data->message->content;
}
}
while (strlen($result) < $maxWords && strlen($result) + $maxWordsPerRequest <= $maxWords) {
$response = $client->chat()->create([
'model' => 'gpt-4',
'messages' => [
[
'role' => 'user',
'content' => '',
],
],
'max_tokens' => $maxWordsPerRequest,
'prompt' => $result,
]);
if (!empty($response->choices)) {
foreach ($response->choices as $data) {
$result .= $data->message->content;
}
}
}
$result = substr($result, 0, $maxWords); // Limit the result to the requested number of words
} catch (Exception $e) {
if ($this->user->is_super_admin) {
$result = 'Error: ' . $e->getMessage();
} else {
$result = __('Sorry, I am not able to write anything for you.');
}
}
}
}
if (empty($result)) {
$result = __('Sorry, I am not able to write anything for you.');
}
// Convert result markdown to html
$result = Str::markdown($result);
return response()->json([
'success' => true,
'result' => $result,
]);
}
在你的问题和澄清评论中,你说你正在寻找一个5000字(令牌)的回应。
您获得连续响应的原因是因为您设置了$maxWordsPerRequest = 2000;
,然后在第一个请求之后,脚本被编写为发送多个单独的请求,并将它们附加到单个字符串中,直到您获得正在传递到所示代码中的$maxWords
值。
发生在这部分代码中;这整个while
语句应该从脚本中删除:
while (strlen($result) < $maxWords && strlen($result) + $maxWordsPerRequest <= $maxWords) {
$response = $client->chat()->create([
'model' => 'gpt-4',
'messages' => [
[
'role' => 'user',
'content' => '',
],
],
'max_tokens' => $maxWordsPerRequest,
'prompt' => $result,
]);
if (!empty($response->choices)) {
foreach ($response->choices as $data) {
$result .= $data->message->content;
}
}
}
此外,在您第一次调用发送请求并获得响应的部分的该部分之前,您也有一个foreach
来检查多个响应选择并将它们附加在一起:
if (!empty($response->choices)) {
foreach ($response->choices as $data) {
$result .= $data->message->content;
}
}
你应该只得到一个响应选择,因为这是模型的默认n
值,你没有请求n
>然而,如果模型返回多个选择,那么foreach
可能会允许额外的响应选择溜过。
要获得期望的结果,请确保设置:
$maxWordsPerRequest = 5000;