jupyterlab / jupyter-ai

A generative AI extension for JupyterLab
https://jupyter-ai.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
3.13k stars 310 forks source link

Provide non-UI way to specify API keys and other essential config fields for Jupyter AI chat #505

Open andrii-i opened 9 months ago

andrii-i commented 9 months ago

Problem

Currently only way for the user to specify an API key for Jupyter AI chat is to use chat settings UI or edit config.json directly (and we don't want users to edit config.json directly). Specifying API key through env variables would only work for AI magics.

Relevant discussion: #503.

Proposed Solution

Provide non-UI way to specify API keys and other essential config fields for Jupyter AI chat. For example, making chat pick up API env variable would be a good start.

atnjqt commented 6 months ago

@andrii-i if you want your vendor specific API key available to all single user notebook instances, you can set this as an environment variable in /opt/jupyterhub/etc/jupyterhub/jupyterhub_config.py

Below is an example using c.Spawner environment vars, notably I am using openai. This is referenced here for jupyter-ai env vars. )

c.Spawner.environment = {
        'PROVIDER_API_KEY':'YOUR_API_KEY_HERE',
        'OPENAI_API_KEY': 'sk-.............'
}

Don't forget to make sure the file is secured with chmod 600 && chown root:root given the secret api keys are in plain text.

OH wait I thought that would work for the UI lab extension but it does not!

eazuman commented 6 months ago

Hello,

Can you please verify if it's correct that setting the provider API_KEY (OPENAI_API_KEY) as an environment variable for Jupyter AI chat is not supported?

This pertains to https://github.com/jupyterlab/jupyter-ai/issues/685.

Specifically, my goal is to configure provider details through the jupyter_jupyter_ai_config.json without including the API key in the configuration file but setting the API KEY as an environment variable.

Our enterprise prefers managing sensitive information through a secret manager, either within 'jupyter_jupyter_ai_config.json' or as an environment variable. what would be the recommended approach to achieve this?

Thank you!

wakkadojo commented 3 months ago

i have a similar question -- the environment variable seems to apply to the magic command and not the chat? is there a way to have the chat sidebar rely on environment variables to set API keys?

ctcjab commented 3 weeks ago

I just left a relevant comment in a related issue (context: this should also work with a config file outside the home directory for interoperability with JupyterHub on Kubernetes). Excerpting here for visibility:

I am using /opt/conda/share/jupyter/lab/settings/overrides.json to accomplish this with all the other extensions whose settings I need to provide custom defaults for for my users[1], and it works great – users get all these extensions configured out-of-the-box, but if they need to customize their settings further, they can do so (including through the JupyterLab settings UI), and their customizations get persisted in their PV-backed home directory on Kubernetes, which takes precedence over my overrides.json. If jupyter-ai could look in overrides.json like these other extensions, that would make things simpler and more consistent for users and for administrators like me. What do you think?

[1] Here are all the other extensions that this approach works with, in case it's helpful:

❯ grep '@' overrides.json
  "@jupyterlab-contrib/spellchecker:plugin": {
  "@jupyterlab/apputils-extension:notification": {
  "@jupyterlab/completer-extension:inline-completer": {
      "@jupyterlab/inline-completer:history": {
      "@jupyterlab/jupyter-ai": {
  "@jupyterlab/completer-extension:manager": {
  "@jupyterlab/console-extension:tracker": {
  "@jupyterlab/extensionmanager-extension:plugin": {
  "@jupyterlab/filebrowser-extension:browser": {
  "@jupyterlab/fileeditor-extension:plugin": {
  "@jupyterlab/notebook-extension:tracker": {
  "@jupyterlab/shortcuts-extension:shortcuts": {
  "@jupyterlab/terminal-extension:plugin": {