Skip to main content

Nonebot2 plugin for using llm

Project description

nonebot

nonebot-plugin-anywhere-llm

为你的 nonebot2 插件提供 LLM 接口

license Python NoneBot

✨ 特性

  • 🚀 快速:开箱即用的LLM集成能力
  • 🛠️ 灵活性:基于文件的 config 设置,方便地对不同群、用户配置不同选项
  • 🏗️ 易用性:简单的 API 设计,方便上手
  • ☁️ 环境感知:自带时间、天气等信息的动态注入,后续提供更多的环境信息注入

📦 安装

方式 1:通过 pip 安装

pip install nonebot-plugin-anywhere-llm

🚀 快速使用

配置

.env

OPENAI_API_KEY="sk-ivwnsnscugorsxqvncgbysxkcsnkccwagebmdqoluuwjlkmk"
OPENAI_BASE_URL="https://api.siliconflow.cn/v1"   # 代理地址
OPENAI_MODEL="deepseek-ai/DeepSeek-R1-Distill-Qwen-7B"

LOCALSTORE_USE_CWD="true" # localstore 设置, 建议设置为 true

快速使用

LLMService = require('nonebot_anywhere_llm').LLMService
llm = LLMService() # 默认配置


test_matcher = on_command("test")
@test_matcher.handle()
async def handle_ask(matcher: Matcher, event: MessageEvent):  
    output = await llm.generate('回复测试', event)
    await matcher.finish(output)

通过文件加载

llm = LLMService.load('my_config.yaml') # 基于文件
llm.save('my_config.yaml') # 保存到 nonebot 插件的 config 目录, 跳过 api_key

文件中缺失的参数会使用默认值

动态参数设置

LLMService = require('nonebot_anywhere_llm').LLMService
llm = LLMService()
print(llm.to_dict())
llm.config.params.temperature = 0.5

system_prompt 模板设置与渲染说明

config.system_prompt下的所有选项接受字符串与文件,其中 template 将在 <current_working_dir>/data/template下查找,其他会在<current_working_dir>/data/avatar下查找
假设通过 .load(my_config) 加载配置

# my_config.yaml
...

system_prompt:
  template: template.md
  avatar: atri.md
  name: ...
  age: ...

构建system_prompt时会对 template 进行渲染
template.md

## 你的任务是根据[角色信息]扮演角色...

### 姓名 ${name}
### 年龄 ${age}
...

## 角色信息
${avatar}

...

system_prompttemplate 外的其他选项渲染到template中, 程序会自动检查目录下对应名字的.md .txt

时间感知与天气感知

# 设置config.md 或者代码中调整
llm.config.messages.injections.time= 123 # 默认 1
llm.config.messages.injections.weather= 1 # 没做 默认 0

需要在config.system_prompt设置对应的${}

...
## 当前时间 ${time}
## 天气 ${weather}

TODO

  • 更具体的时间信息 (工作日、假期、节假日)
  • 天气、地点感知
  • 更适合群聊的config设计

📜 许可证

本项目基于 MIT License 许可证发布。

💡 喜欢这个项目?欢迎 Star⭐,让更多人看到!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonebot_plugin_anywhere_llm-1.1.6.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nonebot_plugin_anywhere_llm-1.1.6-py3-none-any.whl (15.0 kB view details)

Uploaded Python 3

File details

Details for the file nonebot_plugin_anywhere_llm-1.1.6.tar.gz.

File metadata

  • Download URL: nonebot_plugin_anywhere_llm-1.1.6.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.11.8 Linux/6.8.0-57-generic

File hashes

Hashes for nonebot_plugin_anywhere_llm-1.1.6.tar.gz
Algorithm Hash digest
SHA256 4a0b4a2155ecfe37af2f6e867926c0533b0fdb9646f5318d1f46854df62c3531
MD5 c238022d8376def7211a6d8575999427
BLAKE2b-256 59946ba4e9e55e02a5522f1185485b5d5481908bf00b0efe69848335ac75d267

See more details on using hashes here.

File details

Details for the file nonebot_plugin_anywhere_llm-1.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for nonebot_plugin_anywhere_llm-1.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ef74aaac2f76e4012eb6d097f63649c771d5524e68b98913dab74effcfc8bf55
MD5 1c9a466291773c44e871478a332813c6
BLAKE2b-256 97aa0e056e5da3f1f6266ba74f87f1a5a3950061757139d70cd00410b68360d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page