From f4180946b1b09f6f2572f33c01e8447b70125915 Mon Sep 17 00:00:00 2001 From: Ryan Yang Date: Thu, 19 Dec 2024 17:10:50 +0800 Subject: [PATCH] feat: Add AI summary generation and bump version to 0.1.2 - Introduced configuration options for AI summary generation in blog-ai-tool.toml, including a toggle and maximum length setting. - Updated metadata generation logic in blog_seo.py to include AI-generated summaries based on the new configuration. - Bumped project version to 0.1.2 in pyproject.toml and __init__.py to reflect the new features. --- blog-ai-tool.toml | 2 ++ blog-ai-tool/__init__.py | 2 +- blog-ai-tool/blog_seo.py | 15 ++++++++++++--- pyproject.toml | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/blog-ai-tool.toml b/blog-ai-tool.toml index 9a0083e..ac8fd85 100644 --- a/blog-ai-tool.toml +++ b/blog-ai-tool.toml @@ -15,6 +15,8 @@ generate_keywords = false # Whether to generate keywords max_title_length = 100 # Maximum title length max_description_length = 300 # Maximum description length keyword_count = 5 # Number of keywords to generate +ai_summary = false # Whether to generate ai summary +ai_summary_length = 1000 # Maximum ai summary length [translation] use = true # Whether to translate articles diff --git a/blog-ai-tool/__init__.py b/blog-ai-tool/__init__.py index 44dc944..5060beb 100644 --- a/blog-ai-tool/__init__.py +++ b/blog-ai-tool/__init__.py @@ -1,5 +1,5 @@ from .blog_seo import HugoBlogProcessor, metadata_blog_directory from .main import load_config -__version__ = "0.1.1" +__version__ = "0.1.2" __all__ = ["HugoBlogProcessor", "metadata_blog_directory", "load_config"] diff --git a/blog-ai-tool/blog_seo.py b/blog-ai-tool/blog_seo.py index 67ec918..36a5be2 100644 --- a/blog-ai-tool/blog_seo.py +++ b/blog-ai-tool/blog_seo.py @@ -13,16 +13,22 @@ def __init__(self, api_key: str, base_url: str, model: str, language: str, confi def generate_metadata(self, content: str) -> Dict[str, str]: """Generate metadata using OpenAI based on content""" metadata_config = self.config["metadata"] + ai_summary_config = metadata_config["ai_summary"] + if ai_summary_config: + ai_summary_task = f"""4. Based on the following blog content, generate a summary (max {metadata_config['ai_summary_length']} words with plain text)""" + else: + ai_summary_task = "" prompt = f"""Based on the following blog content, generate: -1. A concise title (max {metadata_config['max_title_length']} chars) -2. A brief description (max {metadata_config['max_description_length']} chars) +1. A concise title (max {metadata_config['max_title_length']} words) +2. A brief description (max {metadata_config['max_description_length']} words) 3. {metadata_config['keyword_count']} relevant keywords (space-separated) +{ai_summary_task} The language should be {self.language}. Content: {content} -Format response as JSON with keys: title, description, keywords""" +Format response as JSON with keys: title, description, keywords{", summary" if ai_summary_config else ""}""" response = self._call_openai(prompt) return self._extract_json(response) @@ -49,6 +55,9 @@ def process_markdown(self, file_path: str) -> None: metadata['description'] = generated['description'] if not metadata.get('keywords'): metadata['keywords'] = generated['keywords'] + if generated.get("summary") and not metadata.get('summary'): + metadata['summary'] = generated['summary'] + if needs_update: # Create new post with updated metadata diff --git a/pyproject.toml b/pyproject.toml index abc748d..4a618c4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "hatchling.build" [project] name = "blog-ai-tool" -version = "0.1.1" +version = "0.1.2" description = "Generate seo content (title, description, keywords) for blog post with AI, support blog framework like Hexo, Hugo, etc." readme = "README.md" requires-python = ">=3.8"