2023年3月、OpenAI社が公開したGPT4のLLM(Large Language Models、大規模言語モデル)モデルの登場以降、プログラミングの世界はこれまでにないスピードで進化し、劇的な変化を遂げています。現世の私たちは、日々のニュースに感嘆し、新たなインスピレーションを受けています。まるで、スーパーマリオがスター状態になっているかのように、走馬灯が現実となり、日々が新たな発見で満たされています。これは、過去10年間で飛躍的に進化を続けてきた、データサイエンス、深層学習・AIのムーブメントが一旦の終着点を迎えたようにも感じます。
#' Generate text using OpenAI's API
#'#' This function sends a request to OpenAI's API to generate text based on the provided content and parameters.#'#' @title chat4R#' @description Generate text using OpenAI's API#' @param content The initial text that the model responds to.#' @param api_key The API key for OpenAI. Defaults to the value of the environment variable "OPENAI_API_KEY".#' @param Model The ID of the model to use, such as "gpt-3.5-turbo-16k".#' @param temperature Controls the randomness of the generated text. A value close to 0 produces more deterministic output, while a higher value (up to 2) produces more random output.#' @param simple If TRUE, returns the generated text without newline characters. If FALSE, returns the full response from the API.#' @param fromJSON_parsed If TRUE, parses the raw content of the response into a list. If FALSE, returns the parsed content of the response as a data frame.#' @importFrom httr POST add_headers content#' @importFrom jsonlite toJSON fromJSON#' @return The generated text or the full response from the API, depending on the value of `simple` and `fromJSON_parsed`.#' @export#' @author Satoshi Kume#' @examples#' \dontrun{#' Sys.setenv(OPENAI_API_KEY = "Your API key")#'#' content <- "Translate the following English text to French: '{}'"#'#' chat4R(content, api_key)#' }
chat4R <-function(content,
api_key =Sys.getenv("OPENAI_API_KEY"),
Model ="gpt-3.5-turbo-16k",
temperature =1,
simple=TRUE,
fromJSON_parsed=FALSE){# Define parameters# For more details, refer to: https://platform.openai.com/docs/guides/chat
api_url <-"https://api.openai.com/v1/chat/completions"
n <-1
top_p <-1# Configure headers for the API request
headers <- httr::add_headers(`Content-Type` ="application/json",
`Authorization` =paste("Bearer", api_key))# Define the body of the API request
body <-list(model = Model,
messages =list(list(role ="user", content = content)),
temperature = temperature, top_p = top_p, n = n)# Send a POST request to the OpenAI server
response <- httr::POST(url = api_url,
body = jsonlite::toJSON(body, auto_unbox =TRUE),
encode ="json", config = headers)# Extract and return the response contentif(simple){return(data.frame(httr::content(response,"parsed"))$choices.message.content)}else{if(fromJSON_parsed){
raw_content <- httr::content(response,"raw")
char_content <-rawToChar(raw_content)
parsed_data <- jsonlite::fromJSON(char_content)return(parsed_data)}else{return(data.frame(httr::content(response,"parsed")))}}}
R関数のダイアグラムを作成する
R関数の実行プロセスを可視化するために、GPT4のプラグインの1つである、Show Me Diagramsというツールを使用します。
このツールは、Mermaid、PlantUML、D2などの図表言語を使用して、チャット上でダイアグラムを作成することができます。
ChatGPTのプラグイン設定
ChatGPTは、プラグインを使用して機能を拡張することができます。
Show Me Diagramsというプラグインは、図表言語を使用してダイアグラムを作成できます。
今回は、R関数の実行プロセスを視覚的に表現するのに使用しますが、それだけの用途に止まりません。
使い方は、GPT4を選択して、Show Me Diagramsのプラグイン機能をONにするだけです。
Please use Show Me Diagrams to create a diagram that represents the execution process of R functions, and make sure that the prompt used in the diagram is well thought out.
To create sequence diagrams, please use a diagramming language such as Mermaid or PlantUML.
Your answer should be detailed and comprehensive.
The R functions are as follows.
日本語版プロンプト
Show Me Diagramsを使用してR関数の実行プロセスを表す図を作成し、その中で使用するプロンプトをよく考えて作成してください。
シーケンス図を作成するには、Mermaid や PlantUML などの図解言語を使用してください。
回答は詳細かつ包括的でなければなりません。 R 関数は以下の通りです。
function: roxygen2
author: Your name
Language: English
You are an experienced and knowledgeable R programmer who is being asked to provide a comprehensive definition of a specific R language function.
Your definition should be in the {function} format, and should include specific elements such as @title, @description, @param, @importFrom, @return, @export, @author {author}, and @examples.
It is important that you ensure that the function name you define is also defined in @export.
Additionally, you should translate any comments in the function (starting with #) into English, ensuring that the content of the comments is consistent and correct throughout the function.
If you notice any mistakes or areas for improvement in the R script, you should correct them according to these instructions.
If there is a risk of errors occurring during text generation, you should make any necessary modifications to prevent these errors.
Once you have completed the definition, please provide it as the final product.
Do not omit the output of the code.
英語版プロンプト2
function: roxygen2
author: Your name
Language: English
As an experienced and knowledgeable R programmer, you have been tasked with providing a comprehensive definition of a specific R language function.
Your definition should follow the {function} format and must include specific elements such as @title, @description, @param, @importFrom, @return, @export, @author {author}, and @examples.
It is crucial that you ensure that the function name you define is also defined in @export.
Moreover, you must translate any comments in the function (starting with #) into English, and ensure that the content of the comments is consistent and accurate throughout the function.
If you encounter any errors or areas for improvement in the R script, you must correct them following these instructions.
In case there is a risk of errors occurring during text generation, you should make the necessary modifications to prevent these errors.
Once you have completed the definition, please submit it as the final product.
Do not omit the output of the code.