-
Notifications
You must be signed in to change notification settings - Fork 19
fix DFanso#120 : Replace magic numbers with constants in LLM providers #123
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
Conversation
- Add informational logging for successful Save, ChangeDefault, DeleteModel, and UpdateAPIKey operations - Provide user feedback on state changes with fmt.Printf() following existing patterns - Improve visibility into store operation completion for better debugging experience
fix DFanso#121 : add systematic logging for store operations
- Defined constants for model names, token limits, temperature settings, API endpoints, and headers in the following modules: - Claude - Groq - Gemini - ChatGPT - Grok - Ollama - Improved code maintainability and consistency by eliminating hardcoded values.
This reverts commit 7d01100.
fix DFanso#120 : Replace magic numbers with constants in LLM providers
WalkthroughThis PR centralizes configuration across multiple AI provider integrations (ChatGPT, Claude, Gemini, Grok, Groq, Ollama) by extracting hard-coded string and numeric literals into package-level constants. No functional behavior changes; only internal constant definitions and their usage are modified. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Homogeneous, repetitive refactoring pattern applied consistently across six related files. Each change follows an identical approach: define constants and replace literals. No complex logic, no public API changes, no control-flow modifications. Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
internal/claude/claude.go (1)
14-22: Consider consistent naming convention for all constants.While the constant extraction is correct, the naming is inconsistent:
- Some constants use the
claudeprefix:claudeModel,claudeMaxTokens,claudeAPIEndpoint,claudeAPIVersion- Others use generic names:
contentTypeJSON,xAPIKeyHeader,anthropicVersionHeaderFor consistency with other provider files (grok, groq, ollama) that prefix all constants, consider renaming to:
claudeContentTypeinstead ofcontentTypeJSONclaudeAPIKeyHeaderinstead ofxAPIKeyHeaderclaudeVersionHeaderinstead ofanthropicVersionHeaderThis makes it clearer that these are Claude-specific constants and improves consistency across the codebase.
Apply this diff for consistent naming:
const ( claudeModel = "claude-3-5-sonnet-20241022" claudeMaxTokens = 200 claudeAPIEndpoint = "https://api.anthropic.com/v1/messages" claudeAPIVersion = "2023-06-01" - contentTypeJSON = "application/json" - anthropicVersionHeader = "anthropic-version" - xAPIKeyHeader = "x-api-key" + claudeContentType = "application/json" + claudeVersionHeader = "anthropic-version" + claudeAPIKeyHeader = "x-api-key" )Then update the usages accordingly:
- req.Header.Set("Content-Type", contentTypeJSON) - req.Header.Set(xAPIKeyHeader, apiKey) - req.Header.Set(anthropicVersionHeader, claudeAPIVersion) + req.Header.Set("Content-Type", claudeContentType) + req.Header.Set(claudeAPIKeyHeader, apiKey) + req.Header.Set(claudeVersionHeader, claudeAPIVersion)internal/grok/grok.go (1)
14-20: Minor naming inconsistency in constant prefix.Most constants correctly use the
grokprefix (grokModel,grokTemperature,grokAPIEndpoint,grokContentType), butauthorizationPrefixlacks it.For consistency, consider renaming to
grokAuthorizationPrefix:const ( grokModel = "grok-3-mini-fast-beta" grokTemperature = 0 grokAPIEndpoint = "https://api.x.ai/v1/chat/completions" grokContentType = "application/json" - authorizationPrefix = "Bearer " + grokAuthorizationPrefix = "Bearer " )Then update line 54:
- req.Header.Set("Authorization", fmt.Sprintf("%s%s", authorizationPrefix, apiKey)) + req.Header.Set("Authorization", fmt.Sprintf("%s%s", grokAuthorizationPrefix, apiKey))
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
internal/chatgpt/chatgpt.go(2 hunks)internal/claude/claude.go(3 hunks)internal/gemini/gemini.go(2 hunks)internal/grok/grok.go(3 hunks)internal/groq/groq.go(3 hunks)internal/ollama/ollama.go(4 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
- GitHub Check: Build Go Binary (windows-latest)
- GitHub Check: Test (1.23)
🔇 Additional comments (7)
internal/chatgpt/chatgpt.go (1)
13-15: LGTM! Clean constant extraction.The model constant is well-named and properly applied. This improves maintainability by centralizing the model configuration.
Also applies to: 29-29
internal/gemini/gemini.go (1)
13-16: LGTM! Model and temperature constants properly extracted.The constants follow good naming conventions and centralize configuration values effectively.
Also applies to: 33-34
internal/ollama/ollama.go (1)
14-18: LGTM! Constants properly extracted and applied.All hardcoded values are now centralized as named constants with clear prefixes. The implementation is consistent and improves maintainability.
Also applies to: 37-37, 47-47, 60-60
internal/claude/claude.go (1)
47-48: Constants properly applied throughout the function.The extracted constants are correctly used in the request construction, successfully eliminating the magic values.
Also applies to: 63-63, 68-70
internal/groq/groq.go (2)
39-45: LGTM! Constants are well-defined and consistently applied.All constants follow the
groqprefix convention and are properly used throughout the request construction. The temperature, token limits, system message, content type, and authorization prefix are now centralized.Also applies to: 73-74, 76-76, 96-97
47-56: Clarify the purpose of httpClient initialization.The addition of a package-level
httpClientvariable andinit()function appears to be beyond the scope of extracting magic numbers/constants. The comment indicates it's for test overrides, but this pattern differs from other provider files that callhttpClient.GetClient()directly.Is this change necessary for this PR, or should it be separated into a different PR focused on improving testability?
If this is intentional for testing purposes, consider:
- Adding a comment explaining why Groq needs this pattern while other providers don't
- Potentially applying this pattern consistently across all providers in a separate PR
internal/grok/grok.go (1)
36-36: Constants properly applied throughout.The extracted constants are correctly used in request construction, successfully eliminating hardcoded values and improving maintainability.
Also applies to: 38-38, 47-47, 53-54
DFanso
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
approved 🎊
Summary by CodeRabbit