彧琅環 發表於 2025-12-30 13:17:00

Rime-AI v2版本发布

<h1 id="ai-rime">AI-Rime</h1>
<p>基于Rime的lua脚本系统打造AI增强输入法</p>
<p><img alt="" loading="lazy" src="https://ulln.top:8890/s/N2WAw6rcs7LsMEr/preview" class="lazyload"></p>
<p><img alt="" loading="lazy" src="https://ulln.top:8890/s/YKeG2dETmss23Hq/preview" class="lazyload"></p>
<h1 id="rime-ai-纠错-v1-智能纠错">Rime AI 纠错 v1 智能纠错</h1>
<h2 id="功能说明">功能说明</h2>
<p>极简版 AI 纠错功能:</p>
<ul>
<li>按 <strong>6</strong> 触发纠错,显示"AI纠正中..."</li>
<li>再按 <strong>6</strong> 显示纠正结果</li>
</ul>
<h2 id="文件清单">文件清单</h2>
<table>
<thead>
<tr>
<th>文件</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>ai_corrector_processor.lua</code></td>
<td>按键监听(放入 <code>lua/</code> 目录)</td>
</tr>
<tr>
<td><code>ai_corrector.lua</code></td>
<td>候选词处理(放入 <code>lua/</code> 目录)</td>
</tr>
<tr>
<td><code>ai_corrector_service*</code></td>
<td>后台服务</td>
</tr>
</tbody>
</table>
<h1 id="前言">前言</h1>
<p>Lua 是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放, 其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活的扩展和定制功能。</p>
<p>Rime输入法支持内置简单的Lua脚本,基于此得以设计AI输入法。</p>
<p>Rime的配置文件夹结构大致如下:</p>
<p><strong>Rime</strong></p>
<ul>
<li>Lua——存放lua脚本</li>
<li>build——存放构建后的信息</li>
<li>*.userdb——存放用户输入习惯的记录</li>
<li>*_dicts——存放引入的词典</li>
<li>其他配置文件</li>
</ul>
<h2 id="安装步骤">安装步骤</h2>
<h3 id="1-程序文件部署">1. 程序文件部署</h3>
<p>当前版本总共需要部署三个程序,分别是两个lua程序以及一个可执行程序。</p>
<p>Rime输入法配置文件可通过Rime输入法菜单打开,Mac端叫作<code>用户设定</code>,Windows端叫作<code>用户文件夹</code>。Mac下Rime用户资料默认在<code>Users/用户名/Library/Rime</code>下,可直接在<code>README.md</code>所在目录打开终端执行下面命令进行复制。</p>
<pre><code class="language-bash">cp ai_corrector_processor.lua ~/Library/Rime/lua/
cp ai_corrector.lua ~/Library/Rime/lua/
</code></pre>
<p>Windows端Rime用户文件夹默认路径是<code>C:\Users\用户名\AppData\Roaming\Rime</code>,Linux平台输入框架和版本不同则具体的也会目录不同,需要根据系统是fictx4、fictx5或iBus来定。</p>
<p>将不同平台对应的可执行程序复制到Rime配置文件的根目录下。</p>
<h3 id="2-配置-rime-schema">2. 配置 Rime Schema</h3>
<p>在你的输入方案的schema(例如雾凇拼音rime_ice.schema.yaml文件)中添加:</p>
<pre><code class="language-yaml">engine:
processors:
    - lua_processor@*ai_corrector_processor        # 尽量放在processors前面,以免被其他processor抢先捕获
filters:
    - lua_filter@*ai_corrector        # 这一条必须放在filters的第一个位置,用于修改AI纠正结果显示
