Skip to content

Commit 8caa6e8

Browse files
committed
feat: default pipeline
1 parent a2efb3e commit 8caa6e8

File tree

8 files changed

+135
-23
lines changed

8 files changed

+135
-23
lines changed

pkg/api/http/service/bot.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
from ....core import app
88
from ....entity.persistence import bot as persistence_bot
9+
from ....entity.persistence import pipeline as persistence_pipeline
910

1011

1112
class BotService:
@@ -46,6 +47,16 @@ async def create_bot(self, bot_data: dict) -> str:
4647
"""创建机器人"""
4748
# TODO: 检查配置信息格式
4849
bot_data['uuid'] = str(uuid.uuid4())
50+
51+
# checkout the default pipeline
52+
result = await self.ap.persistence_mgr.execute_async(
53+
sqlalchemy.select(persistence_pipeline.LegacyPipeline).where(persistence_pipeline.LegacyPipeline.is_default == True)
54+
)
55+
pipeline = result.first()
56+
if pipeline is not None:
57+
bot_data['use_pipeline_uuid'] = pipeline.uuid
58+
bot_data['use_pipeline_name'] = pipeline.name
59+
4960
await self.ap.persistence_mgr.execute_async(
5061
sqlalchemy.insert(persistence_bot.Bot).values(bot_data)
5162
)
@@ -60,6 +71,18 @@ async def update_bot(self, bot_uuid: str, bot_data: dict) -> None:
6071
"""更新机器人"""
6172
if 'uuid' in bot_data:
6273
del bot_data['uuid']
74+
75+
# set use_pipeline_name
76+
if 'use_pipeline_uuid' in bot_data:
77+
result = await self.ap.persistence_mgr.execute_async(
78+
sqlalchemy.select(persistence_pipeline.LegacyPipeline).where(persistence_pipeline.LegacyPipeline.uuid == bot_data['use_pipeline_uuid'])
79+
)
80+
pipeline = result.first()
81+
if pipeline is not None:
82+
bot_data['use_pipeline_name'] = pipeline.name
83+
else:
84+
raise Exception("Pipeline not found")
85+
6386
await self.ap.persistence_mgr.execute_async(
6487
sqlalchemy.update(persistence_bot.Bot).values(bot_data).where(persistence_bot.Bot.uuid == bot_uuid)
6588
)

pkg/api/http/service/pipeline.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ async def get_pipeline(self, pipeline_uuid: str) -> dict | None:
6161

6262
return self.ap.persistence_mgr.serialize_model(persistence_pipeline.LegacyPipeline, pipeline)
6363

64-
async def create_pipeline(self, pipeline_data: dict) -> str:
64+
async def create_pipeline(self, pipeline_data: dict, default: bool = False) -> str:
6565
pipeline_data['uuid'] = str(uuid.uuid4())
6666
pipeline_data['for_version'] = self.ap.ver_mgr.get_current_version()
6767
pipeline_data['stages'] = default_stage_order.copy()
68+
pipeline_data['is_default'] = default
6869

6970
# TODO: 检查pipeline config是否完整
7071

pkg/core/bootutils/log.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ async def init_logging(extra_handlers: list[logging.Handler] = None) -> logging.
3131
"%Y-%m-%d", time.localtime()
3232
)
3333

34-
qcg_logger = logging.getLogger("qcg")
34+
qcg_logger = logging.getLogger("langbot")
3535

3636
qcg_logger.setLevel(level)
3737

pkg/entity/persistence/bot.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class Bot(Base):
1313
adapter = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
1414
adapter_config = sqlalchemy.Column(sqlalchemy.JSON, nullable=False)
1515
enable = sqlalchemy.Column(sqlalchemy.Boolean, nullable=False, default=False)
16+
use_pipeline_name = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
1617
use_pipeline_uuid = sqlalchemy.Column(sqlalchemy.String(255), nullable=True)
1718
created_at = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, server_default=sqlalchemy.func.now())
1819
updated_at = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, server_default=sqlalchemy.func.now(), onupdate=sqlalchemy.func.now())

pkg/entity/persistence/pipeline.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class LegacyPipeline(Base):
1313
created_at = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, server_default=sqlalchemy.func.now())
1414
updated_at = sqlalchemy.Column(sqlalchemy.DateTime, nullable=False, server_default=sqlalchemy.func.now(), onupdate=sqlalchemy.func.now())
1515
for_version = sqlalchemy.Column(sqlalchemy.String(255), nullable=False)
16+
is_default = sqlalchemy.Column(sqlalchemy.Boolean, nullable=False, default=False)
1617

1718
stages = sqlalchemy.Column(sqlalchemy.JSON, nullable=False)
1819
config = sqlalchemy.Column(sqlalchemy.JSON, nullable=False)

