增加v2版本的接口,流式输出json,提供追问,及延时追问
This commit is contained in:
parent
9db2f44457
commit
dae5fc202c
|
|
@ -3,6 +3,8 @@ from flask import Flask
|
|||
from flask_cors import CORS
|
||||
from app.api.v1.controllers import api_v1
|
||||
from app.api.v1.zhipu_controller import zhipu_controller
|
||||
from app.api.v2.zhipu_controller_v2 import zhipu_controller_v2
|
||||
|
||||
from app.config import Config
|
||||
|
||||
def create_app():
|
||||
|
|
@ -21,9 +23,11 @@ def create_app():
|
|||
app.register_blueprint(api_v1, url_prefix='/api/v1')
|
||||
|
||||
|
||||
|
||||
# Register Zhipu-related routes under /api/v1/zhipu
|
||||
app.register_blueprint(zhipu_controller, url_prefix='/api/v1')
|
||||
|
||||
# Register Zhipu-related routes under /api/v2
|
||||
app.register_blueprint(zhipu_controller_v2, url_prefix='/api/v2')
|
||||
|
||||
logger.info("Application setup completed")
|
||||
return app
|
||||
|
|
|
|||
|
|
@ -0,0 +1,182 @@
|
|||
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_v2 = Blueprint('zhipu_controller_v2', __name__)
|
||||
zhipu_service = ZhipuService()
|
||||
zhipu_alltool_service = ZhipuAlltoolService()
|
||||
zhipu_file_service = ZhipuFileService()
|
||||
|
||||
response_headers = {'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'X-Accel-Buffering': 'no'}
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def format_chunk(chunk, follow_up, follow_up_after):
|
||||
return {
|
||||
"content": chunk,
|
||||
"follow_up": follow_up,
|
||||
"follow_up_after": follow_up_after
|
||||
}
|
||||
|
||||
@zhipu_controller_v2.route('/zhipu/retrive/stream', methods=['POST'])
|
||||
def retrive_stream():
|
||||
print(f'zhihu_v2_retrive_stream')
|
||||
data = request.json
|
||||
message = data.get('message', '')
|
||||
knowledge_id = data.get('knowledge_id', '')
|
||||
prompt_template = data.get('prompt_template', '')
|
||||
|
||||
classification_result_str = zhipu_alltool_service.func_call_classify(message)
|
||||
print(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:
|
||||
chunk_out = format_chunk(chunk, None, None)
|
||||
print(f'chunk_out: {chunk_out}')
|
||||
yield json.dumps(chunk_out) + '\n'
|
||||
yield json.dumps(format_chunk("", "日报缺乏来源信息", "请补充日报缺乏的信息")) + '\n'
|
||||
return Response(event_stream_websearch_sse(),mimetype='text/event-stream', headers=response_headers)
|
||||
|
||||
elif classification_result.get('category')== 'retrive_knowledge':
|
||||
def event_stream_retrive():
|
||||
for chunk in zhipu_service.retrive_sse(message, knowledge_id, None):
|
||||
if chunk:
|
||||
chunk_out = format_chunk(chunk, None, None)
|
||||
yield json.dumps(chunk_out) + '\n'
|
||||
yield json.dumps(format_chunk("", "日报缺乏来源信息", "请补充日报缺乏的信息")) + '\n'
|
||||
|
||||
return Response(event_stream_retrive(), mimetype='text/event-stream', headers=response_headers)
|
||||
|
||||
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
|
||||
chunk_out = format_chunk(chunk, None, None)
|
||||
yield json.dumps(chunk_out) + '\n'
|
||||
yield json.dumps(format_chunk("", "日报缺乏来源信息", "请补充日报缺乏的信息")) + '\n'
|
||||
|
||||
return Response(event_stream_generate_report(), mimetype='text/event-stream', headers=response_headers)
|
||||
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
|
||||
chunk_out = format_chunk(chunk, None, None)
|
||||
yield json.dumps(chunk_out) + '\n'
|
||||
yield json.dumps(format_chunk("", "日报缺乏来源信息", "请补充日报缺乏的信息")) + '\n'
|
||||
return Response(event_stream_generate_report_updated(), mimetype='text/event-stream', headers=response_headers)
|
||||
elif classification_result.get('category')== 'clear_report':
|
||||
deleted_files = zhipu_file_service.delete_file_by_prefix(prefix="销售日志", project_name="数字电网项目")
|
||||
return format_chunk(deleted_files, None, None)
|
||||
else:
|
||||
return format_chunk( "输入意图判断不明,请明确意图", None, None)
|
||||
|
||||
|
||||
|
||||
@zhipu_controller_v2.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 format_chunk( "请在补充信息中包含项目信息", None, None)
|
||||
else:
|
||||
return format_chunk( "输入意图判断不明,请明确意图", None, None)
|
||||
|
||||
|
||||
# 获取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:
|
||||
chunk_out = format_chunk(chunk, None, None)
|
||||
yield json.dumps(chunk_out) + '\n'
|
||||
yield json.dumps(format_chunk("", "日报缺乏来源信息", "请补充日报缺乏的信息")) + '\n'
|
||||
|
||||
return Response(event_stream(), mimetype='text/event-stream', headers=response_headers)
|
||||
|
||||
Loading…
Reference in New Issue