shiny

components for Shiny

Shiny for Python is a front end framework that allows you to quickly build simple applications. It’s perfect for customizing your own data annotation and review app for LLMs1. This module contains opinionated components that display ChatOpenAI run information in Shiny Apps.


source

render_input_chat

 render_input_chat (run:langfree.transform.RunData, markdown=True)

Render the chat history, except for the last output as a group of cards.

render_input will take an instance of RunData and render a set of Shiny cards, with each card containing one turn of the chat conversation:

from langfree.transform import RunData
with _temp_env_var(tmp_env):  #context manager that has specific environment vars for testing
    _tst_run = RunData.from_run_id('1863d76e-1462-489a-a8a7-e0404239fe47')
    
_rendered_inp = render_input_chat(_tst_run)
/Users/hamel/mambaforge/lib/python3.10/site-packages/langchain_core/_api/beta_decorator.py:86: LangChainBetaWarning: The function `load` is in beta. It is actively being worked on, so the API may change.
  warn_beta(

Below, we render the first card in the conversation:

_rendered_inp.children[0] # the first message in the conversation
SYSTEM(1/3)

You are a helpful documentation Q&A assistant, trained to answer questions from LangSmith's documentation. LangChain is a framework for building applications using large language models. The current time is 2023-09-05 16:49:07.308007.

Relevant documents will be retrieved in the following messages.

Here is the last card in the conversation:

_rendered_inp.children[-1] # the last message in the conversation
USER(3/3)

How do I move my project between organizations?

_rendered_inp.children[3]
FUNCTION RESULTS(4/7)
{'id': 'ee152e0b-0de2-48ea-94e5-a14b8c0d5178',
 'inputs': [{'required': False,
             'suggestions': [{'listing': {'address': '2430 Victory Park Lane '
                                                     'Unit 2508',
                                          'baths': 2.1,
                                          'beds': 2,
                                          'close_date': None,
                                          'close_price': None,
                                          'id': '7ff2fad0-cc63-11e5-913f-f23c91c841bd',
                                          'mls': 'NTREIS',
                                          'mls_number': '13315265',
                                          'price': 1350000,
                                          'status': 'Active',
                                          'type': 'compact_listing'},
                              'type': 'input_form_suggestion',
                              'value': '1st listing'},
                             {'listing': {'address': '2430 Victory Park Lane '
                                                     'Unit 2003',
                                          'baths': 2,
                                          'beds': 1,
                                          'close_date': None,
                                          'close_price': None,
                                          'id': '33836686-d0db-11e5-b351-f23c91c841bd',
                                          'mls': 'NTREIS',
                                          'mls_number': '13317847',
                                          'price': 770000,
                                          'status': 'Active',
                                          'type': 'compact_listing'},
                              'type': 'input_form_suggestion',
                              'value': '2nd listing'},
                             {'listing': {'address': '2430 Victory Park Lane '
                                                     'Unit 3001',
                                          'baths': 7.3,
                                          'beds': 5,
                                          'close_date': None,
                                          'close_price': None,
                                          'id': 'cdaef89c-cb9a-11e5-b4c2-f23c91c841bd',
                                          'mls': 'NTREIS',
                                          'mls_number': '13314620',
                                          'price': 7995000,
                                          'status': 'Active',
                                          'type': 'compact_listing'},
                              'type': 'input_form_suggestion',
                              'value': '3rd listing'},
                             {'listing': {'address': '2430 Victory Park Lane '
                                                     'Unit 2301',
                                          'baths': 1,
                                          'beds': 1,
                                          'close_date': None,
                                          'close_price': None,
                                          'id': '7e84d768-a814-11e5-8e1e-f23c91c841bd',
                                          'mls': 'NTREIS',
                                          'mls_number': '13291126',
                                          'price': 399900,
                                          'status': 'Active',
                                          'type': 'compact_listing'},
                              'type': 'input_form_suggestion',
                              'value': '4th listing'},
                             {'listing': {'address': '2430 Victory Park Lane '
                                                     'Unit 1908',
                                          'baths': 2.1,
                                          'beds': 2,
                                          'close_date': None,
                                          'close_price': None,
                                          'id': '58a718e4-9f54-11e5-a0f8-f23c91c841bd',
                                          'mls': 'NTREIS',
                                          'mls_number': '13286938',
                                          'price': 999000,
                                          'status': 'Active',
                                          'type': 'compact_listing'},
                              'type': 'input_form_suggestion',
                              'value': '5th listing'}],
             'title': 'Which one of the following listings do you want to use?',
             'type': 'input_form_input'}],
 'type': 'input_form'}
assert len(_rendered_inp.children) == len(_run.inputs)

source

render_funcs

 render_funcs (run:langfree.transform.RunData, markdown=True)

Render functions as a group of cards.

Similar to render_input, render_funcs will take an instance of RunData and render a set of Shiny cards, with each card containing a function definition that was passed to OpenAI via the Function Calling API:

_rendered_func = render_funcs(_run)
assert len(_run.funcs) == len(_rendered_func.children)

This is one of the functions:

_rendered_func.children[2]
human-chat(3/5)
Description: You can ask a human for guidance when you think you got stuck or you are not sure what to do next. You can also ask him to clarify something or let user you dont know the answer to his request. You can also chat and hold a conversation with the human. Respond to their basic questions. You can also ask human generic questions. The input should be a question for the human.

{ "type": "object", "$schema": "http://json-schema.org/draft-07/schema#", "properties": { "input": { "type": "string" } }, "additionalProperties": false }

Here is another function:

_rendered_func.children[3]
calculator(4/5)
Description: Useful for getting the result of a math expression. The input to this tool should be a valid mathematical expression that could be executed by a simple calculator.

{ "type": "object", "$schema": "http://json-schema.org/draft-07/schema#", "properties": { "input": { "type": "string" } }, "additionalProperties": false }


source

render_llm_output

 render_llm_output (run, width='100%', height='250px')

Render the LLM output as an editable text box.

Below is a demonstration of using render_llm_output to produce an editable text box component. The goal is to allow the user to edit the output for fine tuning to correct errors.

render_llm_output(_tst_run)

source

invoke_later

 invoke_later (delaySecs:int, callback:<built-infunctioncallable>)

Execute code in a shiny app with a time delay of delaySecs asynchronously.

Footnotes

  1. We tried other similar frameworks like Gradio, Streamlit, and Panel, but found Shiny to fit our needs the best.↩︎