Skip to content

Commit

Permalink
updated response_format for structural output
Browse files Browse the repository at this point in the history
  • Loading branch information
yifeifrank committed Nov 25, 2024
1 parent a7ddfbc commit 994d9e2
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 21 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Package: aitag
Package: aitagR
Title: Annotate Text Data Using AI APIs
Version: 1.3.0
Version: 1.3.3
Authors@R: person("Yifei", "Zhu", , "[email protected]", role = c("aut", "cre"))
Description: This package provides functions to annotate text data using various AI APIs, including OpenAI's GPT, Anthropic's Claude, Perplexity AI, and Oobabooga. The main functions send text data to the respective APIs and save the responses as CSV files in a directory structure. Users can provide custom instructions and control various parameters to customize the behavior of the AI models.
License: MIT + file LICENSE
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
Imports:
httr2,
dplyr,
Expand Down
3 changes: 2 additions & 1 deletion R/claude_api_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#' @examples
#' \dontrun{
#' my_data <- c("Apple", "Tomato", "Broccoli")
#' annotated_data <- tag_claude(my_data, sys_prompt = "Which one is a fruit?")
#' system_prompt <- "Which one is a fruit?"
#' annotated_data <- tag_gpt(my_data, sys_prompt = system_prompt)
#' }
#' @importFrom httr2 request req_headers req_body_json req_timeout req_perform resp_body_json
#' @importFrom stringr str_c
Expand Down
3 changes: 2 additions & 1 deletion R/obabooga_api_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
#' @examples
#' \dontrun{
#' my_data <- c("Apple", "Tomato", "Broccoli")
#' annotated_data <- tag_oobabooga(my_data, sys_prompt = "Which one is a fruit?")
#' system_prompt <- "Which one is a fruit?"
#' annotated_data <- tag_gpt(my_data, sys_prompt = system_prompt)
#' }
#' @importFrom httr2 request req_headers req_body_json req_timeout req_perform resp_body_json
#' @importFrom stringr str_c
Expand Down
29 changes: 19 additions & 10 deletions R/openai_api_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
#' @param verbose Logical. If TRUE, prints progress and error messages. Default is TRUE.
#' @param storage Logical. If TRUE, saves the annotated responses as a CSV file. Default is TRUE.
#' @param rate_limit Numeric. The rate limit for API requests in seconds. Default is 0.6s.
#' @param response_format A list specifying the desired response format (e.g., JSON schema). Default is NULL.
#' @return A character vector containing the annotated responses from the GPT API.
#' The function also saves the annotated responses as a CSV file in the "LLMoutput/columnname/model/.csv" path.
#' @examples
#' \dontrun{
#' my_data <- tibble(c("Apple", "Tomato", "Broccoli"))
#' my_data <- c("Apple", "Tomato", "Broccoli")
#' system_prompt <- "Which one is a fruit?"
#' annotated_data <- tag_gpt(my_data, sys_prompt = system_prompt)
#' }
Expand All @@ -33,7 +34,8 @@ tag_gpt <- function(user_prompt,
model = "gpt-3.5-turbo-0125",
verbose = TRUE,
storage = TRUE,
rate_limit = 0.6) {
rate_limit = 0.6,
response_format = NULL){

# Ensure the API URL is complete
api_url <- stringr::str_c(api_url, "/v1/chat/completions")
Expand All @@ -57,16 +59,23 @@ tag_gpt <- function(user_prompt,
while (!success && attempt <= max_attempts) {
base::Sys.sleep(rate_limit) # Sleep to avoid rate limiting
tryCatch({
body_content <- list(
model = model,
temperature = temperature,
messages = list(
list(role = "system", content = sys_prompt[index]),
list(role = "user", content = to_annotate_text)
)
)

# Add response_format if provided
if (!is.null(response_format)) {
body_content$response_format <- response_format
}

response <- httr2::request(api_url) |>
httr2::req_headers(`Content-Type` = "application/json", `Authorization` = authorization) |>
httr2::req_body_json(list(
model = model,
temperature = temperature,
messages = list(
list(role = "system", content = sys_prompt[index]),
list(role = "user", content = to_annotate_text)
)
)) |>
httr2::req_body_json(body_content) |>
httr2::req_timeout(60000) |> # 60 seconds timeout
httr2::req_perform()

Expand Down
3 changes: 2 additions & 1 deletion R/pplx_api_call.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
#' @examples
#' \dontrun{
#' my_data <- c("Apple", "Tomato", "Broccoli")
#' annotated_data <- tag_perplexity(my_data, sys_prompt = "Which one is a fruit?")
#' system_prompt <- "Which one is a fruit?"
#' annotated_data <- tag_gpt(my_data, sys_prompt = system_prompt)
#' }
#' @importFrom httr2 request req_headers req_body_json req_timeout req_perform resp_body_json
#' @importFrom stringr str_c
Expand Down
3 changes: 2 additions & 1 deletion man/tag_claude.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions man/tag_gpt.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tag_oobabooga.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion man/tag_perplexity.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 994d9e2

Please sign in to comment.