Am currently trying to test large text with OPENAI gpt-4 model.I am trying to refactor the below code so that the model can generate coherent,readable and flowing document.The code below does return 5000 words unfortunately it returns diffferent variations based on a topic.My expectations are that it writes one single full 5000 words document.Any help will be highly appreciated. Check the generated document:https://docs.google.com/document/d/16zzm4w4uegng6BbNdErtaqItwGtLpHH0uEt0qLW59aE/edit?usp=sharing
The Code:
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,
]);
}
source https://stackoverflow.com/questions/76002754/how-do-i-refactor-the-below-code-to-allow-me-generate-coherent-and-flowing-docum
No comments:
Post a Comment