Skip to content

Add article summary feature with OpenAI integration #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

paskal
Copy link
Member

@paskal paskal commented Aug 11, 2024

Summary

This PR adds article summarization capabilities using OpenAI integration:

  • Introduces article summary generation using OpenAI's API
  • Adds a 'summary' query parameter to the parser endpoint
  • Implements MongoDB-based summary caching with automatic expiration
  • Provides metrics for monitoring summary generation performance
  • Adds rate limiting for OpenAI API requests
  • Implements summary display in the preview interface

Key Features

API Integration

  • Integrates with OpenAI API using github.com/sashabaranov/go-openai
  • Adds OpenAI configuration settings in command-line flags and environment variables
  • Supports multiple OpenAI models (default is GPT-4o-mini)
  • Provides a customizable summary generation prompt

Security & Performance

  • Requires a valid server token or authentication for summary requests
  • Implements content length limits for OpenAI requests
  • Features configurable rate limiting for API calls
  • Includes summary caching with automatic expiration and cleanup

User Interface

  • Displays summaries in the preview interface with proper formatting
  • Provides visual separation between article content and summary

Technical Details

  • Creates a MongoDB-based summary cache system
  • Implements metrics for tracking cache hits/misses and response times
  • Handles error cases gracefully with appropriate user feedback
  • Includes comprehensive test coverage
  • Provides mock interfaces for testing

Testing

Tested manually with various article types:

  • Summary generation time is around 4 seconds with GPT-4o
  • Up to 8 seconds with GPT-4o mini
  • Up to 15 seconds with GPT-4

First part of resolution for #27.

Preview interface with summary
Summary in article view

@paskal paskal requested a review from umputun August 11, 2024 23:02
@paskal paskal force-pushed the openai_rules_generator branch 2 times, most recently from a380986 to 3d48955 Compare August 11, 2024 23:08
@coveralls
Copy link

coveralls commented Aug 11, 2024

Pull Request Test Coverage Report for Build 14583058545

Details

  • 219 of 413 (53.03%) changed or added relevant lines in 7 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-13.6%) to 75.184%

Changes Missing Coverage Covered Lines Changed/Added Lines %
backend/main.go 13 14 92.86%
backend/extractor/openai_mock.go 13 24 54.17%
backend/datastore/summaries.go 46 59 77.97%
backend/rest/server.go 19 49 38.78%
backend/extractor/summaries_mock.go 26 93 27.96%
backend/extractor/readability.go 94 166 56.63%
Totals Coverage Status
Change from base Build 14582822970: -13.6%
Covered Lines: 818
Relevant Lines: 1088

💛 - Coveralls

@paskal paskal marked this pull request as draft August 27, 2024 20:16
@paskal paskal force-pushed the openai_rules_generator branch 3 times, most recently from 564cd20 to 48eed4a Compare September 23, 2024 22:34
@paskal paskal force-pushed the openai_rules_generator branch 2 times, most recently from 1cafeda to 28b52a1 Compare October 9, 2024 20:42
@paskal paskal marked this pull request as ready for review October 9, 2024 20:44
@paskal paskal force-pushed the openai_rules_generator branch from 28b52a1 to 7229311 Compare October 9, 2024 20:46
@paskal paskal force-pushed the openai_rules_generator branch 3 times, most recently from 739644f to 04b9ae4 Compare March 22, 2025 23:40
Copy link
Collaborator

@umputun umputun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've reviewed the PR that adds the OpenAI article summary feature, and I'm impressed with the overall implementation. Here's my detailed feedback:

Strengths

  • Well-structured code with proper interfaces and separation of concerns
  • Comprehensive test coverage for the new functionality
  • Effective caching mechanism using content hash to minimize API costs
  • Token-based security for summary generation
  • Clean UI integration for displaying summaries
  • Clear documentation in README

