Typora接入LLM

给大佬的项目[obgnail/typora_plugin: Typora plugin. Feature enhancement tool | Typora 插件,功能增强工具]和博客园地址Typora高级用法 - 小呆呆不爱睡觉 - 博客园引流。

在大佬项目的基础上编写LLM插件。

  1. 通过https://github.com/obgnail/typora_plugin?tab=readme-ov-file安装好typora插件。

  2. {your install folder}\Typora\resources\plugin\global\settings\custom_plugin.user.toml,下复制粘贴,apiKeyurlmodel,修改为自己的版本。这里是阿里云百炼版本。

    [CompleteWithLLM]
    name = "使用LLM补全"
    enable = true
    hide = false
    order = 1
    hotkey_string = "ctrl+shift+H"
    apiKey = "{your api_key}"
    url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions"
    model = "deepseek-v3"
    prompt = '''
    Please read the input text, and follow these instructions:
    1. Optimize and complete the input text. 
    2. The language of the output should be consistent with the language of the input text, even if it is in a mixed-language form.
    3. Don't provide any additional explanation before or after the answer and output directly.
    '''
    
  3. {your install folder}\Typora\resources\plugin\custom\plugins下新建文件CompleteWithLLM.js,复制粘贴。

    // ./plugin/custom/plugins/CompleteWithLLM.js
    
    class CompleteWithLLM extends BaseCustomPlugin {
        // 定义插件的快捷键
        hotkey = () => [this.config.hotkey_string]
    
        // 定义插件的提示信息
        hint = () => "使用 LLM 补全选中的文本"
    
        // 定义插件的初始化逻辑
        init = () => {
            // 初始化变量
            this.selectedText = "";
        }
    
        // 定义插件的样式
        style = () => `
            #complete-with-llm-plugin {
                margin: 10px;
            }
        `
    
        // 定义插件的 HTML 结构
        html = () => "
    "
    // 定义插件的处理逻辑 process = () => { // 这里可以添加一些初始化逻辑 } callback = async anchorNode => { // 获取用户选中的文本 this.selectedText = window.getSelection().toString().replace(/\n\n+/g, '\n'); if (!this.selectedText) { alert("请先选中一段文字"); return; } try { const newContent = await this.sendToLLM(this.selectedText); // 将新内容插入到原内容的下一行 console.log(`inserting the newContent ${newContent}`) // this.insertNewContent(newContent); this.utils.insertText(anchorNode, newContent) } catch (error) { console.error("LLM API 请求失败:", error); alert("LLM API 请求失败,请检查网络或 API 配置"); } } sendToLLM = async function(text) { const {apiKey, url, model, prompt} = this.config; const data = { model: model, messages: [ {"role": "system", "content": prompt}, {"role": "user", "content": `input text: ${text}`} ], stream: false, temperature: 0.01 }; try { console.log(JSON.stringify(data)) const response = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${apiKey}` }, body: JSON.stringify(data), }); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } const responseData = await response.json(); if (responseData.choices && responseData.choices.length > 0) { return responseData.choices[0].message.content; } else { return "No response content found."; } } catch (error) { console.error("Error in chatting with LLM:", error.message); return `Error: ${error.message}`; } } } // 导出插件 module.exports = { plugin: CompleteWithLLM };
  4. 重启Typora,选中文字,右键→常用插件→二级插件→使用LLM补全。也可以使用快捷键ctrl+shift+H。回答内容会自动出现在下一行,自行选择保存那个版本。

你可能感兴趣的:(学习方法)