MCPcopy Create free account
hub / github.com/NatsUIJM/autoContents / fetch_single_offset

Function fetch_single_offset

mainprogress/pdf_metadata_extractor.py:508–559  ·  view source on GitHub ↗

调用 LLM 识别单页的页码偏移量

(client: AsyncOpenAI, model: str, page_num: int, b64_img: str)

Source from the content-addressed store, hash-verified

506当前图片的 PDF 物理页码是:{page_num}
507
508【示例 1】
509物理页码:25
510图片中底部写着:"10"
511计算:25 - 10 = 15
512输出:15
513
514【示例 2】
515物理页码:12
516图片中顶部写着:"- 2 -"
517计算:12 - 2 = 10
518输出:10
519
520【示例 3】
521物理页码:100
522图片中没有明确的阿拉伯数字页码
523输出:Error
524
525【错误示例】
526物理页码:25
527图片中顶部写着:"20"
528计算:25 - 20 = -5
529输出:-5
530
531请仔细观察图片,找到印刷页码,并严格按照上述格式,仅输出计算后的正文偏移量数字。不要输出任何解释。"""
532 try:
533 completion = await client.chat.completions.create(
534 model=model,
535 messages=[
536 {
537 "role": "user",
538 "content": [
539 {"type": "text", "text": prompt},
540 {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64_img}"}},
541 ]
542 }
543 ],
544 extra_body={"enable_thinking": False},
545 temperature=0,
546 )
547 return completion.choices[0].message.content.strip()
548 except Exception as e:
549 error_msg = f"获取第 {page_num} 页偏移量失败:{e}"
550 logger.error(error_msg)
551 write_log(error_msg)
552 return "Error"
553
554async def calculate_offset(pdf_path: str, client: AsyncOpenAI, model: str, initial_data_dir: str) -> int:
555 """
556 自动计算正文偏移量。
557 逻辑优化:
558 1. 先随机取 5 页。
559 2. 统计众数,若众数数量 < 4,则再随机取 5 页(不重复),共 10 页一起统计。
560 3. 将所有过程的图片、原始响应、解析结果保存到 initial_data/offset_log.json。
561 """
562 log_entries = []

Callers 1

calculate_offsetFunction · 0.85

Calls 1

write_logFunction · 0.70

Tested by

no test coverage detected