Suggestions for Improvement

  1. Error Handling & Rate Limiting

    • Consider adding specific handling for OpenAI rate limits and API errors
    • Implement exponential backoff for failed requests
    • Add configurable rate limiting to prevent excessive API costs
  2. Configuration Options

    • Add a configuration option to limit maximum content length sent to OpenAI
    • Consider adding a feature flag to completely disable summary functionality if needed
    • Add timeout configuration specific to OpenAI requests
  3. Caching Enhancement

    • Consider adding TTL (time to live) for cached summaries
    • Implement a cache eviction policy for old/unused summaries
    • Add metrics for cache hit/miss ratio
  4. Documentation

    • Include example response format in README
    • Document error codes specific to summary generation
    • Add information about expected API costs
  5. Testing

    • Add more error path tests (API key missing, OpenAI service down, etc.)
    • Consider adding benchmark tests for performance-critical paths
  6. Security

    • Ensure sensitive content is not leaked to the OpenAI API
    • Consider adding content filtering options before sending to OpenAI
    • Add audit logging for summary generation requests
  7. Performance

    • Since you mentioned GPT-4o mini is slower than GPT-4o, it might be worth investigating if this is consistent or if there are ways to optimize the model selection based on content characteristics
    • Consider implementing concurrent requests for batch processing if that becomes a feature

Overall, this is a well-implemented feature that adds significant value to the application. The code is clean, well-tested, and follows good practices.

Copy link
Collaborator

@umputun umputun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review for PR #32: Add article summary feature with OpenAI integration

Overview

This PR adds functionality to generate article summaries using OpenAI's GPT models with caching in MongoDB. It includes UI integration to display summaries in the preview interface and proper API endpoints with token-based authentication.

Critical Issues

Issue Location Description Recommendation
None identified

Security Concerns

Issue Location Description Recommendation
Sensitive content handling extractor/readability.go No filtering mechanism for content sent to OpenAI Add content sanitization before sending to OpenAI to prevent sensitive information leakage
Rate limit protection extractor/readability.go No explicit rate limiting for OpenAI API calls Implement configurable rate limiting to prevent excessive API costs

Design and Architecture

Issue Location Description Recommendation
Cache management datastore/summaries.go No TTL or cache eviction policy Add TTL for cached summaries and implement cache cleanup to prevent unbounded growth
OpenAI client setup main.go OpenAI client configuration lacks timeout settings Add specific timeout configuration for OpenAI requests
Model selection - GPT-4o appears faster than GPT-4o mini (counter-intuitive) Investigate performance characteristics and consider default to the faster model

Code Quality

Issue Location Description Recommendation
Error handling extractor/readability.go OpenAI-specific errors not handled distinctly Add specialized error handling for common OpenAI API errors (rate limits, context length)
Hardcoded values extractor/readability.go Summary prompt not configurable Extract prompt to configuration to allow customization

Performance Considerations

Issue Location Description Recommendation
Content truncation extractor/readability.go No explicit limit on content sent to OpenAI Add configurable maximum content length to avoid token limit errors
Concurrent requests - No support for batch processing Consider implementing concurrent requests if batch processing becomes a feature

Testing

Issue Location Description Recommendation
Error path testing extractor/readability_test.go Limited tests for API failure scenarios Add tests for various OpenAI API failure modes
Performance testing - No benchmark tests for performance-critical paths Add benchmark tests, especially for content processing and caching

Positive Highlights

  • Well-structured code with proper interfaces and separation of concerns
  • Comprehensive test coverage for the new functionality
  • Effective caching mechanism using content hash to minimize API costs
  • Token-based security for summary generation
  • Clean UI integration for displaying summaries
  • Clear documentation in README

Proposed Changes

1. Issue: Add configurable content length limit

Location: extractor/readability.go

Current code:

// generateSummary creates a summary of the article using OpenAI API
func (f *UReadability) generateSummary(ctx context.Context, content, title string) (string, error) {
    // Current implementation sends full content without explicit limits
}

Proposed change:

