You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
alpha_tools/rpc_alpha_workflow/02_generate_alpha_idea.py

116 lines
4.2 KiB

# -*- coding: utf-8 -*-
import time
from datetime import datetime
from xmlrpc.client import ServerProxy, Fault
is_local = 0
if is_local:
url = 'http://127.0.0.1:28888'
else:
url = 'http://192.168.31.41:32000'
username = 'rpc'
password = 'aaaAAA111'
db = "quantify"
common = ServerProxy(f"{url}/xmlrpc/2/common")
uid = common.authenticate(db, username, password, {})
models = ServerProxy(f"{url}/xmlrpc/2/object")
# 超时时间(秒)
TIMEOUT_SECONDS = 10 * 60 # 10分钟
# 1. 搜索 alpha.idea 模型中当天创建且状态为 generated_prompt 的数据
today = datetime.now().strftime('%Y-%m-%d')
print(f"搜索日期: {today}")
# 构建搜索条件: 当天创建且状态为 generated_prompt
# Odoo 中日期字段通常存储为 datetime,我们使用 date 函数比较
search_domain = [
['status', '=', 'generated_prompt'],
['create_date', '>=', f"{today} 00:00:00"],
['create_date', '<=', f"{today} 23:59:59"]
]
idea_ids = models.execute_kw(db, uid, password, 'alpha.idea', 'search', [search_domain])
if not idea_ids:
print(f"没有找到当天状态为 'generated_prompt' 的 alpha.idea 记录")
exit(0)
print(f"找到 {len(idea_ids)} 条待处理记录: {idea_ids}")
# 2. 遍历这些数据,单线程处理
for idx, idea_id in enumerate(idea_ids, 1):
print(f"\n{'='*60}")
print(f"处理第 {idx}/{len(idea_ids)} 条记录,ID: {idea_id}")
print(f"{'='*60}")
# 点击 post_to_ms 按钮
print("调用 post_to_ms 方法...")
try:
result = models.execute_kw(db, uid, password, 'alpha.idea', 'post_to_ms', [[idea_id]])
print(f"post_to_ms 执行结果: {result}")
except Fault as e:
# 忽略 "cannot marshal None" 错误
if "cannot marshal None" in str(e):
print("post_to_ms 已执行(返回 None,这是正常的)")
else:
print(f"post_to_ms 执行出错: {e}")
continue
# 死循环等待状态变为 llm_received,带10分钟超时
print("等待 LLM 返回结果...")
start_time = time.time()
current_status = None
while True:
# 检查是否超时
elapsed_time = time.time() - start_time
if elapsed_time > TIMEOUT_SECONDS:
print(f"超时!已等待 {elapsed_time/60:.1f} 分钟,强制退出等待...")
# 在 result_message 字段回写 timeout
try:
models.execute_kw(db, uid, password, 'alpha.idea', 'write', [[idea_id], {'result_message': 'timeout'}])
print(f"已在记录 {idea_id} 的 result_message 字段写入 'timeout'")
except Exception as write_e:
print(f"写入 result_message 失败: {write_e}")
break
time.sleep(5) # 每5秒检查一次
record_data = models.execute_kw(db, uid, password, 'alpha.idea', 'read', [[idea_id], ['status', 'name']])
if not record_data:
print("记录不存在")
break
current_status = record_data[0].get('status')
record_name = record_data[0].get('name', 'Unknown')
print(f"当前状态: {current_status} (已等待 {elapsed_time:.0f} 秒)")
if current_status == 'llm_received':
print(f"LLM 已返回结果,共等待 {elapsed_time:.0f} 秒({elapsed_time/60:.1f} 分钟),开始解码模板...")
break
elif current_status == 'failed':
print(f"处理失败,跳过此记录")
break
# 如果状态是 llm_received,调用 decode_template
if current_status == 'llm_received':
print("调用 decode_template 方法...")
try:
result = models.execute_kw(db, uid, password, 'alpha.idea', 'decode_template', [[idea_id]])
print(f"decode_template 执行结果: {result}")
except Fault as e:
# 忽略 "cannot marshal None" 错误
if "cannot marshal None" in str(e):
print("decode_template 已执行(返回 None,这是正常的)")
else:
print(f"decode_template 执行出错: {e}")
print(f"记录 {idea_id} 处理完成")
print(f"{idx}/{len(idea_ids)} 条记录处理完毕,继续下一条...")
print(f"\n{'='*60}")
print("全部流程执行完成!收工!")
print(f"{'='*60}")