LLM生成nvidia-h100-tensor-core-hopper-whitepaper.pdf摘要

LLM生成nvidia-h100-tensor-core-hopper-whitepaper.pdf摘要

LLM生成nvidia-h100-tensor-core-hopper-whitepaper.pdf摘要

代码

python 复制代码
import pdfplumber
import time

def split_text_to_chunks(text, max_chunk_size=8192, delimiter='####'):
    """
    将长文本分割成多块,每块的大小不超过最大块大小,并且以给定的分隔符开头和结尾。
    在分割时确保不会跨段,每段由分隔符开头。
    
    :param text: 要分割的长文本
    :param max_chunk_size: 每一块的最大大小
    :param delimiter: 每一段的起始分隔符
    :return: 分割后的块列表
    """
    # 按照分隔符分割文本,去除空白段
    sections = [section for section in text.split(delimiter) if section.strip()]
    
    chunks = []
    current_chunk = ""
    
    for section in sections:
        # 每一段都要包含起始分隔符
        section = delimiter + section
        
        if len(current_chunk) + len(section) + len(delimiter) * 2 + 2 <= max_chunk_size:
            current_chunk += section
        else:
            # 若当前段添加到当前块后超出最大块大小,则当前块保存
            if current_chunk:
                chunks.append(current_chunk.strip() + "\n")
            current_chunk = section
    
    # 添加最后一个块,如果有内容
    if current_chunk:
        chunks.append(current_chunk.strip() + "\n")
    
    return chunks

def llm_summary(question):
    from http import HTTPStatus
    import dashscope
    dashscope.api_key="sk-"
    from dashscope import Generation    
    
    system_prompt="你是一位GPU专家,能从用户输入的文章中提取出H100相关的特性,输出中文"
    
    messages = [{'role': 'system', 'content': system_prompt},
                {'role': 'user', 'content': question}]

    response = Generation.call(model="qwen-max", messages=messages,result_format='message')
    if response.status_code == HTTPStatus.OK:
        messages.append({'role': response.output.choices[0]['message']['role'],
                        'content': response.output.choices[0]['message']['content']})
        output=response.output.choices[0]['message']['content']
        return output
    return ""
    

def stage_0():    
    pdf_path = 'nvidia-h100-tensor-core-hopper-whitepaper.pdf'
    output=""
    fo=open("H100_Architecture.txt","w",encoding="utf-8")
    with pdfplumber.open(pdf_path) as pdf:
        for idx,page in enumerate(pdf.pages[5:70]):
                lines=page.extract_text().split("\n")[1:-2]
                for line in lines:
                    output+=" "
                    output+=line[:-1].strip()
                    if line[-1]=='.':
                        output+="\n\n####"
    fo.write(output)
    fo.close()        

def stage_1():
    article_body_content = open("H100_Architecture.txt","r",encoding="utf-8").read()
    f=open("H100_Architecture_Summary.txt","a+")
    chunks = split_text_to_chunks(article_body_content)
    total=len(chunks)
    for idx, chunk in enumerate(chunks):
            #print(f"Chunk {idx + 1}: {len(chunk)} \n{chunk}\n")
            summary=llm_summary(chunk)
            print(f" --------- {idx}/{total} @{idx/total:.3f} --------- ")    
            print(summary)
            f.write(f"\n####{summary}")
            f.flush()
            time.sleep(5)

stage_0()  
stage_1()
相关推荐
CHANG_THE_WORLD26 分钟前
PDFium导出pdf 图像
开发语言·c++·pdf
momo_al27 分钟前
Umi-OCR制作双层PDF
pdf·ocr
励志成为美貌才华为一体的女子30 分钟前
pdf解析工具---Miner-u 本地部署记录
学习·pdf
reasonsummer31 分钟前
【办公类-115-02】20251018信息员每周通讯上传之文字稿整理(PDF转docx没有成功)
python·pdf
文火冰糖的硅基工坊2 小时前
[创业之路-702]:“第三次”与“第四次工业革命”的范式跃迁
大数据·人工智能·科技·嵌入式硬件·架构·嵌入式·gpu
top_designer3 小时前
告别“静态”VI手册:InDesign与AE打造可交互的动态品牌规范
设计模式·pdf·交互·vi·工作流·after effects·indesign
文火冰糖的硅基工坊5 小时前
[嵌入式系统-136]:主流AIOT智能体软件技术栈
嵌入式硬件·架构·嵌入式·cpu·gpu
强哥之神6 小时前
从零理解 KV Cache:大语言模型推理加速的核心机制
人工智能·深度学习·机器学习·语言模型·llm·kvcache
数据智能老司机6 小时前
使用 OpenAI Agents SDK 构建智能体——记忆与知识
llm·openai·agent
数据智能老司机6 小时前
使用 OpenAI Agents SDK 构建智能体——代理工具与 MCP
llm·openai·agent