Skip to content

Commit

Permalink
add exception output
Browse files Browse the repository at this point in the history
  • Loading branch information
KroMiose committed Mar 12, 2024
1 parent 040f167 commit eb6318b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ DRIVER=~fastapi
HOST=127.0.0.1 # 配置 NoneBot2 监听的 IP/主机名
PORT=8000 # 配置 NoneBot2 监听的端口
COMMAND_START=[""] # 配置指令起始符
LOG_LEVEL=DEBUG
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ nb plugin install nonebot-plugin-dice-narrator

![alt text](docs/images/example.png)

> 注意: 你可以检定任意内容的文本,但不是所有文本都能得到良好的反馈,建议内容文本为一段可执行的任务,同时可以附上一些简要的任务背景概况;避免使用疑问句、无意义短句等意义不明的语句。
## 开发指南

如果你想为 Dice Narrator 贡献代码,可以参考以下步骤:
Expand Down
9 changes: 6 additions & 3 deletions nonebot_plugin_dice_narrator/matchers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Type

from nonebot import on_command
from nonebot.adapters import Message
from nonebot.adapters import Bot, Message
from nonebot.adapters.onebot.v11 import (
MessageEvent,
)
Expand All @@ -11,6 +11,7 @@

from nonebot_plugin_dice_narrator import config
from nonebot_plugin_dice_narrator.narrator import run_narrator
from nonebot_plugin_dice_narrator.utils.message_parse import gen_chat_text


def register_matcher():
Expand All @@ -27,6 +28,7 @@ def register_matcher():
async def _(
matcher: Matcher,
event: MessageEvent,
bot: Bot,
arg: Message = CommandArg(),
):
global is_progress # 是否产生编辑进度
Expand All @@ -38,7 +40,8 @@ async def _(
)

raw_cmd: str = arg.extract_plain_text()
logger.info(f"接收到指令: {raw_cmd}")
content, _ = await gen_chat_text(event=event, bot=bot)
logger.info(f"接收到指令: {raw_cmd} | Parsed: {content}")

if raw_cmd:
await run_narrator(question=raw_cmd, matcher=matcher)
await run_narrator(question=content[2:].strip(), matcher=matcher)
65 changes: 39 additions & 26 deletions nonebot_plugin_dice_narrator/narrator.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import random
import re

from nonebot import logger
from nonebot.matcher import Matcher

from nonebot_plugin_dice_narrator.config import config
from nonebot_plugin_dice_narrator.utils.common import read_yml_str2data
from nonebot_plugin_dice_narrator.utils.openai_funcs import gen_chat_response_text

SYSTEM_PROMPT = f"""
你是一个游戏DM,接下来用户会向你描述一项玩家任务,并进行一次掷骰,请给出这个任务的执行难度(1~{config.DIFFICULTY_DICE_MAX}),需要说明具体的**玩家行为描述**、四种掷骰结果对应的**事件描述**
你是一个游戏DM,接下来用户会向你描述一项玩家任务,并进行一次掷骰,请根据你的对这个任务的判断,给出对于这个任务**相对客观**的执行难度(1~{config.DIFFICULTY_DICE_MAX}),需要说明具体的**玩家行为描述**、四种掷骰结果对应的**事件描述**:
大成功(completed_success)掷骰结果为{config.DIFFICULTY_DICE_MAX}
成功(success)掷骰结果不小于任务难度
失败(failure)掷骰结果小于任务难度
大失败(critical_failure)掷骰结果为1
大成功(completed_success): 掷骰结果为{config.DIFFICULTY_DICE_MAX}
成功(success): 掷骰结果不小于任务难度
失败(failure): 掷骰结果小于任务难度
大失败(critical_failure): 掷骰结果为1
示例如下
示例如下:
检定任务
检定任务:
逃离触手怪的束缚
你的响应内容格式如下:
```yaml
difficulty: {int(config.DIFFICULTY_DICE_MAX*0.75)}
completed_success: 你展现出了惊人的协作和勇气。巧妙地躲过了触手怪的攻击,找到了一个隐藏的通道,成功逃离了触手怪的魔爪。你喘着粗气,但胜利的喜悦填满了你的心。
completed_success: 你展现出了惊人的技巧和勇气。巧妙地躲过了触手怪的攻击,找到了一个隐藏的通道,成功逃离了触手怪的魔爪。你喘着粗气,但胜利的喜悦填满了你的心。
success: 你奋力抵挡住了触手怪的袭击,成功拉开了距离。虽然你没有完全逃脱,但至少还活着,可以继续寻找其他出路。
failure: 触手怪的一只触手抓住了你,你被拖入了一个黑暗的洞穴。你感到窒息和恐惧,而队友们无能为力。任务失败,你们成为了怪物的俘虏
critical_failure: 你踩到了一块松动的地板,发出了巨大的声响。触手怪立刻察觉到了你的位置,它的触手缠绕住你,将你提起,然后狠狠地摔在地上。你昏迷了过去,不再感知周围的世界
failure: 在昏暗的洞穴中,触手怪的柔滑触手紧紧缠绕着你的身体。你感到了一种异样的窒息与兴奋交织在一起,而队友们只能眼睁睁地看着。任务虽然失败了,但你似乎成为了怪物欲望的俘虏
critical_failure: 你不慎触动了隐藏的机关,引来了触手怪贪婪的目光。它那强有力的触手将你高高举起,在空中展示你无助的姿态后猛地摔向地面。在那一瞬间,意识模糊之际,你感到自己被未知快感淹没
```
特别地,如果玩家给出的任务描述不明确,你可以适当地任意假设其发生的背景
Expand All @@ -42,19 +43,26 @@
检定任务: {question}
目标难度: {difficulty}/{difficulty_max}
检定结果: {dice_num} [{status}]
======
========
{event_content}
"""


async def run_narrator(question: str, matcher: Matcher):
assert question, "question is empty"
res, _ = await gen_chat_response_text(
messages=[
{"role": "system", "content": SYSTEM_PROMPT.strip()},
{"role": "user", "content": USER_PROMPT.strip().format(question=question)},
],
)
try:
res, _ = await gen_chat_response_text(
messages=[
{"role": "system", "content": SYSTEM_PROMPT.strip()},
{
"role": "user",
"content": USER_PROMPT.strip().format(question=question),
},
],
)
except Exception as e:
logger.error(f"与 OpenAI 通信发生错误: {e}")
await matcher.finish("哎呀!与 OpenAI 通信发生错误,请稍后再试 (┬┬﹏┬┬)")

# 提取并解析 yaml
try:
Expand All @@ -77,13 +85,18 @@ async def run_narrator(question: str, matcher: Matcher):
else:
res_key, status = "success", "成功"

print(res_data)
logger.debug(f"检定: `{question}` | 原始返回: {res}")

await matcher.finish(RESPONSE_STRUCT.strip().format(
question=question,
difficulty=int(res_data["difficulty"]),
difficulty_max=config.DIFFICULTY_DICE_MAX,
dice_num=dice_num,
event_content=res_data[res_key],
status=status,
))
try:
await matcher.finish(
RESPONSE_STRUCT.strip().format(
question=question,
difficulty=int(res_data["difficulty"]),
difficulty_max=config.DIFFICULTY_DICE_MAX,
dice_num=dice_num,
event_content=res_data[res_key],
status=status,
),
)
except KeyError:
await matcher.finish("检定失败 >﹏< 请检查任务描述是否合理!")

0 comments on commit eb6318b

Please sign in to comment.