{text_content}+""" + except UnicodeDecodeError: + html_content += f""" +
diff --git a/deepsearcher/agent/deep_search.py b/deepsearcher/agent/deep_search.py index 5ff327e..a804109 100644 --- a/deepsearcher/agent/deep_search.py +++ b/deepsearcher/agent/deep_search.py @@ -136,7 +136,7 @@ class DeepSearch(BaseAgent): max_iter: int, route_collection: bool = False, text_window_splitter: bool = True, - web_search: bool = True, + web_search: bool = False, **kwargs, ): """ @@ -255,7 +255,9 @@ class DeepSearch(BaseAgent): else: send_info("网页搜索未找到相关结果") - retrieved_results = vector_results + web_results + retrieved_results = vector_results + web_results + else: + retrieved_results = vector_results # Format all chunks for batch processing chunks, _ = self._format_chunks(retrieved_results) @@ -470,7 +472,7 @@ class DeepSearch(BaseAgent): absolute_path = str(Path(reference).resolve()) encoded_path = urllib.parse.quote(absolute_path, safe='') # 使用相对路径,这样可以在不同的服务器配置下工作 - formated_refs.append(f"[^{i + 1}]: /file/{encoded_path}\n") + formated_refs.append(f"[^{i + 1}]: [/file/{encoded_path}](/file/{encoded_path})\n") except Exception as _: formated_refs.append(f"[^{i + 1}]: {reference}\n") diff --git a/main.py b/main.py index b53aa09..abb0a43 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import argparse import uvicorn from fastapi import Body, FastAPI, HTTPException, Query from fastapi.middleware.cors import CORSMiddleware -from fastapi.responses import HTMLResponse, StreamingResponse, FileResponse +from fastapi.responses import HTMLResponse, StreamingResponse, FileResponse, PlainTextResponse from fastapi.staticfiles import StaticFiles from pydantic import BaseModel import os @@ -376,7 +376,7 @@ def clear_messages(): @app.get("/file/{file_path:path}") -def serve_file(file_path: str): +def serve_file(file_path: str, download: bool = Query(False, description="Whether to download the file")): """ Serve local files for file:// URIs in generated reports. @@ -388,12 +388,13 @@ def serve_file(file_path: str): file_path (str): The URL-encoded file path Returns: - FileResponse: The file content or an error response + HTMLResponse or PlainTextResponse: The file content displayed in browser Raises: HTTPException: If the file is not found or access is denied """ import urllib.parse + import mimetypes from pathlib import Path try: @@ -415,15 +416,168 @@ def serve_file(file_path: str): if not file_path_obj.is_file(): raise HTTPException(status_code=400, detail=f"Path is not a file: {decoded_path}") - # 尝试读取文件并返回 - try: + # 如果请求下载,直接返回文件 + if download: return FileResponse( path=str(file_path_obj), filename=file_path_obj.name, media_type='application/octet-stream' ) + + # 尝试读取文件内容 + try: + with open(file_path_obj, 'r', encoding='utf-8') as f: + content = f.read() + except UnicodeDecodeError: + # 如果UTF-8解码失败,尝试其他编码 + try: + with open(file_path_obj, 'r', encoding='latin-1') as f: + content = f.read() + except Exception as e: + raise HTTPException(status_code=500, detail=f"Error reading file: {str(e)}") except Exception as e: raise HTTPException(status_code=500, detail=f"Error reading file: {str(e)}") + + # 获取文件类型 + mime_type, _ = mimetypes.guess_type(str(file_path_obj)) + + # 根据文件类型决定如何显示 + if mime_type and mime_type.startswith('text/'): + # 文本文件直接在浏览器中显示 + return PlainTextResponse(content=content, media_type=mime_type) + else: + # 其他文件类型创建HTML页面显示 + html_content = f""" + + +
+ + +{text_content}+""" + except UnicodeDecodeError: + html_content += f""" +