Created
July 27, 2026 17:18
-
-
Save wilyJ80/76f9e519de8395f94f54de5c0ba026dd to your computer and use it in GitHub Desktop.
llama cpp summarize with requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from langchain_core.messages import SystemMessage, HumanMessage | |
| from pydantic import SecretStr | |
| from langchain_openai import ChatOpenAI | |
| from langchain_text_splitters import RecursiveCharacterTextSplitter | |
| from typing import cast | |
| from pymupdf import pymupdf | |
| print('[INFO] Loading text...') | |
| doc = pymupdf.open('./report.pdf') | |
| content = '' | |
| for page in doc: | |
| content += page.get_text() | |
| print('[INFO] Splitting text...') | |
| splitter = RecursiveCharacterTextSplitter(chunk_size=2000, chunk_overlap=200) | |
| splits = splitter.split_text(content) | |
| llm = ChatOpenAI( | |
| model="Qwen3.5-9B-Q4_K_M", | |
| base_url='https://api.com/v1', | |
| api_key=SecretStr('changeme'), | |
| temperature=0, | |
| default_headers={ | |
| "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36" | |
| } | |
| ) | |
| print('[INFO] Starting AI summarization...') | |
| first_summary = '' | |
| for page, split in enumerate(splits): | |
| response = llm.invoke( | |
| [ | |
| SystemMessage(""" | |
| você deve resumir este conteúdo. | |
| mantenha o resumo conciso e traga \"o quê\" ao invés de \"como\". | |
| """), | |
| HumanMessage(f""" | |
| <PÁGINA> | |
| {page} | |
| </PÁGINA> | |
| <CONTEXTO> | |
| {split} | |
| </CONTEXTO> | |
| """) | |
| ] | |
| ) | |
| print( | |
| f""" | |
| ======================= | |
| {response.content} | |
| ======================= | |
| """ | |
| ) | |
| first_summary += f"\n\n{response.content}" | |
| print("======================== RESUMO 1 ==========================") | |
| print(first_summary) | |
| response = llm.invoke( | |
| [ | |
| SystemMessage(""" | |
| você deve resumir este conteúdo. | |
| mantenha o resumo conciso e traga \"o quê\" ao invés de \"como\". | |
| """), | |
| HumanMessage(f""" | |
| <CONTEXTO> | |
| {first_summary} | |
| </CONTEXTO> | |
| """) | |
| ] | |
| ) | |
| result = response.content | |
| print("======================== RESUMO 2 ==========================") | |
| print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment