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

Langchain::LLM::Base#chat() accepts a new response_schema: parameter to force the response to adhere to JSON schema #593

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

andreibondarev
Copy link
Collaborator

@andreibondarev andreibondarev commented Apr 26, 2024

This PR introduces a new parameter response_schema: to the LLM chat() methods that, using Function Calling, forces the response to adhere to a specific schema. Example usage:

json_schema = {
  type: "object",
  properties: {
    name: {
      type: "string",
      description: "Persons name"
    },
    age: {
      type: "number",
      description: "Persons age"
    },
    interests: {
      type: "array",
      items: {
        type: "object",
        properties: {
          interest: {
            type: "string",
            description: "A topic of interest"
          },
          levelOfInterest: {
            type: "number",
            description: "A value between 0 and 100 of how interested the person is in this interest"
          }
        },
        required: ["interest", "levelOfInterest"],
        additionalProperties: false
      },
      minItems: 1,
      maxItems: 3,
      description: "A list of the person's interests"
    }
  },
  required: ["name", "age", "interests"],
  additionalProperties: false
}

llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])

response = llm.chat messages: [{ role: :user, content: "Extract Jason is 25 years, and likes to play soccer"}], response_schema: json_schema

response.response_schema
#=> {"name"=>"Jason", "age"=>25, "interests"=>[{"interest"=>"soccer", "levelOfInterest"=>80}]}

@andreibondarev andreibondarev linked an issue Apr 26, 2024 that may be closed by this pull request
@andreibondarev andreibondarev changed the title Langchain::LLM::Base#chat() accepts a new response_schema: parameter to force the response to adhere to JSON schema Langchain::LLM::Base#chat() accepts a new response_schema: parameter to force the response to adhere to JSON schema Apr 26, 2024
@sergiobayona
Copy link

This is good and it is a move in the right direction but there are some key points that are missing:

  1. Generating the json_schema only is not enough. The JSON Schema specification provides validation vocabulary. For example format, min_length, enum, default, etc. Check out https://json-schema.org/draft/2020-12/json-schema-validation. EasyTalk does this with a validate() method. Ensuring that the returned data validates correctly against the schema is probably the most important thing the Instructor library does.
  2. Retry logic. If the returned data is invalid, it does a retry to indicate to the LLM that the payload was invalid and coerce it to correct return the right data.
  3. Support for multiple objects. This is basically "parallel function calling". We were talking about this yesterday. If you run a function call asking "What is the weather in Boston, Los Angeles and Miami?" the LLM will return 3 items (objects) in the payload. The code does not account for that scenario.

There are other more advanced features like streaming and threading or Ruby fibers so that you can process calls in parallel (different from parallel function calling) etc that I want to add Instructor-rb. Those scaling features are the money makers and the reason why Instructor has been successful.

# @return [Hash] JSON schema structured response
def response_schema
if tool_calls
JSON.parse(tool_calls.first.dig("function", "arguments"))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you have a module called Langchain::LLM::OpenAIResponse you should use it here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when the LLM sends back an array of objects?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't work with parallel function-calling right now if that's what you're asking.

@andreibondarev
Copy link
Collaborator Author

This is good and it is a move in the right direction but there are some key points that are missing:

  1. Generating the json_schema only is not enough. The JSON Schema specification provides validation vocabulary. For example format, min_length, enum, default, etc. Check out https://json-schema.org/draft/2020-12/json-schema-validation. EasyTalk does this with a validate() method. Ensuring that the returned data validates correctly against the schema is probably the most important thing the Instructor library does.
  2. Retry logic. If the returned data is invalid, it does a retry to indicate to the LLM that the payload was invalid and coerce it to correct return the right data.
  3. Support for multiple objects. This is basically "parallel function calling". We were talking about this yesterday. If you run a function call asking "What is the weather in Boston, Los Angeles and Miami?" the LLM will return 3 items (objects) in the payload. The code does not account for that scenario.

There are other more advanced features like streaming and threading or Ruby fibers so that you can process calls in parallel (different from parallel function calling) etc that I want to add Instructor-rb. Those scaling features are the money makers and the reason why Instructor has been successful.

  1. Using EasyTalk, can I do something like UserDetail.new(raw_json).validate()?
  2. Does instructor always retry or can you "do it once, fail and raise an error"?
  3. Correct, parallel function calling is not supported yet.

@palladius
Copy link
Contributor

QQ. Yesterday I learnt at RubyDay of a recent Data class (since 3.2) for immutable hashes (https://www.shakacode.com/blog/ruby-3-2-adds-a-new-data-class/). This seems like a perfect fit - not much for the json_schema but to force the result to fit into a Data which you might construct based on the json_schema.
Just thinking out loud here - would it make sense? Feel free to shoot me down if I said sth stupid - which is likely.

[ I say this because I'm using it to accept different Gemini responses (good vs error response) by forcing it into two wlel-known, different schema. ]

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

Successfully merging this pull request may close these issues.

Implement instructor-style JSON validation
3 participants