Skip to content

Commit c5521ae

Browse files
committed
Store chatgpt refine prompts into conf file
1 parent bc7022e commit c5521ae

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

R/chatgpt-wraps.R

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,33 @@
11
#' Summarise document using LangChain and ChatGPT
22
#'
3-
#' This function is a R wrapper of `chatgpt_chaindoc.py`, a python function aimed
4-
#' to summarise large text documents using LangChain and ChatGPT.
3+
#' This function is a R wrapper of `chatgpt_chaindoc.py`, a python script function
4+
#' aimed to elaborate large text documents using LangChain and ChatGPT.
5+
#' `chatgpt_chaindoc.py` uses the refine chain method to elaborate the text
6+
#' (see \url{https://langchain.readthedocs.io/en/latest/modules/indexes/chain_examples/summarize.html#the-refine-chain} for details).
57
#'
68
#' @param document_path The path of the txt document to summarise.
79
#' @param openaikey The OpenAI key token.
810
#' @param engine ChatGPT model (see \url{https://platform.openai.com/docs/models}).
9-
#' @param temperature Level of randomness or "creativity" in the generated text (see \url{https://platform.openai.com/docs/api-reference/completions/create})
11+
#' @param temperature Level of randomness or "creativity" in the generated text (see \url{https://platform.openai.com/docs/api-reference/completions/create}).
12+
#' @param refine_text A text useful to refine the original prompt (see \url{https://langchain.readthedocs.io/en/latest/modules/indexes/chain_examples/summarize.html#the-refine-chain} for details).
1013
#'
1114
#' @return A summarised text.
1215
#' @export
13-
#'
14-
15-
summarise_chatgpt_wrapper <- function(document_path, openaikey, engine, temperature) {
16+
#' @examples
17+
#' \dontrun{
18+
#' output <-
19+
#' summarise_chatgpt_wrapper(
20+
#' document_path = "inst/docs_dataverse/5636634.txt",
21+
#' openaikey = OPENAI_KEY,
22+
#' engine = "gpt-3.5-turbo",
23+
#' temperature = 0.7,
24+
#' refine_text = pars$openai$refine_prompts$p1
25+
#' )
26+
#' cat(output$output_text)
27+
#' }
28+
summarise_chatgpt_wrapper <- function(document_path, openaikey, engine, temperature, refine_text) {
1629
chatgpt_chain_py <- reticulate::import("chatgpt_chaindoc")
1730
py_function <- chatgpt_chain_py$summarize_document
18-
result <- py_function(document_path, openaikey, engine, temperature)
31+
result <- py_function(document_path, openaikey, engine, temperature, refine_text)
1932
return(result)
2033
}

inst/conf.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
default:
2+
openai:
3+
token: !expr Sys.getenv('OPENAI_KEY')
4+
refine_prompts:
5+
p1: |
6+
"Your job is to produce a final, extensive sucess-story in the context of food systems, malnutrition and social development\n\n"
7+
"We have provided an existing summary up to a certain point: {existing_answer}\n"
8+
"We have the opportunity to refine the existing summary to a final story"
9+
"(only if needed) with some more context below.\n"
10+
"------------\n"
11+
"{text}\n"
12+
"------------\n"
13+
"Given the new context, refine the original summary in a final and extensive sucess-story highlighting the most relevant results and providing a concise story' title"
14+
"Also, only if relevant, provide a concise table (or tables) summarising the most important findings"
15+
"If the context isn't useful, return the original extensive summary."
16+
dataverse:
17+
token: !expr Sys.getenv('DATAVERSE_KEY')
18+
19+
local:
20+
openai:
21+
token: !expr readLines("auth_aquadata/openai-key")
22+
dataverse:
23+
token: !expr readLines('auth_aquadata/dataverse-token')

man/summarise_chatgpt_wrapper.Rd

Lines changed: 27 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)