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

feat: set API key from settings modal #1319

Merged
merged 16 commits into from Apr 29, 2024
Merged

feat: set API key from settings modal #1319

merged 16 commits into from Apr 29, 2024

Conversation

Sparkier
Copy link
Collaborator

@Sparkier Sparkier commented Apr 23, 2024

depends on #1291

fix #1146

@Sparkier Sparkier marked this pull request as ready for review April 23, 2024 21:22
@rbren
Copy link
Collaborator

rbren commented Apr 23, 2024

🤔 this didn't seem to work for me--I plugged in a bad API key, and it just used the key I had set on the backend

@Sparkier
Copy link
Collaborator Author

Hmm thank you for testing. Will debug.

@Sparkier Sparkier requested a review from rbren April 23, 2024 22:58
@Sparkier
Copy link
Collaborator Author

I think fixed. Not sure if I caught all edge cases.

@@ -25,6 +25,9 @@ function SettingsModal({ isOpen, onOpenChange }: SettingsProps) {
const [agents, setAgents] = React.useState<string[]>([]);
const [settings, setSettings] =
React.useState<Partial<Settings>>(currentSettings);
const [apiKey, setApiKey] = React.useState<string>(
Copy link
Collaborator

@amanape amanape Apr 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've extended getCurrentSettings to also include the API key. Maybe you can utilize the settings state that now already stores this data directly rather than use a separate state to handle the API key.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, good point.

@amanape
Copy link
Collaborator

amanape commented Apr 24, 2024

Also, it seems that this branch does not contain the most recent updates made to the SettingsModal.test.tsx file (mock path change and eslint import/order, compare with main).

@Sparkier Sparkier requested a review from amanape April 25, 2024 22:06
@amanape
Copy link
Collaborator

amanape commented Apr 26, 2024

There is some strange behavior regarding the way settings are stored. First off, some issues I am facing with the API key:

  • If I export key from terminal, it does not display on settings (this is because settings searches in storage rather than env)
  • Changing the key does not immediately take effect, it requires a reload.

After some exploring, I also found a few issues:

saveSettings (src/services/settingsService)

This function is called when the user presses the "save" button. To summarize, it gets existing settings from local storage, and compares it with the new settings passed as an argument. It then gets the difference, and dispatches updates.

One issue is that you set the value of the API key to local storage directly before the settings are saved (see handleAPIKeyChange of SettingsModal.tsx). This means that saveSettings cannot properly identify the updates because it fetches local storage to compare with new settings, but the API key now exists in both, therefore there is no difference to dispatch.

Settings Store

Speaking of dispatching to settings store, I cannot find where it is actually used. It may be outdated and no longer required, and may be why no getters exist. It seems that the current way of setting and retrieving settings are through the local storage.

I suppose this is something to keep in mind for the future, so that we can slowly retire this store in favor of local storage, but this needs extra attention which is beyond the scope of this PR.

initializeAgent (src/services/settingsService)

Unexpectedly, this does not behave as expected after updating the key, which is most likely the reason we have to refresh the page directly to have the agent use the new key.

I found that changing the function definition (initializeAgent(settings: Settings): void, where settings = { ...currentSettings, ...updatedSettings }) and passing new settings directly does in fact result in the agent (re)initializing after a key change, an does not require a refresh to use the new key. Although, unexpectedly, the local storage settings no longer updated on the new key. Meaning when I did refresh, it used an outdated key. (probably related to some other changes I made elsewhere during debugging, so maybe you can do some fine-tuned debugging of your own)


It may not be important to load an API key set by environment variables because we probably want the user to enter their key via UI, especially in the future if/when we move away from being self-hosted.

Other than that, everything seems to be OK. I hope the above information will be useful for you to solve the refresh problem.

@rbren
Copy link
Collaborator

rbren commented Apr 27, 2024

@Sparkier I can't actually get this to work 🤔

It seems to always send an empty LLM_API_KEY to the websocket. As @amanape points out, it also doesn't save the new settings if you only change API key.

@amanape I recently did a huge simplification of how settings are saved--definitely possible we can simplify further

@Sparkier
Copy link
Collaborator Author

@Sparkier I can't actually get this to work 🤔

It seems to always send an empty LLM_API_KEY to the websocket. As @amanape points out, it also doesn't save the new settings if you only change API key.

@amanape I recently did a huge simplification of how settings are saved--definitely possible we can simplify further

I think my changes conflicted with the settings simplification. I just merged things and tested and I can change the API key from the UI now.

@amanape
Copy link
Collaborator

amanape commented Apr 29, 2024

Seems to work nicely!

@Sparkier Sparkier requested a review from amanape April 29, 2024 20:02
@Sparkier Sparkier enabled auto-merge (squash) April 29, 2024 20:02
@Sparkier Sparkier merged commit fa067ed into main Apr 29, 2024
22 checks passed
@Sparkier Sparkier deleted the ab-ui-key branch April 29, 2024 20:18
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.

Setting for API key in the UI
3 participants