Docs/Refactor: add provider-agnostic configuration examples and dynamic key resolution (#4)#6
Docs/Refactor: add provider-agnostic configuration examples and dynamic key resolution (#4)#6phatngo511 wants to merge 5 commits intosunilp:masterfrom
Conversation
Refactor: make API key selection provider-specific in config.py (sunilp#4).
Updated the create_client factory function in model_client.py to support custom API keys for local model providers. Instead of a hardcoded "local" string, it now prioritizes the provided api_key, making the setup more flexible for users using custom local endpoints.
Updated the create_client factory function in model_client.py to support custom API keys for local model providers. Instead of a hardcoded "local" string, it now prioritizes the provided api_key, making the setup more flexible for users using custom local endpoints.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughRefactors configuration to be provider-agnostic: Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~18 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (3)
src/shared/config.py (1)
31-35: Consider supportingLOCAL_API_KEYfor authenticated local endpoints.Since
create_clientnow supports custom API keys for local providers, you may want to add support for aLOCAL_API_KEYenvironment variable here for consistency:Suggested enhancement
`@property` def api_key(self) -> str: """Dynamically fetch the correct API key based on the provider.""" if self.provider == "openai": return os.getenv("OPENAI_API_KEY", "") if self.provider == "anthropic": return os.getenv("ANTHROPIC_API_KEY", "") + if self.provider == "local": + return os.getenv("LOCAL_API_KEY", "") return ""This would enable authenticated local deployments (e.g., vLLM with API keys) to work seamlessly through
ModelConfig.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/shared/config.py` around lines 31 - 35, The current API key selection only returns OPENAI_API_KEY or ANTHROPIC_API_KEY based on self.provider; add support for authenticated local endpoints by checking if self.provider == "local" (or whatever local provider identifier is used in ModelConfig) and returning os.getenv("LOCAL_API_KEY", "") so create_client can use custom API keys for local deployments—update the method/property that inspects self.provider (the code block shown) to include this additional branch while preserving existing fallbacks.src/shared/model_client.py (1)
202-204: Translate comment to English for codebase consistency.The logic change is correct and aligns well with the dynamic key resolution from
ModelConfig. However, the comment is in Vietnamese while the rest of the codebase uses English.Suggested fix
- # FIX: Sửa lỗi hardcode 'local'. Ưu tiên dùng api_key nếu có. + # Use provided api_key if available, otherwise default to "local" actual_key = api_key if api_key else "local"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/shared/model_client.py` around lines 202 - 204, Replace the Vietnamese inline comment above the dynamic key resolution with an English comment that explains the change: that we avoid hardcoding "local" and prefer the supplied api_key (falling back to "local" only if api_key is falsy). Update the comment near actual_key = api_key if api_key else "local" to something like "Prefer provided api_key; fall back to 'local' to avoid hardcoding" and ensure references to ModelConfig/OpenAIClient/model_name/base_url (url) remain unchanged..env.example (1)
21-26: Consider documentingLOCAL_API_KEYoption for authenticated local endpoints.The
create_clientfunction now supports custom API keys for the local provider, but there's no corresponding environment variable documented here. Some local deployments (e.g., vLLM with authentication) may require an API key.Suggested addition
# --- Option 3: Local Models (e.g., Ollama) --- # MODEL_PROVIDER=local +# LOCAL_API_KEY=your-local-key-here # Optional: only if your local server requires auth # LOCAL_MODEL_URL=http://localhost:11434/v1 # MODEL_NAME=llama3 # MODEL_NAME_CHEAP=llama3 # EVAL_JUDGE_MODEL=llama3Note: This would also require updating
ModelConfig.api_keyinsrc/shared/config.pyto readLOCAL_API_KEYwhen the provider islocal.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.env.example around lines 21 - 26, Add a documented LOCAL_API_KEY entry to .env.example and wire it into the config so local provider auth works: update the example block for the local model option to include a commented LOCAL_API_KEY line, and modify ModelConfig.api_key (used by create_client) to read LOCAL_API_KEY when MODEL_PROVIDER or equivalent is set to "local" so authenticated local endpoints are supported by create_client.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In @.env.example:
- Around line 21-26: Add a documented LOCAL_API_KEY entry to .env.example and
wire it into the config so local provider auth works: update the example block
for the local model option to include a commented LOCAL_API_KEY line, and modify
ModelConfig.api_key (used by create_client) to read LOCAL_API_KEY when
MODEL_PROVIDER or equivalent is set to "local" so authenticated local endpoints
are supported by create_client.
In `@src/shared/config.py`:
- Around line 31-35: The current API key selection only returns OPENAI_API_KEY
or ANTHROPIC_API_KEY based on self.provider; add support for authenticated local
endpoints by checking if self.provider == "local" (or whatever local provider
identifier is used in ModelConfig) and returning os.getenv("LOCAL_API_KEY", "")
so create_client can use custom API keys for local deployments—update the
method/property that inspects self.provider (the code block shown) to include
this additional branch while preserving existing fallbacks.
In `@src/shared/model_client.py`:
- Around line 202-204: Replace the Vietnamese inline comment above the dynamic
key resolution with an English comment that explains the change: that we avoid
hardcoding "local" and prefer the supplied api_key (falling back to "local" only
if api_key is falsy). Update the comment near actual_key = api_key if api_key
else "local" to something like "Prefer provided api_key; fall back to 'local' to
avoid hardcoding" and ensure references to
ModelConfig/OpenAIClient/model_name/base_url (url) remain unchanged.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 89387843-98aa-42cd-bc36-d19a820eb884
📒 Files selected for processing (3)
.env.examplesrc/shared/config.pysrc/shared/model_client.py
sunilp
left a comment
There was a problem hiding this comment.
Thanks for the contribution! The .env.example restructure and the dynamic api_key property are both clean improvements.
Two changes needed before merge:
-
Restore the "Why this matters" docstring in
model_client.py(lines 5-9 in the original). This is a book repo where educational comments are intentional teaching content, not boilerplate to trim. -
Change the comment on line 202 to English. The repo is English-only.
Happy to merge once those are addressed.
|
Hi @sunilp, I've updated model_client.py to restore the original docstring and translated the comment to English. Thanks for your guidance! |
Hi @sunilp,
I've completed issue #4 with these improvements:
Fixes Add provider-agnostic configuration examples #4.
Summary by CodeRabbit