Skip to content

Commit

Permalink
Merge pull request #264 from LlmKira/dev_file_reader
Browse files Browse the repository at this point in the history
File reader
  • Loading branch information
sudoskys committed Sep 24, 2023
2 parents 9d3ac2f + 155f743 commit 8a71f72
Show file tree
Hide file tree
Showing 21 changed files with 627 additions and 137 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
OPENAI_API_KEY=sk-xxx
OPENAI_API_MODEL=gpt-3.5-turbo-0613
# OPENAI_API_ENDPOINT=https://api.openai.com/v1
# OPENAI_API_ORG_ID=org-xxx
OPENAI_API_PROXY=socks5://127.0.0.1:7890
TELEGRAM_BOT_PROXY_ADDRESS=socks5://127.0.0.1:7890
TELEGRAM_BOT_TOKEN=123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
Expand Down
29 changes: 22 additions & 7 deletions docs/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,18 @@ plugin.py # 插件主文件

所以需要做 暂存链管理器 + 参数修改 的人回路。

## 动态消息组构建

出于对文件系统的考虑,我们需要一个 **动态消息组构建器** ,可以从文件事件中抽取相关事件。

由于消息系统的特殊性,所以可以只在历史记录中显示简报,用插件处理数据。

## "非活动接受者"

对于非活动接受者,比如 Github Issue 平台,我们如何让Telegram和其连接?

采用订阅器即可解决此问题。

# 问题

## 多后端必要性的论证
Expand All @@ -141,21 +153,24 @@ plugin.py # 插件主文件

但是目前本项目已经和 Openai 深度绑定,所以这是一个低优先级事项。

## "非活动接受者"

对于非活动接受者,比如 Github Issue 平台,我们如何让Telegram和其连接?

## Meta 信息头的暗桩

为了指示消息的处理阶段,我们有一个由混乱的命名规则组成的元信息头,这个可能是一个问题。

## “权限” 认证

添加同级的 receiver 组件进行 **认证重发**
添加同级的 receiver 组件进行 **认证重发**,我们需要一个认证组件,将需要认证的操作进行存储,然后通过命令释放带有通过标记的请求。(重新代发)

## 动态消息组构建
### 多响应方案

虽然收发广播机制看似完美,但是在实际使用中,我们发现了一个问题:**多响应**

每次我们最多调用一个响应函数,但是在某些情况下,我们需要调用多个响应函数。

拟定方案:
方案 1. 传入拆解函数,衍生多个响应函数。
方案 2. 设定重复调用循环链条,由计数器和结束标记插件控制结束。代发机制的利用。

出于对文件系统的考虑,我们需要一个 **动态消息组构建器** ,可以从文件事件中抽取相关事件。



8 changes: 5 additions & 3 deletions middleware/llm_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @Author : sudoskys
# @File : llm_task.py
# @Software: PyCharm
import os
from typing import List, Literal

from loguru import logger
Expand Down Expand Up @@ -95,7 +96,8 @@ async def func_message(self) -> Message:
"""
处理消息转换和调用工具
"""
model_name = "gpt-3.5-turbo-0613"
# 模型内容
model_name = os.getenv("OPENAI_API_MODEL", "gpt-3.5-turbo-0613")
self.scraper.reduce_messages(limit=openai.Openai.get_token_limit(model=model_name))
message = self.scraper.get_messages()
_functions = self.functions if self.functions else None
Expand All @@ -104,13 +106,13 @@ async def func_message(self) -> Message:

# 消息缓存读取和转换
# 断点
logger.debug(f" [x] Openai request:{message}")
logger.info(f" [x] Openai request --org {driver.org_id} --url {driver.endpoint} --message {message} ")
endpoint = openai.Openai(
config=driver,
model=model_name,
messages=message,
functions=_functions,
echo=True
echo=False
)

# 调用Openai
Expand Down
4 changes: 4 additions & 0 deletions middleware/user/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ async def _sync(self, user_id: int):

@property
def llm_driver(self):
if not self.sub_info.update_driver:
return openai.Openai.Driver()
return self.sub_info.llm_driver

async def block_plugin(self, plugin_name: str):
Expand All @@ -53,9 +55,11 @@ async def set_endpoint(self, endpoint: str = None, api_key: str = None):
self.sub_info.llm_driver.endpoint = endpoint
if api_key:
self.sub_info.llm_driver.api_key = api_key
self.sub_info.update_driver = True
await self._upload(user_id=self.sub_info.user_id)

async def clear_endpoint(self):
self.sub_info.update_driver = False
self.sub_info.llm_driver = openai.Openai.Driver()
await self._upload(user_id=self.sub_info.user_id)

Expand Down
3 changes: 2 additions & 1 deletion middleware/user/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# @Author : sudoskys
# @File : schema.py
# @Software: PyCharm
from typing import List, Optional
from typing import List

from pydantic import BaseModel, Field, BaseSettings

Expand Down Expand Up @@ -35,6 +35,7 @@ def by_function(cls, function_name: str,
plugin_subs: Plugin = Field(Plugin(), description="插件的设置")
costs: List[Cost] = Field([], description="消费记录")
llm_driver: openai.Openai.Driver = openai.Openai.Driver()
update_driver: bool = False

def total_cost(self):
return sum([cost.token_usage for cost in self.costs])
4 changes: 2 additions & 2 deletions plugins/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def pre_check(self):
import bilibili_api
return True
except ImportError as e:
logger.error(f"plugin:package <bilibili_api> not installed:{e}")
logger.error(f"plugin:bilibili:package <bilibili_api> not installed:{e}")
return False

def func_message(self, message_text):
Expand Down Expand Up @@ -121,7 +121,7 @@ async def llm_task(task, task_desc, raw_data):
"""
_submanager = SubManager(user_id=task.sender.user_id)
driver = _submanager.llm_driver # 由发送人承担接受者的成本
model_name = "gpt-3.5-turbo-0613"
model_name = os.getenv("OPENAI_API_MODEL", "gpt-3.5-turbo-0613")
endpoint = openai.Openai(
config=driver,
model=model_name,
Expand Down
3 changes: 2 additions & 1 deletion plugins/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# @Author : sudoskys
# @File : search.py
# @Software: PyCharm
import os

from loguru import logger
from pydantic import BaseModel
Expand Down Expand Up @@ -124,7 +125,7 @@ async def failed(self, platform, task, receiver, reason):
async def llm_task(task, task_desc, raw_data):
_submanager = SubManager(user_id=task.sender.user_id)
driver = _submanager.llm_driver # 由发送人承担接受者的成本
model_name = "gpt-3.5-turbo-0613"
model_name = os.getenv("OPENAI_API_MODEL", "gpt-3.5-turbo-0613")
endpoint = openai.Openai(
config=driver,
model=model_name,
Expand Down
5 changes: 3 additions & 2 deletions plugins/sticker.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,19 @@ async def run(self, task: TaskHeader, receiver: TaskHeader.Location, arg, **kwar
try:
_file = []
for item in task.message:
assert isinstance(item, RawMessage), "item must be RawMessage"
if item.file:
for i in item.file:
_file.append(i)
_set = Sticker.parse_obj(arg)
_file_obj = [await RawMessage.download_file(file_id=i.file_id) for i in sorted(set(_file), key=_file.index)]
# 去掉None
_file_obj = [BytesIO(item) for item in _file_obj if item]
_file_obj = [item for item in _file_obj if item]
_result = []
if not _file_obj:
return
for item in _file_obj:
image = await resize_image(item)
image = await resize_image(BytesIO(item.file_data))
file = BytesIO()
file.name = "sticker.webp"
image.save(file, "WEBP")
Expand Down
Loading

0 comments on commit 8a71f72

Please sign in to comment.