Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for json mode #54

Open
LukasWallrich opened this issue Dec 15, 2023 · 1 comment
Open

Allow for json mode #54

LukasWallrich opened this issue Dec 15, 2023 · 1 comment

Comments

@LukasWallrich
Copy link

It would be great to use JSON mode, yet even with the development version, I don't see how to pass that parameter. Could that maybe be added?

https://platform.openai.com/docs/guides/text-generation/json-mode

@irudnyts
Copy link
Owner

irudnyts commented Jan 9, 2024

@LukasWallrich, this feature was actually in the development version. But there are a couple of pitfalls that require care. Please see an example below with commented points:

remotes::install_github("irudnyts/openai", ref = "r6")
library(openai)

client <- OpenAI()
completion <- client$chat$completions$create(
    model = "gpt-3.5-turbo-1106", # ☝️ must be either "gpt-4-1106-preview" or "gpt-3.5-turbo-1106"
    messages = list(list("role" = "user", "content" = "What's up? Answer in JSON.")), # ☝️ You must include something like the answer should be in JSON
    response_format = list("type" = "json_object")
)

completion$choices[[1]]$message$content

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants