From 48750824338591a0346337b993589b8556fa479f Mon Sep 17 00:00:00 2001 From: Alter-xyz <88554920+alterxyz@users.noreply.github.com> Date: Wed, 15 May 2024 01:54:16 -0400 Subject: [PATCH 1/2] feat: dify chatbot --- handlers/dify.py | 110 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 handlers/dify.py diff --git a/handlers/dify.py b/handlers/dify.py new file mode 100644 index 0000000..058a058 --- /dev/null +++ b/handlers/dify.py @@ -0,0 +1,110 @@ +from os import environ +import time + +from telebot import TeleBot +from telebot.types import Message + +from . import * + +# TODO: update requirements.txt and setup tools +# pip install dify-client +from dify_client import ChatClient +from telegramify_markdown import convert +from telegramify_markdown.customize import markdown_symbol + +# If you want, Customizing the head level 1 symbol +markdown_symbol.head_level_1 = "📌" +markdown_symbol.link = "🔗" # If you want, Customizing the link symbol + +DIFY_API_KEY = environ.get("DIFY_API_KEY") + +if DIFY_API_KEY: + client = ChatClient(api_key=DIFY_API_KEY) + +# Global history cache +dify_player_dict = {} +dify_player_c = {} + + +def dify_handler(message: Message, bot: TeleBot) -> None: + """dify : /dify """ + m = message.text.strip() + c = None + player_message = [] + # restart will lose all TODO + if str(message.from_user.id) not in dify_player_dict: + dify_player_dict[str(message.from_user.id)] = ( + player_message # for the imuutable list + ) + else: + player_message = dify_player_dict[str(message.from_user.id)] + # get c from dify_player_c + c = dify_player_c.get(str(message.from_user.id), None) + + if m.strip() == "clear": + bot.reply_to( + message, + "just clear your dify messages history", + ) + player_message.clear() + c = None + return + + if m[:4].lower() == "new ": + m = m[4:].strip() + player_message.clear() + c = None + + m = enrich_text_with_urls(m) + + who = "dify" + # show something, make it more responsible + reply_id = bot_reply_first(message, who, bot) + + player_message.append({"role": "user", "content": m}) + # keep the last 5, every has two ask and answer. + if len(player_message) > 10: + player_message = player_message[2:] + + dify_reply_text = "" + try: + r = client.create_chat_message( + inputs={}, + query=m, + user=str(message.from_user.id), + response_mode="blocking", + conversation_id=c, + ) + j = r.json() + + content = j.get("answer", None) + # get c by j.get then save c to dify_player_c + dify_player_c[str(message.from_user.id)] = j.get("conversation_id", None) + if not content: + dify_reply_text = f"{who} did not answer." + player_message.pop() + else: + dify_reply_text = content + player_message.append( + { + "role": "assistant", + "content": dify_reply_text, + } + ) + + except Exception as e: + print(e) + bot_reply_markdown(reply_id, who, "answer wrong", bot) + # pop my user + player_message.pop() + return + + # reply back as Markdown and fallback to plain text if failed. + bot_reply_markdown(reply_id, who, dify_reply_text, bot) + + +if DIFY_API_KEY: + + def register(bot: TeleBot) -> None: + bot.register_message_handler(dify_handler, commands=["dify"], pass_bot=True) + bot.register_message_handler(dify_handler, regexp="^dify:", pass_bot=True) From ae8031bb2b794d6a90cb713cc8e02350b93fed1c Mon Sep 17 00:00:00 2001 From: Alter-xyz <88554920+alterxyz@users.noreply.github.com> Date: Fri, 17 May 2024 23:14:36 -0400 Subject: [PATCH 2/2] feat: dify chatbot --- README.md | 7 +++++++ handlers/dify.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 762944f..33625f9 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,13 @@ Note, if you are using third party service, you need to `export OPENAI_API_BASE= 2. export TOGETHER_API_KEY=${the_key} 3. use `qwen_pro: ${message}` to ask +## Bot -> dify + +1. visit https://cloud.dify.ai/ get selected Chatbot's API Secret key +2. export DIFY_API_KEY=${the_key} +3. use `dify: ${message}` to ask + +Note, currently its support dify Chatbot with instructions(System prompt) and different MODEL with its parameters. ## HOW TO Install and Run diff --git a/handlers/dify.py b/handlers/dify.py index 058a058..7cbd6ac 100644 --- a/handlers/dify.py +++ b/handlers/dify.py @@ -23,7 +23,7 @@ if DIFY_API_KEY: # Global history cache dify_player_dict = {} -dify_player_c = {} +dify_player_c = {} # History cache is supported by dify cloud conversation_id. def dify_handler(message: Message, bot: TeleBot) -> None: