---
title: "Using ChatGPT in R to analyse the methodology in journal articles"
author: "Burnet Bioinformatics group"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: true
    toc_float: true
    fig_width: 13
    fig_height: 9
    code_folding: show
theme: cosmo
---

## Introduction

There is a major problem with the state of methodology of enrichment analysis with many articles lacking
details about what was done, undermining the validitiy of the scientific results.
Here we want to show how to use AI APIs to examine the methods in articles.
This approach can be used to evaluate articles in the review phase.

We are using ChatGPT nano for this.

```{r,setup}

library(LLMR)

# get API key - keep it secret
OPENAI_API_KEY <- readLines("openai.key")[1]

```

## First test

In the first test, we just ask a simple question.

```{r,test1}

# Create a configuration with more parameters
openai_cfg <- llm_config(
  provider = "openai",
  model = "gpt-4.1-nano",
  api_key = OPENAI_API_KEY,
  temperature = .5,
  max_tokens = 250
)

# Ask a simple question
resp <- call_llm( openai_cfg, c(
    system = "You are an expert data scientist. You always respond in terse
    bullet lists.",
    user = "When will you ever use OLS?"
  ),
  json = TRUE
)

cat("GPT-4o-mini says:\n", resp, "\n")

```

## Extract information from an article

In this next example, we will load a text file and then ask ChatGPT about it.
This allows us to run a checklist against articles.

```{r,textanalysis1}

# extract text
text_file_path = "PMC6350986.txt"
text_content <- readLines(text_file_path)
text_content <- paste(text_content, collapse = "\n")

# Prepare your question
question <-  "In the attached article, extract the following informaton: (1) The bioinformatics
    tools used for pathway enrichment analysis. (2) Whether the version number was provided.
    (3) Whether p-value correction was used in functional enrichment analysis. (4) If
    over-representation analysis was used, whether a background gene list was used."

# Create the prompt combining the text and the question
prompt <- paste(
  "Here is a text:\n",
  text_content,
  "\nQuestion: ",
  question,
  "\nAnswer:"
)

response <- call_llm(config = openai_cfg , prompt, verbose = TRUE)

cat("LLM output:\n",response, "\n")

```

As you can see, ChatGPT has provided a response.
On first glance it appears accurate, but we'll need to confirm carefully.

## Conclusion

We managed to get ChatGPT API calls working in R including the examination of journal articles in text format.
This opens the door to other applications such as checklisting previous literature and helping journals to
identify articles lacking methodological information.

## Session

```{r,session}

sessionInfo()

```
