【AI大模型应用开发】【项目实战】32.Agent智扫引擎项目-(六)模型部署

📅 2026/7/28 22:27:01
【AI大模型应用开发】【项目实战】32.Agent智扫引擎项目-(六)模型部署
一.服务端具体位置如下:/main.py 或者 /app.py具体代码如下:# 通过接口方式来访问 import time from Agent.ReAct import ReActAgent from Models.Factory import ChatModelFactory from Tools.Tools import * from langchain_community.chat_message_histories.in_memory import ChatMessageHistory from flask import Flask, request, jsonify import re app = Flask(__name__) def launch_agent(query, agent, uuid=1001): chat_history = ChatMessageHistory() reply = agent.execute(query, uuid, chat_history=chat_history, verbose=True) return reply @app.route("/predict", methods=["POST"]) def main(): try: # 1.获取用户问题 data = request.get_json() query = data.get("query", "") uuid = data.get('uuid', 1001) if not query: return jsonify({"error": "请输入文本"}), 400 # 2.实例化大模型 llm = ChatModelFactory().get_model() # 3.注册工具 tools = [ rag_qa_tool, report_generation_tool, ] # 4.实例化ReAct agent = ReActAgent( llm=llm, tools=tools, main_prompt_file_path="prompts/main.txt" ) # 5.智能体主逻辑恢复 response = launch_agent(query, agent, uuid) if 'think' in response and "/think" in response: answer = re.sub(r"think.*?/think", "", response, flags=re.DOTALL) elif 'think' in response and "/think\n\n答:" in response: answer = re.sub(r"think.*?/think\n\n答:", "", response, flags=re.DOTALL) else: answer = response print(f'answer:{answer}') print('-----------------------------------------------------------------------') # json输出 return jsonify({"answer": answer}), 200 except Exception as e: print(e) return jsonify({'error': str(e)}), 500 if __name__ == "__main__": app.run(host="0.0.0.0", port=8002)