-
Notifications
You must be signed in to change notification settings - Fork 1
/
chat4R_history.R
60 lines (50 loc) · 2.19 KB
/
chat4R_history.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
#' Chat History for R
#'
#' @title chat4R_history: Use chat history for interacting with GPT.
#' @description This function use the chat history with the the specified GPT model, and chat the AI model.
#' @param history A list of message objects.
#' Each object should have a 'role' that can be 'system', 'user', or 'assistant',
#' and 'content' which is the content of the message from that role.
#' @param api_key A string. Input your OpenAI API key. Defaults to the value of the environment variable "OPENAI_API_KEY".
#' @param Model A string. The model to use for the chat completion. Default is "gpt-3.5-turbo-16k".
#' @param temperature The temperature to use for the chat completion. Default is 1.
#' @importFrom httr add_headers POST content
#' @importFrom jsonlite toJSON
#' @return A data frame containing the parsed response from the Web API server.
#' @export chat4R_history
#' @author Satoshi Kume
#' @examples
#' \dontrun{
#' Sys.setenv(OPENAI_API_KEY = "Your API key")
#'
#' history <- list(list('role' = 'system', 'content' = 'You are a helpful assistant.'),
#' list('role' = 'user', 'content' = 'Who won the world series in 2020?'))
#'
#' chat4R_history(history)
#' }
#'
chat4R_history <- function(history,
api_key = Sys.getenv("OPENAI_API_KEY"),
Model = "gpt-3.5-turbo-16k",
temperature = 1) {
# Setting parameters
# See detail: https://platform.openai.com/docs/guides/chat
api_url <- "https://api.openai.com/v1/chat/completions"
n <- 1
top_p <- 1
# Creating headers
headers <-
httr::add_headers(`Content-Type` = "application/json",
`Authorization` = paste("Bearer", api_key))
# Using the history variable to create arguments
body <-
list(model = Model,
messages = history,
temperature = temperature, top_p = top_p, n = n)
# Sending a request to the server
response <- httr::POST(url = api_url,
body = jsonlite::toJSON(body, auto_unbox = TRUE),
encode = "json", config = headers)
# Parsing the result
return(data.frame(content=httr::content(response, "parsed")$choices[[1]]$message$content))
}