thumbnail
让博客支持使用 ChatGPT 生成文章摘要是一种什么样的体验?
本文最后更新于 541 天前,其中的信息可能已经有所发展或是发生改变。
由 ChatGPT 生成的文章摘要
本文讲述了博主为了给自己的 Argon 主题添加基于 ChatGPT 的人工智能摘要功能而付出的努力。文章介绍了开发流程,包括使用 haozi-team/chatgpt-php 库对接 OpenAI 接口,修改 settings.php 和 inc/fun/post-extra-meta-editor.php 文件添加全局和文章单独的设置,以允许用户配置 OpenAI 地址,密钥,以及使用 AI 摘要功能的选项,并在 inc/fun/post.php 和 template-parts/entry/excerpt.php 中添加页面样式。最终,博主满意地得出结论:这个功能非常好用,易于操作,能够很好地满足需求。

让博客支持使用 ChatGPT 生成文章摘要是一种什么样的体验?

起因

Sakurairo 主题支持了基于 ChatGPT 的 AI 摘要功能,我有点眼红,但是因为那是个主题限定功能,而我用的又是 Argon,遂想着让 Argon 也支持 AI 摘要功能。

截至目前,此功能已经合并到了 argon-theme 主仓库的 dev 分支,pr 现场可参见:#607

开发

和 Sakurairo 相同,我采用了 haozi-team/chatgpt-php 库以对接 OpenAI 接口(顺便给 Argon 添加了 Compose 支持) 。

接下来,在 settings.phpinc/fun/post-extra-meta-editor.php 添加了全局和文章单独设置,以允许用户配置 OpenAI 地址,密钥,以及是否需要使用 AI 摘要功能。

最后,在 inc/fun/post.phptemplate-parts/entry/excerpt.php 添加页面样式,就完成了开发。

有关 ChatGPT 部分的核心代码如下:

<?php

use GuzzleHttp\Exception\GuzzleException;
use HaoZiTeam\ChatGPT\V2 as ChatGPTV2;

function argon_generate_chatgpt_client(): ?ChatGPTV2 {

    $apikey  = get_option( "argon_openai_api_key" );
    $baseurl = $GLOBALS["openai_baseurl"];

    if ( ! $apikey || ! isset( $baseurl ) ) {
        return null;
    }

    return new ChatGPTV2( $apikey, $baseurl , model: get_option('argon_ai_model', 'gpt-3.5-turbo'), timeout: 30 );
}

/**
 * @throws Exception|GuzzleException
 */
function argon_generate_article_summary( int $post_id, WP_Post $post ): string {
    $client = argon_generate_chatgpt_client();

    $client->addMessage(
        "You are an article summary generator, ".
        "please generate the summary of the given article ".
        "with provided title and content, ".
        "the language of the summary must equals to the article's mainly used language, ".
        "do not write summary in third person.".
        "system" );
    $client->addMessage( "The title of the article:" . $post->post_title );

    $content = $post->post_content;
    $content = wp_strip_all_tags(apply_filters('the_content', $content));
    $max_content_length = get_option( 'argon_ai_max_content_length', 4000 );
    if ($max_content_length > 0){
        $content = substr($content, 0, $max_content_length);
    }

    $client->addMessage( "The content of the article:" . $content );
    $previous_summary = get_post_meta( $post_id, "argon_ai_summary", true );
    if ( $previous_summary != "" ) {
        $client->addMessage( "The previous summary of the article, please generate the summary similar with the previous one: " . $previous_summary);
    }

    $post_argon_ai_extra_prompt_mode = get_post_meta( $post_id, "argon_ai_extra_prompt_mode", true );
    $post_argon_ai_extra_prompt      = get_post_meta( $post_id, "argon_ai_extra_prompt", true );
    $global_argon_ai_extra_prompt    = get_option( 'argon_ai_extra_prompt', "" );
    switch ( $post_argon_ai_extra_prompt_mode ) {
        case 'replace':
            $client->addMessage( $post_argon_ai_extra_prompt, 'system' );
            break;
        case 'append':
            $client->addMessage( $global_argon_ai_extra_prompt . $post_argon_ai_extra_prompt, 'system' );
            break;
        case 'none':
            break;
        case 'default':
        default:
            $client->addMessage( $global_argon_ai_extra_prompt, 'system' );
            break;
    }

    $result = "";
    foreach ( $client->ask( "Now please generate the summary of the article given before" ) as $item ) {
        $result .= $item['answer'];
    }

    return $result;
}


function argon_on_save_post( int $post_id, WP_Post $post, string $old_status ): void {
//  if ( false !== wp_is_post_autosave( $post_id ) || 'auto-draft' == $post->post_status ) {
//      return;
//  }

    // If this is a revision, get real post ID.
    $revision_id = wp_is_post_revision( $post_id );

    if ( false !== $revision_id ) {
        $post_id = $revision_id;
    }

    if ( get_option( 'argon_ai_post_summary', false ) == 'false' || get_post_meta( $post_id, "argon_ai_post_summary", true ) == 'false' ) {
        return;
    }
    $update                                 = $old_status === $post->post_status;
    $post_argon_ai_no_update_post_summary   = get_post_meta( $post_id, "argon_ai_no_update_post_summary", true );
    $global_argon_ai_no_update_post_summary = get_option( 'argon_ai_no_update_post_summary', true );
    if ( $update && $post_argon_ai_no_update_post_summary != 'false' && ( $post_argon_ai_no_update_post_summary == 'true' || $global_argon_ai_no_update_post_summary == 'true' ) ) {
        return;
    }

    if ( get_option( 'argon_ai_async_generate', true ) != 'false' ) {
        wp_schedule_single_event( time() + 1, 'argon_update_ai_post_meta', array( $post_id ) );
    } else {
        argon_update_ai_post_meta( $post_id );
    }
}

