Class: LLM::Schema
- Inherits:
-
Object
- Object
- LLM::Schema
- Defined in:
- lib/llm/schema.rb
Class Method Summary collapse
Instance Method Summary collapse
- #gemini_response_format ⇒ Object
-
#initialize(name, schema) ⇒ Schema
constructor
A new instance of Schema.
- #response_format ⇒ Object
- #transform_schema(schema) ⇒ Object
Constructor Details
#initialize(name, schema) ⇒ Schema
Returns a new instance of Schema.
3 4 5 6 |
# File 'lib/llm/schema.rb', line 3 def initialize(name, schema) @name = name @schema = schema end |
Class Method Details
.from_file(file_path) ⇒ Object
8 9 10 |
# File 'lib/llm/schema.rb', line 8 def self.from_file(file_path) new(File.basename(file_path, ".json"), JSON.parse(File.read(file_path))) end |
Instance Method Details
#gemini_response_format ⇒ Object
23 24 25 |
# File 'lib/llm/schema.rb', line 23 def gemini_response_format transform_schema(@schema) end |
#response_format ⇒ Object
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/llm/schema.rb', line 12 def response_format { type: "json_schema", json_schema: { name: @name, strict: true, schema: @schema } } end |
#transform_schema(schema) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/llm/schema.rb', line 27 def transform_schema(schema) # Initialize the result as an empty hash. openapi_schema = {} # Process the "type" field and handle nullability. if schema.key?("type") if schema["type"].is_a?(Array) # Check for "null" in the type array to mark the schema as nullable. if schema["type"].include?("null") openapi_schema["nullable"] = true # Remove "null" from the type array; if a single type remains, use that. remaining_types = schema["type"] - ["null"] openapi_schema["type"] = (remaining_types.size == 1) ? remaining_types.first : remaining_types else openapi_schema["type"] = schema["type"] end else openapi_schema["type"] = schema["type"] end end # Map simple fields directly: "format", "description", "enum", "maxItems", "minItems". ["format", "description", "enum", "maxItems", "minItems"].each do |field| openapi_schema[field] = schema[field] if schema.key?(field) end # Recursively process "properties" if present. if schema.key?("properties") && schema["properties"].is_a?(Hash) openapi_schema["properties"] = {} schema["properties"].each do |prop, prop_schema| openapi_schema["properties"][prop] = transform_schema(prop_schema) end end # Copy "required" if present. openapi_schema["required"] = schema["required"] if schema.key?("required") # Copy "propertyOrdering" if present (non-standard field). openapi_schema["propertyOrdering"] = schema["propertyOrdering"] if schema.key?("propertyOrdering") # Recursively process "items" for array types. if schema.key?("items") openapi_schema["items"] = transform_schema(schema["items"]) end openapi_schema end |