You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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"""importosimportsyssys.path.append(os.path.abspath('.'))
fromopenaiimportAsyncOpenAIimportchainlitasclfromdotenvimportload_dotenvload_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 clientcl.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_updateasyncdefon_settings_update(settings: dict):
print("Settings updated:", settings)
@cl.on_messageasyncdefon_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 clientcl.instrument_openai()
response=awaitclient.chat.completions.create(
messages=[
{
"content": "You are a helpful bot, you always reply in English",
"role": "system"
},
{
"content": message.content,
"role": "user"
}
],
**settings
)
awaitcl.Message(content=response.choices[0].message.content).send()
if__name__=="__main__":
fromchainlit.cliimportrun_chainlitrun_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%
The text was updated successfully, but these errors were encountered:
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
The text was updated successfully, but these errors were encountered: