Chainlit(User Interface)For LLMs
Installation and basic use case
pip install chainlit
Basic Code
import chainlit as cl
@cl.on_message
async def main(message: str):
# Your custom logic goes here...
# Send a response back to the user
await cl.Message(
content=f"Received: {message}",
).send()
Explanation
@cl.on_message async
is a decorator. You have to define your function under it, and when you type a prompt in the chat interface, it sends the prompt to chain-lit, processing takes place(custom logic), and output is displayed in the chat interface.
async
is used as a keyword to define an asynchronous function(function that can run concurrently with other functions)
await
the keyword is used within an async
function to pause the execution of that function until a coroutine or asynchronous task is completed. It is a fundamental part of asynchronous programming in Python. It is often used when performing non-blocking operations like I/O (input/output) operations, such as making network requests or reading/writing files.
async def main(message: str):
: This line defines an asynchronous function named main
. The async
keyword indicates that this function can be executed asynchronously. It can pause and yield control to other tasks when it encounters an await
expression.
await cl.Message(...)
: Inside the main
function, you use the await
keyword to call the cl.Message(...).send()
method. The await
keyword is used to await the completion of asynchronous operations. In this case, it is likely used to send a message asynchronously, allowing other tasks to continue running while waiting for the message to be sent.
Execution
chanlit_tut1 is the name of your Python file.
When you include the -w flag, Chainlit is told to turn on a feature called auto-reloading. This means you won’t have to stop and start the server each time you want to see changes you’ve made to your application. Now, you can access your chatbot’s interface at http://localhost:8000 without any extra steps.
chainlit run chanlit_tut1.py -w
Reference: Chainlit Documentation https://docs.chainlit.io/