AI Chatbot Platform API v3
AI Chatbot
Version 3

Send streaming message

Copy link

The streaming message feature allows you to stream bot messages directly from the LLM provider such as OpenAI or Anthropic to Sendbird channels. Streaming message is when the words of your bot responses are displayed as they are being generated. This allows you to show the response to the user as soon as the first word is generated, rather than waiting for the entire response to be generated before displaying it. This can dramatically reduce the end-user’s wait time and improve user experience.


HTTP request

Copy link
POST https://api-{application_id}.sendbird.com/v3/bots/{bot_userid}/send_stream

Parameters

Copy link

The following table lists the parameters that this action supports.

Required
Parameter nameTypeDescription

bot_userid

string

Specifies the unique ID of a bot.


Request body

Copy link

The following table lists the properties of an HTTP request that this action supports.

Initial message transmission

Copy link

For the initial message chunk sent to the server, the request body includes the full message and the channel URL.

Properties
RequiredTypeDescription

message

string

Specifies the streamed chunk of the message sent by the bot.

channel_url

string

Specifies the URL of the channel where the message is sent to.

OptionalTypeDescription

target_message_id

string

Specifies the ID of the message the bot is responding to.

{
    "message": "This is the first chunk of the bot message.",
    "channel_url": "sendbird_channel_url"
}

Subsequent message transmission

Copy link

For subsequent message chunks sent to the server, the request body should contain only the delta of the message, referred to as message_delta.

Properties
RequiredTypeDescription

message_delta

string

The continuing updates of the message being sent as they are generated.

{
    "message_delta": "Continuing the previous message..."
}

Integration example

Copy link

The following is an example of how to integrate the streaming message feature into your application.

import json
from openai import OpenAI
import requests

DEFAULT_PROMPT = """
    You are a helpful assistant from Sendbird
"""


def stream_chat(prompt: str):
    first_chunk = True # Flag to identify the first chunk
    client = OpenAI(api_key='YOUR_API_KEY')
    response = client.chat.completions.create(
        model="gpt-4-1106-preview,
        messages=[
            {"role": "system", "content": DEFAULT_PROMPT},
            {"role": "user", "content": prompt}
        ],
        stream=True # Enable streaming of response messages
    )

    for chunk in response:
        content = chunk.choices[0].delta.content
        if content is not None:
            if first_chunk:
                # If it's the first chunk, send the initial message along with the channel URL.
                data = {
                    'channel_url': 'SENDBIRD_CHANNEL_URL',
                    'message': content,
                    #'target_message_id': 'ID of the user message you are responding to' (optional)
                }
                yield json.dumps(data)
                first_chunk = False # Update the flag so that subsequent chunks are sent as deltas
            else:
                # For subsequent chunks, send only the delta of the message.
                yield json.dumps({'message_delta': content})

    

res = requests.post(
    'https://api-{APP_ID}.sendbird.com/v3/bots/{bot_userid}/send_stream',
    data=stream_chat('Tell me about Sendbird'), # User input
    headers={'Api-Token': 'YOUR_API_TOKEN'},
)
print(res.content)

Responses

Copy link

If successful, this action returns a nested message object sent by the bot in the response body.

{
    "message": {
        "type": "MESG",
        "message_id": 1844308962,
        "message": "This is the first chunk of the bot message. Continuing the previous message...",
        "data": "",
        "custom_type": "auto_answer_introduction",
        "file": {},
        "created_at": 1684312426066,
        "user": {
            "user_id": "toolbox_bot",
            "profile_url": "",
            "require_auth_for_profile_image": false,
            "nickname": "toolbox_bot",
            "metadata": {},
            "role": "",
            "is_active": true
        },
        "channel_url": "sendbird_channel_url",
        "updated_at": 0,
        "message_survival_seconds": -1,
        "mentioned_users": [],
        "mention_type": "users",
        "silent": false,
        "message_retention_hour": -1,
        "channel_type": "group",
        "translations": {},
        "is_removed": false,
        "is_op_msg": false,
        "message_events": {
            "send_push_notification": "receivers",
            "update_unread_count": true,
            "update_mention_count": true,
            "update_last_message": true
        }
    }
}

In the case of an error, an error object is returned. A detailed list of error codes is available here.