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

Fix Incorrect Fallback Logic for Image Model Provider API Keys #1270

Closed
UD1sto opened this issue Dec 20, 2024 · 0 comments
Closed

Fix Incorrect Fallback Logic for Image Model Provider API Keys #1270

UD1sto opened this issue Dec 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@UD1sto
Copy link
Contributor

UD1sto commented Dec 20, 2024

Describe the bug
At packages/core/src/generate.ts When the imageModelProvider did not match modelProvider, the code iteratively checked for API keys in a fallback chain. It stopped at the first available key (e.g., FAL_API_KEY), even if it belonged to the wrong provider. As a result, the selected API key could be incorrect (e.g., returning and using a key from FAL instead key from Venice).

To Reproduce

  1. Set imageModelProvider to VENICE in the character configuration.
  2. Provide both FAL_API_KEY and VENICE_API_KEY in .env(or any other relevant key combination).
  3. When requesting an image generation observe that the code selects FAL_API_KEY instead of VENICE_API_KEY.

Expected behavior
If a specific imageModelProvider is set, the code should return the matching API key for that provider if it exists. If no specific provider key is found, the code should then try the fallback chain in sequence.

image

Fix
image

const apiKey = runtime.imageModelProvider === runtime.modelProvider ? runtime.token : (() => { // First try to match the specific provider switch (runtime.imageModelProvider) { case ModelProviderName.HEURIST: return runtime.getSetting("HEURIST_API_KEY"); case ModelProviderName.TOGETHER: return runtime.getSetting("TOGETHER_API_KEY"); case ModelProviderName.FAL: return runtime.getSetting("FAL_API_KEY"); case ModelProviderName.OPENAI: return runtime.getSetting("OPENAI_API_KEY"); case ModelProviderName.VENICE: return runtime.getSetting("VENICE_API_KEY"); default: // If no specific match, try the fallback chain return (runtime.getSetting("HEURIST_API_KEY") ?? runtime.getSetting("TOGETHER_API_KEY") ?? runtime.getSetting("FAL_API_KEY") ?? runtime.getSetting("OPENAI_API_KEY") ?? runtime.getSetting("VENICE_API_KEY"); } })();

This fix updates the logic to first attempt to retrieve the API key from the specifically selected imageModelProvider. If that is not present, it will proceed down the fallback chain. This ensures that if an an image model api key is set, it is correctly chosen over other existing keys which are higher in the selection list, like FAL_API_KEY.

PS: Solution is implemented and tested, creating bug report for tracked pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant