File size: 1,141 Bytes
4358660
 
b3c7a65
 
 
4358660
b3c7a65
4358660
b3c7a65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4358660
 
 
b3c7a65
 
 
 
 
 
 
 
 
 
 
 
4358660
 
 
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
41
42
library(shiny)
library(bslib)
library(httr2)
library(purrr)
library(glue)

source("helper.R")

ui <- page_sidebar(
  title = "Chatbot with R & OpenAI",
  theme = bs_theme(bootswatch = "vapor"),
  sidebar = sidebar(
    open = "closed",
    selectInput("model", "Model",
                choices = c("gpt-3.5-turbo", "gpt-4")),
    selectInput("task", "Task",
                choices = c("general", "code"))
  ),
  textAreaInput("prompt", NULL, width = "400px"),
  actionButton("chat", NULL, icon = icon("paper-plane"),
               width = "75px",
               class = "m-2 btn-secondary"),
  uiOutput("chat_history")
)

server <- function(input, output, session) {
  rv <- reactiveValues()
  rv$chat_history <- NULL
  observe({
    req(input$prompt != "")
    response <- chat(input$prompt,
                     history = rv$chat_history,
                     system_prompt = input$task)
    rv$chat_history <- update_history(rv$chat_history, input$prompt, response)
    output$chat_history <- renderUI(
      map(rv$chat_history, \(x) markdown(glue("{x$role}: {x$content}")))
    )
  }) |> bindEvent(input$chat)
}

shinyApp(ui, server)