加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
cardBuild.py 23.42 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
import re
import lark_oapi as lark
from exts import cache
from model import AppCache
# 构建模型切换卡片
def model_select_card_build(user_id: str) -> str:
robot_user_model = get_robot_user_model(user_id)
card = {
"config": {
"enable_forward": False,
"update_multi": False
},
"elements": [
{
"tag": "action",
"actions": [
{
"tag": "select_static",
"placeholder": {
"tag": "plain_text",
"content": ""
},
"value": {
"text": "domain_version"
},
"initial_option": robot_user_model.robot_domain,
"options": [
{
"text": {
"tag": "plain_text",
"content": "spark1.5-chat"
},
"value": "general"
},
{
"text": {
"tag": "plain_text",
"content": "spark2.1-chat"
},
"value": "generalv2"
},
{
"text": {
"tag": "plain_text",
"content": "spark3.1-chat"
},
"value": "generalv3"
}
],
"confirm": {
"title": {
"tag": "plain_text",
"content": "您确定要更改模型吗?"
},
"text": {
"tag": "plain_text",
"content": "选择模型可以让AI更好地理解您的需求"
}
}
}
]
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": "🤖温馨提示:选择内置模型,让AI更好的理解您的需求。"
}
]
}
],
"header": {
"template": "green",
"title": {
"content": "🚀 AI模型切换",
"tag": "plain_text"
}
}
}
return lark.JSON.marshal(card)
# 构建发散切换卡片
def diverge_select_card_build(user_id: str) -> str:
robot_user_model = get_robot_user_model(user_id)
card = {
"config": {
"enable_forward": False,
"update_multi": False
},
"elements": [
{
"tag": "action",
"actions": [
{
"tag": "select_static",
"placeholder": {
"tag": "plain_text",
"content": ""
},
"value": {
"text": "temperature"
},
"initial_option": str(robot_user_model.robot_temperature),
"options": [
{
"text": {
"tag": "plain_text",
"content": "严谨"
},
"value": "1.0"
},
{
"text": {
"tag": "plain_text",
"content": "简洁"
},
"value": "0.75"
},
{
"text": {
"tag": "plain_text",
"content": "标准"
},
"value": "0.5"
},
{
"text": {
"tag": "plain_text",
"content": "发散"
},
"value": "0.25"
}
],
"confirm": {
"title": {
"tag": "plain_text",
"content": "您确定要更改发散模式吗?"
},
"text": {
"tag": "plain_text",
"content": "选择内置模式,可以让AI更好地理解您的需求"
}
}
}
]
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": "🤖温馨提示:选择内置模型,让AI更好的理解您的需求。"
}
]
}
],
"header": {
"template": "blue",
"title": {
"content": "🤖 发散模式选择",
"tag": "plain_text"
}
}
}
return lark.JSON.marshal(card)
# 构建清空上下文卡片
def clear_card_build() -> str:
card = {
"config": {
"enable_forward": False,
"update_multi": False
},
"header": {
"template": "indigo",
"title": {
"content": "🆑 机器人提醒",
"tag": "plain_text"
}
},
"elements": [
{
"tag": "action",
"actions": [
{
"tag": "button",
"text": {
"tag": "plain_text",
"content": "立刻清除"
},
"type": "danger",
"value": {
"text": "clear"
},
"confirm": {
"title": {
"tag": "plain_text",
"content": "您确定要清除对话上下文吗"
},
"text": {
"tag": "plain_text",
"content": "请注意,这将开始一个全新的对话,您将无法利用之前话题的历史消息"
}
}
}
]
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": "🤖温馨提示:请注意,这将开始一个全新的对话,您将无法利用之前话题的历史信息"
}
]
}
]
}
return lark.JSON.marshal(card)
# 构建help卡片
def help_card_build(user_id: str) -> str:
robot_user_model = get_robot_user_model(user_id)
card = {
"config": {
"enable_forward": False,
"update_multi": False
},
"header": {
"template": "blue",
"title": {
"content": "🎒需要帮助吗?",
"tag": "plain_text"
}
},
"elements": [
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": "👋 **你好呀,我是一款基于认知大模型技术的智能聊天机器人!**\n了解更多玩法技巧,请点击右侧「使用说明」👀👀👉"
},
"extra": {
"tag": "button",
"text": {
"tag": "lark_md",
"content": "使用说明"
},
"type": "primary",
"multi_url": {
"url": "https://ginvkqq2ftg.feishu.cn/docx/FVEpdmPzZo7KPaxAeKYcxNTEnSb?from=from_copylink",
"pc_url": "",
"android_url": "",
"ios_url": ""
}
}
},
{
"tag": "hr"
},
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": " 🆑 **清除话题上下文**\n文本回复 清除 或 /clear"
},
"extra": {
"tag": "button",
"text": {
"tag": "lark_md",
"content": "立刻清除"
},
"type": "danger",
"confirm": {
"title": {
"tag": "plain_text",
"content": "您确定要清除对话上下文吗"
},
"text": {
"tag": "plain_text",
"content": "请注意,这将开始一个全新的对话,您将无法利用之前话题的历史消息"
}
},
"value": {
"text": "clear"
}
}
},
{
"tag": "hr"
},
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": "🚀 **AI模型切换**\n文本回复 模型 或 /model"
},
"extra": {
"tag": "select_static",
"placeholder": {
"tag": "plain_text",
"content": ""
},
"value": {
"text": "domain_version"
},
"initial_option": robot_user_model.robot_domain,
"options": [
{
"text": {
"tag": "plain_text",
"content": "spark1.5-chat"
},
"value": "general"
},
{
"text": {
"tag": "plain_text",
"content": "spark2.1-chat"
},
"value": "generalv2"
},
{
"text": {
"tag": "plain_text",
"content": "spark3.1-chat"
},
"value": "generalv3"
}
],
"confirm": {
"title": {
"tag": "plain_text",
"content": "您确定要更改模型吗?"
},
"text": {
"tag": "plain_text",
"content": "选择模型可以让AI更好地理解您的需求"
}
}
}
},
{
"tag": "hr"
},
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": "🤖 **发散模式选择**\n文本回复 发散模式 或 /ai_mode"
},
"extra": {
"tag": "select_static",
"placeholder": {
"tag": "plain_text",
"content": ""
},
"value": {
"text": "temperature"
},
"initial_option": str(robot_user_model.robot_temperature),
"options": [
{
"text": {
"tag": "plain_text",
"content": "严谨"
},
"value": "1.0"
},
{
"text": {
"tag": "plain_text",
"content": "简洁"
},
"value": "0.75"
},
{
"text": {
"tag": "plain_text",
"content": "标准"
},
"value": "0.5"
},
{
"text": {
"tag": "plain_text",
"content": "发散"
},
"value": "0.25"
}
],
"confirm": {
"title": {
"tag": "plain_text",
"content": "您确定要更改发散模式吗?"
},
"text": {
"tag": "plain_text",
"content": "选择内置模式,可以让AI更好地理解您的需求"
}
}
}
},
{
"tag": "hr"
},
{
"tag": "div",
"text": {
"tag": "lark_md",
"content": "🎒 需要更多帮助\n文本回复 帮助 或 /help"
},
"extra": {
"tag": "button",
"text": {
"tag": "lark_md",
"content": "意见反馈"
},
"type": "primary",
"multi_url": {
"url": "https://ginvkqq2ftg.feishu.cn/share/base/form/shrcnbIwSasRyMXXvCbTyDRr1xX",
"android_url": "",
"ios_url": "",
"pc_url": ""
}
}
}
]
}
return lark.JSON.marshal(card)
# 构建通用机器人提醒卡片
def robot_reminder_card_build(header: str, content: str, note: str) -> str:
card = {
"config": {
"enable_forward": False,
"update_multi": False
},
"elements": [
{
"tag": "markdown",
"content": content
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": "🤖温馨提示✨✨:" + note
}
]
}
],
"header": {
"template": "indigo",
"title": {
"content": header,
"tag": "plain_text"
}
}
}
return lark.JSON.marshal(card)
# 根据user_id获取robot_user_model
def get_robot_user_model(user_id: str) -> AppCache:
robot_user_model_json = cache.get(":robot_user_model:" + user_id)
if robot_user_model_json is None:
robot_user_model = AppCache.builder() \
.user_id(user_id) \
.robot_appid("f4317c24") \
.robot_api_secret("ZjZhNGM3YzkwYzJhYzIwYjUxYjk3ZDMx") \
.robot_api_key("750605805c6e5191737087ec504f600d") \
.robot_domain("spark3.1-chat") \
.robot_spark_url("ws://spark-api.xf-yun.com/v3.1/chat") \
.robot_temperature(0.5) \
.build()
cache.set(":robot_user_model:" + user_id, robot_user_model.to_dict())
else:
robot_user_model = AppCache(robot_user_model_json)
return robot_user_model
# 构建卡片
def build_card(header: str, time: str, content: str, end: bool, robot: bool) -> str:
if content:
content = re.sub(r'(?m)^(.*)$', r'**\1**', content)
elif robot:
card = {
"elements": [
{
"tag": "markdown",
"content": content,
"text_align": "left"
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": "正在思考,请稍等..."
}
]
}
]
}
return lark.JSON.marshal(card)
if robot:
if end:
note = "🤖温馨提示✨✨:输入<帮助> 或 /help 即可获取帮助菜单"
else:
note = "正在处理中,请稍等..."
card = {
"elements": [
{
"tag": "markdown",
"content": content,
"text_align": "left"
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": note
}
]
}
]
}
return lark.JSON.marshal(card)
if end:
card = {
"elements": [
{
"tag": "column_set",
"flex_mode": "none",
"background_style": "default",
"columns": [
{
"tag": "column",
"width": "weighted",
"weight": 1,
"vertical_align": "top",
"elements": [
{
"tag": "div",
"text": {
"content": "**🕐 完成时间:**\n" + time,
"tag": "lark_md"
}
},
{
"tag": "markdown",
"content": content,
"text_align": "left"
}
]
}
]
},
{
"tag": "column_set",
"flex_mode": "none",
"background_style": "default",
"columns": []
},
{
"tag": "hr"
},
{
"tag": "div",
"fields": [
{
"is_short": True,
"text": {
"tag": "lark_md",
"content": "**📝祝您生活愉快**"
}
}
]
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": "🤖温馨提示✨✨:输入<帮助> 或 /help 即可获取帮助菜单"
}
]
}
],
"header": {
"template": "violet",
"title": {
"content": header,
"tag": "plain_text"
}
}
}
return lark.JSON.marshal(card)
card = {
"elements": [
{
"tag": "column_set",
"flex_mode": "none",
"background_style": "default",
"columns": [
{
"tag": "column",
"width": "weighted",
"weight": 1,
"vertical_align": "top",
"elements": [
{
"tag": "div",
"text": {
"content": "**🕐 响应时间:**\n" + time,
"tag": "lark_md"
}
},
{
"tag": "markdown",
"content": content,
"text_align": "left"
}
]
}
]
},
{
"tag": "column_set",
"flex_mode": "none",
"background_style": "default",
"columns": []
},
{
"tag": "action",
"actions": [
{
"tag": "button",
"text": {
"tag": "plain_text",
"content": "赞一下"
},
"type": "primary",
"value": {
"success": True,
"text": "praise"
}
},
{
"tag": "button",
"text": {
"tag": "plain_text",
"content": "踩一下"
},
"type": "danger",
"value": {
"success": False,
"text": "praise"
}
}
]
},
{
"tag": "hr"
},
{
"tag": "note",
"elements": [
{
"tag": "plain_text",
"content": "🤖温馨提示✨✨:输入<帮助> 或 /help 即可获取帮助菜单"
}
]
}
],
"header": {
"template": "violet",
"title": {
"content": header,
"tag": "plain_text"
}
}
}
return lark.JSON.marshal(card)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化