++
This commit is contained in:
+79
-2
@@ -169,9 +169,10 @@ async def generate_alpha(request: Request):
|
||||
"user_universe": data.get('user_universe'),
|
||||
"user_delay": int(data.get('user_delay')) if data.get('user_delay') else None,
|
||||
"user_category": data.get('user_category'),
|
||||
"user_data_type": data.get('user_data_type', 'MATRIX')
|
||||
"user_data_type": data.get('user_data_type', 'MATRIX'),
|
||||
"max_retries": int(data.get('max_retries', 20))
|
||||
}
|
||||
print(f"配置已构建: LLM_model={config['LLM_model_name']}, alpha_id={config['alpha_id']}")
|
||||
print(f"配置已构建: LLM_model={config['LLM_model_name']}, alpha_id={config['alpha_id']}, max_retries={config['max_retries']}")
|
||||
|
||||
# 将配置写入临时 JSON 文件,供 Transformer 脚本读取
|
||||
config_path = os.path.join(transformer_dir, f'config_{task_id}.json')
|
||||
@@ -191,6 +192,18 @@ async def generate_alpha(request: Request):
|
||||
env={**os.environ, "PYTHONIOENCODING": "utf-8"}
|
||||
)
|
||||
print(f"Transformer 脚本执行完成,返回码: {process.returncode}")
|
||||
|
||||
# 打印 Transformer 的输出到终端
|
||||
if process.stdout:
|
||||
print(f"\n{'='*50}")
|
||||
print("Transformer 输出:")
|
||||
print(f"{'='*50}")
|
||||
print(process.stdout)
|
||||
if process.stderr:
|
||||
print(f"\n{'='*50}")
|
||||
print("Transformer 错误:")
|
||||
print(f"{'='*50}")
|
||||
print(process.stderr)
|
||||
|
||||
# 定义输出文件路径
|
||||
output_file = os.path.join(transformer_dir, 'output', 'Alpha_generated_expressions_success.json')
|
||||
@@ -356,6 +369,70 @@ async def health_check():
|
||||
return {"status": "healthy", "service": "alpha-transformer"}
|
||||
|
||||
|
||||
@app.post("/api/test-llm")
|
||||
async def test_llm_connection(request: Request):
|
||||
"""
|
||||
测试 LLM 连接
|
||||
接收 LLM 配置,尝试连接并返回测试结果
|
||||
"""
|
||||
try:
|
||||
data = await request.json()
|
||||
|
||||
api_key = data.get('llm_api_key')
|
||||
base_url = data.get('llm_base_url')
|
||||
model = data.get('llm_model')
|
||||
|
||||
if not api_key or not base_url or not model:
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content={"success": False, "error": "Missing required LLM configuration"}
|
||||
)
|
||||
|
||||
print(f"测试 LLM 连接: {base_url}, 模型: {model}")
|
||||
|
||||
# 导入 openai
|
||||
import openai
|
||||
|
||||
# 创建客户端
|
||||
client = openai.AsyncOpenAI(
|
||||
api_key=api_key,
|
||||
base_url=base_url
|
||||
)
|
||||
|
||||
# 尝试发送一个简单的请求
|
||||
response = await client.chat.completions.create(
|
||||
model=model,
|
||||
messages=[
|
||||
{"role": "user", "content": "Hello, this is a connection test. Reply with 'OK' only."}
|
||||
],
|
||||
max_tokens=10,
|
||||
timeout=30
|
||||
)
|
||||
|
||||
# 检查响应
|
||||
if response and response.choices and len(response.choices) > 0:
|
||||
content = response.choices[0].message.content
|
||||
print(f"✓ LLM 连接测试成功: {content[:50]}...")
|
||||
return JSONResponse(content={
|
||||
"success": True,
|
||||
"message": "LLM 连接成功",
|
||||
"response": content[:100]
|
||||
})
|
||||
else:
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"success": False, "error": "Empty response from LLM"}
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
print(f"✗ LLM 连接测试失败: {error_msg}")
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content={"success": False, "error": error_msg}
|
||||
)
|
||||
|
||||
|
||||
@app.get("/api/download/{alpha_id}")
|
||||
async def download_results(alpha_id: str):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user