Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 1 | # Helper function to find README.md file in current or parent directories |
| 2 | find_readme_path <- function() { |
| 3 | readme_paths <- c("Readme.md", "../Readme.md", "../../Readme.md") |
| 4 | for (path in readme_paths) { |
| 5 | if (file.exists(path)) { |
| 6 | return(path) |
| 7 | } |
| 8 | } |
| 9 | return(NULL) |
| 10 | } |
| 11 | |
| 12 | # Helper function to read README content |
| 13 | read_readme_content <- function() { |
| 14 | readme_path <- find_readme_path() |
| 15 | if (is.null(readme_path)) { |
| 16 | return(NULL) |
| 17 | } |
| 18 | readme_content <- readLines(readme_path) |
| 19 | paste(readme_content, collapse = "\n") |
| 20 | } |
| 21 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 22 | # Helper function to call LLM API using tidyllm |
| 23 | call_llm_api <- function(prompt, max_tokens = 500, temperature = 0.1, model = LLM_MODEL) { |
| 24 | library(tidyllm) |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 25 | |
| 26 | tryCatch({ |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 27 | # Determine the provider based on model name |
| 28 | if (grepl("^gpt-", model, ignore.case = TRUE)) { |
| 29 | provider <- openai() |
| 30 | } else if (grepl("^claude-", model, ignore.case = TRUE)) { |
| 31 | provider <- claude() |
| 32 | } else { |
| 33 | stop(paste("Unsupported model:", model)) |
| 34 | } |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 35 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 36 | # Use tidyllm unified API |
| 37 | result <- llm_message(prompt) |> |
| 38 | chat( |
| 39 | .provider = provider, |
| 40 | .model = model, |
| 41 | .temperature = temperature, |
| 42 | .max_tries = 3 |
| 43 | ) |
| 44 | |
| 45 | # Extract the reply text |
| 46 | get_reply(result) |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 47 | }, error = function(e) { |
| 48 | if (grepl("429", as.character(e))) { |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 49 | skip("LLM API rate limit exceeded - please try again later or check your API key/credits") |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 50 | } else if (grepl("401", as.character(e))) { |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 51 | skip("LLM API authentication failed - please check your API keys (OPENAI_API_KEY or ANTHROPIC_API_KEY)") |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 52 | } else { |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 53 | stop(paste("LLM API error:", as.character(e))) |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 54 | } |
| 55 | }) |
| 56 | } |
| 57 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 58 | |
Marc Kupietz | 8e1b77d | 2025-07-05 20:21:59 +0200 | [diff] [blame] | 59 | # Configuration variables |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 60 | #LLM_MODEL <- "gpt-4o-mini" |
| 61 | LLM_MODEL <- "claude-3-5-sonnet-latest" |
| 62 | #LLM_MODEL <- "claude-3-7-sonnet-latest" |
| 63 | #LLM_MODEL <- "claude-sonnet-4-0" |
Marc Kupietz | 10dcfee | 2025-07-05 19:13:27 +0200 | [diff] [blame] | 64 | KORAP_URL <- "https://korap.ids-mannheim.de/instance/wiki" |
| 65 | |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 66 | # Helper function to create README-guided prompt |
| 67 | create_readme_prompt <- function(task_description, specific_task) { |
| 68 | readme_text <- read_readme_content() |
| 69 | if (is.null(readme_text)) { |
| 70 | stop("README.md not found") |
| 71 | } |
| 72 | |
| 73 | paste0( |
| 74 | "You are an expert R programmer. Based on the following README documentation for the RKorAPClient package, ", |
| 75 | task_description, "\n\n", |
Marc Kupietz | 8e1b77d | 2025-07-05 20:21:59 +0200 | [diff] [blame] | 76 | "IMPORTANT: Use the KorAP URL '", KORAP_URL, "' as the 1st parameter (KorAPUrl) in KorAPConnection.\n\n", |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 77 | "README Documentation:\n", |
| 78 | readme_text, |
| 79 | "\n\nTask: ", specific_task, |
| 80 | "\n\nProvide only the R code without explanations." |
| 81 | ) |
| 82 | } |
| 83 | |
| 84 | # Helper function to extract R code from markdown code blocks |
| 85 | extract_r_code <- function(response_text) { |
| 86 | # Remove markdown code blocks if present |
| 87 | code <- gsub("```[rR]?\\n?", "", response_text) |
| 88 | code <- gsub("```\\n?$", "", code) |
| 89 | # Remove leading/trailing whitespace |
| 90 | trimws(code) |
| 91 | } |
| 92 | |
Marc Kupietz | e759b34 | 2025-07-05 19:48:20 +0200 | [diff] [blame] | 93 | # Helper function to test code syntax |
| 94 | test_code_syntax <- function(code) { |
| 95 | tryCatch({ |
| 96 | parse(text = code) |
| 97 | TRUE |
| 98 | }, error = function(e) { |
| 99 | cat("Syntax error:", as.character(e), "\n") |
| 100 | FALSE |
| 101 | }) |
| 102 | } |
| 103 | |
| 104 | # Helper function to run code if RUN_LLM_CODE is set |
| 105 | run_code_if_enabled <- function(code, test_name) { |
| 106 | if (nzchar(Sys.getenv("RUN_LLM_CODE")) && Sys.getenv("RUN_LLM_CODE") == "true") { |
| 107 | cat("Running generated code for", test_name, "...\n") |
| 108 | tryCatch({ |
| 109 | result <- eval(parse(text = code)) |
| 110 | cat("Code executed successfully. Result type:", class(result), "\n") |
| 111 | if (is.data.frame(result)) { |
| 112 | cat("Result dimensions:", nrow(result), "rows,", ncol(result), "columns\n") |
| 113 | if (nrow(result) > 0) { |
| 114 | cat("First few rows:\n") |
| 115 | print(head(result, 3)) |
| 116 | } |
| 117 | } else { |
| 118 | cat("Result preview:\n") |
| 119 | print(result) |
| 120 | } |
| 121 | return(TRUE) |
| 122 | }, error = function(e) { |
| 123 | cat("Runtime error:", as.character(e), "\n") |
| 124 | return(FALSE) |
| 125 | }) |
| 126 | } else { |
| 127 | cat("Skipping code execution (set RUN_LLM_CODE=true to enable)\n") |
| 128 | return(NA) |
| 129 | } |
| 130 | } |
| 131 | |
Marc Kupietz | 8e1b77d | 2025-07-05 20:21:59 +0200 | [diff] [blame] | 132 | test_that(paste(LLM_MODEL, "can solve frequency query task with README guidance"), { |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 133 | # Check for README file |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 134 | skip_if_not(!is.null(find_readme_path()), "Readme.md not found in current or parent directories") |
| 135 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 136 | # Note: tidyllm will handle API key checking and give appropriate errors |
| 137 | |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 138 | # Create the prompt with README context and task |
| 139 | prompt <- create_readme_prompt( |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 140 | "write R code to perform a frequency query for the word 'Deutschland' across multiple years (2022-2024). The code should use the RKorAPClient package and return a data frame.", |
| 141 | "Write R code to query frequency of 'Deutschland' from 2022-2024 using RKorAPClient." |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 142 | ) |
| 143 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 144 | # Call LLM API |
| 145 | generated_response <- call_llm_api(prompt, max_tokens = 500) |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 146 | generated_code <- extract_r_code(generated_response) |
| 147 | |
| 148 | # Basic checks on the generated code |
| 149 | expect_true(grepl("KorAPConnection", generated_code), "Generated code should include KorAPConnection") |
| 150 | expect_true(grepl("frequencyQuery", generated_code), "Generated code should include frequencyQuery") |
| 151 | expect_true(grepl("Deutschland", generated_code), "Generated code should include the search term 'Deutschland'") |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 152 | expect_true(grepl("202[2-4]", generated_code), "Generated code should include years 2022-2024") |
Marc Kupietz | 10dcfee | 2025-07-05 19:13:27 +0200 | [diff] [blame] | 153 | expect_true(grepl(KORAP_URL, generated_code, fixed = TRUE), "Generated code should include the specified KorAP URL") |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 154 | |
| 155 | # Check that the generated code contains essential RKorAPClient patterns |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 156 | # expect_true(grepl("\\|>", generated_code) || grepl("%>%", generated_code), "Generated code should use pipe operators") |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 157 | |
Marc Kupietz | e759b34 | 2025-07-05 19:48:20 +0200 | [diff] [blame] | 158 | # Test code syntax |
| 159 | syntax_valid <- test_code_syntax(generated_code) |
| 160 | expect_true(syntax_valid, "Generated code should be syntactically valid R code") |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 161 | |
| 162 | # Print the generated code for manual inspection |
| 163 | cat("Generated code:\n", generated_code, "\n") |
Marc Kupietz | e759b34 | 2025-07-05 19:48:20 +0200 | [diff] [blame] | 164 | |
| 165 | # Run the code if RUN_LLM_CODE is set |
| 166 | execution_result <- run_code_if_enabled(generated_code, "frequency query") |
| 167 | if (!is.na(execution_result)) { |
| 168 | expect_true(execution_result, "Generated code should execute without runtime errors") |
| 169 | } |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 170 | }) |
| 171 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 172 | |
Marc Kupietz | 8e1b77d | 2025-07-05 20:21:59 +0200 | [diff] [blame] | 173 | test_that(paste(LLM_MODEL, "can solve collocation analysis task with README guidance"), { |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 174 | # Check for README file |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 175 | skip_if_not(!is.null(find_readme_path()), "Readme.md not found in current or parent directories") |
| 176 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 177 | # Note: tidyllm will handle API key checking and give appropriate errors |
| 178 | |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 179 | # Create the prompt for collocation analysis |
| 180 | prompt <- create_readme_prompt( |
Marc Kupietz | e759b34 | 2025-07-05 19:48:20 +0200 | [diff] [blame] | 181 | "write R code to perform a collocation analysis for the lemma 'setzen'. The code should use the RKorAPClient package's collocationAnalysis function.", |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 182 | "Write R code to perform collocation analysis for 'setzen' using RKorAPClient." |
| 183 | ) |
| 184 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 185 | # Call LLM API |
| 186 | generated_response <- call_llm_api(prompt, max_tokens = 500) |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 187 | generated_code <- extract_r_code(generated_response) |
| 188 | |
| 189 | # Basic checks on the generated code |
| 190 | expect_true(grepl("KorAPConnection", generated_code), "Generated code should include KorAPConnection") |
| 191 | expect_true(grepl("collocationAnalysis", generated_code), "Generated code should include collocationAnalysis") |
| 192 | expect_true(grepl("setzen", generated_code), "Generated code should include the search term 'setzen'") |
| 193 | expect_true(grepl("auth", generated_code), "Generated code should include auth() for collocation analysis") |
Marc Kupietz | 10dcfee | 2025-07-05 19:13:27 +0200 | [diff] [blame] | 194 | expect_true(grepl(KORAP_URL, generated_code, fixed = TRUE), "Generated code should include the specified KorAP URL") |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 195 | |
Marc Kupietz | e759b34 | 2025-07-05 19:48:20 +0200 | [diff] [blame] | 196 | # Test code syntax |
| 197 | syntax_valid <- test_code_syntax(generated_code) |
| 198 | expect_true(syntax_valid, "Generated code should be syntactically valid R code") |
| 199 | |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 200 | # Print the generated code for manual inspection |
| 201 | cat("Generated collocation analysis code:\n", generated_code, "\n") |
Marc Kupietz | e759b34 | 2025-07-05 19:48:20 +0200 | [diff] [blame] | 202 | |
| 203 | # Run the code if RUN_LLM_CODE is set |
| 204 | execution_result <- run_code_if_enabled(generated_code, "collocation analysis") |
| 205 | if (!is.na(execution_result)) { |
| 206 | expect_true(execution_result, "Generated code should execute without runtime errors") |
| 207 | } |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 208 | }) |
| 209 | |
Marc Kupietz | 8e1b77d | 2025-07-05 20:21:59 +0200 | [diff] [blame] | 210 | test_that(paste(LLM_MODEL, "can solve corpus query task with README guidance"), { |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 211 | # Check for README file |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 212 | skip_if_not(!is.null(find_readme_path()), "Readme.md not found in current or parent directories") |
| 213 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 214 | # Note: tidyllm will handle API key checking and give appropriate errors |
| 215 | |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 216 | # Create the prompt for corpus query |
| 217 | prompt <- create_readme_prompt( |
| 218 | "write R code to perform a simple corpus query for 'Hello world' and fetch all results. The code should use the RKorAPClient package.", |
| 219 | "Write R code to query 'Hello world' and fetch all results using RKorAPClient." |
| 220 | ) |
| 221 | |
Marc Kupietz | 345211a | 2025-07-06 12:52:24 +0200 | [diff] [blame^] | 222 | # Call LLM API |
| 223 | generated_response <- call_llm_api(prompt, max_tokens = 300) |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 224 | generated_code <- extract_r_code(generated_response) |
| 225 | |
| 226 | # Basic checks on the generated code |
| 227 | expect_true(grepl("KorAPConnection", generated_code), "Generated code should include KorAPConnection") |
| 228 | expect_true(grepl("corpusQuery", generated_code), "Generated code should include corpusQuery") |
| 229 | expect_true(grepl("Hello world", generated_code), "Generated code should include the search term 'Hello world'") |
| 230 | expect_true(grepl("fetchAll", generated_code), "Generated code should include fetchAll") |
Marc Kupietz | 10dcfee | 2025-07-05 19:13:27 +0200 | [diff] [blame] | 231 | expect_true(grepl(KORAP_URL, generated_code, fixed = TRUE), "Generated code should include the specified KorAP URL") |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 232 | |
| 233 | # Check that the generated code follows the README example pattern |
| 234 | expect_true(grepl("\\|>", generated_code) || grepl("%>%", generated_code), |
| 235 | "Generated code should use pipe operators") |
| 236 | |
Marc Kupietz | e759b34 | 2025-07-05 19:48:20 +0200 | [diff] [blame] | 237 | # Test code syntax |
| 238 | syntax_valid <- test_code_syntax(generated_code) |
| 239 | expect_true(syntax_valid, "Generated code should be syntactically valid R code") |
| 240 | |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 241 | # Print the generated code for manual inspection |
| 242 | cat("Generated corpus query code:\n", generated_code, "\n") |
Marc Kupietz | e759b34 | 2025-07-05 19:48:20 +0200 | [diff] [blame] | 243 | |
| 244 | # Run the code if RUN_LLM_CODE is set |
| 245 | execution_result <- run_code_if_enabled(generated_code, "corpus query") |
| 246 | if (!is.na(execution_result)) { |
| 247 | expect_true(execution_result, "Generated code should execute without runtime errors") |
| 248 | } |
Marc Kupietz | 0614370 | 2025-07-05 17:49:31 +0200 | [diff] [blame] | 249 | }) |