updated prompt and generation technique

This commit is contained in:
tcsenpai 2025-05-23 11:30:19 +02:00
parent 0c1be59296
commit 3e69817ba0
3 changed files with 90 additions and 99 deletions

View File

@ -21,4 +21,21 @@ available_languages = en,es,fr,de,it,pt,nl,ja,ko,zh
enabled = false
url = http://localhost:11434
default_model = mistral
summarize_prompt = Please provide a comprehensive yet concise summary of the following text. Focus on the main points, key arguments, and important details while maintaining accuracy and completeness. Here's the text to summarize:
summarize_prompt = Your mission is to create a **detailed and comprehensive summary**.
Before you dive into summarizing, a quick heads-up on the input:
* If the text looks like a subtitle file (you know the drill: timestamps, short, disconnected lines), first mentally stitch it together into a flowing, continuous narrative. Then, summarize *that* coherent version.
Now, for the summary itself, here's what I'm looking for:
1. **Focus on Comprehensive Coverage:** As you generate a more detailed summary, ensure you thoroughly cover the main ideas, key arguments, significant supporting details, important examples or explanations offered in the text, and the overall conclusions or takeaways. Don't just skim the surface.
2. **Depth and Desired Length (This is Crucial!):**
* **Target Range:** Produce a summary that is approximately **10 percent to 25 percent of the original text's length**. For example, if the original text is 1000 words, aim for a summary in the 100-250 word range. If it's 100 lines, aim for 10-25 lines. Use your best judgment to hit this target.
* **Information Density:** The goal here is not just arbitrary length, but to fill that length with **all genuinely significant information**. Prioritize retaining details that contribute to a deeper understanding of the subject. It's better to include a supporting detail that seems relevant than to omit it and risk losing nuance.
* **Beyond a Basic Abstract:** This should be much more than a high-level overview. Think of it as creating a condensed version of the text that preserves a good deal of its informative richness and narrative flow. The emphasis is on **thoroughness and completeness of key information** rather than extreme brevity.
3. **Accuracy is King:** What you write needs to be a faithful representation of the source material. No making things up, and no injecting your own opinions unless they're explicitly in the text.
4. **Clarity and Cohesion:** Even though it's longer, the summary should still be well-organized, clear, and easy to read.
* "Present the summary as a series of well-developed paragraphs."
* "Give me a detailed summary of approximately [calculate 10-25 percent of expected input length] words."
* "The summary should be extensive, aiming for about 15 percent of the original content's length."

View File

