File size: 1,210 Bytes
ea8b731
 
8b78dce
 
ea8b731
8b78dce
96cd409
ea8b731
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b78dce
96cd409
ea8b731
 
 
 
 
 
 
96cd409
ea8b731
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# -----------------------------------------------------------------------------
# A basic example of dynamically re-rendering a Shiny Chat instance with different models.
# To run it, you'll need an OpenAI API key.
# To get one, follow the instructions at https://platform.openai.com/docs/quickstart
# -----------------------------------------------------------------------------
import os

from langchain_openai import ChatOpenAI

from shiny.express import input, render, ui

ui.input_select("model", "Model", choices=["gpt-4o", "gpt-3.5-turbo"])


@render.express
def chat_ui():

    chat = ui.Chat(
        id="chat",
        messages=[
            {
                "content": f"Hi! I'm a {input.model()} model. How can I help you today?",
                "role": "assistant",
            }
        ],
    )

    chat.ui()

    llm = ChatOpenAI(
        model=input.model(),
        # Provide your API key here (or set the environment variable)
        api_key=os.environ.get("OPENAI_API_KEY"),  # type: ignore
    )

    @chat.on_user_submit
    async def _():
        messages = chat.messages(format="langchain")
        response = llm.astream(messages)
        await chat.append_message_stream(response)