Skip to main content

Agent chatbot for gradio application for Sail Autopilot

Project description

gradio_autopilot_chatbot

PyPI - Version

Agent chatbot for gradio application for Sail Autopilot

Installation

pip install gradio_autopilot_chatbot

Usage

import gradio as gr
from gradio_autopilot_chatbot import AgentChatbot


example = AgentChatbot().example_value()

with gr.Blocks() as demo:
    with gr.Row():
        # AgentChatbot(label="Blank"),  # blank component
        AgentChatbot(
            value=example, 
            label="Populated",
            avatar_images=(
            "/Users/franklee/Documents/Projects/Development/Agent-Chatbot/agentchatbot/demo/avatars/user.png",
            "/Users/franklee/Documents/Projects/Development/Agent-Chatbot/agentchatbot/demo/avatars/terminal.png",
            "/Users/franklee/Documents/Projects/Development/Agent-Chatbot/agentchatbot/demo/avatars/robot.png",
            
            ),
            editable="llm",
    ),  # populated component


if __name__ == "__main__":
    demo.launch()

AgentChatbot

Initialization

name type default description
value
list[MessageDict | Message] | TupleFormat | Callable | None
None Default list of messages to show in chatbot, where each message is of the format {"role": "user", "content": "Help me."}. Role can be one of "user", "assistant", or "system". Content should be either text, or media passed as a Gradio component, e.g. {"content": gr.Image("lion.jpg")}. If a function is provided, the function will be called each time the app loads to set the initial value of this component.
type
Literal["messages", "tuples"] | None
None The format of the messages passed into the chat history parameter of `fn`. If "messages", passes the value as a list of dictionaries with openai-style "role" and "content" keys. The "content" key's value should be one of the following - (1) strings in valid Markdown (2) a dictionary with a "path" key and value corresponding to the file to display or (3) an instance of a Gradio component. At the moment Image, Plot, Video, Gallery, Audio, HTML, and Model3D are supported. The "role" key should be one of 'user' or 'assistant'. Any other roles will not be displayed in the output. If this parameter is 'tuples', expects a `list[list[str | None | tuple]]`, i.e. a list of lists. The inner list should have 2 elements: the user message and the response message, but this format is deprecated.
label
str | I18nData | None
None the label for this component. Appears above the component and is also used as the header if there are a table of examples for this component. If None and used in a `gr.Interface`, the label will be the name of the parameter this component is assigned to.
every
Timer | float | None
None Continously calls `value` to recalculate it if `value` is a function (has no effect otherwise). Can provide a Timer whose tick resets `value`, or a float that provides the regular interval for the reset Timer.
inputs
Component | Sequence[Component] | set[Component] | None
None Components that are used as inputs to calculate `value` if `value` is a function (has no effect otherwise). `value` is recalculated any time the inputs change.
show_label
bool | None
None if True, will display label.
container
bool
True If True, will place the component in a container - providing some extra padding around the border.
scale
int | None
None relative size compared to adjacent Components. For example if Components A and B are in a Row, and A has scale=2, and B has scale=1, A will be twice as wide as B. Should be an integer. scale applies in Rows, and to top-level Components in Blocks where fill_height=True.
min_width
int
160 minimum pixel width, will wrap if not sufficient screen space to satisfy this value. If a certain scale value results in this Component being narrower than min_width, the min_width parameter will be respected first.
visible
bool
True If False, component will be hidden.
elem_id
str | None
None An optional string that is assigned as the id of this component in the HTML DOM. Can be used for targeting CSS styles.
elem_classes
list[str] | str | None
None An optional list of strings that are assigned as the classes of this component in the HTML DOM. Can be used for targeting CSS styles.
autoscroll
bool
True If True, will automatically scroll to the bottom of the textbox when the value changes, unless the user scrolls up. If False, will not scroll to the bottom of the textbox when the value changes.
render
bool
True If False, component will not render be rendered in the Blocks context. Should be used if the intention is to assign event listeners now but render the component later.
key
int | str | tuple[int | str, ...] | None
None in a gr.render, Components with the same key across re-renders are treated as the same component, not a new component. Properties set in 'preserved_by_key' are not reset across a re-render.
preserved_by_key
list[str] | str | None
"value" A list of parameters from this component's constructor. Inside a gr.render() function, if a component is re-rendered with the same key, these (and only these) parameters will be preserved in the UI (if they have been changed by the user or an event listener) instead of re-rendered based on the values provided during constructor.
height
int | str | None
400 The height of the component, specified in pixels if a number is passed, or in CSS units if a string is passed. If messages exceed the height, the component will scroll.
resizable
bool
False If True, the user of the Gradio app can resize the chatbot by dragging the bottom right corner.
resizeable
bool
False None
max_height
int | str | None
None The maximum height of the component, specified in pixels if a number is passed, or in CSS units if a string is passed. If messages exceed the height, the component will scroll. If messages are shorter than the height, the component will shrink to fit the content. Will not have any effect if `height` is set and is smaller than `max_height`.
min_height
int | str | None
None The minimum height of the component, specified in pixels if a number is passed, or in CSS units if a string is passed. If messages exceed the height, the component will expand to fit the content. Will not have any effect if `height` is set and is larger than `min_height`.
editable
Literal["llm"] | None
None Allows user to edit messages in the chatbot. If set to "user", allows editing of user messages. If set to "all", allows editing of assistant messages as well.
latex_delimiters
list[dict[str, str | bool]] | None
None A list of dicts of the form {"left": open delimiter (str), "right": close delimiter (str), "display": whether to display in newline (bool)} that will be used to render LaTeX expressions. If not provided, `latex_delimiters` is set to `[{ "left": "$$", "right": "$$", "display": True }]`, so only expressions enclosed in $$ delimiters will be rendered as LaTeX, and in a new line. Pass in an empty list to disable LaTeX rendering. For more information, see the [KaTeX documentation](https://katex.org/docs/autorender.html).
rtl
bool
False If True, sets the direction of the rendered text to right-to-left. Default is False, which renders text left-to-right.
show_share_button
bool | None
None If True, will show a share icon in the corner of the component that allows user to share outputs to Hugging Face Spaces Discussions. If False, icon does not appear. If set to None (default behavior), then the icon appears if this Gradio app is launched on Spaces, but not otherwise.
show_copy_button
bool
False If True, will show a copy button for each chatbot message.
watermark
str | None
None If provided, this text will be appended to the end of messages copied from the chatbot, after a blank line. Useful for indicating that the message is generated by an AI model.
avatar_images
tuple[
        str | Path | None,
        str | Path | None,
        str | Path | None,
    ]
    | None
None Tuple of two avatar image paths or URLs for user and bot (in that order). Pass None for either the user or bot image to skip. Must be within the working directory of the Gradio app or an external URL.
sanitize_html
bool
True If False, will disable HTML sanitization for chatbot messages. This is not recommended, as it can lead to security vulnerabilities.
render_markdown
bool
True If False, will disable Markdown rendering for chatbot messages.
feedback_options
list[str] | tuple[str, ...] | None
"Like", "Dislike" A list of strings representing the feedback options that will be displayed to the user. The exact case-sensitive strings "Like" and "Dislike" will render as thumb icons, but any other choices will appear under a separate flag icon.
feedback_value
Sequence[str | None] | None
None A list of strings representing the feedback state for entire chat. Only works when type="messages". Each entry in the list corresponds to that assistant message, in order, and the value is the feedback given (e.g. "Like", "Dislike", or any custom feedback option) or None if no feedback was given for that message.
line_breaks
bool
True If True (default), will enable Github-flavored Markdown line breaks in chatbot messages. If False, single new lines will be ignored. Only applies if `render_markdown` is True.
layout
Literal["panel", "bubble"] | None
None If "panel", will display the chatbot in a llm style layout. If "bubble", will display the chatbot with message bubbles, with the user and bot messages on alterating sides. Will default to "bubble".
placeholder
str | None
None a placeholder message to display in the chatbot when it is empty. Centered vertically and horizontally in the AgentChatbot. Supports Markdown and HTML. If None, no placeholder is displayed.
examples
list[ExampleMessage] | None
None A list of example messages to display in the chatbot before any user/assistant messages are shown. Each example should be a dictionary with an optional "text" key representing the message that should be populated in the AgentChatbot when clicked, an optional "files" key, whose value should be a list of files to populate in the AgentChatbot, an optional "icon" key, whose value should be a filepath or URL to an image to display in the example box, and an optional "display_text" key, whose value should be the text to display in the example box. If "display_text" is not provided, the value of "text" will be displayed.
group_consecutive_messages
bool
True If True, will display consecutive messages from the same role in the same bubble. If False, will display each message in a separate bubble. Defaults to True.
allow_tags
list[str] | bool
False If a list of tags is provided, these tags will be preserved in the output chatbot messages, even if `sanitize_html` is `True`. For example, if this list is ["thinking"], the tags `` and `` will not be removed. If True, all custom tags (non-standard HTML tags) will be preserved. If False, no tags will be preserved (default behavior).
_retryable
bool
True If True, will show a retry button for the last assistant message. When clicked, it clears the LLM output and allows regeneration. Defaults to True.

Events

name description
change Triggered when the value of the AgentChatbot changes either because of user input (e.g. a user types in a textbox) OR because of a function update (e.g. an image receives a value from the output of an event trigger). See .input() for a listener that is only triggered by user input.
select Event listener for when the user selects or deselects the AgentChatbot. Uses event data gradio.SelectData to carry value referring to the label of the AgentChatbot, and selected to refer to state of the AgentChatbot. See EventData documentation on how to use this event data
like This listener is triggered when the user likes/dislikes from within the AgentChatbot. This event has EventData of type gradio.LikeData that carries information, accessible through LikeData.index and LikeData.value. See EventData documentation on how to use this event data.
retry This listener is triggered when the user clicks the retry button in the chatbot message.
undo This listener is triggered when the user clicks the undo button in the chatbot message.
example_select This listener is triggered when the user clicks on an example from within the AgentChatbot. This event has SelectData of type gradio.SelectData that carries information, accessible through SelectData.index and SelectData.value. See SelectData documentation on how to use this event data.
option_select This listener is triggered when the user clicks on an option from within the AgentChatbot. This event has SelectData of type gradio.SelectData that carries information, accessible through SelectData.index and SelectData.value. See SelectData documentation on how to use this event data.
copy This listener is triggered when the user copies content from the AgentChatbot. Uses event data gradio.CopyData to carry information about the copied content. See EventData documentation on how to use this event data
edit This listener is triggered when the user edits the AgentChatbot (e.g. image) using the built-in editor.

User function

The impact on the users predict function varies depending on whether the component is used as an input or output for an event (or both).

  • When used as an Input, the component only impacts the input signature of the user function.
  • When used as an output, the component only impacts the return signature of the user function.

The code snippet below is accurate in cases where the component is used as both an input and an output.

  • As output: Is passed, if type is 'tuples', passes the messages in the chatbot as a list[list[str | None | tuple]], i.e. a list of lists. The inner list has 2 elements: the user message and the response message. Each message can be (1) a string in valid Markdown, (2) a tuple if there are displayed files: (a filepath or URL to a file, [optional string alt text]), or (3) None, if there is no message displayed. If type is 'messages', passes the value as a list of dictionaries with 'role' and 'content' keys. The content key's value supports everything the tuples format supports.
  • As input: Should return, if type is tuples, expects a list[list[str | None | tuple]], i.e. a list of lists. The inner list should have 2 elements: the user message and the response message. The individual messages can be (1) strings in valid Markdown, (2) tuples if sending files: (a filepath or URL to a file, [optional string alt text]) -- if the file is image/video/audio, it is displayed in the AgentChatbot, or (3) None, in which case the message is not displayed. If type is 'messages', passes the value as a list of dictionaries with 'role' and 'content' keys. The content key's value supports everything the tuples format supports.
def predict(
    value: list[list[str | tuple[str] | tuple[str, str] | None]]
   | list[MessageDict]
) -> Sequence[
       tuple[
           typing.Union[str, tuple[str], NoneType][
               str, tuple[str], None
           ],
           typing.Union[str, tuple[str], NoneType][
               str, tuple[str], None
           ],
       ]
       | list[
           typing.Union[str, tuple[str], NoneType][
               str, tuple[str], None
           ]
       ]
   ]
   | list[MessageDict | Message]
   | None:
    return value

MessageDict

class MessageDict(TypedDict):
    name: str
    content: str | FileDataDict | tuple | Component
    role: Literal["user", "assistant", "system"]
    metadata: NotRequired[MetadataDict]
    options: NotRequired[list[OptionDict]]

FileDataDict

class FileDataDict(TypedDict):
    path: str  # server filepath
    url: NotRequired[str | None]  # normalised server url
    size: NotRequired[int | None]  # size in bytes
    orig_name: NotRequired[str | None]  # original filename
    mime_type: NotRequired[str | None]
    is_stream: NotRequired[bool]
    meta: dict[Literal["_type"], Literal["gradio.FileData"]]

MetadataDict

class MetadataDict(TypedDict):
    title: NotRequired[str]
    id: NotRequired[int | str]
    parent_id: NotRequired[int | str]
    log: NotRequired[str]
    duration: NotRequired[float]
    status: NotRequired[Literal["pending", "done"]]

OptionDict

class OptionDict(TypedDict):
    value: str
    label: NotRequired[str]

Message

class Message(GradioModel):
    name: str
    role: str
    metadata: MetadataDict | None = None
    content: Union[str, FileMessage, ComponentMessage]
    options: list[OptionDict] | None = None

FileMessage

class FileMessage(GradioModel):
    file: FileData
    alt_text: str | None = None

ComponentMessage

class ComponentMessage(GradioModel):
    component: str
    value: Any
    constructor_args: dict[str, Any]
    props: dict[str, Any]

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gradio_autopilot_chatbot-0.0.5.tar.gz (2.2 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gradio_autopilot_chatbot-0.0.5-py3-none-any.whl (2.0 MB view details)

Uploaded Python 3

File details

Details for the file gradio_autopilot_chatbot-0.0.5.tar.gz.

File metadata

  • Download URL: gradio_autopilot_chatbot-0.0.5.tar.gz
  • Upload date:
  • Size: 2.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for gradio_autopilot_chatbot-0.0.5.tar.gz
Algorithm Hash digest
SHA256 7cbb9eecb45ea5020866a4eda78338e392a9f83ef455214f006412f191c1bcd0
MD5 efc11969545f46177d461a9b5b635cc2
BLAKE2b-256 659f2e76196fcbb45c6c837cf2003a14b433a8648b37b8fce8060878646eddbd

See more details on using hashes here.

File details

Details for the file gradio_autopilot_chatbot-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for gradio_autopilot_chatbot-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 b535cf13d38b947a21d8678c06ef153e0666dda92bd6c3df5cb90c7a2dd07155
MD5 1b6d744f9114a7188d5f0ca8f65fc54f
BLAKE2b-256 794adf56399a8c606533a2743f502bf5e7dcf12ed0fa8a18d6c352ade3c333cb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page