### Run local development server Source: https://github.com/chainlit/docs/blob/main/AGENTS.md Starts the local Mintlify development server. Requires Node.js installed. ```bash # Run local dev server (requires Node.js) npx mint dev ``` -------------------------------- ### Start FastAPI Server Source: https://github.com/chainlit/docs/blob/main/integrations/fastapi.mdx Command to run the FastAPI server using uvicorn. Ensure uvicorn is installed. ```bash uvicorn main:app --host 0.0.0.0 --port 80 ``` -------------------------------- ### Install @chainlit/react-client Source: https://github.com/chainlit/docs/blob/main/deploy/react/installation-and-setup.mdx Install the package using npm. This package relies on Recoil for state management. ```bash npm install @chainlit/react-client ``` -------------------------------- ### Verify Chainlit Installation Source: https://github.com/chainlit/docs/blob/main/get-started/installation.mdx Run this command after installation to launch the Chainlit UI and confirm it's working correctly. ```bash chainlit hello ``` -------------------------------- ### Install Boto3 dependency Source: https://github.com/chainlit/docs/blob/main/data-layers/dynamodb.mdx Install the required AWS SDK for Python before initializing the data layer. ```bash pip install boto3 ``` -------------------------------- ### Install Dependencies Source: https://github.com/chainlit/docs/blob/main/examples/openai-sql.mdx Install the necessary Python packages for Chainlit and OpenAI integration. ```bash pip install chainlit openai ``` -------------------------------- ### Run development server Source: https://github.com/chainlit/docs/blob/main/README.md Execute this command at the root of the documentation directory to start the local development environment. ```bash npx mint dev ``` -------------------------------- ### Prompt Configuration Example Source: https://github.com/chainlit/docs/blob/main/authentication/oauth.mdx Example of setting the OAUTH_PROMPT environment variable to force a consent prompt for all providers. ```bash # Force consent prompt for all providers OAUTH_PROMPT=consent ``` -------------------------------- ### Simple example Source: https://github.com/chainlit/docs/blob/main/api-reference/chat-profiles.mdx A basic example demonstrating how to set up chat profiles using the `@cl.set_chat_profiles` decorator. ```python import chainlit as cl @cl.set_chat_profiles async def chat_profile(): return [ cl.ChatProfile( name="GPT-3.5", markdown_description="The underlying LLM model is **GPT-3.5**.", icon="https://picsum.photos/200", ), cl.ChatProfile( name="GPT-4", markdown_description="The underlying LLM model is **GPT-4**.", icon="https://picsum.photos/250", ), ] @cl.on_chat_start async def on_chat_start(): chat_profile = cl.user_session.get("chat_profile") await cl.Message( content=f"starting chat using the {chat_profile} chat profile" ).send() ``` -------------------------------- ### Install Discord Library Source: https://github.com/chainlit/docs/blob/main/deploy/discord.mdx Install the required discord library manually as it is not included in Chainlit dependencies. ```bash pip install discord ``` -------------------------------- ### MultiSelect Widget Usage Example Source: https://github.com/chainlit/docs/blob/main/api-reference/input-widgets/multiselect.mdx An example demonstrating how to use the MultiSelect widget in a Chainlit application. ```APIDOC ## Usage Example ### Description This example shows how to integrate the MultiSelect widget into the chat settings and retrieve the selected values. ### Method N/A (This is a client-side widget configuration) ### Endpoint N/A ### Parameters N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ### Code Example ```python import chainlit as cl from chainlit.input_widget import MultiSelect @cl.on_chat_start async def start(): settings = await cl.ChatSettings( [ MultiSelect( id="Tools", label="Enabled Tools", values=["web_search", "calculator", "file_reader", "code_interpreter"], initial=["web_search", "calculator"], ) ] ).send() value = settings["Tools"] ``` ``` -------------------------------- ### Configure LangGraph with Tools Source: https://github.com/chainlit/docs/blob/main/integrations/langchain.mdx Setup a LangGraph agent with tool binding and model configuration. ```python from typing import Literal from langchain_core.tools import tool from langchain_openai import ChatOpenAI from langgraph.prebuilt import ToolNode from langchain.schema.runnable.config import RunnableConfig from langchain_core.messages import HumanMessage import chainlit as cl @tool def get_weather(city: Literal["nyc", "sf"]): """Use this to get weather information.""" if city == "nyc": return "It might be cloudy in nyc" elif city == "sf": return "It's always sunny in sf" else: raise AssertionError("Unknown city") tools = [get_weather] model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0) final_model = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0) model = model.bind_tools(tools) # NOTE: this is where we're adding a tag that we'll can use later to filter the model stream events to only the model called in the final node. # This is not necessary if you call a single LLM but might be important in case you call multiple models within the node and want to filter events ``` -------------------------------- ### Run the Chainlit Application Source: https://github.com/chainlit/docs/blob/main/examples/openai-sql.mdx Execute the Chainlit application from the command line to start the text-to-SQL service. ```bash chainlit run app.py -w ``` -------------------------------- ### Install Dependencies for Document QA Source: https://github.com/chainlit/docs/blob/main/examples/qa.mdx Installs the necessary Python packages for building the document QA chatbot. Ensure you have OpenAI API keys configured. ```bash pip install langchain langchain-community chromadb tiktoken openai langchain-openai ``` -------------------------------- ### Chainlit App Setup Source: https://github.com/chainlit/docs/blob/main/data-layers/sqlalchemy.mdx Example of setting up the SQLAlchemy data layer with an Azure storage client in a Chainlit app. ```python import chainlit as cl from chainlit.data.sql_alchemy import SQLAlchemyDataLayer from chainlit.data.storage_clients.azure import AzureStorageClient storage_client = AzureStorageClient(account_url="", container="") @cl.data_layer def get_data_layer(): return SQLAlchemyDataLayer(conninfo="", storage_provider=storage_client) ``` -------------------------------- ### Install Botbuilder Library Source: https://github.com/chainlit/docs/blob/main/deploy/teams.mdx Install the Botbuilder core library, which is not included in Chainlit's default dependencies. ```bash pip install botbuilder-core ``` -------------------------------- ### Custom Provider Environment Variables Example Source: https://github.com/chainlit/docs/blob/main/authentication/oauth.mdx Example environment variables required for a custom OAuth provider. ```bash YOUR_PROVIDER_CLIENT_ID YOUR_PROVIDER_CLIENT_SECRET ``` -------------------------------- ### Run a Chainlit application Source: https://github.com/chainlit/docs/blob/main/backend/command-line.mdx Starts the Chainlit server for a specified target file or module. ```bash chainlit run [OPTIONS] TARGET ``` -------------------------------- ### Set Starter Messages Source: https://github.com/chainlit/docs/blob/main/examples/openai-sql.mdx Define starter messages to guide users on how to interact with the text-to-SQL app. ```python @cl.set_starters async def starters(): return [ cl.Starter( label=">50 minutes watched", message="Compute the number of customers who watched more than 50 minutes of video this month." ) ] ``` -------------------------------- ### Install PII analysis dependencies Source: https://github.com/chainlit/docs/blob/main/examples/security.mdx Terminal commands to install the necessary Python packages for Presidio and Spacy. ```shell pip install presidio-analyzer presidio-anonymizer spacy python -m spacy download en_core_web_lg ``` -------------------------------- ### Install Chainlit with Pip Source: https://github.com/chainlit/docs/blob/main/get-started/installation.mdx Use this command to install the Chainlit package. Ensure you have Python 3.9 or higher. ```bash pip install chainlit ``` -------------------------------- ### Install Slack Bolt Library Source: https://github.com/chainlit/docs/blob/main/deploy/slack.mdx Install the necessary Slack Bolt library using pip. ```bash pip install slack_bolt ``` -------------------------------- ### URL Parameter Example Source: https://github.com/chainlit/docs/blob/main/deploy/webapp.mdx Demonstrates how to use the 'prompt' URL parameter to pre-fill the chat input. ```url https://your-app.com/?prompt=Hello%20World ``` -------------------------------- ### Default UI Configuration Source: https://github.com/chainlit/docs/blob/main/backend/config/ui.mdx Example of default UI configuration in TOML format. ```toml [UI] # Name of the assistant. name = "Assistant" # default_theme = "dark" ``` -------------------------------- ### FastAPI App with Mounted Chainlit Source: https://github.com/chainlit/docs/blob/main/integrations/fastapi.mdx Sets up a FastAPI application and mounts the Chainlit sub-application. Ensure 'fastapi' and 'chainlit' are installed. ```python from fastapi import FastAPI from chainlit.utils import mount_chainlit app = FastAPI() @app.get("/app") def read_main(): return {"message": "Hello World from main app"} mount_chainlit(app=app, target="my_cl_app.py", path="/chainlit") ``` -------------------------------- ### Sidebar Mode Configuration Source: https://github.com/chainlit/docs/blob/main/deploy/copilot.mdx Example of mounting the Chainlit widget in sidebar mode. ```javascript window.mountChainlitWidget({ chainlitServer: "http://localhost:8000", displayMode: "sidebar", }); ``` -------------------------------- ### Run Chainlit App Source: https://github.com/chainlit/docs/blob/main/deploy/discord.mdx Start the Chainlit application without the default UI using the -h flag. ```bash chainlit run my_app.py -h ``` -------------------------------- ### Run Chainlit application example Source: https://github.com/chainlit/docs/blob/main/customisation/translation.mdx Command to run a Chainlit application, used here in the context of resetting translations. ```bash chainlit run my-app.py ``` -------------------------------- ### Create and Use a Checkbox Input Widget Source: https://github.com/chainlit/docs/blob/main/api-reference/input-widgets/checkbox.mdx Demonstrates how to create a Checkbox widget for chat settings and retrieve its value. Ensure Chainlit is installed and imported. ```python import chainlit as cl from chainlit.input_widget import Checkbox @cl.on_chat_start async def start(): settings = await cl.ChatSettings( [ Checkbox( id="VerboseMode", label="Enable verbose output", initial=False, ) ] ).send() value = settings["VerboseMode"] ``` -------------------------------- ### Example Chainlit App for Slack Integration Source: https://github.com/chainlit/docs/blob/main/deploy/slack.mdx A simple Chainlit application demonstrating interaction with Slack. ```python import chainlit as cl @cl.on_message async def on_message(msg: cl.Message): # Access the original slack event print(cl.user_session.get("slack_event")) # Access the slack user print(cl.user_session.get("user")) # Access potential attached files attached_files = msg.elements await cl.Message(content="Hello World").send() ``` -------------------------------- ### TaskList Usage Example Source: https://github.com/chainlit/docs/blob/main/api-reference/elements/tasklist.mdx Demonstrates how to create and manage a TaskList with multiple tasks, update their statuses, and send them to the UI. ```python import chainlit as cl @cl.on_chat_start async def main(): # Create the TaskList task_list = cl.TaskList() task_list.status = "Running..." # Create a task and put it in the running state task1 = cl.Task(title="Processing data", status=cl.TaskStatus.RUNNING) await task_list.add_task(task1) # Create another task that is in the ready state task2 = cl.Task(title="Performing calculations") await task_list.add_task(task2) # Optional: link a message to each task to allow task navigation in the chat history message = await cl.Message(content="Started processing data").send() task1.forId = message.id # Update the task list in the interface await task_list.send() # Perform some action on your end await cl.sleep(1) # Update the task statuses task1.status = cl.TaskStatus.DONE task2.status = cl.TaskStatus.FAILED task_list.status = "Failed" await task_list.send() ``` -------------------------------- ### Initialize a Chainlit project Source: https://github.com/chainlit/docs/blob/main/backend/command-line.mdx Creates a configuration file at .chainlit/config.toml in the current directory. ```bash chainlit init ``` -------------------------------- ### Initialize Chainlit and OpenAI Client Source: https://github.com/chainlit/docs/blob/main/examples/openai-sql.mdx Import necessary libraries, instrument OpenAI for Chainlit, and initialize the OpenAI asynchronous client. ```python from openai import AsyncOpenAI import chainlit as cl cl.instrument_openai() client = AsyncOpenAI(api_key="YOUR_OPENAI_API_KEY") ``` -------------------------------- ### Project Configuration Example (TOML) Source: https://github.com/chainlit/docs/blob/main/backend/config/project.mdx This TOML snippet shows default configuration settings for a Chainlit project, including user environment variables, session timeouts, caching, and allowed origins. ```toml [project] # List of environment variables to be provided by each user to use the app. user_env = [] # Whether to persist user environment variables (API keys) to the database persist_user_env = false # Whether to mask user environment variables (API keys) in the UI with password type mask_user_env = false # Duration (in seconds) during which the session is saved when the connection is lost session_timeout = 3600 # Duration (in seconds) of the user session expiry user_session_timeout = 1296000 # 15 days # Enable third parties caching (e.g., LangChain cache) cache = false # Authorized origins allow_origins = ["*"] ``` -------------------------------- ### Starter Categories with Profile Source: https://github.com/chainlit/docs/blob/main/concepts/starters.mdx This example demonstrates how to define starter categories based on the `chat_profile` parameter. It returns different categories depending on whether the profile is 'Advanced' or general. ```python @cl.set_starter_categories async def starter_categories(user: cl.User, language: str, chat_profile: str): if chat_profile == "Advanced": return [ cl.StarterCategory( label="Advanced", starters=[ cl.Starter(label="Deep dive", message="Explain in detail"), ], ), ] return [ cl.StarterCategory( label="General", starters=[ cl.Starter(label="Hello", message="Say hello"), ], ), ] ``` -------------------------------- ### Audio Class Initialization Source: https://github.com/chainlit/docs/blob/main/api-reference/elements/audio.mdx Documentation for the Audio class constructor and its available attributes for configuring audio elements in the UI. ```APIDOC ## Audio Class ### Description The `Audio` class allows you to display an audio player for a specific audio file in the chatbot user interface. You must provide either a url, a path, or content bytes. ### Attributes - **name** (str) - Required - The name of the audio file to be displayed in the UI. - **display** (ElementDisplay) - Optional - Determines where the element should be displayed in the UI. Choices are "side" (default), "inline", or "page". - **url** (str) - Optional - The remote URL of the audio. - **path** (str) - Optional - The local file path of the audio. - **content** (bytes) - Optional - The file content of the audio in bytes format. - **auto_play** (bool) - Optional - Whether the audio should start playing automatically. ### Usage Example ```python import chainlit as cl @cl.on_chat_start async def main(): elements = [ cl.Audio(name="example.mp3", path="./example.mp3", display="inline"), ] await cl.Message( content="Here is an audio file", elements=elements, ).send() ``` ``` -------------------------------- ### Implement LCEL with Chainlit Source: https://github.com/chainlit/docs/blob/main/integrations/langchain.mdx Use these examples to set up a Runnable chain for chat sessions. Fallback to the sync implementation if your agent or chain lacks native async support. ```python from langchain_openai import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema import StrOutputParser from langchain.schema.runnable import Runnable from langchain.schema.runnable.config import RunnableConfig from typing import cast import chainlit as cl @cl.on_chat_start async def on_chat_start(): model = ChatOpenAI(streaming=True) prompt = ChatPromptTemplate.from_messages( [ ( "system", "You're a very knowledgeable historian who provides accurate and eloquent answers to historical questions.", ), ("human", "{question}"), ] ) runnable = prompt | model | StrOutputParser() cl.user_session.set("runnable", runnable) @cl.on_message async def on_message(message: cl.Message): runnable = cast(Runnable, cl.user_session.get("runnable")) # type: Runnable msg = cl.Message(content="") async for chunk in runnable.astream( {"question": message.content}, config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]), ): await msg.stream_token(chunk) await msg.send() ``` ```python from langchain_openai import ChatOpenAI from langchain.prompts import ChatPromptTemplate from langchain.schema import StrOutputParser from langchain.schema.runnable import Runnable from langchain.schema.runnable.config import RunnableConfig import chainlit as cl @cl.on_chat_start async def on_chat_start(): model = ChatOpenAI(streaming=True) prompt = ChatPromptTemplate.from_messages( [ ( "system", "You're a very knowledgeable historian who provides accurate and eloquent answers to historical questions.", ), ("human", "{question}"), ] ) runnable = prompt | model | StrOutputParser() cl.user_session.set("runnable", runnable) @cl.on_message async def on_message(message: cl.Message): runnable = cl.user_session.get("runnable") # type: Runnable msg = cl.Message(content="") for chunk in await cl.make_async(runnable.stream)( {"question": message.content}, config=RunnableConfig(callbacks=[cl.LangchainCallbackHandler()]), ): await msg.stream_token(chunk) await msg.send() ``` -------------------------------- ### Copilot Function Calling Example (Python) Source: https://github.com/chainlit/docs/blob/main/deploy/copilot.mdx Example of how to define and call a CopilotFunction in a Chainlit server. ```python import chainlit as cl @cl.on_message async def on_message(msg: cl.Message): if cl.context.session.client_type == "copilot": fn = cl.CopilotFunction(name="test", args={"msg": msg.content}) res = await fn.acall() await cl.Message(content=res).send() ``` -------------------------------- ### Initialize Chat with User Input Source: https://github.com/chainlit/docs/blob/main/api-reference/lifecycle-hooks/on-chat-start.mdx Use the on_chat_start hook to greet the user and ask for their name upon connection. The response is then displayed to the user. ```python from chainlit import AskUserMessage, Message, on_chat_start @on_chat_start async def main(): res = await AskUserMessage(content="What is your name?", timeout=30).send() if res: await Message( content=f"Your name is: {res['output']}.\nChainlit installation is working!\nYou can now start building your own chainlit apps!", ).send() ``` -------------------------------- ### Setup Semantic Kernel with Chainlit Filter Source: https://github.com/chainlit/docs/blob/main/integrations/semantic-kernel.mdx This code initializes the Semantic Kernel, adds an AI service, imports a native plugin, and integrates the Chainlit filter to capture function calls as UI Steps. Ensure your LLM API keys are set in the environment. ```python import chainlit as cl import semantic_kernel as sk from semantic_kernel.connectors.ai import FunctionChoiceBehavior from semantic_kernel.connectors.ai.open_ai import ( OpenAIChatCompletion, OpenAIChatPromptExecutionSettings, ) from semantic_kernel.functions import kernel_function from semantic_kernel.contents import ChatHistory request_settings = OpenAIChatPromptExecutionSettings( function_choice_behavior=FunctionChoiceBehavior.Auto(filters={"excluded_plugins": ["ChatBot"]}) ) # Example Native Plugin (Tool) class WeatherPlugin: @kernel_function(name="get_weather", description="Gets the weather for a city") def get_weather(self, city: str) -> str: """Retrieves the weather for a given city.""" if "paris" in city.lower(): return f"The weather in {city} is 20°C and sunny." elif "london" in city.lower(): return f"The weather in {city} is 15°C and cloudy." else: return f"Sorry, I don't have the weather for {city}." @cl.on_chat_start async def on_chat_start(): # Setup Semantic Kernel kernel = sk.Kernel() # Add your AI service (e.g., OpenAI) # Make sure OPENAI_API_KEY and OPENAI_ORG_ID are set in your environment ai_service = OpenAIChatCompletion(service_id="default", ai_model_id="gpt-4o") kernel.add_service(ai_service) # Import the WeatherPlugin kernel.add_plugin(WeatherPlugin(), plugin_name="Weather") # Instantiate and add the Chainlit filter to the kernel # This will automatically capture function calls as Steps sk_filter = cl.SemanticKernelFilter(kernel=kernel) cl.user_session.set("kernel", kernel) cl.user_session.set("ai_service", ai_service) cl.user_session.set("chat_history", ChatHistory()) @cl.on_message async def on_message(message: cl.Message): kernel = cl.user_session.get("kernel") # type: sk.Kernel ai_service = cl.user_session.get("ai_service") # type: OpenAIChatCompletion chat_history = cl.user_session.get("chat_history") # type: ChatHistory # Add user message to history chat_history.add_user_message(message.content) # Create a Chainlit message for the response stream answer = cl.Message(content="") async for msg in ai_service.get_streaming_chat_message_content( chat_history=chat_history, user_input=message.content, settings=request_settings, kernel=kernel, ): if msg.content: await answer.stream_token(msg.content) # Add the full assistant response to history chat_history.add_assistant_message(answer.content) # Send the final message await answer.send() ``` -------------------------------- ### Customizing UI text example Source: https://github.com/chainlit/docs/blob/main/customisation/translation.mdx Example of how to change the label of a navigation tab by modifying a translation key. ```json "components.organisms.header.readme": "Documentation" ``` -------------------------------- ### Configure Chainlit with LiteLLM Proxy Source: https://github.com/chainlit/docs/blob/main/integrations/litellm.mdx Initializes the AsyncOpenAI client with the LiteLLM proxy URL and instruments it for Chainlit UI tracking. ```python from openai import AsyncOpenAI import chainlit as cl client = AsyncOpenAI( api_key="anything", # litellm proxy virtual key base_url="http://0.0.0.0:4000" # litellm proxy base_url ) # Instrument the OpenAI client cl.instrument_openai() settings = { "model": "gpt-3.5-turbo", # model you want to send litellm proxy "temperature": 0, # ... more settings } @cl.on_message async def on_message(message: cl.Message): response = await client.chat.completions.create( messages=[ { "content": "You are a helpful bot, you always reply in Spanish", "role": "system" }, { "content": message.content, "role": "user" } ], **settings ) await cl.Message(content=response.choices[0].message.content).send() ``` -------------------------------- ### Polars DataFrame Example Source: https://github.com/chainlit/docs/blob/main/api-reference/elements/dataframe.mdx This example demonstrates how to create and send a polars DataFrame to the Chainlit UI. ```python import polars as pl import chainlit as cl @cl.on_chat_start async def start(): df = pl.DataFrame( { "Name": ["Alice", "Bob", "Charlie"], "Age": [25, 30, 35], "City": ["New York", "Los Angeles", "Chicago"], } ) elements = [cl.Dataframe(data=df, display="inline", name="Dataframe")] await cl.Message(content="This message has a Dataframe", elements=elements).send() ``` -------------------------------- ### Pandas DataFrame Example Source: https://github.com/chainlit/docs/blob/main/api-reference/elements/dataframe.mdx This example demonstrates how to create and send a pandas DataFrame to the Chainlit UI. ```python import pandas as pd import chainlit as cl @cl.on_chat_start async def start(): # Create a sample DataFrame with more than 10 rows to test pagination functionality data = { "Name": [ "Alice", "David", "Charlie", "Bob", "Eva", "Grace", "Hannah", "Jack", "Frank", "Kara", "Liam", "Ivy", "Mia", "Noah", "Olivia", ], "Age": [25, 40, 35, 30, 45, 55, 60, 70, 50, 75, 80, 65, 85, 90, 95], "City": [ "New York", "Houston", "Chicago", "Los Angeles", "Phoenix", "San Antonio", "San Diego", "San Jose", "Philadelphia", "Austin", "Fort Worth", "Dallas", "Jacksonville", "Columbus", "Charlotte", ], "Salary": [ 70000, 100000, 90000, 80000, 110000, 130000, 140000, 160000, 120000, 170000, 180000, 150000, 190000, 200000, 210000, ], } df = pd.DataFrame(data) elements = [cl.Dataframe(data=df, display="inline", name="Dataframe")] await cl.Message(content="This message has a Dataframe", elements=elements).send() ``` -------------------------------- ### Install Dependencies Source: https://github.com/chainlit/docs/blob/main/data-layers/sqlalchemy.mdx Install the required dependencies for setting up the data layer on a PostgreSQL database with an Azure storage client. ```bash pip install asyncpg SQLAlchemy azure-identity azure-storage-file-datalake aiohttp greenlet ``` -------------------------------- ### Starters with Chat Profiles Source: https://github.com/chainlit/docs/blob/main/concepts/starters.mdx This example demonstrates how to define starters that are associated with specific chat profiles. The `cl.set_chat_profiles` decorator is used to define a function that returns a list of chat profiles, each with its own set of starters. ```python import chainlit as cl @cl.set_chat_profiles async def chat_profile(current_user: cl.User): if current_user.metadata["role"] != "ADMIN": return None return [ cl.ChatProfile( name="My Chat Profile", icon="https://picsum.photos/250", markdown_description="The underlying LLM model is **GPT-3.5**, a *175B parameter model* trained on 410GB of text data.", starters=[ cl.Starter( label="Morning routine ideation", message="Can you help me create a personalized morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.", icon="/public/idea.svg", ), cl.Starter( label="Explain superconductors", message="Explain superconductors like I'm five years old.", icon="/public/learn.svg", ), ], ) ] ``` -------------------------------- ### Customizing chat watermark example Source: https://github.com/chainlit/docs/blob/main/customisation/translation.mdx Example of how to customize the chat watermark text, including adding a link to a privacy policy. Markdown is supported. ```json "chat": { "watermark": "LLMs can make mistakes. [See our privacy policy](https://example.com/privacy)." } ``` -------------------------------- ### Define Starters for Assistants Source: https://github.com/chainlit/docs/blob/main/guides/migration/1.1.300.mdx Use the @cl.set_starters decorator to provide up to 4 suggestions for users to initiate conversations. ```python import chainlit as cl @cl.set_starters async def set_starters(): return [ cl.Starter( label="Morning routine ideation", message="Can you help me create a personalized morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.", icon="/public/idea.svg", ), cl.Starter( label="Explain superconductors", message="Explain superconductors like I'm five years old.", icon="/public/learn.svg", ), cl.Starter( label="Python script for daily email reports", message="Write a script to automate sending daily email reports in Python, and walk me through how I would set it up.", icon="/public/terminal.svg", ), cl.Starter( label="Text inviting friend to wedding", message="Write a text asking a friend to be my plus-one at a wedding next month. I want to keep it super short and casual, and offer an out.", icon="/public/write.svg", ) ] # ... ``` -------------------------------- ### Get Copilot Thread ID Source: https://github.com/chainlit/docs/blob/main/deploy/copilot.mdx Retrieves the current thread ID from localStorage. ```javascript const threadId = window.getChainlitCopilotThreadId(); console.log("Current thread ID:", threadId); ``` -------------------------------- ### Starters Source: https://github.com/chainlit/docs/blob/main/concepts/starters.mdx This code snippet demonstrates how to define starters using the `@cl.set_starters` decorator. It returns a list of `cl.Starter` objects, each with a label, message, and icon. One starter also includes a `command` attribute. ```python import chainlit as cl @cl.set_starters async def set_starters(): return [ cl.Starter( label="Morning routine ideation", message="Can you help me create a personalized morning routine that would help increase my productivity throughout the day? Start by asking me about my current habits and what activities energize me in the morning.", icon="/public/idea.svg", ), cl.Starter( label="Explain superconductors", message="Explain superconductors like I'm five years old.", icon="/public/learn.svg", ), cl.Starter( label="Python script for daily email reports", message="Write a script to automate sending daily email reports in Python, and walk me through how I would set it up.", icon="/public/terminal.svg", command="code", ), cl.Starter( label="Text inviting friend to wedding", message="Write a text asking a friend to be my plus-one at a wedding next month. I want to keep it super short and casual, and offer an out.", icon="/public/write.svg", ) ] # ... ``` -------------------------------- ### Configure OpenAI-compatible client in Chainlit Source: https://github.com/chainlit/docs/blob/main/integrations/message-based.mdx Initializes an AsyncOpenAI client and instruments it with Chainlit. Ensure no other conflicting integrations like LangChain are active to avoid duplicate UI steps. ```python from openai import AsyncOpenAI import chainlit as cl client = AsyncOpenAI(base_url="http://localhost:1234/v1", api_key="lm-studio") # Instrument the OpenAI client cl.instrument_openai() settings = { "model": "gpt-3.5-turbo", "temperature": 0, # ... more settings } @cl.on_message async def on_message(message: cl.Message): response = await client.chat.completions.create( messages=[ { "content": "You are a helpful bot, you always reply in Spanish", "role": "system" }, { "content": message.content, "role": "user" } ], **settings ) await cl.Message(content=response.choices[0].message.content).send() ``` -------------------------------- ### Define Prompt Template and LLM Settings Source: https://github.com/chainlit/docs/blob/main/examples/openai-sql.mdx Set up the prompt template for the LLM and configure its generation settings like model, temperature, and token limits. ```python template = """SQL tables (and columns): * Customers(customer_id, signup_date) * Streaming(customer_id, video_id, watch_date, watch_minutes) A well-written SQL query that {input}: ```""" settings = { "model": "gpt-3.5-turbo", "temperature": 0, "max_tokens": 500, "top_p": 1, "frequency_penalty": 0, "presence_penalty": 0, "stop": ["```"] } ``` -------------------------------- ### Image Class Initialization Source: https://github.com/chainlit/docs/blob/main/api-reference/elements/image.mdx Documentation for the Image class constructor and its available attributes for configuring image display. ```APIDOC ## Image Class ### Description The Image class is used to create and handle image elements to be sent and displayed in the chatbot user interface. You must provide either a url, a path, or content bytes. ### Attributes - **name** (str) - Required - The name of the image to be displayed in the UI. - **display** (ElementDisplay) - Optional - Determines how the image element should be displayed in the UI. Choices are "side", "inline", or "page". - **size** (ElementSize) - Optional - Determines the size of the image. Only works with display="inline". Choices are "small", "medium" (default), or "large". - **url** (str) - Optional - The remote URL of the image source. - **path** (str) - Optional - The local file path of the image. - **content** (bytes) - Optional - The file content of the image in bytes format. ### Usage Example ```python import chainlit as cl @cl.on_chat_start async def start(): image = cl.Image(path="./cat.jpeg", name="image1", display="inline") await cl.Message( content="This message has an image!", elements=[image], ).send() ``` ``` -------------------------------- ### Configure Custom CSS Source: https://github.com/chainlit/docs/blob/main/customisation/custom-css.mdx Example of how to specify a custom CSS file in the .chainlit/config.toml file. ```toml ```toml config.toml [UI] # ... # This can either be a css file in your `public` dir or a URL custom_css = '/public/stylesheet.css' ``` ``` -------------------------------- ### Localized Starters Source: https://github.com/chainlit/docs/blob/main/concepts/starters.mdx This code snippet shows how to provide localized starters by checking the `language` parameter in the `@cl.set_starters` callback. It returns different starters for 'fr' (French) and other languages. ```python @cl.set_starters async def set_starters(current_user: cl.User, language: str): if language == "fr": return [ cl.Starter( label="Routine matinale", message="Pouvez-vous m'aider à créer une routine matinale personnalisée ?", icon="/public/idea.svg", ), ] return [ cl.Starter( label="Morning routine ideation", message="Can you help me create a personalized morning routine?", icon="/public/idea.svg", ), ] ``` -------------------------------- ### Date range Source: https://github.com/chainlit/docs/blob/main/api-reference/input-widgets/datepicker.mdx Example of using the DatePicker for selecting a date range. ```python import chainlit as cl from chainlit.input_widget import DatePicker @cl.on_chat_start async def start(): settings = await cl.ChatSettings( [ DatePicker( id="date_range", label="Date Range", mode="range", initial=("2025-06-01", "2025-06-30"), ) ] ).send() value = settings["date_range"] ``` -------------------------------- ### Single date Source: https://github.com/chainlit/docs/blob/main/api-reference/input-widgets/datepicker.mdx Example of using the DatePicker for single date selection. ```python import chainlit as cl from chainlit.input_widget import DatePicker @cl.on_chat_start async def start(): settings = await cl.ChatSettings( [ DatePicker( id="start_date", label="Start Date", placeholder="Pick a date", min_date="2025-01-01", max_date="2026-12-31", ) ] ).send() value = settings["start_date"] ``` -------------------------------- ### Ask for a python file with specific extension Source: https://github.com/chainlit/docs/blob/main/api-reference/ask/ask-for-file.mdx This example demonstrates how to use the `accept` parameter with a dictionary to specify accepted MIME types and their corresponding file extensions. Here, it's configured to accept Python files (`.py`) with the `text/plain` MIME type. ```python import chainlit as cl file = await cl.AskFileMessage( content="Please upload a python file to begin!", accept={"text/plain": [".py"]} ).send() ``` -------------------------------- ### Chainlit Chatbot Logic Source: https://github.com/chainlit/docs/blob/main/integrations/fastapi.mdx Defines the chatbot's behavior when a chat starts. Requires the 'chainlit' library. ```python import chainlit as cl @cl.on_chat_start async def main(): await cl.Message(content="Hello World").send() ``` -------------------------------- ### Initialize a Switch widget Source: https://github.com/chainlit/docs/blob/main/api-reference/input-widgets/switch.mdx Use the Switch widget within a ChatSettings configuration to capture user boolean input. ```python import chainlit as cl from chainlit.input_widget import Switch @cl.on_chat_start async def start(): settings = await cl.ChatSettings( [ Switch(id="Streaming", label="OpenAI - Stream Tokens", initial=True), ] ).send() value = settings["Streaming"] ``` -------------------------------- ### Define on_chat_end hook Source: https://github.com/chainlit/docs/blob/main/concepts/chat-lifecycle.mdx Triggered when the chat session ends due to disconnection or starting a new session. ```python @cl.on_chat_end def on_chat_end(): print("The user disconnected!") ``` -------------------------------- ### Access authenticated user in chat Source: https://github.com/chainlit/docs/blob/main/authentication/overview.mdx Retrieve the current user object from the session during the chat start event. ```python @cl.on_chat_start async def on_chat_start(): app_user = cl.user_session.get("user") await cl.Message(f"Hello {app_user.identifier}").send() ``` -------------------------------- ### Starter Categories Source: https://github.com/chainlit/docs/blob/main/concepts/starters.mdx This code snippet shows how to group starters into categories using the `@cl.set_starter_categories` decorator. Each `cl.StarterCategory` has a label, an optional icon, and a list of starters. ```python import chainlit as cl @cl.set_starter_categories async def starter_categories(user: cl.User): return [ cl.StarterCategory( label="Creative", icon="https://example.com/creative.png", # optional starters=[ cl.Starter(label="Write a poem", message="Write a poem about the sea"), cl.Starter(label="Write a story", message="Write a short story"), ], ), cl.StarterCategory( label="Educational", starters=[ cl.Starter(label="Explain", message="Explain quantum computing"), ], ), ] ``` -------------------------------- ### JWT Token Generation (TypeScript) Source: https://github.com/chainlit/docs/blob/main/deploy/copilot.mdx TypeScript example for generating a JWT access token for Copilot authentication. ```typescript import jwt from "jsonwebtoken"; const CHAINLIT_AUTH_SECRET = "your-secret"; interface Metadata { [key: string]: any; } function createJwt(identifier: string, metadata: Metadata): string { const toEncode = { identifier: identifier, metadata: metadata, exp: Math.floor(Date.now() / 1000) + 60 * 60 * 24 * 15, // 15 days }; const encodedJwt = jwt.sign(toEncode, CHAINLIT_AUTH_SECRET, { algorithm: "HS256", }); return encodedJwt; } const accessToken = createJwt("user-1", { name: "John Doe" }); ```