function argon_update_ai_post_meta( $post_id ): void {
    try {
        $summary = argon_generate_article_summary( $post_id, get_post( $post_id ) );
        update_post_meta( $post_id, "argon_ai_summary", $summary );
    } catch ( Exception|GuzzleException $e ) {
        error_log( $e );
    }
}

add_action( "argon_update_ai_post_meta", "argon_update_ai_post_meta" );
add_action( "publish_post", "argon_on_save_post", 20, 3 );

成品

总体来说,是非常满足我的需求的,用起来也很方便。

扫码关注 HikariLan's Blog 微信公众号,及时获取最新博文!


微信公众号图片

评论

  1. who
    Windows Edge 112.0.1722.58
    1 年前
    2023-4-28 13:46:10

    呐呐,下次可以让AI自动发文章给我读吗?
    呐呐,贺兰哥哥的虚拟模型什么时候上线?

  2. ZX夏夜之风
    Linux Edge 108.0.1462.48
    1 年前
    2023-5-01 7:17:45

    cool诶

  3. Ken
    Windows Firefox 115.0
    1 年前
    2023-7-31 2:31:09

    这个插件什么时候能发出来?

    • 博主
      Ken
      Linux Chrome 114.0.0.0
      1 年前
      2023-7-31 8:57:32

      这不是一个插件,这个功能是内嵌在 Argon 主题内的… 如果你用 Argon,那么去 Argon 的 GitHub 仓库下载 dev 分支就可以用了

      • 名字
        贺兰星辰
        Windows Chrome 128.0.0.0
        4 周前
        2024-9-21 21:25:49

        我使用Argon的master分支安装的主题
        现在想使用dev分支进行文件覆盖安装
        请问dev分支是否兼容master分支呢?🙂

        • 博主
          名字
          Windows Chrome 128.0.0.0
          4 周前
          2024-9-21 21:26:34

          hi! 我认为是完全可以的,因为本博客就是这样迁移过去的

          • 名字
            贺兰星辰
            Windows Chrome 128.0.0.0
            已编辑
            4 周前
            2024-9-21 22:18:29

            抱歉,这么晚才看到你的回复
            我想询问一下,对于旧文件你是如何处理的呢?
            我是指master的文件有些似乎已经在dev分支中弃用了,这些文件需要手动删除吗?
            还是说我应该这样做:

            1. 备份主题设置
            2. 删除原主题文件夹 (我有子主题文件夹,此文件夹我不会删除)
            3. 将dev分支的主题上传并导入设置

            很抱歉一次性询问这么多问题,因为我无法在互联网上找到相应信息
            对你的叨扰我再次道歉🫡

          • 博主
            名字
            Windows Chrome 128.0.0.0
            4 周前
            2024-9-21 22:25:31

            是的,你的做法是对的,所有数据存储在另外的地方,主题文件夹不会存储数据,直接删掉换新的就行

  4. 名字
    Windows Chrome 128.0.0.0
    4 周前
    2024-9-22 13:15:10

    我在安装Argon主题的dev分支时WordPress报错
    查询debug日志得知 “主题中的一个文件 getallheaders.php 未找到,导致致命错误”
    我尝试在本地查找文件,但事实上这是一个空目录
    我在GitHub的文件树上发现 vendor 文件夹下的大部分文件都未成功提交,特征为文件夹上有一个白色的向右箭头且不能打开


    查看图片
    示例


    这似乎意味着你的代码上传失败
    我找到的可能的解决方法: 
    https://cloud.tencent.com/developer/ask/sof/108152528
    https://www.jianshu.com/p/7cc6ea70e48e
    如果你需要我可以提供我的 debug.log

    • 博主
      名字
      Windows Chrome 128.0.0.0
      4 周前
      2024-9-22 13:17:36

      白色箭头指的是 Git Submodule,这不是代码上传的问题,是你没有正确拉取所有代码
      试试使用 git clone ... --recursive

  5. Mingker
    Windows Chrome 128.0.0.0
    4 周前
    2024-9-23 21:24:25

    我使用dev分支的内容直接覆盖了master分支
    但是我没有看见相关功能
    请问这个功能的开启路径在哪里?

    • 博主
      Mingker
      Windows Chrome 128.0.0.0
      4 周前
      2024-9-23 21:30:53

      先确定下你安装的确实是 dev 分支的主题而不是原来的(建议删了重建而不是覆盖),然后,你应该可以在 Argon 主题设置中看到相关内容

      • Mingker
        贺兰星辰
        Windows Chrome 128.0.0.0
        4 周前
        2024-9-23 21:35:50

        感谢大佬回复!
        我去试试

发送评论 编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