response.end() 正在中止线程

本文讨论了在编程中使用trycatch的几种方式,并提出了一种观点:有时候避免使用trycatch可以减少问题的发生。如果确实需要使用,则建议将其放在finally块内或整个程序的外围。
不用try catch就不会有问题了。当然如果一定要用try catch,放在finally里面或者放在最外面就行了
提供中止按钮,调用接口,中止ai答复 def call_ollama_stream(messages): """调用Ollama API(流式)- 使用官方API格式""" try: # 格式化消息为模型期望的对话模板 formatted_messages = format_messages_for_model(messages) # 构建请求数据 - 使用Ollama官方格式 data = { "model": MODEL_NAME, "messages": formatted_messages, "stream": True } # 流式响应 response = requests.post( f"{OLLAMA_HOST}/api/chat", json=data, stream=True, timeout=120 ) # 检查响应状态 if response.status_code != 200: logger.error(f"Ollama API 错误: {response.status_code} - {response.text}") yield json.dumps({"error": f"API错误: HTTP {response.status_code}"}) return # 处理流式响应 - 更健壮的解析逻辑 for line in response.iter_lines(): if line: try: # 解析JSON行 json_data = json.loads(line) logger.debug(f"原始响应: {json_data}") # 添加调试日志 # 尝试多种可能的字段获取内容 content = None # 尝试从 message.delta.content 获取 if "message" in json_data and "delta" in json_data["message"] and "content" in json_data["message"][ "delta"]: content = json_data["message"]["delta"]["content"] # 尝试从 message.content 获取 elif "message" in json_data and "content" in json_data["message"]: content = json_data["message"]["content"] # 尝试从 choices[0].delta.content 获取 elif "choices" in json_data and len(json_data["choices"]) > 0: choice = json_data["choices"][0] if "delta" in choice and "content" in choice["delta"]: content = choice["delta"]["content"] elif "message" in choice and "content" in choice["message"]: content = choice["message"]["content"] # 尝试从 response 字段获取 elif "response" in json_data: content = json_data["response"] # 尝试从 content 字段获取 elif "content" in json_data: content = json_data["content"] # 如果找到内容,返回 if content: logger.debug(f"提取内容: {content}") yield content else: logger.debug(f"未找到内容字段: {json_data}") except json.JSONDecodeError as e: logger.warning(f"无法解析JSON行: {line}, 错误: {e}") except Exception as e: logger.warning(f"解析JSON行时出错: {str(e)}") except Exception as e: logger.error(f"调用Ollama API时出错: {str(e)}", exc_info=True) yield json.dumps({"error": f"API错误: {str(e)}"})
最新发布
10-09
def call_ollama_stream(messages): """调用Ollama API(流式)- 使用官方API格式""" try: # 格式化消息为模型期望的对话模板 formatted_messages = format_messages_for_model(messages) # 构建请求数据 - 使用Ollama官方格式 data = { "model": MODEL_NAME, "messages": formatted_messages, "stream": True } # 流式响应 response = requests.post( f"{OLLAMA_HOST}/api/chat", json=data, stream=True, timeout=120 ) # 检查响应状态 if response.status_code != 200: logger.error(f"Ollama API 错误: {response.status_code} - {response.text}") yield json.dumps({"error": f"API错误: HTTP {response.status_code}"}) return # 处理流式响应 - 更健壮的解析逻辑 for line in response.iter_lines(): if line: try: # 解析JSON行 json_data = json.loads(line) logger.debug(f"原始响应: {json_data}") # 添加调试日志 # 尝试多种可能的字段获取内容 content = None # 尝试从 message.delta.content 获取 if "message" in json_data and "delta" in json_data["message"] and "content" in json_data["message"][ "delta"]: content = json_data["message"]["delta"]["content"] # 尝试从 message.content 获取 elif "message" in json_data and "content" in json_data["message"]: content = json_data["message"]["content"] # 尝试从 choices[0].delta.content 获取 elif "choices" in json_data and len(json_data["choices"]) > 0: choice = json_data["choices"][0] if "delta" in choice and "content" in choice["delta"]: content = choice["delta"]["content"] elif "message" in choice and "content" in choice["message"]: content = choice["message"]["content"] # 尝试从 response 字段获取 elif "response" in json_data: content = json_data["response"] # 尝试从 content 字段获取 elif "content" in json_data: content = json_data["content"] # 如果找到内容,返回 if content: logger.debug(f"提取内容: {content}") yield content else: logger.debug(f"未找到内容字段: {json_data}") except json.JSONDecodeError as e: logger.warning(f"无法解析JSON行: {line}, 错误: {e}") except Exception as e: logger.warning(f"解析JSON行时出错: {str(e)}") except Exception as e: logger.error(f"调用Ollama API时出错: {str(e)}",给我写一个中止ai回答
10-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值