Skip to content

Commit

Permalink
Merge pull request #413 from LlmKira/main
Browse files Browse the repository at this point in the history
Build Docker
  • Loading branch information
sudoskys authored May 21, 2024
2 parents b8fe5ca + 30e88c1 commit e8b6226
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 299 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,15 @@ Use `docker exec -it llmbot /bin/bash` to view Shell in Docker, enter `exit` to

```shell
clear - Deletes chat records
login - Login to the bot
help - Displays documentation
chat - Conversation
task - Use a function to converse
ask - Disable function-based conversations
tool - Lists all functions
login - Login
auth - Authorize a function
env - Environment variables of the function
learn - Learn your instructions, /learn reset to clear
```

## 💻 How to Develop Plugins?
Expand Down
5 changes: 4 additions & 1 deletion app/middleware/llm_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ async def request_openai(
# TODO:实现消息时序切片
# 日志
logger.info(
f"[x] Openai request" f"\n--message {len(messages)} " f"\n--tools {tools}"
f"Request Details:"
f"\n--message {len(messages)} "
f"\n--tools {tools} "
f"\n--model {credential.api_model}"
)
for msg in messages:
if isinstance(msg, UserMessage):
Expand Down
2 changes: 1 addition & 1 deletion app/receiver/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ async def run_pending_task(task: TaskHeader, pending_task: ToolCall):
if len(task.task_sign.tool_calls_pending) == 0:
if not has_been_called_recently(userid=task.receiver.uid, n_seconds=3):
credentials = await read_user_credential(user_id=task.receiver.uid)
if global_credential:
if global_credential and not credentials:
credentials = global_credential
logic = LLMLogic(
api_key=credentials.api_key,
Expand Down
9 changes: 7 additions & 2 deletions app/receiver/receiver_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from app.components.credential import global_credential
from app.middleware.llm_task import OpenaiMiddleware
from llmkira.kv_manager.env import EnvManager
from llmkira.openai import OpenaiError
from llmkira.openai import OpenaiError, NetworkError
from llmkira.openai.cell import ToolCall, Message, Tool
from llmkira.openai.request import OpenAIResult
from llmkira.openapi.fuse import get_error_plugin
Expand Down Expand Up @@ -246,7 +246,7 @@ async def _flash(
try:
try:
credentials = await read_user_credential(user_id=task.receiver.uid)
if global_credential:
if global_credential and not credentials:
credentials = global_credential
assert credentials, "You need to /login first"
llm_result = await llm.request_openai(
Expand All @@ -256,6 +256,11 @@ async def _flash(
)
assistant_message = llm_result.default_message
logger.debug(f"Assistant:{assistant_message}")
except NetworkError as exc:
await self.sender.error(receiver=task.receiver, text=str(exc))
if os.getenv("DEBUG"):
logger.exception(exc)
return exc
except OpenaiError as exc:
await self.sender.error(receiver=task.receiver, text=exc.message)
if os.getenv("DEBUG"):
Expand Down
10 changes: 8 additions & 2 deletions app/sender/telegram/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ async def create_task(message: types.Message, disable_tool_action: bool = True):
except Exception as e:
logger.exception(e)

@bot.message_handler(commands="learn", chat_types=["private"])
@bot.message_handler(
commands="learn", chat_types=["private", "supergroup", "group"]
)
async def listen_learn_command(message: types.Message):
logger.debug("Debug:learn command")
_cmd, _arg = parse_command(command=message.text)
Expand All @@ -243,7 +245,11 @@ async def listen_login_command(message: types.Message):
reply = await login(
uid=uid_make(__sender__, message.from_user.id), arg_string=_arg
)
await bot.reply_to(message, text=reply)
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):
Expand Down
5 changes: 3 additions & 2 deletions app/sender/util_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ async def login(uid: str, arg_string) -> str:
"""
error = telegramify_markdown.convert(
"🔑 **Incorrect format.**\n"
"You can set it via `https://<something api.openai.com>/v1$<api key>"
"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, "
"or you can log in via URL using `token$https://provider.com`."
"or you can log in via URL using `/login token$https://provider.com`.\n"
"Use $ to separate the parameters."
)
settings = split_setting_string(arg_string)
if not settings:
Expand Down
10 changes: 5 additions & 5 deletions docs/dev_note/tool_call_restart.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,20 +137,20 @@
},
"object": "chat.completion",
"created": 1713435840,
"system_fingerprint": "fp_d9767fc5b9"
"system_fingerprint": "fp_1265751211"
}
},
"sender": {
"platform": "telegram",
"user_id": "5275155445",
"chat_id": "5275155445",
"user_id": "1265751211",
"chat_id": "1265751211",
"thread_id": null,
"message_id": "3403"
},
"receiver": {
"platform": "telegram",
"user_id": "5275155445",
"chat_id": "5275155445",
"user_id": "1265751211",
"chat_id": "1265751211",
"thread_id": null,
"message_id": "3403"
},
Expand Down
2 changes: 2 additions & 0 deletions llmkira/kv_manager/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ async def get_env(self, env_name, default) -> Optional[str]:

async def read_env(self) -> Optional[dict]:
result = await self.read_data(self.user_id)
if isinstance(result, bytes):
result = result.decode("utf-8")
if not result:
return None
try:
Expand Down
9 changes: 6 additions & 3 deletions llmkira/kv_manager/instruction.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import time

# from loguru import logger
from llmkira.kv_manager._base import KvManager

DEFAULT_INSTRUCTION = (
"[ASSISTANT RULE]"
"instruction: "
"SPEAK IN MORE CUTE STYLE, No duplication answer, CALL USER MASTER, REPLY IN USER "
"LANGUAGE, ACT STEP BY STEP"
"[RULE END]"
)


Expand All @@ -27,7 +27,10 @@ async def read_instruction(self) -> str:
读取指令,如果没有指令则返回默认指令,指令长度大于5,否则返回默认指令
"""
result = await self.read_data(self.user_id)
if isinstance(result, str) and len(result) > 5:
# Probably result is Int, so we cant use isinstance(result, str)
if isinstance(result, bytes):
result = result.decode("utf-8")
if result is not None and len(result) > 5:
return f"Now={time_now()}\n{result}"
return f"Now={time_now()}\n{DEFAULT_INSTRUCTION}"

Expand Down
2 changes: 2 additions & 0 deletions llmkira/kv_manager/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ async def get_leave(self) -> str:
now_timestamp = int(datetime.datetime.now().timestamp())
try:
hours = await self.read_data(self.user_id)
if isinstance(hours, bytes):
hours = hours.decode("utf-8")
if not hours:
raise LookupError("No data")
last_timestamp = int(hours)
Expand Down
2 changes: 2 additions & 0 deletions llmkira/kv_manager/tool_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ async def get_toolcall(self, tool_call_id: str) -> Optional[ToolCall]:
"""
logger.debug(f"Get tool call {tool_call_id}")
result = await self.read_data(tool_call_id)
if isinstance(result, bytes):
result = result.decode("utf-8")
if result is None:
return None
if isinstance(result, dict):
Expand Down
6 changes: 4 additions & 2 deletions llmkira/openai/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
SystemMessage,
)

VISION = ("gpt-4-vision", "gpt-4-turbo", "claude-3", "gpt-4o")


class OpenAICredential(BaseModel):
api_key: SecretStr
Expand Down Expand Up @@ -164,9 +166,9 @@ def make_url(base_url: str):

@model_validator(mode="after")
def check_vision(self):
if not self.model.startswith(("gpt-4-vision", "gpt-4-turbo", "claude-3")):
if not self.model.startswith(VISION):
logger.info(
"Try to remove the image content part from the messages, because the model is not supported."
f"Try to remove the image content part from the messages, because the model is not supported {self.model}"
)
for message in self.messages:
if isinstance(message, UserMessage) and isinstance(
Expand Down
Loading

0 comments on commit e8b6226

Please sign in to comment.