@ -1,8 +1,8 @@
import requests
from typing import Optional
import configparser
import os
import configparser
import logging
from typing import List, Optional
from ollama import Client
# Configure logging
logging.basicConfig(
@ -11,81 +11,54 @@ logging.basicConfig(
logger = logging.getLogger(__name__)
def load_config() -> configparser.ConfigParser:
"""Load configuration from config.ini file."""
config = configparser.ConfigParser()
config_path = os.path.join(os.path.dirname(__file__), "config.ini")
config.read(config_path)
return config
config = load_config()
class OllamaHandler:
def __init__(self):
self.enabled = config["ollama"].getboolean("enabled")
self.url = config["ollama"]["url"]
self.default_model = config["ollama"]["default_model"]
self.prompt = config["ollama"]["summarize_prompt"]
logger.info(
f"Initialized Ollama handler with URL: {self.url}, Default model: {self.default_model}"
)
logger.info(f"Ollama enabled: {self.enabled}")
"""Initialize Ollama handler with configuration."""
self.config = self._load_config()
self.endpoint = self.config["ollama"]["url"]
self.default_model = self.config["ollama"]["default_model"]
self.summarize_prompt = self.config["ollama"]["summarize_prompt"]
self.client = Client(host=self.endpoint)
self.available = self._check_availability()
logger.info(f"Initialized Ollama handler with endpoint: {self.endpoint}")
logger.info(f"Default model: {self.default_model}")
logger.info(f"Ollama available: {self.available}")
def _load_config(self) -> configparser.ConfigParser:
"""Load configuration from config.ini file."""
config = configparser.ConfigParser()
config_path = os.path.join(os.path.dirname(__file__), "config.ini")
config.read(config_path)
return config
def _check_availability(self) -> bool:
"""Check if Ollama server is available."""
try:
self.client.list()
logger.info("Ollama server is available")
return True
except Exception as e:
logger.warning(f"Ollama server is not available: {str(e)}")
return False
def is_available(self) -> bool:
"""Check if Ollama is available and enabled."""
if not self.enabled:
logger.info("Ollama is disabled in config")
return False
try:
logger.info(f"Checking Ollama availability at {self.url}")
response = requests.get(f"{self.url}/api/tags")
available = response.status_code == 200
logger.info(
f"Ollama server response: {'available' if available else 'unavailable'}"
)
return available
except Exception as e:
logger.error(f"Error checking Ollama availability: {str(e)}")
return False
"""Return whether Ollama is available."""
return self.available
def get_available_models(self) -> list:
def get_available_models(self) -> List[str]:
"""Get list of available Ollama models."""
try:
logger.info("Fetching available Ollama models")
response = requests.get(f"{self.url}/api/tags")
if response.status_code == 200:
models = [model["name"] for model in response.json()["models"]]
logger.info(
f"Found {len(models)} available models: {', '.join(models)}"
)
return models
logger.warning(
f"Failed to fetch models. Status code: {response.status_code}"
)
return []
models = self.client.list()
model_names = [model["name"] for model in models["models"]]
logger.info(f"Found {len(model_names)} available models")
return model_names
except Exception as e:
logger.error(f"Error fetching Ollama models: {str(e)}")
logger.error(f"Error getting available models: {str(e)}")
return []
def validate_model(self, model_name: str) -> tuple[bool, Optional[str]]:
"""Validate if a model exists and return the first available model if not."""
available_models = self.get_available_models()
if not available_models:
return False, None
if model_name in available_models:
return True, model_name
logger.warning(
f"Model {model_name} not found in available models. Using first available model: {available_models[0]}"
)
return True, available_models[0]
def get_default_model(self) -> Optional[str]:
"""Get the default model, falling back to first available if default is not found."""
if not self.is_available():
def get_default_model(self) -> str:
"""Get the default model, falling back to first available if configured model not found."""
if not self.available:
return None
available_models = self.get_available_models()
@ -95,44 +68,44 @@ class OllamaHandler:
if self.default_model in available_models:
logger.info(f"Using configured default model: {self.default_model}")
return self.default_model
else:
logger.warning(
f"Configured model '{self.default_model}' not found, using first available model: {available_models[0]}"
)
return available_models[0]
logger.warning(
f"Configured model '{self.default_model}' not found in available models. Using first available model: {available_models[0]}"
)
return available_models[0]
def summarize(self, text: str, model: Optional[str] = None) -> Optional[str]:
def summarize(self, text: str, model: str = None) -> Optional[str]:
"""Summarize text using Ollama."""
if not self.is_available():
logger.warning("Attempted to summarize with Ollama unavailable")
if not self.available:
logger.warning("Cannot summarize: Ollama is not available")
return None
# Validate and get the correct model
is_valid, valid_model = self.validate_model(model or self.default_model)
if not is_valid:
logger.error("No valid Ollama models available")
if not text:
logger.warning("Cannot summarize: Empty text provided")
return None
prompt = f"{self.prompt}\n\n{text}"
logger.info(f"Generating summary using model: {valid_model}")
logger.info(f"Input text length: {len(text)} characters")
model = model or self.default_model
if not model:
logger.warning("Cannot summarize: No model specified")
return None
try:
response = requests.post(
f"{self.url}/api/generate",
json={"model": valid_model, "prompt": prompt, "stream": False},
logger.info(f"Generating summary using model: {model}")
logger.info(f"Input text length: {len(text)} characters")
# Generate the summary using the prompt from config
response = self.client.chat(
model=model,
messages=[
{"role": "system", "content": self.summarize_prompt},
{"role": "user", "content": text},
],
)
if response.status_code == 200:
summary = response.json()["response"]
logger.info(
f"Successfully generated summary of length: {len(summary)} characters"
)
return summary
logger.error(
f"Failed to generate summary. Status code: {response.status_code}"
)
return None
summary = response["message"]["content"]
logger.info(f"Summary generated. Length: {len(summary)} characters")
return summary
except Exception as e:
logger.error(f"Error during summarization: {str(e)}")
logger.error(f"Error generating summary: {str(e)}")
return None

View File

@ -5,4 +5,5 @@ torch>=2.0.0
torchaudio>=2.0.0
yt-dlp>=2023.12.30
pytube>=15.0.0
requests>=2.31.0
requests>=2.31.0
ollama>=0.3.0