Skip to main content
Glama

read_and_summarize_pdf_file

Extract and condense PDF content into a concise summary by specifying the file path and desired compression ratio. Ideal for quickly accessing key information from lengthy documents.

Instructions

读取PDF文件并总结内容(限制2k字符) Args: filepath: PDF文件路径 target_ratio: 目标压缩比例,0.1-1.0之间 Returns: PDF内容总结

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
filepathYes
target_ratioNo

Implementation Reference

  • The main execution function for the tool, decorated with @mcp.tool() which also handles registration. Validates input, reads PDF content via helper, summarizes it, and formats the response.
    @mcp.tool() async def read_and_summarize_pdf_file(filepath: str, ctx: Context, target_ratio: float = 0.2) -> str: """ 读取PDF文件并总结内容(限制2k字符) Args: filepath: PDF文件路径 target_ratio: 目标压缩比例,0.1-1.0之间 Returns: PDF内容总结 """ try: # 验证参数 if not 0.1 <= target_ratio <= 1.0: return "错误: target_ratio 必须在 0.1 到 1.0 之间" ctx.info(f"开始读取PDF文件: {filepath}") # 读取PDF content = file_processor.read_pdf_file(filepath) # 总结内容 summary = await summarizer.summarize_content(content, target_ratio) ctx.info("PDF文件读取和总结完成") return f"PDF文件: {filepath}\n\n总结:\n{summary}" except Exception as e: logger.error(f"PDF文件总结失败: {e}") return f"PDF文件总结失败: {str(e)}"
  • Helper method in FileProcessor class to read and extract text from PDF files using PyPDF2.
    @staticmethod def read_pdf_file(filepath: str) -> str: """读取PDF文件""" try: content = "" with open(filepath, 'rb') as f: pdf_reader = PyPDF2.PdfReader(f) for page in pdf_reader.pages: content += page.extract_text() + "\n" return content.strip() except Exception as e: logger.error(f"读取PDF文件失败 {filepath}: {e}") raise Exception(f"无法读取PDF文件: {str(e)}")
  • Core summarization helper in ContentSummarizer class that calls the LLM API to condense content based on target ratio.
    async def summarize_content(self, content: str, target_ratio: float = 0.2, custom_prompt: str = None) -> str: """ 使用大模型总结内容 Args: content: 要总结的内容 target_ratio: 目标压缩比例 (默认20%) custom_prompt: 自定义总结提示词 Returns: 总结后的内容 """ try: # 检查内容长度,避免超出限制 if len(content) > MAX_INPUT_TOKENS * 3: # 粗略估算token content = content[:MAX_INPUT_TOKENS * 3] logger.warning("内容过长,已截断") # 构建总结提示词 if custom_prompt: prompt = custom_prompt else: target_length = min(max(int(len(content) * target_ratio), 100), 1000) prompt = f"""请将以下内容总结为约{target_length}字的精炼版本,保留核心信息和关键要点: {content} 总结要求: 1. 保持原文的主要观点和逻辑结构 2. 去除冗余和次要信息 3. 使用简洁明了的语言 4. 确保信息的准确性和完整性""" response = self.client.chat.completions.create( model=OPENAI_MODEL, messages=[ {"role": "system", "content": "你是一个专业的内容总结专家,擅长将长文本压缩为精炼的摘要。"}, {"role": "user", "content": prompt} ], max_tokens=MAX_OUTPUT_TOKENS, temperature=0.1 ) return response.choices[0].message.content.strip() except Exception as e: logger.error(f"内容总结失败: {e}") return f"总结失败: {str(e)}"

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/yzfly/fullscope-mcp-server'

If you have feedback or need assistance with the MCP directory API, please join our Discord server