Spaces:
Running
Running
| from langchain.llms import LlamaCpp | |
| from langchain.prompts import PromptTemplate | |
| from langchain.chains import LLMChain | |
| llm = LlamaCpp( | |
| model_path="models/llama-2-7b.Q4_K_M.gguf", | |
| n_ctx=2048, | |
| temperature=0.3, | |
| verbose=False | |
| ) | |
| template = """Riassumi il seguente documento. Poi estrai: | |
| - Parole chiave | |
| - Argomento principale | |
| - Categoria (paper, tutorial, ricerca, codice) | |
| - Eventuali date | |
| Testo: | |
| {text} | |
| Restituisci in JSON con queste chiavi: | |
| "summary", "keywords", "category", "topic" | |
| """ | |
| prompt = PromptTemplate(input_variables=["text"], template=template) | |
| chain = LLMChain(llm=llm, prompt=prompt) | |
| def summarize_text(text): | |
| output = chain.run(text=text[:4000]) | |
| import json | |
| return json.loads(output) | |