From 9fe90a69f79d273357482420fadcc13c9c725ace Mon Sep 17 00:00:00 2001 From: martsforever Date: Thu, 4 Sep 2025 18:55:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E9=80=9A=E8=BF=87=E5=AF=B9=E8=AF=9D=E5=AE=9E=E7=8E=B0=E9=85=92?= =?UTF-8?q?=E5=BA=97=E9=A2=84=E5=AE=9A=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controller/add_langgraph_chat_route.py | 52 +++++++++++++++++----- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/app/controller/add_langgraph_chat_route.py b/app/controller/add_langgraph_chat_route.py index 5119790..ce465af 100644 --- a/app/controller/add_langgraph_chat_route.py +++ b/app/controller/add_langgraph_chat_route.py @@ -3,7 +3,8 @@ import json import time from typing import Union, Annotated, TypedDict -from fastapi import FastAPI +import httpx +from fastapi import FastAPI, Depends from langchain_core.messages import AIMessage, ToolMessage from langchain_core.runnables import RunnableConfig from langchain_core.tools import tool @@ -13,6 +14,8 @@ from langgraph.types import interrupt, Command from pydantic import BaseModel, Field from starlette.responses import StreamingResponse +from app.config.env import env +from app.controller.add_user_route import oauth2_scheme from app.model.ConversationModel import ConversationService from app.utils.db_utils import AsyncSessionDep from app.utils.llm_utils import create_llm @@ -66,21 +69,42 @@ class BookHotelData(TypedDict): @tool(name_or_callable="tool_book_hotel", description="一个用于预定酒店的工具") -def tool_book_hotel(config: RunnableConfig) -> str: +async def tool_book_hotel(config: RunnableConfig) -> str: + """ + 触发中断,输入酒店预定表单信息 + """ book_hotel_data: Union[BookHotelData, str] = interrupt({"title": "请输入酒店预定信息", "formCode": "bookHotel"}) + if book_hotel_data == "N": return f"用户选择取消预定酒店" hotel_id = book_hotel_data.get('hotel_id') proj_id = book_hotel_data.get('proj_id') user_id = book_hotel_data.get('user_id') + token = config['configurable']['token'] - print("hotel_id", hotel_id) - print("proj_id", proj_id) - print("user_id", user_id) - print("config", config) + # print("================ tool_book_hotel ================") + # print("hotel_id", hotel_id) + # print("proj_id", proj_id) + # print("user_id", user_id) - return "预定成功" + async with httpx.AsyncClient() as client: + response = await client.post( + url=f"http://localhost:{env.server_port}/book_hotel", + headers={ + "Authorization": f"Bearer {token}" + }, + json={ + "hotel_id": hotel_id, + "user_id": user_id, + "proj_id": proj_id, + }, + ) + response_data = response.json() + # print("================ response_data ================") + # print(response_data) + + return "预定成功," + response_data.get('message') class ChatMessage(BaseModel): @@ -133,12 +157,16 @@ class ChatAgent: def add_langgraph_chat_route(app: FastAPI): # 流式对话接口 @app.post("/langgraph/stream") - async def langgraph_stream(body: dict, checkpointer: AsyncPostgresSaverDep): + async def langgraph_stream( + body: dict, + token: str = Depends(oauth2_scheme), + ): print(":::::::::::::::::::::::::langgraph_stream:::::::::::::::::::::") print(body) stream_input = body.get('input') stream_config = body.get('config') + stream_config['configurable']['token'] = token print("stream_input", stream_input) print("stream_config", stream_config) @@ -207,13 +235,17 @@ def add_langgraph_chat_route(app: FastAPI): # 恢复中断接口 @app.post("/langgraph/chat_resume/{thread_id}") - async def langgraph_chat(body: dict, thread_id: str): + async def langgraph_chat( + body: dict, + thread_id: str, + token: str = Depends(oauth2_scheme), + ): graph = await ChatAgent.get_agent() chat_state = await ChatAgent.get_chat_state(thread_id) chat_history_list = chat_state.get('messages') graph_state = await graph.ainvoke( Command(resume=body.get('resume_data')), - config={"configurable": {"thread_id": thread_id}} + config={"configurable": {"thread_id": thread_id, "token": token}} ) return { **graph_state,