Skip to content

Commit f3a46d5

Browse files
committed
update for message api rework
1 parent cb38347 commit f3a46d5

24 files changed

+178
-258
lines changed

api-reference/action.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "Action"
44

55
The `Action` class is designed to create and manage actions to be sent and displayed in the chatbot user interface. Actions consist of buttons that the user can interact with, and these interactions trigger specific functionalities within your app.
66

7-
### Parameters
7+
### Attributes
88

99
<ParamField path="name" type="str">
1010
Name of the action, this should be used in the action_callback
@@ -32,7 +32,7 @@ import chainlit as cl
3232

3333
@cl.action_callback("action_button")
3434
def on_action(action):
35-
cl.send_message(f"Executed {action.name}")
35+
cl.Message(content=f"Executed {action.name}").send()
3636
# Optionally remove the action button from the chatbot user interface
3737
action.remove()
3838

@@ -43,5 +43,5 @@ def start():
4343
cl.Action(name="action_button", value="example_value", description="Click me!")
4444
]
4545

46-
cl.send_message(content="Interact with this action button:", actions=actions)
46+
cl.Message(content="Interact with this action button:", actions=actions).send()
4747
```

api-reference/ask/ask-for-file.mdx

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
---
2-
title: "ask_for_file"
2+
title: "AskFileMessage"
33
---
44

55
Ask the user to upload a file before continuing.
66
If the user does not answer in time (see timeout), a TimeoutError will be raised or None will be returned depending on raise_on_timeout.
77
If a project ID is configured, the messages will be uploaded to the cloud storage.
88

9-
### Parameters
9+
### Attributes
1010

11-
<ParamField path="title" type="str">
11+
<ParamField path="content" type="str">
1212
Text displayed above the upload button.
1313
</ParamField>
1414
<ParamField path="accept" type="List[str]">
@@ -33,20 +33,23 @@ If a project ID is configured, the messages will be uploaded to the cloud storag
3333
### Usage
3434

3535
```python Code Example
36-
from chainlit import send_message, ask_for_file, on_chat_start
36+
import chainlit as cl
3737

3838

39-
@on_chat_start
40-
def main():
41-
file = None # Wait for the user to upload a file
42-
while file == None:
43-
file = ask_for_file(
44-
title="Please upload a text file to begin!", accept=["text/plain"]
45-
)
39+
@cl.on_chat_start
40+
def start():
41+
file = None
4642

47-
# Decode bytes to text
43+
# Wait for the user to upload a file
44+
while file == None:
45+
file = cl.AskFileMessage(
46+
content="Please upload a text file to begin!", accept=["text/plain"]
47+
).send()
48+
# Decode the file
4849
text = file.content.decode("utf-8")
4950

50-
send_message(f"`{file.name}` uploaded, you can now ask questions!")
51-
51+
# Let the user know that the system is ready
52+
cl.Message(
53+
content=f"`{file.name}` uploaded, it contains {len(text)} characters!"
54+
).send()
5255
```

api-reference/ask/ask-for-input.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
---
2-
title: "ask_for_input"
2+
title: "AskUserMessage"
33
---
44

55
Ask for the user input before continuing.
66
If the user does not answer in time (see timeout), a TimeoutError will be raised or None will be returned depending on raise_on_timeout.
77
If a project ID is configured, the messages will be uploaded to the cloud storage.
88

9-
### Parameters
9+
### Attributes
1010

1111
<ParamField path="content" type="str">
1212
The content of the message.
@@ -31,14 +31,14 @@ If a project ID is configured, the messages will be uploaded to the cloud storag
3131
### Usage
3232

3333
```python Code Example
34-
from chainlit import send_message, ask_for_input, on_chat_start
34+
import chainlit as cl
3535

3636

37-
@on_chat_start
37+
@cl.on_chat_start
3838
def main():
39-
res = ask_for_input(content="What is your name?")
39+
res = cl.AskUserMessage(content="What is your name?", timeout=10).send()
4040
if res:
41-
send_message(
41+
cl.Message(
4242
content=f"Your name is: {res['content']}",
43-
)
43+
).send()
4444
```

api-reference/elements/local-image.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "Local Image"
44

55
The `LocalImage` class is designed to create and handle local image elements to be sent and displayed in the chatbot user interface.
66

7-
### Parameters
7+
### Attributes
88

99
<ParamField path="name" type="str">
1010
The name of the image to be displayed in the UI.
@@ -39,7 +39,7 @@ elements = [
3939
LocalImage(name="image1", display="inline", path="./image1.jpg")
4040
]
4141

42-
cl.send_message(content="Look at this local image!", elements=elements)
42+
cl.Message(content="Look at this local image!", elements=elements).send()
4343
```
4444

4545
### Usage without scope

api-reference/elements/remote-image.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "Remote Image"
44

55
The `RemoteImage` class allows you to display an image hosted remotely in the chatbot UI. Instead of uploading a local image, this class takes a URL of an image hosted online, creates an image element and sends it to the UI.
66

7-
### Parameters
7+
### Attributes
88

99
<ParamField path="name" type="str">
1010
The name of the image to be displayed in the UI.
@@ -34,7 +34,7 @@ elements = [
3434
cl.RemoteImage(name="remote_image", url=image_url, display="inline")
3535
]
3636

