mirror of
https://github.com/tcsenpai/youlama.git
synced 2025-06-06 19:25:39 +00:00
updated prompt and generation technique
This commit is contained in:
parent
0c1be59296
commit
3e69817ba0
@ -21,4 +21,21 @@ available_languages = en,es,fr,de,it,pt,nl,ja,ko,zh
|
|||||||
enabled = false
|
enabled = false
|
||||||
url = http://localhost:11434
|
url = http://localhost:11434
|
||||||
default_model = mistral
|
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."
|
||||||
|
|
@ -1,8 +1,8 @@
|
|||||||
import requests
|
|
||||||
from typing import Optional
|
|
||||||
import configparser
|
|
||||||
import os
|
import os
|
||||||
|
import configparser
|
||||||
import logging
|
import logging
|
||||||
|
from typing import List, Optional
|
||||||
|
from ollama import Client
|
||||||
|
|
||||||
# Configure logging
|
# Configure logging
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
@ -11,81 +11,54 @@ logging.basicConfig(
|
|||||||
logger = logging.getLogger(__name__)
|
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:
|
class OllamaHandler:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.enabled = config["ollama"].getboolean("enabled")
|
"""Initialize Ollama handler with configuration."""
|
||||||
self.url = config["ollama"]["url"]
|
self.config = self._load_config()
|
||||||
self.default_model = config["ollama"]["default_model"]
|
self.endpoint = self.config["ollama"]["url"]
|
||||||
self.prompt = config["ollama"]["summarize_prompt"]
|
self.default_model = self.config["ollama"]["default_model"]
|
||||||
logger.info(
|
self.summarize_prompt = self.config["ollama"]["summarize_prompt"]
|
||||||
f"Initialized Ollama handler with URL: {self.url}, Default model: {self.default_model}"
|
self.client = Client(host=self.endpoint)
|
||||||
)
|
self.available = self._check_availability()
|
||||||
logger.info(f"Ollama enabled: {self.enabled}")
|
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:
|
def is_available(self) -> bool:
|
||||||
"""Check if Ollama is available and enabled."""
|
"""Return whether Ollama is available."""
|
||||||
if not self.enabled:
|
return self.available
|
||||||
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
|
|
||||||
|
|
||||||
def get_available_models(self) -> list:
|
def get_available_models(self) -> List[str]:
|
||||||
"""Get list of available Ollama models."""
|
"""Get list of available Ollama models."""
|
||||||
try:
|
try:
|
||||||
logger.info("Fetching available Ollama models")
|
models = self.client.list()
|
||||||
response = requests.get(f"{self.url}/api/tags")
|
model_names = [model["name"] for model in models["models"]]
|
||||||
if response.status_code == 200:
|
logger.info(f"Found {len(model_names)} available models")
|
||||||
models = [model["name"] for model in response.json()["models"]]
|
return model_names
|
||||||
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 []
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error fetching Ollama models: {str(e)}")
|
logger.error(f"Error getting available models: {str(e)}")
|
||||||
return []
|
return []
|
||||||
|
|
||||||
def validate_model(self, model_name: str) -> tuple[bool, Optional[str]]:
|
def get_default_model(self) -> str:
|
||||||
"""Validate if a model exists and return the first available model if not."""
|
"""Get the default model, falling back to first available if configured model not found."""
|
||||||
available_models = self.get_available_models()
|
if not self.available:
|
||||||
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():
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
available_models = self.get_available_models()
|
available_models = self.get_available_models()
|
||||||
@ -95,44 +68,44 @@ class OllamaHandler:
|
|||||||
if self.default_model in available_models:
|
if self.default_model in available_models:
|
||||||
logger.info(f"Using configured default model: {self.default_model}")
|
logger.info(f"Using configured default model: {self.default_model}")
|
||||||
return 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(
|
def summarize(self, text: str, model: str = None) -> Optional[str]:
|
||||||
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]:
|
|
||||||
"""Summarize text using Ollama."""
|
"""Summarize text using Ollama."""
|
||||||
if not self.is_available():
|
if not self.available:
|
||||||
logger.warning("Attempted to summarize with Ollama unavailable")
|
logger.warning("Cannot summarize: Ollama is not available")
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Validate and get the correct model
|
if not text:
|
||||||
is_valid, valid_model = self.validate_model(model or self.default_model)
|
logger.warning("Cannot summarize: Empty text provided")
|
||||||
if not is_valid:
|
|
||||||
logger.error("No valid Ollama models available")
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
prompt = f"{self.prompt}\n\n{text}"
|
model = model or self.default_model
|
||||||
logger.info(f"Generating summary using model: {valid_model}")
|
if not model:
|
||||||
logger.info(f"Input text length: {len(text)} characters")
|
logger.warning("Cannot summarize: No model specified")
|
||||||
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.post(
|
logger.info(f"Generating summary using model: {model}")
|
||||||
f"{self.url}/api/generate",
|
logger.info(f"Input text length: {len(text)} characters")
|
||||||
json={"model": valid_model, "prompt": prompt, "stream": False},
|
|
||||||
|
# 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["message"]["content"]
|
||||||
summary = response.json()["response"]
|
logger.info(f"Summary generated. Length: {len(summary)} characters")
|
||||||
logger.info(
|
return summary
|
||||||
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
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Error during summarization: {str(e)}")
|
logger.error(f"Error generating summary: {str(e)}")
|
||||||
return None
|
return None
|
||||||
|
@ -6,3 +6,4 @@ torchaudio>=2.0.0
|
|||||||
yt-dlp>=2023.12.30
|
yt-dlp>=2023.12.30
|
||||||
pytube>=15.0.0
|
pytube>=15.0.0
|
||||||
requests>=2.31.0
|
requests>=2.31.0
|
||||||
|
ollama>=0.3.0
|
Loading…
x
Reference in New Issue
Block a user