v2.0.0 大模型驱动版:移除智能体,直接调用大模型接口
- 核心改造:不再使用 openclaw 智能体执行处理步骤,改为直接调用大模型接口 - 新增 services/llm_client.py:OpenAI 兼容接口客户端,支持多模型配置管理 - 步骤4/5 由大模型直接完成(提取产品数据、填充字段) - 步骤6 改为直接调用 ParamHub API 提交审核 - 新增 llm_configs 数据库表,默认配置 unsloth/Qwen3.6-27B-Q4_K_M (262144上下文) - 新增 /api/llm 配置管理 API:增删改查、切换激活、测试连接 - 前端首页新增「大模型配置」面板,可随时新增/切换模型 - 处理步骤名称更新为「大模型」版
This commit is contained in:
@@ -204,6 +204,21 @@ class Database:
|
||||
)
|
||||
''')
|
||||
|
||||
# 大模型配置表
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS llm_configs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
base_url TEXT NOT NULL,
|
||||
api_key TEXT,
|
||||
model_name TEXT NOT NULL,
|
||||
max_context INTEGER DEFAULT 262144,
|
||||
is_active INTEGER DEFAULT 0,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
''')
|
||||
|
||||
# 异常产品表
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS abnormal_products (
|
||||
@@ -226,6 +241,15 @@ class Database:
|
||||
|
||||
# 创建索引
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_process_steps_session ON process_steps(process_id)')
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_llm_configs_active ON llm_configs(is_active)')
|
||||
|
||||
# 首次启动时写入默认大模型配置(如果表为空)
|
||||
cursor.execute('SELECT COUNT(*) FROM llm_configs')
|
||||
if cursor.fetchone()[0] == 0:
|
||||
cursor.execute('''
|
||||
INSERT INTO llm_configs (name, base_url, api_key, model_name, max_context, is_active)
|
||||
VALUES (?, ?, ?, ?, ?, 1)
|
||||
''', ('本地Qwen3.6', 'http://192.168.2.7:18003/v1', 'sk-xxxx', 'unsloth/Qwen3.6-27B-Q4_K_M', 262144))
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_process_sessions_status ON process_sessions(status)')
|
||||
cursor.execute('CREATE INDEX IF NOT EXISTS idx_abnormal_products_status ON abnormal_products(status)')
|
||||
|
||||
@@ -945,5 +969,89 @@ class Database:
|
||||
result['search_results'] = json.loads(result['search_results'])
|
||||
return result
|
||||
|
||||
# ========== 大模型配置操作 ==========
|
||||
def get_llm_configs(self):
|
||||
"""获取所有大模型配置"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT * FROM llm_configs ORDER BY is_active DESC, id ASC')
|
||||
return [dict(row) for row in cursor.fetchall()]
|
||||
|
||||
def get_llm_config(self, config_id):
|
||||
"""获取单个大模型配置"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT * FROM llm_configs WHERE id = ?', (config_id,))
|
||||
row = cursor.fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
def get_active_llm_config(self):
|
||||
"""获取当前激活的大模型配置"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT * FROM llm_configs WHERE is_active = 1 LIMIT 1')
|
||||
row = cursor.fetchone()
|
||||
return dict(row) if row else None
|
||||
|
||||
def add_llm_config(self, name, base_url, api_key, model_name, max_context=262144):
|
||||
"""新增大模型配置"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
# 如果是第一条配置,自动设为激活
|
||||
cursor.execute('SELECT COUNT(*) FROM llm_configs')
|
||||
count = cursor.fetchone()[0]
|
||||
is_active = 1 if count == 0 else 0
|
||||
cursor.execute('''
|
||||
INSERT INTO llm_configs (name, base_url, api_key, model_name, max_context, is_active)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
''', (name, base_url, api_key, model_name, max_context, is_active))
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
|
||||
def update_llm_config(self, config_id, **kwargs):
|
||||
"""更新大模型配置"""
|
||||
allowed = ['name', 'base_url', 'api_key', 'model_name', 'max_context']
|
||||
updates = []
|
||||
values = []
|
||||
for key, value in kwargs.items():
|
||||
if key in allowed:
|
||||
updates.append(f'{key} = ?')
|
||||
values.append(value)
|
||||
if not updates:
|
||||
return False
|
||||
updates.append('updated_at = CURRENT_TIMESTAMP')
|
||||
values.append(config_id)
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f'UPDATE llm_configs SET {" , ".join(updates)} WHERE id = ?', values)
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
def delete_llm_config(self, config_id):
|
||||
"""删除大模型配置(激活中的配置不允许删除)"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute('SELECT is_active FROM llm_configs WHERE id = ?', (config_id,))
|
||||
row = cursor.fetchone()
|
||||
if not row:
|
||||
return False, '配置不存在'
|
||||
if row['is_active'] == 1:
|
||||
return False, '当前激活的配置不能删除,请先切换'
|
||||
cursor.execute('DELETE FROM llm_configs WHERE id = ?', (config_id,))
|
||||
conn.commit()
|
||||
return True, '已删除'
|
||||
|
||||
def set_active_llm_config(self, config_id):
|
||||
"""切换激活的大模型配置"""
|
||||
with self.get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
# 先全部取消激活
|
||||
cursor.execute('UPDATE llm_configs SET is_active = 0, updated_at = CURRENT_TIMESTAMP')
|
||||
# 设置新的激活
|
||||
cursor.execute('UPDATE llm_configs SET is_active = 1, updated_at = CURRENT_TIMESTAMP WHERE id = ?', (config_id,))
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
|
||||
# 全局数据库实例
|
||||
db = Database()
|
||||
Reference in New Issue
Block a user