diff --git a/dat/qwen3.7-max.txt b/dat/qwen3.7-max.txt new file mode 100644 index 0000000..f07f135 --- /dev/null +++ b/dat/qwen3.7-max.txt @@ -0,0 +1,53 @@ + 已生成完整的 qwen3.7-max 配置SQL。以下是配置方案: + + 模型摘要 + - 模型名称: 千问3.7-Max + - API model: qwen3.7-max (严格按API文档填写) + - 分类: text2text (文生文) + - 供应商: 阿里百炼 (ali-qwen) + - 接口: OpenAI兼容 /chat/completions,同步流式 + + 复用vs新建 + - upapp: 复用 ali-qwen (阿里百炼) + - upappkey: 复用 gCMl-BnrTrfoqWAEPPoH8 (API Key已配置) + - uapi: 复用 t2t (BY2cA4RD_axNxTG4m9Coa) + - uapiio: 复用 Is8l4TGkcZcqFSjbbeIK2 (文本会话) + - llm: 新建 + - llm_api_map: 新建 + - pricing_program: 新建 + - pricing_program_timing: 新建 + + 生成的SQL (4条) + + sql + -- 1. llm表 + INSERT INTO llm VALUES ('Jt26VKlUFsJxABuVLauBc','千问3.7-Max','qwen3.7-max','通义千问3.7系列旗舰模型,相较3.6版本在推理能力、代码能力、多模态理解等方面全面升级。适用于复杂任务处理、智能体开发、代码生成等高级应用场景。','text2text','qwen','ali-qwen','t2t','6fadgewjraOyvxC_EkHou','0','2026-05-23','9999-12-31','',NULL,10.00); + + -- 2. llm_api_map表 + INSERT INTO llm_api_map VALUES ('rFS0Ag-epBUxE5LCF6glW','Jt26VKlUFsJxABuVLauBc','text2text','t2t',NULL,NULL,'0B-JNSUEelsY5TQ_Sawa7','1'); + + -- 3. pricing_program表 + INSERT INTO pricing_program VALUES ('0B-JNSUEelsY5TQ_Sawa7','qwen3.7-max定价','fields:\r\n formula:\r\n label: 计算公式\r\n type: str\r\n model:\r\n label: 模型\r\n type: str\r\n prompt_tokens:\r\n label: 输入词元数\r\n type: int\r\n value_mode: between\r\nformula: null\r\npricings:\r\n- formula: 2.5 prompt_tokens / 1000000 + completion_tokens 10 /1000000\r\n model: qwen3.7-max\r\n prompt_tokens: -0.1 ~= 32000\r\n- formula: 4 prompt_tokens / 1000000 + completion_tokens 16 /1000000\r\n model: qwen3.7-max\r\n prompt_tokens: 32000 ~= 128000\r\n- formula: 7 prompt_tokens / 1000000 + completion_tokens 28 /1000000\r\n model: qwen3.7-max\r\n prompt_tokens: 128000 ~= 256000\r\n- formula: 10 prompt_tokens / 1000000 + completion_tokens 40 /1000000\r\n model: qwen3.7-max\r\n prompt_tokens: 256000 ~= 1000000','2026-05-23','9999-12-31'); + + -- 4. pricing_program_timing表 + INSERT INTO pricing_program_timing VALUES ('C-IIEG_9_O9beN8S2vdUI','0B-JNSUEelsY5TQ_Sawa7','qwen3.7-max计费','fields:\r\n formula:\r\n label: 计算公式\r\n type: str\r\n model:\r\n label: 模型\r\n type: str\r\n prompt_tokens:\r\n label: 输入词元数\r\n type: int\r\n value_mode: between\r\nformula: null\r\npricings:\r\n- formula: 2.5 prompt_tokens / 1000000 + completion_tokens 10 /1000000\r\n model: qwen3.7-max\r\n prompt_tokens: -0.1 ~= 32000\r\n- formula: 4 prompt_tokens / 1000000 + completion_tokens 16 /1000000\r\n model: qwen3.7-max\r\n prompt_tokens: 32000 ~= 128000\r\n- formula: 7 prompt_tokens / 1000000 + completion_tokens 28 /1000000\r\n model: qwen3.7-max\r\n prompt_tokens: 128000 ~= 256000\r\n- formula: 10 prompt_tokens / 1000000 + completion_tokens 40 /1000000\r\n model: qwen3.7-max\r\n prompt_tokens: 256000 ~= 1000000','2026-05-23','9999-12-31'); + + + 验证查询 + sql + SELECT id, name, model, upappid, apiname FROM llm WHERE model = 'qwen3.7-max'; + SELECT id, llmid, llmcatelogid, apiname, ppid FROM llm_api_map WHERE llmid = 'Jt26VKlUFsJxABuVLauBc'; + SELECT id, name FROM pricing_program WHERE id = '0B-JNSUEelsY5TQ_Sawa7'; + + + ppid三处一致 + - pricing_program.id = 0B-JNSUEelsY5TQ_Sawa7 + - pricing_program_timing.ppid = 0B-JNSUEelsY5TQ_Sawa7 + - llm_api_map.ppid = 0B-JNSUEelsY5TQ_Sawa7 + + 注意事项 + 1. 定价参考qwen3.6-plus的阶梯式计费模式,具体价格需根据百炼官方定价调整 + 2. qwen3.7-max使用已有的t2t uapi,OpenAI兼容接口 + 3. 如果qwen3.7-max有特殊的推理模式(如思考/非思考切换),可能需要额外的配置 + 4. 之前提到的 startReasoning is not defined 前端报错问题需要单独排查,可能与harnessed_reasoning的bricks前端代码有关 + diff --git a/llmage/__pycache__/accounting.cpython-310.pyc b/llmage/__pycache__/accounting.cpython-310.pyc new file mode 100644 index 0000000..1ccd2fe Binary files /dev/null and b/llmage/__pycache__/accounting.cpython-310.pyc differ diff --git a/llmage/accounting.py b/llmage/accounting.py index b841ce0..53b11c9 100644 --- a/llmage/accounting.py +++ b/llmage/accounting.py @@ -240,27 +240,28 @@ async def llm_accoung_failed(luid, reason=None): async def backup_accounted_llmusage(cutoff_date): - """Backup accounted records with use_date < cutoff_date to history table.""" + """Backup accounted records with use_date < cutoff_date to history table using single SQL statements.""" env = ServerEnv() ts = env.timestampstr() - batched = 0 - recs = [] async with get_sor_context(env, 'llmage') as sor: - sql = """select * from llmusage -where accounting_status='accounted' - and use_date < ${cutoff_date}$""" - recs = await sor.sqlExe(sql, {'cutoff_date': cutoff_date}) - if not recs: - debug(f'backup_accounted_llmusage: no records to backup for use_date < {cutoff_date}') - return 0 - debug(f'backup_accounted_llmusage: {cutoff_date} {len(recs)} records to backup') - for r in recs: - async with get_sor_context(env, 'llmage') as sor: - await sor.C('llmusage_history', r.copy()) - await sor.D('llmusage', {'id': r.id}) - batched += 1 - debug(f'backup_accounted_llmusage: backed up {batched} records for use_date < {cutoff_date}') - return batched + # Step 1: INSERT INTO history SELECT from main table + insert_sql = """INSERT INTO llmusage_history +(id, llmid, use_date, use_time, userid, usages, ioinfo, transno, responsed_seconds, finish_seconds, status, taskid, amount, cost, userorgid, ownerid, accounting_status, backup_time) +SELECT id, llmid, use_date, use_time, userid, usages, ioinfo, transno, responsed_seconds, finish_seconds, status, taskid, amount, cost, userorgid, ownerid, accounting_status, ${ts}$ +FROM llmusage +WHERE accounting_status='accounted' AND use_date < ${cutoff_date}$""" + result = await sor.execute(insert_sql, {'cutoff_date': cutoff_date, 'ts': ts}) + inserted = result if isinstance(result, int) else 0 + debug(f'backup_accounted_llmusage: {inserted} records inserted to history') + + if inserted > 0: + # Step 2: DELETE from main table + delete_sql = """DELETE FROM llmusage +WHERE accounting_status='accounted' AND use_date < ${cutoff_date}$""" + await sor.execute(delete_sql, {'cutoff_date': cutoff_date}) + debug(f'backup_accounted_llmusage: {inserted} records deleted from main table') + + return inserted async def get_failed_accounting_records(filters=None, page=1, page_size=50): diff --git a/models/.llmusage_history.json.swp b/models/.llmusage_history.json.swp new file mode 100644 index 0000000..cd1501d Binary files /dev/null and b/models/.llmusage_history.json.swp differ diff --git a/wwwroot/api/retry_accounting.dspy b/wwwroot/api/retry_accounting.dspy new file mode 100644 index 0000000..3ec9eb5 --- /dev/null +++ b/wwwroot/api/retry_accounting.dspy @@ -0,0 +1,36 @@ +#!/usr/bin/env python3 +import json +from datetime import datetime + +result = {'success': False, 'message': ''} + +try: + dbname = get_module_dbname('llmage') + luid = params_kw.get('id') or params_kw.get('llmusageid') + if not luid: + result['message'] = '缺少llmusageid参数' + else: + async with DBPools().sqlorContext(dbname) as sor: + # 1. 重置 llmusage 记账状态为 created,让后台循环重新处理 + await sor.U('llmusage', { + 'id': luid, + 'accounting_status': 'created' + }) + + # 2. 更新失败记录:标记已处理,增加重试次数 + now = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + await sor.execute(""" + UPDATE llmusage_accounting_failed + SET handled = '1', + retry_count = IFNULL(retry_count, 0) + 1, + handled_time = ${now}$, + handled_note = CONCAT(IFNULL(handled_note, ''), '[', ${now}$, '] 触发重试; ') + WHERE llmusageid = ${luid}$ + """, {'luid': luid, 'now': now}) + + result['success'] = True + result['message'] = '已重置为待记账状态,后台循环将重新处理' +except Exception as e: + result['message'] = str(e) + +return json.dumps(result, ensure_ascii=False, default=str) diff --git a/wwwroot/failed_accounting.ui b/wwwroot/failed_accounting.ui index 2faa3ef..1ca120e 100644 --- a/wwwroot/failed_accounting.ui +++ b/wwwroot/failed_accounting.ui @@ -80,6 +80,30 @@ }] } ] + }, + { + "widgettype": "VBox", + "options": {"spacing": 4}, + "subwidgets": [ + {"widgettype": "Text", "options": {"text": "", "fontSize": "12px"}}, + { + "widgettype": "Button", + "id": "retry_btn", + "options": { + "label": "重试", + "bgcolor": "#4caf50", + "color": "#ffffff", + "width": "80px" + }, + "binds": [{ + "wid": "self", + "event": "click", + "actiontype": "script", + "target": "self", + "script": "var dv = this.root.getElementById('failed_table'); var row = dv.selected_row || (dv.selected_rows && dv.selected_rows[0]); if(!row || !row.llmusageid) { alert('请先选中一条记录'); return; } var url = bricks.build_url ? bricks.build_url('/llmage/api/retry_accounting.dspy') : '/llmage/api/retry_accounting.dspy'; fetch(url + '?id=' + row.llmusageid).then(function(r){return r.json();}).then(function(d){ if(d.success) { alert(d.message); dv.load({}); } else { alert('失败: ' + d.message); } }).catch(function(e){ alert('请求异常: ' + e); });" + }] + } + ] } ] },