240 lines
10 KiB
Python
240 lines
10 KiB
Python
import logging,json
|
||
from datetime import datetime
|
||
from flask import Blueprint, request, Response,session
|
||
from app.services.zhipu_service import ZhipuService
|
||
from app.services.zhipu_alltool_service import ZhipuAlltoolService
|
||
from app.services.zhipu_file_service import ZhipuFileService
|
||
from app.utils.prompt_repository import PromptRepository # Add this import
|
||
|
||
zhipu_controller = Blueprint('zhipu_controller', __name__)
|
||
zhipu_service = ZhipuService()
|
||
zhipu_alltool_service = ZhipuAlltoolService()
|
||
zhipu_file_service = ZhipuFileService()
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
@zhipu_controller.route('/zhipu/stream', methods=['POST'])
|
||
def stream_sse():
|
||
data = request.json
|
||
message = data.get('message', '')
|
||
|
||
def event_stream():
|
||
for chunk in zhipu_service.talk_to_zhipu_sse(message):
|
||
if chunk:
|
||
yield chunk
|
||
|
||
return Response(event_stream(), content_type='text/event-stream')
|
||
|
||
@zhipu_controller.route('/zhipu/non-stream', methods=['POST'])
|
||
def non_stream():
|
||
data = request.json
|
||
message = data.get('message', '')
|
||
|
||
response = zhipu_service.talk_to_zhipu(message)
|
||
print(f'response: {response}')
|
||
return response
|
||
|
||
@zhipu_controller.route('/zhipu/retrive/non-stream', methods=['POST'])
|
||
def retrive_non_stream():
|
||
data = request.json
|
||
message = data.get('message', '')
|
||
knowledge_id = data.get('knowledge_id', '')
|
||
prompt_template = data.get('prompt_template', '')
|
||
|
||
response = zhipu_service.retrive(message, knowledge_id, None)
|
||
print(f'response: {response}')
|
||
return response
|
||
|
||
@zhipu_controller.route('/zhipu/retrive/stream', methods=['POST'])
|
||
def retrive_stream():
|
||
data = request.json
|
||
message = data.get('message', '')
|
||
knowledge_id = data.get('knowledge_id', '')
|
||
prompt_template = data.get('prompt_template', '')
|
||
logger.info(f'/zhipu/retrive/stream: {message}')
|
||
|
||
classification_result_str = zhipu_alltool_service.func_call_classify(message)
|
||
logger.info(f'classification_result: {classification_result_str}')
|
||
classification_result = json.loads(classification_result_str)
|
||
|
||
if classification_result.get('category') == 'web_search':
|
||
def event_stream_websearch_sse():
|
||
for chunk in zhipu_alltool_service.web_search_sse(message):
|
||
if chunk:
|
||
yield chunk
|
||
return Response(event_stream_websearch_sse(), content_type='text/event-stream')
|
||
|
||
elif classification_result.get('category')== 'retrive_knowledge':
|
||
def event_stream_retrive():
|
||
for chunk in zhipu_service.retrive_sse(message, knowledge_id, None):
|
||
if chunk:
|
||
yield chunk
|
||
return Response(event_stream_retrive(), content_type='text/event-stream')
|
||
|
||
elif classification_result.get('category')== 'generate_report':
|
||
def event_stream_generate_report():
|
||
# Here is the hack information
|
||
# 1. 获取到当前的日期
|
||
# 2. append 项目信息
|
||
prompt_project_info = "项目:数字电网项目"
|
||
prompt_date = datetime.now().strftime("%Y-%m-%d")
|
||
prompt_report_template = PromptRepository().get_prompt("report_template")
|
||
prompt_report_title = f"根据日志模版的样式,查询{prompt_project_info} 销售日志并生成日志报告,注意需要同时提取非项目进展的信息。如果有缺失的要点(时间、参与人、事件、获得信息、信息来源、事件结果描述),假如日志没有明确按照要点提供信息,则认为缺失,不要从其他要点中总结,\n {prompt_report_template}。输出(按照模版的markdown格式):1. 日志报告 \n 2. 缺失的要点(如果存在)"
|
||
generated_report = ""
|
||
for chunk in zhipu_service.retrive_sse(prompt_report_title + message, knowledge_id, None):
|
||
if chunk:
|
||
print(chunk)
|
||
generated_report += chunk
|
||
yield chunk
|
||
|
||
# # 3. 判断日志是否存在缺失项
|
||
# for chunk in zhipu_service.check_report_missing_info(generated_report):
|
||
# if chunk:
|
||
# yield chunk
|
||
|
||
return Response(event_stream_generate_report(), content_type='text/event-stream')
|
||
elif classification_result.get('category')== 'update_report':
|
||
# 1. submit report
|
||
submit_result = zhipu_file_service.submit_file(prefix="销售日志",project_name="数字电网项目", file_content=message)
|
||
|
||
#2. 重新生成日报
|
||
def event_stream_generate_report_updated():
|
||
# Here is the hack information
|
||
# 1. 获取到当前的日期
|
||
# 2. append 项目信息
|
||
prompt_project_info = "项目:数字电网项目"
|
||
prompt_date = datetime.now().strftime("%Y-%m-%d")
|
||
prompt_report_template = PromptRepository().get_prompt("report_template")
|
||
prompt_report_title = f"根据日志模版的样式,查询{prompt_project_info} 销售日志并生成日志报告,注意需要同时提取非项目进展的信息。如果有缺失的要点(时间、参与人、事件、获得信息、信息来源、事件结果描述),如果日志没有明确按照要点提供信息,则认为缺失,不要从其他要点中总结,如有多个版本,请合并信息\n {prompt_report_template}。输出(按照模版的markdown格式):1. 日志报告 \n 2. 缺失的要点(如果存在)"
|
||
generated_report = ""
|
||
for chunk in zhipu_service.retrive_sse(prompt_report_title + message, knowledge_id, None):
|
||
if chunk:
|
||
print(chunk)
|
||
generated_report += chunk
|
||
yield chunk
|
||
return Response(event_stream_generate_report_updated(), content_type='text/event-stream')
|
||
elif classification_result.get('category')== 'clear_report':
|
||
deleted_files = zhipu_file_service.delete_file_by_prefix(prefix="销售日志", project_name="数字电网项目")
|
||
return deleted_files
|
||
else:
|
||
return "输入意图判断不明,请明确意图"
|
||
|
||
|
||
|
||
@zhipu_controller.route('/zhipu/analysis/stream', methods=['POST'])
|
||
def analysis_stream():
|
||
data = request.json
|
||
message = data.get('message', '')
|
||
knowledge_id = data.get('knowledge_id', '')
|
||
|
||
intent_categories =["analyze_sales","provide_sales_info"]
|
||
|
||
classification_result_str = zhipu_alltool_service.func_call_classify(message, intent_categories)
|
||
print(f'classification_result: {classification_result_str}')
|
||
classification_result = json.loads(classification_result_str)
|
||
|
||
|
||
additional_business_info = ""
|
||
if classification_result.get('category') == 'analyze_sales':
|
||
# do analyze sales as before
|
||
pass
|
||
elif classification_result.get('category') == 'provide_sales_info':
|
||
contain_project_info = zhipu_alltool_service.func_call_yes_or_no(message, "是否包含项目信息")
|
||
print(f'contain_project_info: {contain_project_info}')
|
||
contain_project_info = json.loads(contain_project_info)
|
||
if contain_project_info.get('answer') == 'yes':
|
||
additional_business_info = message
|
||
else:
|
||
return "请在补充信息中包含项目信息"
|
||
else:
|
||
return "输入意图判断不明,请明确意图"
|
||
|
||
|
||
# 获取business info
|
||
prompt_get_business_info = f"""
|
||
请根据用户提供的如下信息,查找相关的 '当前详细状态及Close节奏','Sales stage' 信息,并返回给用户:
|
||
{message}
|
||
"""
|
||
business_info = zhipu_service.retrive(prompt_get_business_info, knowledge_id, None)
|
||
print(f'business_info: {business_info}')
|
||
|
||
analysis_rule = PromptRepository().get_prompt('sales_analysis')
|
||
print(f'analysis_rule: {analysis_rule}')
|
||
|
||
# 根据当前详细状态及Close节奏,以及Sales stage,给出分析
|
||
prompt_analysis = f"""
|
||
请根据查询到的上述商机信息:
|
||
{business_info}
|
||
"""
|
||
|
||
if additional_business_info and additional_business_info != "":
|
||
prompt_analysis += f"""
|
||
同时,请考虑以下额外的商机信息:
|
||
{additional_business_info}
|
||
"""
|
||
|
||
prompt_analysis += f"""
|
||
根据如下各销售阶段的销售阶段任务、销售关键动作、阶段转化标准:
|
||
{analysis_rule}
|
||
结合上述商机信息的对应阶段,分析并判断其销售动作是否完成了前一阶段的准出标准,以及是否支持将销售阶段转化到当前阶段
|
||
1. **销售阶段分析**
|
||
2. **销售动作日志分析**
|
||
3. **销售动作与销售阶段的关系**
|
||
4. **判断结果**
|
||
5. **销售阶段分析报告**
|
||
|
||
如果用户在下面的输入指令中指定了只需要上面所列的某个或某几个分析,请只输出指定分析的结果,如果未指定,请输出所有分析结果
|
||
{message}
|
||
"""
|
||
|
||
def event_stream():
|
||
for chunk in zhipu_service.talk_to_zhipu_sse(prompt_analysis):
|
||
if chunk:
|
||
yield chunk
|
||
|
||
return Response(event_stream(), content_type='text/event-stream')
|
||
|
||
|
||
@zhipu_controller.route('/zhipu/alltool/websearch/stream', methods=['POST'])
|
||
def alltool_stream():
|
||
data = request.json
|
||
message = data.get('message', '')
|
||
|
||
def event_stream():
|
||
for chunk in zhipu_alltool_service.web_search_sse(message):
|
||
if chunk:
|
||
yield chunk
|
||
return Response(event_stream(), content_type='text/event-stream')
|
||
|
||
@zhipu_controller.route('/zhipu/alltool/classify/non-stream', methods=['POST'])
|
||
def alltool_classify_non_stream():
|
||
data = request.json
|
||
message = data.get('message', '')
|
||
response = zhipu_alltool_service.func_call_classify(message)
|
||
print(f'response: {response}')
|
||
return response
|
||
|
||
@zhipu_controller.route('/zhipu/alltool/yes-no/non-stream', methods=['POST'])
|
||
def alltool_classify_yes_no_non_stream():
|
||
data = request.json
|
||
message = data.get('message', '')
|
||
question = data.get('question', '')
|
||
response = zhipu_alltool_service.func_call_yes_or_no(message,question)
|
||
print(f'response: {response}')
|
||
return response
|
||
|
||
@zhipu_controller.route('/zhipu/file/submit', methods=['POST'])
|
||
def submit_report():
|
||
data = request.json
|
||
report_text = data.get('report_text', '')
|
||
project_name = data.get('project_name', '')
|
||
prefix = data.get('prefix', '')
|
||
submit_result = zhipu_file_service.submit_file(prefix=prefix,project_name=project_name, file_content=report_text)
|
||
return submit_result
|
||
|
||
@zhipu_controller.route('/zhipu/file/list', methods=['POST'])
|
||
def get_file_list():
|
||
file_list = zhipu_file_service.get_file_list()
|
||
return file_list
|
||
|