// generateSummary creates a summary of the article using OpenAI API
func (f *UReadability) generateSummary(ctx context.Context, content, title string) (string, error) {
    // Add content truncation
    if f.MaxContentLength > 0 && len(content) > f.MaxContentLength {
        content = content[:f.MaxContentLength] + "..."
    }
    
    // Rest of implementation
}

Rationale: Prevents hitting OpenAI token limits unexpectedly and provides control over API costs.

2. Issue: Add TTL for cached summaries

Location: datastore/summaries.go

Current code:

// Summary contains information about a cached summary
type Summary struct {
    ID        string    `bson:"_id"`     // SHA256 hash of the content
    Content   string    `bson:"content"` // Original content that was summarized
    Summary   string    `bson:"summary"` // Generated summary
    Model     string    `bson:"model"`   // OpenAI model used for summarization
    CreatedAt time.Time `bson:"created_at"`
    UpdatedAt time.Time `bson:"updated_at"`
}

Proposed change:

// Summary contains information about a cached summary
type Summary struct {
    ID        string    `bson:"_id"`     // SHA256 hash of the content
    Content   string    `bson:"content"` // Original content that was summarized
    Summary   string    `bson:"summary"` // Generated summary
    Model     string    `bson:"model"`   // OpenAI model used for summarization
    CreatedAt time.Time `bson:"created_at"`
    UpdatedAt time.Time `bson:"updated_at"`
    ExpiresAt time.Time `bson:"expires_at"` // When this summary expires
}

// Save creates or updates summary in the database
func (s SummariesDAO) Save(ctx context.Context, summary Summary) error {
    // Set default expiration if not provided
    if summary.ExpiresAt.IsZero() {
        summary.ExpiresAt = time.Now().AddDate(0, 1, 0) // Default 1 month TTL
    }
    
    // Rest of implementation
}

Rationale: Provides automatic expiration of cached summaries, preventing unbounded database growth.

Summary

This PR is well-implemented with good code quality, architecture, and testing. The addition of article summarization using OpenAI's GPT models with MongoDB caching is a valuable feature. The main recommendations focus on:

  1. Improving error handling for OpenAI-specific errors
  2. Adding content limits and filtering for security and cost control
  3. Implementing cache management policies
  4. Adding configuration options for fine-tuning the summarization behavior

Overall, this is a solid implementation that follows good practices. With the suggested improvements, it will be more robust, secure, and maintainable.

@paskal paskal force-pushed the openai_rules_generator branch 4 times, most recently from 283cad3 to e26bdfc Compare April 21, 2025 19:36
- Introduce 'summary' query parameter in /api/content/v1/parser endpoint
- Integrate OpenAI API for generating article summaries
- Add OpenAIKey field to Server struct and corresponding command-line
flag
- Update extractArticleEmulateReadability to handle summary requests
- Add generateSummary method using OpenAI's GPT-4o model (turns out to
be faster than even 4o mini)
- Add OpenAIClient interface and mock for testing
- Update README.md with new configuration options and API details

This feature allows users to request a summary of extracted articles
using OpenAI's GPT-4o model. To ensure secure usage, summary generation
requires a valid server token. The changes include comprehensive error
handling and test coverage for various scenarios, including token
validation and server misconfiguration.

# Conflicts:
#	backend/go.mod
#	backend/rest/server.go
@paskal paskal force-pushed the openai_rules_generator branch from e26bdfc to b83c76c Compare April 21, 2025 23:05
@paskal paskal requested a review from umputun April 21, 2025 23:06
Implement MongoDB cache for summaries to reduce API costs and improve performance.
Rename parameters from OpenAI-specific to more generic API names.
Support configurable model selection through ModelType enum or direct model names.
Add comprehensive tests with mocks for summary generation and caching.
Update documentation in README with summary feature details.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <[email protected]>
@paskal paskal force-pushed the openai_rules_generator branch from b83c76c to 8db4950 Compare April 21, 2025 23:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants