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"]
...
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%
how to use the Public Apps & Environment Variables
Path: /backend/env-variables https://docs.chainlit.io/backend/env-variables
Code and Python and Pkgs
I don't fully understand the above settings. Here is my sample code:
moreover in the '.chainlit/config.toml' I made this change.
Environment Setting (Python 3.11.9):
Issue / Error message