Skip to content
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

Refactor Commit Message Generation with Improved API Support #1

Merged
merged 3 commits into from
Dec 5, 2024

Conversation

cr2007
Copy link
Contributor

@cr2007 cr2007 commented Dec 4, 2024

PR Description: Refactor Commit Message Generation with Improved API Support

Overview

This PR refactors the commit message generation logic by consolidating local and remote model handling into a unified function, improving maintainability and clarity. It also adds a robust URL parsing method and enhances error handling for improved stability in diverse environments. The main benefit here is that it eliminates the need for another library to be installed and imported.

Key Changes

  1. Unified generate_commit_message Function:

    • Combined local (Ollama) and remote (OpenAI/Gemini) logic into a single function.
    • With Ollama also having built-in capability with OpenAI API, it made more sense to combine the 2 functions into a single one.
    • Reduced redundancy by using a remote flag to switch between local and remote models.
  2. Enhanced API Key Handling:

    • Refined get_api_key to accept a remote parameter, supporting both local and remote models dynamically.
  3. Improved URL Handling:

    • Introduced parse_host to handle URL parsing, ensuring compatibility with IPv6 and custom ports.
    • Extracted from Ollama Python library (source)
  4. Error Handling & Encoding:

    • Enhanced subprocess calls with explicit UTF-8 encoding and error handling for robustness.
  5. Code Simplification:

    • Removed redundant functions and streamlined logic in interaction_loop.

Benefits

  • Increased Maintainability: Easier to extend or modify the commit generation logic without duplicating code.
  • Better Compatibility: Supports various API hosts, protocols, and IP formats (IPv4/IPv6).
  • Enhanced User Experience: Cleaner interaction loop and more reliable commit message generation.

This refactor positions the application for future scalability and ensures a smoother user experience.

- Unified local and remote model handling under
  `generate_commit_message_remote_model` with a new `remote` flag.
- Replaced Ollama-specific logic with a generalized approach to API key
  detection.
- Added `parse_host` function for handling remote host parsing,
  including IPv6 support.
- Improved error handling, logging, and type hints for better code
  clarity and robustness.
- Enhanced flexibility for both OpenAI and Gemini models with
  customizable base URLs.

Still a work-in-progress, need to tweak some basic changes
- Updated `subprocess.run` to use `shell=True` for `clear_screen`
  command to ensure compatibility with Windows environments.
- Updated Ollama base URL with '/v1' for correct API endpoint.
- Integrated `OLLAMA_HOST` environment variable for dynamic URL
  configuration.
- Renamed `generate_commit_message_remote_model` to
  `generate_commit_message`.
@yankeexe yankeexe merged commit 800ca0d into yankeexe:main Dec 5, 2024
1 check passed
@yankeexe
Copy link
Owner

yankeexe commented Dec 5, 2024

Thanks @cr2007 for the neat upgrade!

@cr2007 cr2007 deleted the ollama-model-refactor branch December 5, 2024 07:13
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.

2 participants