37-
cl.send_message(content="Look at this remote image!", elements=elements)
37+
cl.Message(content="Look at this remote image!", elements=elements).send()
3838
```
3939

4040
### Usage without scope

api-reference/elements/text.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "Text"
44

55
The `Text` class allows you to display a text element in the chatbot UI. This class takes a string and creates a text element that can be sent to the UI.
66

7-
### Parameters
7+
### Attributes
88

99
<ParamField path="name" type="str">
1010
The name of the text element to be displayed in the UI.
@@ -35,7 +35,7 @@ elements = [
3535
cl.Text(name="simple_text", text=text_content, display="inline")
3636
]
3737

38-
cl.send_message(content="Check out this text element!", elements=elements)
38+
cl.Message(content="Check out this text element!", elements=elements).send()
3939
```
4040

4141
### Usage without scope

api-reference/langchain/langchain-postprocess.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: "langchain_postprocess"
44

55
Useful to post process the response a LangChain object instantiated with [@langchain_factory](/api-reference/langchain/langchain-factory).
66
The decorated function takes the raw output of the LangChain object as input.
7-
The response will NOT be automatically sent to the UI, you need to call [send_message](/api-reference/send-message).
7+
The response will NOT be automatically sent to the UI, you need to send a [Message](/api-reference/message).
88

99
### Parameters
1010

@@ -30,5 +30,5 @@ def main():
3030

3131
@cl.langchain_postprocess
3232
def postprocess(output: str):
33-
cl.send_message("In the end it doesn't even matter.")
33+
cl.Message(content="In the end it doesn't even matter.").send()
3434
```

api-reference/langchain/langchain-run.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: "langchain_run"
55
Useful to override the default behavior of the LangChain object instantiated with [@langchain_factory](/api-reference/langchain/langchain-factory).
66
Use when your agent run method has custom parameters.
77
Takes the LangChain agent and the user input as parameters.
8-
The response will NOT be automatically sent to the UI, you need to call [send_message](/api-reference/send-message).
8+
The response will NOT be automatically sent to the UI, you need to send a [Message](/api-reference/message).
99

1010
### Parameters
1111

@@ -35,5 +35,5 @@ def main():
3535
@cl.langchain_run
3636
def run(agent, input_str):
3737
res = agent("2+2")
38-
cl.send_message(res["text"])
38+
cl.Message(content=res["text"]).send()
3939
```

api-reference/send-message.mdx renamed to api-reference/message.mdx

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
---
2-
title: "send_message"
2+
title: "Message"
33
---
44

5-
Send a message to the chatbot UI.
6-
If a project ID is configured, the message will be uploaded to the cloud storage.
5+
The `Message` class is designed to send, stream, edit or remove messages in the chatbot user interface.
76

8-
### Parameters
7+
### Attributes
98

109
<ParamField path="content" type="str">
1110
The content of the message.
@@ -37,18 +36,60 @@ If a project ID is configured, the message will be uploaded to the cloud storage
3736
If positive, the message will be nested in the UI.
3837
</ParamField>
3938

40-
### Returns
39+
### Send a message
4140

42-
<ResponseField name="response" type="str" required>
43-
The message ID.
44-
</ResponseField>
41+
```python Code Example
42+
import chainlit as cl
43+
44+
45+
@cl.on_message
46+
def main(message: str):
47+
cl.Message(
48+
content=f"Received: {message}",
49+
).send()
50+
```
4551

46-
### Usage
52+
### Stream a message
4753

4854
```python Code Example
49-
from chainlit import send_message, on_message
55+
import chainlit as cl
5056

51-
@on_message
52-
def main(message: str):
53-
send_message(content=f"Received: {message}")
57+
token_list = ["the", "quick", "brown", "fox"]
58+
59+
60+
@cl.on_chat_start
61+
def main():
62+
msg = cl.Message(content="")
63+
for token in token_list:
64+
msg.stream_token(token)
65+
66+
msg.send()
67+
```
68+
69+
### Update a message
70+
71+
```python Code Example
72+
import chainlit as cl
73+
74+
75+
@cl.on_chat_start
76+
def main():
77+
msg = cl.Message(content="Hello!")
78+
msg.send()
79+
cl.sleep(2)
80+
msg.update(content="Hello again!")
81+
```
82+
83+
### Remove a message
84+
85+
```python Code Example
86+
import chainlit as cl
87+
88+
89+
@cl.on_chat_start
90+
def main():
91+
msg = cl.Message(content="Message 1")
92+
msg.send()
93+
cl.sleep(2)
94+
msg.remove()
5495
```

api-reference/on-chat-start.mdx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@ title: "on_chat_start"
44

55
Hook to react to the user websocket connection event.
66

7-
<RequestExample>
7+
### Usage
8+
89
```python Code Example
9-
from chainlit import send_message, ask_for_input, on_chat_start
10+
from chainlit import AskUserMessage, Message, on_chat_start
1011

1112

1213
@on_chat_start
1314
def main():
14-
res = ask_for_input(content="What is your name?")
15+
res = AskUserMessage(content="What is your name?", timeout=30).send()
1516
if res:
16-
send_message(
17-
content=f"Your name is: {res['content']}",
18-
)
17+
Message(
18+
content=f"Your name is: {res['content']}.\nChainlit installation is working!\nYou can now start building your own chainlit apps!",
19+
).send()
1920
```
20-
</RequestExample>

0 commit comments

Comments
 (0)