-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat_interface.R
67 lines (54 loc) · 1.95 KB
/
chat_interface.R
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#Install the required packages
install.packages("shiny","openai","jsonlite")
###Loading the requried packages
library(shiny)
library(openai)
library(jsonlite)
##Setting the enviroment variables
Sys.setenv(openai_api_key = "{secret_key}")
#{Secret_key-- Input the secret key you copied from your Openai account here
ui <- fluidPage(
titlePanel("Chat Interface"),
sidebarLayout(
sidebarPanel(
textInput("user_message", "Enter your message:"),
sliderInput("temperature", "Temperature:", min = 0, max = 1, value = 0.7, step = 0.1),
actionButton("submit_message", "Submit Message")
),
mainPanel(
# in the UI function
titlePanel("Response:"),
textOutput(outputId = "assistant")
)
)
)
server <- function(input, output, session) {
chat_history <- list() # Store chat history
observeEvent(input$submit_message, {
user_message <- input$user_message
# Add user's message to chat history
chat_history <- append(chat_history, list(list("role" = "user",
"content" = user_message)))
# Clear the user input field
updateTextInput(session, "user_message", value = "")
# Call the create_chat_completion function
assistant_response <- create_chat_completion(
model = "gpt-3.5-turbo",
messages = chat_history,
temperature = input$temperature,
openai_api_key = Sys.getenv("openai_api_key")
)
# Extract and append assistant's response content to chat history
assistant_content <- assistant_response[[5]]
chat_history <- append(chat_history, list(list("role" = "assistant",
"content" = assistant_content)))
# Update the chat display in the mainPanel
output$assistant <-
renderText({
paste(assistant_content[4])
})
})
}
#Add the final Shiny code that integrate the UI and the Server logic together
shinyApp(ui, server)
##Run the Shiny app