pkg/persistence/mgr.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import asyncio
44
import datetime
55
import typing
6+
import json
7+
import uuid
68

79
import sqlalchemy.ext.asyncio as sqlalchemy_asyncio
810
import sqlalchemy
@@ -13,6 +15,7 @@
1315
from .databases import sqlite
1416
from ..utils import constants
1517
from .migrations import dbm001_migrate_v3_config
18+
from ..api.http.service import pipeline as pipeline_service
1619

1720

1821
class PersistenceManager:
@@ -47,7 +50,10 @@ async def create_tables(self):
4750

4851
await conn.commit()
4952

53+
# ======= write initial data =======
54+
5055
# write initial metadata
56+
self.ap.logger.info("Creating initial metadata...")
5157
for item in metadata.initial_metadata:
5258
# check if the item exists
5359
result = await self.execute_async(
@@ -58,6 +64,30 @@ async def create_tables(self):
5864
await self.execute_async(
5965
sqlalchemy.insert(metadata.Metadata).values(item)
6066
)
67+
68+
# write default pipeline
69+
result = await self.execute_async(
70+
sqlalchemy.select(pipeline.LegacyPipeline)
71+
)
72+
if result.first() is None:
73+
self.ap.logger.info("Creating default pipeline...")
74+
75+
pipeline_config = json.load(open('templates/default-pipeline-config.json'))
76+
77+
pipeline_data = {
78+
'uuid': str(uuid.uuid4()),
79+
'for_version': self.ap.ver_mgr.get_current_version(),
80+
'stages': pipeline_service.default_stage_order,
81+
'is_default': True,
82+
'name': 'Chat Pipeline',
83+
'description': '默认对话配置流水线',
84+
'config': pipeline_config,
85+
}
86+
87+
await self.execute_async(
88+
sqlalchemy.insert(pipeline.LegacyPipeline).values(pipeline_data)
89+
)
90+
# =================================
6191

6292
# run migrations
6393
database_version = await self.execute_async(

pkg/plugin/context.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -366,27 +366,6 @@ class RuntimeContainer(pydantic.BaseModel):
366366
class Config:
367367
arbitrary_types_allowed = True
368368

369-
def to_setting_dict(self):
370-
return {
371-
'name': self.plugin_name,
372-
'description': self.plugin_description.to_dict(),
373-
'version': self.plugin_version,
374-
'author': self.plugin_author,
375-
'source': self.plugin_repository,
376-
'main_file': self.main_file,
377-
'pkg_path': self.pkg_path,
378-
'priority': self.priority,
379-
'enabled': self.enabled,
380-
}
381-
382-
def set_from_setting_dict(
383-
self,
384-
setting: dict
385-
):
386-
self.plugin_repository = setting['source']
387-
self.priority = setting['priority']
388-
self.enabled = setting['enabled']
389-
390369
def model_dump(self, *args, **kwargs):
391370
return {
392371
'name': self.plugin_name,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
{
2+
"trigger": {
3+
"group-respond-rules": {
4+
"at": true,
5+
"prefix": [
6+
"ai"
7+
],
8+
"regexp": [],
9+
"random": 0.0
10+
},
11+
"access-control": {
12+
"mode": "blacklist",
13+
"blacklist": [],
14+
"whitelist": []
15+
},
16+
"ignore-rules": {
17+
"prefix": [],
18+
"regexp": []
19+
}
20+
},
21+
"safety": {
22+
"content-filter": {
23+
"scope": "all",
24+
"check-sensitive-words": true
25+
},
26+
"rate-limit": {
27+
"window-length": 60,
28+
"limitation": 60,
29+
"strategy": "drop"
30+
}
31+
},
32+
"ai": {
33+
"runner": {
34+
"runner": "local-agent"
35+
},
36+
"local-agent": {
37+
"model": "",
38+
"max-round": 10,
39+
"prompt": [
40+
{
41+
"role": "system",
42+
"content": "You are a helpful assistant."
43+
}
44+
]
45+
},
46+
"dify-service-api": {
47+
"base-url": "https://api.dify.ai/v1",
48+
"app-type": "chat",
49+
"api-key": "your-api-key",
50+
"thinking-convert": "plain",
51+
"timeout": 30
52+
},
53+
"dashscope-app-api": {
54+
"app-type": "agent",
55+
"api-key": "your-api-key",
56+
"app-id": "your-app-id",
57+
"references-quote": "参考资料来自:"
58+
}
59+
},
60+
"output": {
61+
"long-text-processing": {
62+
"threshold": 1000,
63+
"strategy": "forward",
64+
"font-path": ""
65+
},
66+
"force-delay": {
67+
"min": 0,
68+
"max": 0
69+
},
70+
"misc": {
71+
"hide-exception": true,
72+
"at-sender": true,
73+
"quote-origin": true,
74+
"track-function-calls": false
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)