Hardcoding API keys in your code is not a good practice. It makes your code less portable and less flexible. It also makes it harder to keep your code secure. Instead, you should use environment variables to store values that are specific to your development environment.
Chainlit will automatically load environment variables from a
.env file in the root of your project. This file should be added to your
.gitignore file so that it is not committed to your repository.
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
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:
from chainlit import user_session user_env = user_session.get("env")
Let’s say you added
import chainlit as cl from langchain.chat_models import ChatOpenAI llm = ChatOpenAI(temperature=0, openai_api_key=user_env.get("OPENAI_API_KEY"))