Refactor AI services and improve modularity and error handling using template method pattern#61
Open
Refactor AI services and improve modularity and error handling using template method pattern#61
Conversation
Introduce `BaseAIService`, an abstract class handling daily limits, request preparation, and error logging, as a foundation for AI-related services. Implement `OpenAIService`, extending `BaseAIService`, to integrate with OpenAI's GPT-3.5 API for dynamic, personalized cover letter generation.
Moved OpenAI macro registration to a dedicated AIServiceProvider to improve modularity and separation of concerns. Updated the provider registration in the bootstrap file and removed related logic from AppServiceProvider.
Removed AIService and replaced it with BaseAIService for better modularization. Improved error handling by centralizing logic into a `handleError` method and logging detailed exception data for debugging. Streamlined the response streaming process with logging to better track partial AI responses.
SumonMSelim
reviewed
May 4, 2025
| $isRegeneration ? $this->feedback : null, | ||
| $previousAnswer | ||
| ); | ||
| $aiService = app(BaseAIService::class); |
There was a problem hiding this comment.
Don't create services like this. better use dependency injection. It will be easy to test.
SumonMSelim
reviewed
May 4, 2025
| final public function getChatResponse(User $user, array $jobData, callable $callback, ?string $feedback = null, ?string $previousAnswer = null): string | ||
| { | ||
| try { | ||
| Log::info('BaseAIService: Starting getChatResponse'); |
There was a problem hiding this comment.
You shouldn't log unnecessary information too much as info. Better use these as debug logs if necessary. Otherwise, your log will get full of information that might not be needed generally.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In this PR I have introduced template method pattern for ai service integration. The main purpose of this architectural change for future-proof. I will add multiple llm api soon for cover letter generation that's why I have decided to implement this pattern