Skip to content

Commit

Permalink
Merge pull request #424 from LlmKira/main
Browse files Browse the repository at this point in the history
release image
  • Loading branch information
sudoskys authored Oct 13, 2024
2 parents e8b6226 + ccecd13 commit b918510
Show file tree
Hide file tree
Showing 16 changed files with 1,310 additions and 1,154 deletions.
12 changes: 12 additions & 0 deletions .nerve.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
contributor = "b4e0cd99-289b-4586-992c-6f159c436101"
# https://github.com/LlmKira/contributor/blob/main/.nerve.toml

language = "English"
issue_auto_label = true
# Whether to automatically label issues
issue_title_format = true
# Whether to use the default issue title format
issue_body_format = false
# Whether to use the default issue body format
issue_close_with_report = true
# Whether to close the issue with a report
8 changes: 4 additions & 4 deletions app/components/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Credential(BaseModel):
api_key: str
api_endpoint: str
api_model: str
api_tool_model: str = "gpt-3.5-turbo"
api_tool_model: str = "gpt-4o-mini"

@classmethod
def from_provider(cls, token, provider_url):
Expand All @@ -36,7 +36,7 @@ def from_provider(cls, token, provider_url):
api_key=user_data["api_key"],
api_endpoint=user_data["api_endpoint"],
api_model=user_data["api_model"],
api_tool_model=user_data.get("api_tool_model", "gpt-3.5-turbo"),
api_tool_model=user_data.get("api_tool_model", "gpt-4o-mini"),
)