</code></pre>
<p>或者在输入方案的custom文件中添加:</p>
<pre><code class="language-yaml">patch:        # 更推荐这种添加方案,不影响原有配置文件
engine/processors/@after 0: lua_processor@*ai_corrector_processor
engine/filters/@before last: lua_filter@*ai_corrector
</code></pre>
<p>例如对雾凇拼音的<code>rime_ice.shcema.yaml</code>配置文件创建一个<code>rime_ice.custom.yaml</code>文件,然后添加上面的<code>patch内容</code>。</p>
<h3 id="3-启动服务">3. 启动服务</h3>
<ul>
<li>
<p>Windows平台在Rime根目录下打开PowerShell,执行命令开启服务,终端会显示纠错请求信息。Windows平台可以利用nssm制作后台服务(注意工作目录设置成Rime配置文件根目录)。</p>
<pre><code class="language-shell">./ai_corrector_service_Windows/ai_corrector_service.exe
</code></pre>
</li>
<li>
<p>Linux平台同样在Rime配置根目录打开终端,执行命令启动服务。</p>
<pre><code class="language-shell">./ai_corrector_service_Linux/ai_corrector_service.bin
</code></pre>
<p>如果启动失败的话检查权限是否正确,通过命令赋予运行权限。</p>
<pre><code class="language-shell">chmod +x ./ai_corrector_service_Linux/ai_corrector_service.bin
</code></pre>
<p>Linux可用systemd设置成服务后台运行,直接执行bin文件即可,建议设置工作目录是Rime配置文件根目录。</p>
</li>
<li>
<p>Mac平台将<code>ai_corrector_service_Mac.app</code>放置在Rime配置文件根目录后<strong>不可以</strong>直接双击运行,因为服务程序没有UI,因此同样需要在Rime配置文件根目录打开终端执行命令:</p>
<pre><code class="language-shell">./ai_corrector_service_Mac.app/Contents/MacOS/ai_corrector_service
</code></pre>
</li>
</ul>
<p>​        Mac电脑可用launched设置成服务后台运行。</p>
<h3 id="4-重新部署-rime">4. 重新部署 Rime</h3>
<h2 id="使用方法">使用方法</h2>
<p>本方案是基于雾凇拼音输入方案而设计的,其他拼音方案同样可用。雾凇拼音默认设置5个候选词,因此我将数字键6作为了触发纠错功能的触发按键。</p>
<p>上面部署阶段成功后,具体的操作流程如下:</p>
<ol>
<li>输入拼音,看到候选词</li>
<li>按 <strong>6</strong> 触发纠错</li>
<li>再按 <strong>6</strong> 显示结果</li>
<li>按 <strong>空格</strong> 选择结果</li>
</ol>
<p><strong>结束语:</strong></p>
<p>因为Rime本身的设计,lua脚本只能在输入发生变动时触发,因此该版本需要先按数字键6触发AI纠错请求,等1-2s后再按6触发AI返回结果显示。等待按第二次键很是恼人,目前已经在设计第二版本,期望可以解决避免需要二次按键触发的问题。</p>
<p>当前版本prompt提示词经过数百次尝试优化,已经达到较好的效果。如果关注较多的话考虑下一版本公开提示词,到时候可以自定义做更多花样。</p>
<p>资源链接:</p>
<p>https://1815368419.v.123pan.cn/1815368419/26934630</p>
<h1 id="rime-ai-纠错-v2-可配置版">Rime AI 纠错 v2 可配置版</h1>
<h2 id="功能说明-1">功能说明</h2>
<p>支持外部配置文件和自动触发的 AI 纠错功能:</p>
<ul>
<li>按 <strong>6</strong> 触发纠错</li>
<li>按 <strong>0</strong> 显示结果(或开启自动触发后自动显示)</li>
</ul>
<h2 id="文件清单-1">文件清单</h2>
<table>
<thead>
<tr>
<th>文件</th>
<th>说明</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>ai_corrector_processor.lua</code></td>
<td>按键监听(放入 <code>lua/</code> 目录)</td>
</tr>
<tr>
<td><code>ai_corrector.lua</code></td>
<td>候选词处理(放入 <code>lua/</code> 目录)</td>
</tr>
<tr>
<td><code>ai_corrector_service.*</code></td>
<td>后台服务</td>
</tr>
<tr>
<td><code>ai_corrector_config.json</code></td>
<td>配置文件</td>
</tr>
</tbody>
</table>
<h2 id="安装步骤-1">安装步骤</h2>
<h3 id="1-复制文件">1. 复制文件</h3>
<p><strong>见版本1说明</strong></p>
<h3 id="2-修改配置">2. 修改配置</h3>
<p>编辑 <code>ai_corrector_config.json</code>:</p>
<pre><code class="language-json">{
    "api_key": "你的API密钥",
    "auto_trigger": true
}
</code></pre>
<p>配置文件记录着请求AI的信息,请把他放置在Rime配置目录下。</p>
<h3 id="3-配置-rime-schema">3. 配置 Rime Schema</h3>
<p><strong>见版本1说明</strong></p>
<h3 id="4-启动服务">4. 启动服务</h3>
<p><strong>见版本1说明</strong></p>
<h3 id="5-重新部署-rime">5. 重新部署 Rime</h3>
<h2 id="配置选项">配置选项</h2>
<table>
<thead>
<tr>
<th>选项</th>
<th>说明</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>provider</code></td>
<td>AI 提供商 (deepseek/openai/ollama)</td>
<td>deepseek</td>
</tr>
<tr>
<td><code>api_key</code></td>
<td>API 密钥</td>
<td>空</td>
</tr>
<tr>
<td><code>api_url</code></td>
<td>API 地址</td>
<td>DeepSeek</td>
</tr>
<tr>
<td><code>model</code></td>
<td>模型名称</td>
<td>deepseek-chat</td>
</tr>
<tr>
<td><code>auto_trigger</code></td>
<td>自动显示结果</td>
<td>true</td>
</tr>
<tr>
<td><code>prompt</code></td>
<td>纠错提示词</td>
<td>内置</td>
</tr>
</tbody>
</table>
<h2 id="自动触发">自动触发</h2>
<p>开启 <code>auto_trigger</code> 后:</p>
<ol>
<li>按 6 触发纠错</li>
<li>AI 返回结果后自动模拟按键 0</li>
<li>结果自动显示</li>
</ol>
<p>需要权限:</p>
<ul>
<li><strong>macOS</strong>: 系统设置 → 隐私与安全性 → 辅助功能 → 添加 服务程序</li>
</ul>
<p>资源链接:</p>
<p>https://1815368419.v.123pan.cn/1815368419/26963692</p>
<h1 id="v2-linux修复版本">V2-Linux修复版本</h1>
<p><strong>前言:</strong></p>
<ul>
<li>
<p>由于Linux系统输入法框架的设计,首发v2版本出现配置文件找错目录的问题,现已经经过修改和测试,在作者Ubuntu24 x11系统上已经可以正常运行,如若在其他发行版上出现问题请及时反馈。</p>
</li>
<li>
<p>v2版本是基于python的pynput模块实现发送模拟按键0来代替用户第二次按键触发直接修改输入法第一个候选词显示为AI返回结果的,但是优于Linux下x11和Wayland对pynput支持较差,因此此次修复版本针对这一问题做出了深度修复与测试。作者Ubuntu24 x11环境下可以实现正常纠错及自动触发,优于作者设备有限,没能测试Wayland,因此下面给出x11的实际可行配置策略及Wayland理论可行配置策略。</p>
</li>
</ul>
<h1 id="linux-模拟按键配置指南">Linux 模拟按键配置指南</h1>
<p>本文档说明在 Linux 环境下配置 AI 纠错服务自动触发功能所需的依赖和设置。</p>
<h2 id="问题背景">问题背景</h2>
<p>Python 的 <code>pynput</code> 库在 Linux 下存在以下问题:</p>
<ul>
<li><strong>X11</strong>:需要额外权限配置,且不够稳定</li>
<li><strong>Wayland</strong>:静默失败,无法正常工作</li>
</ul>
<p>因此,在 Linux 下程序<strong>跳过 pynput</strong>,改用系统工具。</p>
<hr>
<h2 id="环境检测">环境检测</h2>
<p>程序通过环境变量 <code>XDG_SESSION_TYPE</code> 自动检测当前桌面环境:</p>
<pre><code class="language-bash">echo $XDG_SESSION_TYPE
# 输出 "x11" 或 "wayland"
</code></pre>
<hr>
<h2 id="x11-环境">X11 环境</h2>
<h3 id="使用工具xdotool">使用工具:xdotool</h3>
<h3 id="安装">安装</h3>
<pre><code class="language-bash"># Ubuntu/Debian
sudo apt install xdotool

# Fedora
sudo dnf install xdotool

# Arch
sudo pacman -S xdotool
</code></pre>
<h3 id="测试">测试</h3>
<pre><code class="language-bash">xdotool key 0
</code></pre>
<h3 id="无需额外配置">无需额外配置</h3>
<p>X11 下 xdotool 开箱即用,无需守护进程或特殊权限。</p>
<hr>
<h2 id="wayland-环境">Wayland 环境</h2>
<h3 id="使用工具ydotool">使用工具:ydotool</h3>
<h3 id="安装-1">安装</h3>
<pre><code class="language-bash"># Ubuntu/Debian
sudo apt install ydotool

# Fedora
sudo dnf install ydotool

# Arch
sudo pacman -S ydotool
</code></pre>
<h3 id="启动守护进程">启动守护进程</h3>
<p>ydotool 需要后台运行 <code>ydotoold</code> 守护进程:</p>
<pre><code class="language-bash"># 手动启动
sudo ydotoold &amp;

# 或使用 systemd(推荐)
sudo systemctl enable ydotool
sudo systemctl start ydotool
</code></pre>
<h3 id="用户权限">用户权限</h3>
<p>将用户添加到 <code>input</code> 组:</p>
<pre><code class="language-bash">sudo usermod -aG input $USER
# 注销并重新登录生效
</code></pre>
<h3 id="测试-1">测试</h3>
<pre><code class="language-bash"># 数字 0 的 Linux 内核键码是 11
# 格式:keycode:1 (按下) keycode:0 (释放)
ydotool key 11:1 11:0
</code></pre>
<hr>
<p><strong>结语:</strong> 以上是针对Linux用户需要特殊配置的说明,若有其他问题请即时反馈。</p>
<p>资源链接:https://1815368419.v.123pan.cn/1815368419/26969029</p><br><br>
来源:https://www.cnblogs.com/gepai/p/19420809
頁: [1]
查看完整版本: Rime-AI v2版本发布