gradio RuntimeError: async generator raised StopAsyncIteration
主要是return和yield混用导致的,yield是可以流式回复,return一次性回答;需要通义,都改成yield,但不是流式的内容需要最后再加个return
移除了所有的 return 语句,改为 yield 加 return。在生成器函数中,return 用来终止生成器。
简化了代码结构,使每个分支都遵循相同的模式:
生成内容
yield 内容
return 终止生成器
所有返回值都通过 yield 发送,确保了统一的数据流模式。
错误处理也使用相同的模式,确保错误信息也通过 yield 发送。
def process_content(content):"""Helper function to handle both streaming and non-streaming content"""if isinstance(content, str):yield contentelse:yield from contentdef process_query(query):messages = [{"role": "system", "content": prompt}, {"role": "user", "content": "\n"+query}]message = send_messages(messages)print(type(message), message)if "json" in message:match = re.search(r'json(.*?)```', message, re.DOTALL)json_str = match.group(1)data = json.loads(json_str)print(type(data), data)if "response" in data:if data["response"]:print(data["response"])yield data["response"]returnagent_str = data["action"][0]# Free chat handlingif "free_chat" in agent_str:try:content = agent_str.split("'")[1]messages_all.append({"role": "user", "content": content})stream = send_messages_stream(messages_all)contents = ""for chunk in stream:contents += chunk.choices[0].delta.contentyield contentsreturnexcept:pass# Hotel checkingif "check_hotel" in agent_str:try:results = eval(agent_str)if isinstance(results, str):yield resultsreturnresult_lists = []for num, result in enumerate(results):result_lists.append([num+1, result["hotelName"],result["address"],result["minRoomPrice"]])hotels_str = "\n".join([", ".join(map(str, hotel)) for hotel in result_lists])yield hotels_strreturnexcept Exception as e:print(e)yield "没有找到合适酒店,也可以通过下面链接查询:https://ai.118114.cn/h5/subpackage/travel/hotel"return# Flight checkingif "check_flight" in agent_str:try:results = eval(agent_str)if isinstance(results, str):yield resultsreturnresult_lists = []for num, result in enumerate(results):result_lists.append([num+1,result["departureAirportName"],result["arrivalAirportName"],result["disAmt"],result["airlineName"],result["deptm"],result["arrtm"]])flight_str = "\n".join([", ".join(map(str, flight)) for flight in result_lists])yield flight_strreturnexcept Exception as e:print(e)yield "没有找到合适机票,可以通过下面链接查询:https://ai.118114.cn/h5/subpackage/travel/plane"return# Train checkingif "check_train" in agent_str:try:results = eval(agent_str)if isinstance(results, str):yield resultsreturnresult_lists = []for num, result in enumerate(results):result_lists.append([num+1,result["trainCode"],result["startStationName"],result["endStationName"],result["lowestPriceStr"]])train_str = "\n".join([", ".join(map(str, train)) for train in result_lists])yield train_strreturnexcept Exception as e:print(e)yield "没有找到合适火车票,可以通过下面链接查询:https://ai.118114.cn/h5/subpackage/travel/train"returnyield agent_strreturnelse:if "response" in message:data = json.loads(message)if data["response"]:yield data["response"]returnelif "action" in message:data = json.loads(message)agent_str = data["action"][0]# Handle each action type using the same pattern as above# Free chatif "free_chat" in agent_str:try:content = agent_str.split("'")[1]messages_all.append({"role": "user", "content": content})stream = send_messages_stream(messages_all)contents = ""for chunk in stream:contents += chunk.choices[0].delta.contentyield contentsreturnexcept:pass# Rest of the action handlers follow the same pattern...yield agent_strreturnelse:yield messagereturnclient = OpenAI(base_url="https://api.deepseek.com", api_key="sk****")gr.ChatInterface(process_query, title="118114智能体出行查询").launch(server_name="0.0.0.0",server_port=8888,show_error=True,debug=True
)