修改提示词生成不了的bug
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -43,38 +43,59 @@ def get_system_prompt(template_id=None):
|
||||
|
||||
请直接返回优化后的提示词,不要添加任何解释或其他内容。"""
|
||||
|
||||
def generate_with_llm(input_text, template_id=None):
|
||||
"""调用大模型API生成提示词"""
|
||||
try:
|
||||
system_prompt = get_system_prompt(template_id)
|
||||
|
||||
# 打印参数
|
||||
print("\n=== API 调用参数 ===")
|
||||
print(f"模板ID: {template_id}")
|
||||
print(f"输入文本: {input_text}")
|
||||
print(f"系统提示: {system_prompt}")
|
||||
print("==================\n")
|
||||
|
||||
response = client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": input_text}
|
||||
],
|
||||
temperature=0.7,
|
||||
max_tokens=500
|
||||
)
|
||||
|
||||
# 打印响应
|
||||
generated_text = response.choices[0].message.content.strip()
|
||||
print("\n=== API 响应结果 ===")
|
||||
print(f"生成的提示词: {generated_text}")
|
||||
print("==================\n")
|
||||
|
||||
return generated_text
|
||||
except Exception as e:
|
||||
current_app.logger.error(f'LLM API调用失败: {str(e)}')
|
||||
return "提示词生成失败,请稍后重试"
|
||||
def generate_with_llm(input_text, template_id=None, max_retries=3):
|
||||
"""调用大模型API生成提示词,带重试机制"""
|
||||
import time
|
||||
|
||||
system_prompt = get_system_prompt(template_id)
|
||||
|
||||
# 打印参数
|
||||
print("\n=== API 调用参数 ===")
|
||||
print(f"模板ID: {template_id}")
|
||||
print(f"输入文本: {input_text}")
|
||||
print(f"系统提示: {system_prompt}")
|
||||
print("==================\n")
|
||||
|
||||
for attempt in range(max_retries):
|
||||
try:
|
||||
response = client.chat.completions.create(
|
||||
model="deepseek-chat",
|
||||
messages=[
|
||||
{"role": "system", "content": system_prompt},
|
||||
{"role": "user", "content": input_text}
|
||||
],
|
||||
temperature=0.7,
|
||||
max_tokens=500,
|
||||
timeout=60 # 设置60秒超时
|
||||
)
|
||||
|
||||
# 打印响应
|
||||
generated_text = response.choices[0].message.content.strip()
|
||||
print("\n=== API 响应结果 ===")
|
||||
print(f"生成的提示词: {generated_text}")
|
||||
print("==================\n")
|
||||
|
||||
return generated_text
|
||||
|
||||
except Exception as e:
|
||||
error_msg = str(e)
|
||||
current_app.logger.error(f'LLM API调用失败 (尝试 {attempt + 1}/{max_retries}): {error_msg}')
|
||||
|
||||
# 如果是最后一次尝试,返回错误信息
|
||||
if attempt == max_retries - 1:
|
||||
if "timeout" in error_msg.lower() or "time" in error_msg.lower():
|
||||
return "提示词生成超时,请稍后重试"
|
||||
elif "connection" in error_msg.lower() or "network" in error_msg.lower():
|
||||
return "网络连接失败,请检查网络后重试"
|
||||
elif "rate limit" in error_msg.lower() or "quota" in error_msg.lower():
|
||||
return "API调用频率过高,请稍后重试"
|
||||
else:
|
||||
return f"提示词生成失败: {error_msg}"
|
||||
|
||||
# 等待后重试
|
||||
time.sleep(2 ** attempt) # 指数退避
|
||||
|
||||
return "提示词生成失败,请稍后重试"
|
||||
|
||||
def get_template_icon(category):
|
||||
"""根据分类返回对应的Font Awesome图标类名"""
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user