This function creates a JSON schema suitable for use with the API functions in tidyllm.
Arguments
- name
A character vector specifying the schema name. This serves as an identifier for the schema.
- ...
Named arguments where each name represents a field in the schema and each value specifies the type. Supported types include R data types:
"character": Represents a charcater type
"string": Allowed shorthand for charachter type
"factor(...)": A string with specific allowable values, represented as enum in JSON. Specify options as factor(option1, option2).
"logical": Represents a boolean.
"numeric": Represents a number.
"type[]": Appending [] allows for vector of a given type, e.g., "character[]".
Value
A list representing the JSON schema with the specified fields and types, suitable for passing to openai()'s .json_schema parameter.
Details
The tidyllm_schema() function is designed to make defining JSON schemas for tidyllm more concise and user-friendly. It maps R-like types to JSON schema types and validates inputs to enforce tidy data principles. Nested structures are not allowed to maintain compatibility with tidy data conventions.
Note
Factor types (factor(...)) are treated as enumerations in JSON and are limited to a set of allowable string values. Arrays of a given type can be specified by appending [] to the type.
Examples
if (FALSE) { # \dontrun{
# Define a schema with tidy data principles
json_schema <- tidyllm_schema(
name = "DocumentAnalysisSchema",
Title = "character",
Authors = "character[]",
SuggestedFilename = "character",
Type = "factor(Policy, Research)",
Answer_Q1 = "character",
Answer_Q2 = "character",
Answer_Q3 = "character",
Answer_Q4 = "character",
KeyCitations = "character[]"
)
# Pass the schema to openai()
result <- openai(
.llm = msg,
.json_schema = json_schema
)
} # }