irudnyts / openai

An R package-wrapper around OpenAI API
https://irudnyts.github.io/openai/
Other
164 stars 28 forks source link

Include messages argument as part of output object? #38

Open sda030 opened 1 year ago

sda030 commented 1 year ago

Hi, great package. For the sake of reproducibility and for ease of continuing a chat, could the create_chat_completion() returned result object include the inputted settings (in particular the messages argument)?

irudnyts commented 1 year ago

Hi @sda030, thanks for this. In principle, I can do it. However, I tried to be as consistent with the official Python package as possible. If I will return other objects, then it won't be inline.

I suggest keeping messages in a stand-alone variable:

library(openai)

messages <- list(
    list(
        "role" = "system",
        "content" = "You are a helpful assistant."
    ),
    list(
        "role" = "user",
        "content" = "Who won the world series in 2020?"
    ),
    list(
        "role" = "assistant",
        "content" = "The Los Angeles Dodgers won the World Series in 2020."
    ),
    list(
        "role" = "user",
        "content" = "Where was it played?"
    )
)

response <- create_chat_completion(
    model = "gpt-3.5-turbo",
    messages = messages
)

Or even keeping it in one list:

library(openai)

chat <- list()

chat[["input_messages"]] <- list(
    list(
        "role" = "system",
        "content" = "You are a helpful assistant."
    ),
    list(
        "role" = "user",
        "content" = "Who won the world series in 2020?"
    ),
    list(
        "role" = "assistant",
        "content" = "The Los Angeles Dodgers won the World Series in 2020."
    ),
    list(
        "role" = "user",
        "content" = "Where was it played?"
    )
)

chat[["reply"]] <- create_chat_completion(
    model = "gpt-3.5-turbo",
    messages = chat[["input_messages"]]
)

Please let me know if this helps.