智能专家3号
Some checks failed
Flask 提示词大师 - CI/CD 流水线 / 代码质量检查 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 单元测试 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 集成测试 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 构建Docker镜像 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 部署到测试环境 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 部署到生产环境 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 部署监控系统 (push) Has been cancelled
Some checks failed
Flask 提示词大师 - CI/CD 流水线 / 代码质量检查 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 单元测试 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 集成测试 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 构建Docker镜像 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 部署到测试环境 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 部署到生产环境 (push) Has been cancelled
Flask 提示词大师 - CI/CD 流水线 / 部署监控系统 (push) Has been cancelled
This commit is contained in:
@@ -6,15 +6,18 @@
|
||||
"""
|
||||
from flask import Blueprint, render_template, request, jsonify
|
||||
from openai import OpenAI
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import time
|
||||
from src.flask_prompt_master import db
|
||||
from src.flask_prompt_master.models.models import User, Prompt
|
||||
from src.flask_prompt_master.models.history_models import PromptHistory, UserStatistics
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
expert_generate_3_bp = Blueprint('expert_generate_3', __name__)
|
||||
_dedup_cache = {}
|
||||
|
||||
_llm_client = OpenAI(
|
||||
api_key=os.environ.get('LLM_API_KEY') or 'sk-fdf7cc1c73504e628ec0119b7e11b8cc',
|
||||
@@ -153,6 +156,26 @@ def expert_generate_3_api():
|
||||
raw_input = (payload.get('input_text') or '').strip()
|
||||
if not raw_input:
|
||||
return jsonify({'code': 400, 'message': '请输入您的需求', 'data': None})
|
||||
|
||||
temperature = payload.get('temperature')
|
||||
temperature = float(temperature) if temperature is not None else 0.7
|
||||
temperature = max(0.0, min(2.0, temperature))
|
||||
max_tokens = payload.get('max_tokens')
|
||||
max_tokens = int(max_tokens) if max_tokens is not None else 1000
|
||||
max_tokens = max(100, min(4000, max_tokens))
|
||||
timeout = payload.get('timeout')
|
||||
timeout = int(timeout) if timeout is not None else 60
|
||||
timeout = max(10, min(300, timeout))
|
||||
|
||||
uid = _resolve_user_id()
|
||||
req_key = (uid, hashlib.md5(raw_input.encode()).hexdigest())
|
||||
now_ts = time.time()
|
||||
if req_key in _dedup_cache and (now_ts - _dedup_cache[req_key]) < 8:
|
||||
return jsonify({'code': 429, 'message': '请勿重复提交,请稍后再试', 'data': None})
|
||||
_dedup_cache[req_key] = now_ts
|
||||
if len(_dedup_cache) > 500:
|
||||
_dedup_cache.clear()
|
||||
|
||||
resp1 = _llm_client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=[
|
||||
@@ -160,7 +183,7 @@ def expert_generate_3_api():
|
||||
{"role": "user", "content": raw_input}
|
||||
],
|
||||
temperature=0.1,
|
||||
timeout=60
|
||||
timeout=timeout
|
||||
)
|
||||
intent_raw = (resp1.choices[0].message.content or "").strip()
|
||||
intent_raw = intent_raw.replace('```json', '').replace('```', '').strip()
|
||||
@@ -189,14 +212,13 @@ def expert_generate_3_api():
|
||||
{"role": "system", "content": tpl.format(analysis=analysis_str)},
|
||||
{"role": "user", "content": raw_input}
|
||||
],
|
||||
temperature=0.7,
|
||||
max_tokens=1000,
|
||||
timeout=60
|
||||
temperature=temperature,
|
||||
max_tokens=max_tokens,
|
||||
timeout=timeout
|
||||
)
|
||||
result_prompt = (resp2.choices[0].message.content or "").strip()
|
||||
if not result_prompt:
|
||||
return jsonify({'code': 500, 'message': '生成失败,请重试', 'data': None})
|
||||
uid = _resolve_user_id()
|
||||
try:
|
||||
db.session.add(Prompt(input_text=raw_input, generated_text=result_prompt, user_id=uid))
|
||||
db.session.commit()
|
||||
|
||||
Reference in New Issue
Block a user