Structured outputs for LLMs
instructor-rb
Structured extraction in Ruby, powered by llms, designed for simplicity, transparency, and control.
Instructor-rb is a Ruby library that makes it a breeze to work with structured outputs from large language models (LLMs). Built on top of EasyTalk, it provides a simple, transparent, and user-friendly API to manage validation, retries, and streaming responses. Get ready to supercharge your LLM workflows!
Getting Started
1. Install Instructor-rb at the command prompt if you haven't yet:
$ gem install instructor-rb
2. In your Ruby project, require the gem:
require 'instructor'
3. At the beginning of your script, initialize and patch the client:
For the OpenAI client:
client = Instructor.from_openai(OpenAI::Client)
For the Anthropic client:
client = Instructor.from_anthropic(Anthropic::Client)
Usage
export your API key:
export OPENAIAPIKEY=sk-...
or for Anthropic:
export ANTHROPICAPIKEY=sk-...
Then use Instructor by defining your schema in Ruby using the defineschema block and EasyTalk's schema definition syntax. Here's an example in:
require 'instructor'
class UserDetail include EasyTalk::Model
define_schema do property :name, String property :age, Integer end end
client = Instructor.from_openai(OpenAI::Client).new
user = client.chat( parameters: { model: 'gpt-3.5-turbo', messages: [{ role: 'user', content: 'Extract Jason is 25 years old' }] }, response_model: UserDetail )
user.name
=> "Jason"
user.age => 25
โน๏ธ Tip: Support in other languages
Check out ports to other languages below:
- Python - TS/JS - Ruby - Elixir
If you want to port Instructor to another language, please reach out to us on Twitter we'd love to help you get started!
Why use Instructor?
- OpenAI Integration โ Integrates seamlessly with OpenAI's API, facilitating efficient data management and manipulation.
- Customizable โ It offers significant flexibility. Users can tailor validation processes and define unique error messages.
- Tested and Trusted โ Its reliability is proven by extensive real-world application.
Contributing
If you want to help out, checkout some of the issues marked as good-first-issue or help-wanted. Found here. They could be anything from code improvements, a guest blog post, or a new cook book.
Checkout the [contribution guide]() for details on how to set things up, testing, changesets and guidelines.
License
This project is licensed under the terms of the MIT License.
TODO
- [ ] Add patch
- [ ] Add response_model
- [ ] Support async
- [ ] Support stream=True, Partial[T] and iterable[T]
- [ ] Support Streaming
- [ ] Optional/Maybe types
- [ ] Add Tutorials, include in docs
- [ ] Logging for Distillation / Finetuning
- [ ] Add
llm_validator