-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAiHandler.php
150 lines (129 loc) · 3.56 KB
/
OpenAiHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
namespace OpenAiHandler;
require_once "vendor/autoload.php";
require_once "./PromptProcessor.php";
use Orhanerday\OpenAi\OpenAi;
class OpenAIHandler
{
private string $prompt;
private OpenAi $openai;
private string $model;
public function __construct($array_obj)
{
$this->setOpenai(new OpenAi($array_obj["openai_key"]));
$this->setPrompt($array_obj["prompt"]);
$this->setModel("gpt-3.5-turbo");
}
public function prepare(
string $user,
int $conclusion_length = 100
){
try {
$messages = [];
// Adiciona a mensagem do usuário em questão
// Adiciona a mensagem do sistema
/*
$_SESSION[$u] = [
"role" => "system",
"content" => "you are a scientist."
];
*/
$_CHAT[$user][] = [
"role" => "user",
"content" => $this->getPrompt()
];
$max_tokens = $conclusion_length * 2;
$response = $this->openai->chat([
'model' => $this->getModel(),
'messages' => $_CHAT[$user],
'max_tokens' => $max_tokens,
'n' => 1,
'temperature' => 0,
'presence_penalty' => 1,
'frequency_penalty' => 1,
'top_p' => 1
]);
// Extrai o texto gerado e remove a parte do prompt
$r = json_decode($response, true);
if(empty($r["choices"])){
return "Sorry, I couldn't understand your question. Please try again.";
}
if(!empty($r["error"])){
return 'Sorry, there was an error processing your question. Please try again later.';
}else{
$text = $r["choices"][0]["message"]["content"];
if (in_array(substr($text, 0, 1), [".",","])) {
$text = str_replace(substr($text, 0, 1), "", $text);
}
$messages[] = $r["choices"][0]["message"];
return $text;
}
} catch (\Throwable $e) {
echo $e->getMessage();
return 'Sorry, there was an error processing your question. Please try again later.';
}
}
/**
* Gets the value of prompt
*
* @return string
*/
public function getPrompt(): string
{
return $this->prompt;
}
/**
* Sets the value of prompt
*
* @param string $prompt description
*
* @return OpenAiHandler
*/
public function setPrompt(string $prompt): OpenAiHandler
{
$this->prompt = $prompt;
return $this;
}
/**
* Gets the value of model
*
* @return string
*/
public function getModel(): string
{
return $this->model;
}
/**
* Sets the value of model
*
* @param string $model description
*
* @return OpenAiHandler
*/
public function setModel(string $model): OpenAiHandler
{
$this->model = $model;
return $this;
}
/**
* Gets the value of openai
*
* @return OpenAi
*/
public function getOpenai(): OpenAi
{
return $this->openai;
}
/**
* Sets the value of openai
*
* @param OpenAi $openai description
*
* @return OpenAiHandler
*/
public function setOpenai(OpenAi $openai): OpenAiHandler
{
$this->openai = $openai;
return $this;
}
}