Expand All @@ -47,8 +47,8 @@ def from_provider(cls, token, provider_url):
global_credential = Credential(
api_key=os.getenv("GLOBAL_OAI_KEY"),
api_endpoint=os.getenv("GLOBAL_OAI_ENDPOINT"),
api_model=os.getenv("GLOBAL_OAI_MODEL", "gpt-3.5-turbo"),
api_tool_model=os.getenv("GLOBAL_OAI_TOOL_MODEL", "gpt-3.5-turbo"),
api_model=os.getenv("GLOBAL_OAI_MODEL", "gpt-4o-mini"),
api_tool_model=os.getenv("GLOBAL_OAI_TOOL_MODEL", "gpt-4o-mini"),
)
else:
global_credential = None
16 changes: 15 additions & 1 deletion app/sender/discord/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
save_credential,
dict2markdown,
learn_instruction,
logout,
)
from llmkira.openapi.trigger import get_trigger_loop
from ...components.credential import Credential, ProviderError
Expand Down Expand Up @@ -286,7 +287,7 @@ async def listen_endpoint_command(
api_endpoint: str,
api_key: str,
api_model: str,
api_tool_model: str = "gpt-3.5-turbo",
api_tool_model: str = "gpt-4o-mini",
):
try:
credential = Credential(
Expand Down Expand Up @@ -319,6 +320,19 @@ async def listen_endpoint_command(
ephemeral=True,
)

@client.include
@crescent.command(
dm_enabled=True, name="logout", description="clear your credential"
)
async def listen_logout_command(ctx: crescent.Context):
reply = await logout(
uid=uid_make(__sender__, ctx.user.id),
)
return await ctx.respond(
ephemeral=True,
content=reply,
)

@client.include
@crescent.command(
dm_enabled=True, name="clear", description="clear your message history"
Expand Down
3 changes: 2 additions & 1 deletion app/sender/discord/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ def help_message():
`/tool` - Check all useful tools
`/clear` - wipe memory of your chat
`/auth` - activate a task (my power)
`/login` - login
`/login` - set credential
`/logout` - clear credential
`/login_via_url` - login via url
`/env` - set environment variable, split by ; , use `/env ENV=NONE` to disable a env.
`/learn` - set your system prompt, reset by `/learn reset`
Expand Down
14 changes: 12 additions & 2 deletions app/sender/kook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
save_credential,
dict2markdown,
learn_instruction,
logout,
)
from llmkira.openapi.trigger import get_trigger_loop
from ...components.credential import ProviderError, Credential
Expand Down Expand Up @@ -296,8 +297,8 @@ async def listen_login_command(
msg: Message,
api_endpoint: str,
api_key: str,
api_model: str = "gpt-3.5-turbo",
api_tool_model: str = "gpt-3.5-turbo",
api_model: str = "gpt-4o-mini",
api_tool_model: str = "gpt-4o-mini",
):
try:
credential = Credential(
Expand Down Expand Up @@ -331,6 +332,15 @@ async def listen_login_command(
type=MessageTypes.KMD,
)

@bot.command(name="logout")
async def listen_logout_command(msg: Message):
reply = await logout(uid=uid_make(__sender__, msg.author_id))
return await msg.reply(
content=convert(reply),
is_temp=True,
type=MessageTypes.KMD,
)

@bot.command(name="clear")
async def listen_clear_command(msg: Message):
await global_message_runtime.update_session(
Expand Down
3 changes: 2 additions & 1 deletion app/sender/kook/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def help_message():
`/tool` - Check all useful tools
`/clear` - wipe memory of your chat
`/auth` - activate a task (my power)
`/login` - login openai
`/login` - set credential
`/logout` - clear credential
`/login_via_url` - login via provider url
`/env` - set environment variable, split by ; , use `/env ENV=NONE` to disable a env.
`/learn` - set your system prompt, reset by `/learn reset`
Expand Down
11 changes: 11 additions & 0 deletions app/sender/slack/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
login,
dict2markdown,
learn_instruction,
logout,
)
from app.setting.slack import BotSetting
from llmkira.kv_manager.env import EnvManager
Expand Down Expand Up @@ -248,6 +249,16 @@ async def listen_login_command(ack: AsyncAck, respond: AsyncRespond, command):
)
return await respond(text=reply)

@bot.command(command="/logout")
async def listen_logout_command(ack: AsyncAck, respond: AsyncRespond, command):
command: SlashCommand = SlashCommand.model_validate(command)
await ack()
if not command.text:
return
_arg = command.text
reply = await logout(uid=uid_make(__sender__, command.user_id))
return await respond(text=reply)

@bot.command(command="/env")
async def listen_env_command(ack: AsyncAck, respond: AsyncRespond, command):
command: SlashCommand = SlashCommand.model_validate(command)
Expand Down
1 change: 1 addition & 0 deletions app/sender/slack/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def help_message():
`/clear` - forget...you
`/auth` - activate a task (my power),but outside the thread
`/login` - login via url or raw
`/logout` - clear credential
`/env` - set environment variable, split by ; , use `/env ENV=NONE` to disable a env.
`/learn` - set your system prompt, reset by `/learn reset`
Expand Down
27 changes: 23 additions & 4 deletions app/sender/telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# @Software: PyCharm
from typing import Optional, Union, List

import telegramify_markdown
from loguru import logger
from telebot import formatting, util
from telebot import types
from telebot.async_telebot import AsyncTeleBot
from telebot.asyncio_storage import StateMemoryStorage
from telebot.formatting import escape_markdown
from telegramify_markdown import convert

from app.sender.util_func import (
Expand All @@ -23,6 +23,7 @@
TimerObjectContainer,
dict2markdown,
learn_instruction,
logout,
)
from app.setting.telegram import BotSetting
from llmkira.kv_manager.env import EnvManager
Expand Down Expand Up @@ -69,7 +70,15 @@ async def transcribe(
event_messages = []
files = [file for file in files if file] # No None
for index, message in enumerate(messages):
message_text = getattr(message, "text", "empty")
message_text = (
(
getattr(message, "text", None)
or getattr(message, "caption", None)
or "empty"
)
if message is not None
else "empty"
)
event_messages.append(
EventMessage(
chat_id=str(message.chat.id),
Expand Down Expand Up @@ -251,6 +260,17 @@ async def listen_login_command(message: types.Message):
parse_mode="MarkdownV2",
)

@bot.message_handler(commands="logout", chat_types=["private"])
async def listen_logout_command(message: types.Message):
logger.debug("Debug:logout command")
_cmd, _arg = parse_command(command=message.text)
reply = await logout(uid=uid_make(__sender__, message.from_user.id))
await bot.reply_to(
message,
text=reply,
parse_mode="MarkdownV2",
)

@bot.message_handler(commands="env", chat_types=["private"])
async def listen_env_command(message: types.Message):
_cmd, _arg = parse_command(command=message.text)
Expand Down Expand Up @@ -299,8 +319,7 @@ async def listen_help_command(message: types.Message):
_message = await bot.reply_to(
message,
text=formatting.format_text(
formatting.mbold("🥕 Help"),
escape_markdown(help_message()),
telegramify_markdown.convert(help_message()),
separator="\n",
),
parse_mode="MarkdownV2",
Expand Down
28 changes: 16 additions & 12 deletions app/sender/telegram/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@

def help_message():
return """
/help - show help message
/chat - just want to chat with me
/task - chat with function_enable
/ask - chat with function_disable
/tool - check all useful tools
/clear - clear the chat history
/auth - auth the tool_call
/learn - set your system prompt, reset by `/learn reset`
# Command List
Private Chat Only:
/login - login via url or something
/env - set v-env split by ; , use `/env ENV=NONE` to disable a env.
`/help` - show help message
`/chat` - just want to chat with me
`/task` - chat with function_enable
`/ask` - chat with function_disable
`/tool` - check all useful tools
`/clear` - clear the chat history
`/auth` - auth the tool_call
`/learn` - set your system prompt, reset by `/learn reset`
!Please confirm that that bot instance is secure, some plugins may be dangerous on unsafe instance.
**Private Chat Only**
`/login` - login via url or something
`/logout` - clear credential
`/env` - set v-env split by ; , use `/env ENV=NONE` to disable a env.
> Please confirm that that bot instance is secure, some plugins may be dangerous on unsafe instance, wink~
"""
16 changes: 14 additions & 2 deletions app/sender/util_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ async def login(uid: str, arg_string) -> str:
error = telegramify_markdown.convert(
"🔑 **Incorrect format.**\n"
"You can set it via `/login https://<something api.openai.com>/v1$<api key>"
"$<model such as gpt-4-turbo>$<tool_model such as gpt-3.5-turbo>` format, "
"$<model such as gpt-4-turbo>$<tool_model such as gpt-4o-mini>` format, "
"or you can log in via URL using `/login token$https://provider.com`.\n"
"Use $ to separate the parameters."
)
Expand Down Expand Up @@ -202,7 +202,7 @@ async def login(uid: str, arg_string) -> str:
if len(settings) == 4:
api_tool_model = settings[3]
else:
api_tool_model = "gpt-3.5-turbo"
api_tool_model = "gpt-4o-mini"
credential = Credential(
api_endpoint=api_endpoint,
api_key=api_key,
Expand All @@ -220,6 +220,18 @@ async def login(uid: str, arg_string) -> str:
return error


async def logout(uid: str) -> str:
"""
Logout
:param uid: uid_make
:return: str message
"""
user = await USER_MANAGER.read(user_id=uid)
user.credential = None
await USER_MANAGER.save(user_model=user)
return telegramify_markdown.convert("Logout success! Welcome back master!")


class TimerObjectContainer:
def __init__(self):
self.users = {}
Expand Down
Empty file added app/setting/whitelist.py
Empty file.
43 changes: 25 additions & 18 deletions llmkira/extra/voice_hook.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import List

from fast_langdetect import parse_sentence
from fast_langdetect import detect_multilingual
from loguru import logger

from llmkira.extra.voice import request_cn, request_en
Expand All @@ -11,23 +11,35 @@
from llmkira.task.schema import EventMessage, Location


def check_string(text):
def detect_text(text: str) -> list:
"""
检查字符串是否符合要求
:param text: 字符串
:return: 是否符合要求
检测文本的语言
:param text: 文本
:return: 语言
"""
parsed_text = parse_sentence(text)
text = text.replace("\n", "")
text = text[:200]
parsed_text = detect_multilingual(text)
if not parsed_text:
return False
return []
lang_kinds = []
for lang in parsed_text:
if lang.get("lang", "RU") not in ["ZH", "EN"]:
return False
lang_kinds.append(lang.get("lang"))
lang_ = lang.get("lang", None)
if lang_:
lang_kinds.append(lang_)
return lang_kinds


def check_string(text):
"""
检查字符串是否 TTS 可以处理
:param text: 字符串
:return: 是否符合要求
"""
lang_kinds = detect_text(text)
limit = 200
if len(set(lang_kinds)) == 1:
if lang_kinds[0] in ["EN"]:
if lang_kinds[0] in ["en"]:
limit = 500
if "\n\n" in text or text.count("\n") > 3 or len(text) > limit or "```" in text:
return False
Expand Down Expand Up @@ -59,16 +71,11 @@ async def hook_run(self, *args, **kwargs):
for message in messages:
if not check_string(message.text):
return args, kwargs
parsed_text = parse_sentence(message.text)
if not parsed_text:
return args, kwargs
lang_kinds = []
for lang in parsed_text:
lang_kinds.append(lang.get("lang"))
lang_kinds = detect_text(message.text)
reecho_api_key = await EnvManager(locate.uid).get_env(
"REECHO_VOICE_KEY", None
)
if (len(set(lang_kinds)) == 1) and (lang_kinds[0] in ["EN"]):
if (len(set(lang_kinds)) == 1) and (lang_kinds[0] in ["en"]):
voice_data = await request_en(message.text)
else:
voice_data = await request_cn(
Expand Down
Loading

0 comments on commit b918510

Please sign in to comment.