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

Issue on docs: how to use the Environment Variables for Public Apps #139

Open
cuicaihao opened this issue May 19, 2024 · 1 comment
Open

Comments

@cuicaihao
Copy link

how to use the Public Apps & Environment Variables

  • langchain == 0.2.0
  • openai == 1.30.1

Path: /backend/env-variables
https://docs.chainlit.io/backend/env-variables

Public Apps & Environment Variables
If you want to share your app to a broader audience, you should not put your own OpenAI API keys in the .env file. Instead, you >should use user_env in the Chainlit config to ask each user to provide their own keys.
You can then access the user’s keys in your code using:

user_env = cl.user_session.get("env")

Code and Python and Pkgs

I don't fully understand the above settings.
Here is my sample code:

"""Python file to serve as the frontend"""
import os
import sys
sys.path.append(os.path.abspath('.'))

from openai import AsyncOpenAI
import chainlit as cl
 
from dotenv import load_dotenv
load_dotenv()

# user_env = cl.user_session.get("OPENAI_API_KEY")
# os.environ["OPENAI_API_KEY"] = user_env["OPENAI_API_KEY"]

api_key = os.environ.get("OPENAI_API_KEY")
client = AsyncOpenAI(api_key=api_key)

# Instrument the OpenAI client
cl.instrument_openai()

settings = {
    "model": "gpt-3.5-turbo",
    "temperature": 0,
    # ... more settings
}


# @cl.on_chat_start
# async def on_start():
#     await cl.Message("Hello world from Caihao Cui!").send()

@cl.on_settings_update
async def on_settings_update(settings: dict):
    print("Settings updated:", settings)


@cl.on_message
async def on_message(message: cl.Message):

    user_env = cl.user_session.get("user_env")
    api_key = user_env["OPENAI_API_KEY"]
    open.api_key = api_key
    
    # Instrument the OpenAI client
    cl.instrument_openai()
    response = await client.chat.completions.create(
        messages=[
            {
                "content": "You are a helpful bot, you always reply in English",
                "role": "system"
            },
            {
                "content": message.content,
                "role": "user"
            }
        ],
        **settings
    )
    await cl.Message(content=response.choices[0].message.content).send()


if __name__ == "__main__":
    from chainlit.cli import run_chainlit
    run_chainlit(__file__)

moreover in the '.chainlit/config.toml' I made this change.

...
# List of environment variables to be provided by each user to use the app.
user_env = ["OPENAI_API_KEY"]
...

Environment Setting (Python 3.11.9):

chainlit == 1.1.101
langchain == 0.2.0
openai == 1.30.1
streamlit == 1.34.0
streamlit-chat == 0.1.1

Issue / Error message

2024-05-19 16:09:34 - Loaded .env file
2024-05-19 16:09:35 - Your app is available at http://localhost:8000
2024-05-19 16:09:36 - Translation file for en not found. Using default translation en-US.
2024-05-19 16:09:37 - Translated markdown file for en not found. Defaulting to chainlit.md.
2024-05-19 16:09:40 - 'NoneType' object is not subscriptable
Traceback (most recent call last):
  File "/Users/PROJECTG_ROOT/.venv/lib/python3.11/site-packages/chainlit/utils.py", line 40, in wrapper
    return await user_function(**params_values)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/UsersPROJECTG_ROOT/demo_app/main.py", line 41, in on_message
    api_key = user_env["OPENAI_API_KEY"]
              ~~~~~~~~^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not subscriptable
2024-05-19 16:09:48 - Translation file for en not found. Using default translation en-US.
^C% 
@jefflavallee
Copy link

Your user_env = cl.user_session.get("user_env") should be user_env = cl.user_session.get("env")

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

No branches or pull requests

2 participants