Skip to content

Commit

Permalink
✨ 增加AI流式输出开关及AI绘图支持
Browse files Browse the repository at this point in the history
  • Loading branch information
Licoy committed May 15, 2023
1 parent 4366fa0 commit 1cb7c20
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 19 deletions.
6 changes: 3 additions & 3 deletions assets/dist/js/libs.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/dist/js/page-ai.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion assets/js/page-ai.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jQuery(function () {
const el = $(e.currentTarget)
const inputEl = $(".chat-input")
const text = $.trim(inputEl.val())
const useImgMode = $("#chat-use-img-mode").is(":checked")
if (text === "") {
$p.toast('请先输入内容')
return
Expand Down Expand Up @@ -69,7 +70,7 @@ jQuery(function () {
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({text: text}),
body: JSON.stringify({text: text, imgMode:useImgMode}),
});
if (!f.ok) {
callback("请求失败:发起请求错误", true, true)
Expand Down
56 changes: 47 additions & 9 deletions inc/ajax/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,33 @@ function pk_ajax_ai_ask()
if (empty($openai_api_key)) {
wp_die('<code>请先配置OpenAI API Key</code>');
}
set_time_limit(0);
ini_set('memory_limit', '-1');
header('X-Accel-Buffering: no');
$openaiClient = new OpenAi($openai_api_key);
$openaiClient->setBaseURL($openai_url);
$use_img_mode = $body['imgMode'] ?? false;
if($use_img_mode){
try{
$chat_res = $openaiClient->image([
'prompt' => $text,
'n' => 1,
'size' => pk_get_option('openai_dall_e_size','512x512'),
'response_format' => 'url',
]);
$res = json_decode($chat_res);
if(!$res){
wp_die('<code>AI绘图失败:解析响应错误</code>');
}
$answer = $res->data[0]->url;
wp_die('![img]('.$answer.')');
}catch (Exception $e){
wp_die('<code>AI绘图失败:'.$e->getMessage().'</code>');
}
}
$use_stream = pk_is_checked('openai_stream');
if ($use_stream) {
set_time_limit(0);
ini_set('memory_limit', '-1');
header('X-Accel-Buffering: no');
}
$sys_content = pk_get_option('openai_model_sys_content');
$messages = [];
$use_total_token = pk_openai_token_len($text);
Expand All @@ -47,21 +69,24 @@ function pk_ajax_ai_ask()
'model' => 'gpt-3.5-turbo',
'messages' => $messages,
'temperature' => $temperature,
"stream" => true,
];
if ($use_stream) {
$args['stream'] = true;
}
if ($max_tokens > 0) {
$args['max_tokens'] = $max_tokens - $use_total_token;
}
$last_not_lines = '';
$callback = function ($curl_info, $data) use (&$last_not_lines) {
try{
try {
$try_json = json_decode($data, true);
if($try_json && isset($try_json['error'])){
if ($try_json && isset($try_json['error'])) {
echo $try_json['error']['message'];
ob_flush();
flush();
}
}catch (\Throwable $e){}
} catch (\Throwable $e) {
}
$tmp_lines = explode("\n", $last_not_lines . $data);
$last_not_lines = '';
$use_lines = [];
Expand Down Expand Up @@ -104,8 +129,21 @@ function pk_ajax_ai_ask()
return strlen($data);
};
try {
$openaiClient->chat($args,$callback);
wp_die();
if ($use_stream) {
$openaiClient->chat($args, $callback);
wp_die();
} else {
$chat_res = $openaiClient->chat($args);
$res = json_decode($chat_res);
if (!$res) {
wp_die('<code>AI问答解析:' . $chat_res . '</code>');
}
if (isset($res->error)) {
wp_die('<code>AI问答异常:' . $res->error . '</code>');
}
$answer = $res->choices[0]->message->content;
wp_die($answer);
}
} catch (Exception $e) {
wp_die('<code>AI问答出错:' . $e->getMessage() . '</code>');
}
Expand Down
20 changes: 18 additions & 2 deletions inc/setting/options/OptionAi.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ function get_fields(): array
'type'=>'textarea',
'tips'=>__('模型系统预设,可让AI主动进行一些违规话题的屏蔽,不懂勿轻易填充', PUOCK),
],
[
'id'=>'openai_dall_e',
'label'=>__('AI绘画支持', PUOCK),
'type'=>'switch',
'tips'=>__('启用后前端界面<code>勾选绘画模式</code>即可绘画', PUOCK),
],
[
'id'=>'openai_dall_e_size',
'label'=>__('AI绘画图片大小', PUOCK),
'type'=>'select',
'sdt'=>'512x512',
'options'=>[
['label'=>'256x256','value'=>'256x256'],
['label'=>'512x512','value'=>'512x512'],
['label'=>'1024x1024','value'=>'1024x1024']
],
],
[
'id'=>'openai_guest_use',
'label'=>__('允许游客使用', PUOCK),
Expand All @@ -57,9 +74,8 @@ function get_fields(): array
],
[
'id'=>'openai_stream',
'label'=>__('使用Stream(实时输出)模式<code>即将支持</code>', PUOCK),
'label'=>__('使用Stream(实时输出)模式', PUOCK),
'type'=>'switch',
'disabled'=>true,
'tips'=>__('启用后请关闭nginx的<code>gzip</code>模式', PUOCK),
],
[
Expand Down
15 changes: 12 additions & 3 deletions pages/template-chatgpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,18 @@ class="col-lg-<?php pk_hide_sidebar_out('12', '8') ?> col-md-12 <?php pk_open_bo
<span class="visually-hidden">Loading...</span>
</div>
</div>
<div class="d-flex justify-content-end mt10 d-none chat-btn-box">
<button class="btn btn-primary btn-sm mr-2 chat-submit"><i class="fa-regular fa-paper-plane me-1"></i>立即提问</button>
<button class="btn btn-dark btn-sm chat-clear-history"><i class="fa-solid fa-broom me-1"></i>清屏</button>
<div class="d-flex justify-content-between align-items-center mt10 d-none chat-btn-box">
<div class="form-check form-switch">
<?php if(pk_is_checked('openai_dall_e')):?>
<input class="form-check-input" name="remember" type="checkbox" role="switch"
id="chat-use-img-mode">
<label class="form-check-label fs14" for="chat-use-img-mode">AI绘画</label>
<?php endif; ?>
</div>
<div>
<button class="btn btn-primary btn-sm mr-2 chat-submit"><i class="fa-regular fa-paper-plane me-1"></i>立即提问</button>
<button class="btn btn-dark btn-sm chat-clear-history"><i class="fa-solid fa-broom me-1"></i>清屏</button>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit 1cb7c20

Please sign in to comment.