Class: Langchain::OutputParsers::StructuredOutputParser
- Defined in:
- lib/langchain/output_parsers/structured_output_parser.rb
Overview
Structured Output Parser
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
Class Method Summary collapse
-
.from_json_schema(schema) ⇒ Object
Creates a new instance of the class using the given JSON::Schema.
Instance Method Summary collapse
-
#get_format_instructions ⇒ String
Returns a string containing instructions for how the output of a language model should be formatted according to the @schema.
-
#initialize(schema:) ⇒ StructuredOutputParser
constructor
Initializes a new instance of the class.
-
#parse(text) ⇒ Object
Parse the output of an LLM call extracting an object that abides by the @schema.
- #to_h ⇒ Object
Constructor Details
#initialize(schema:) ⇒ StructuredOutputParser
Initializes a new instance of the class.
13 14 15 |
# File 'lib/langchain/output_parsers/structured_output_parser.rb', line 13 def initialize(schema:) @schema = validate_schema!(schema) end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
8 9 10 |
# File 'lib/langchain/output_parsers/structured_output_parser.rb', line 8 def schema @schema end |
Class Method Details
.from_json_schema(schema) ⇒ Object
Creates a new instance of the class using the given JSON::Schema.
29 30 31 |
# File 'lib/langchain/output_parsers/structured_output_parser.rb', line 29 def self.from_json_schema(schema) new(schema: schema) end |
Instance Method Details
#get_format_instructions ⇒ String
Returns a string containing instructions for how the output of a language model should be formatted according to the @schema.
according to the @schema.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/langchain/output_parsers/structured_output_parser.rb', line 38 def get_format_instructions <<~INSTRUCTIONS You must format your output as a JSON value that adheres to a given "JSON Schema" instance. "JSON Schema" is a declarative language that allows you to annotate and validate JSON documents. For example, the example "JSON Schema" instance {"properties": {"foo": {"description": "a list of test words", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}} would match an object with one required property, "foo". The "type" property specifies "foo" must be an "array", and the "description" property semantically describes it as "a list of test words". The items within "foo" must be strings. Thus, the object {"foo": ["bar", "baz"]} is a well-formatted instance of this example "JSON Schema". The object {"properties": {"foo": ["bar", "baz"]}}} is not well-formatted. Your output will be parsed and type-checked according to the provided schema instance, so make sure all fields in your output match the schema exactly and there are no trailing commas! Here is the JSON Schema instance your output must adhere to. Include the enclosing markdown codeblock: ```json #{schema.to_json} ``` INSTRUCTIONS end |
#parse(text) ⇒ Object
Parse the output of an LLM call extracting an object that abides by the @schema
61 62 63 64 65 66 67 68 |
# File 'lib/langchain/output_parsers/structured_output_parser.rb', line 61 def parse(text) json = text.include?("```") ? text.strip.split(/```(?:json)?/)[1] : text.strip parsed = JSON.parse(json) JSON::Validator.validate!(schema, parsed) parsed rescue => e raise OutputParserException.new("Failed to parse. Text: \"#{text}\". Error: #{e}", text) end |
#to_h ⇒ Object
17 18 19 20 21 22 |
# File 'lib/langchain/output_parsers/structured_output_parser.rb', line 17 def to_h { _type: "StructuredOutputParser", schema: schema.to_json } end |