Skip to content

Commit

Permalink
Merge pull request #1 from sergiobayona/dry-rb-approach
Browse files Browse the repository at this point in the history
Dry rb approach
  • Loading branch information
sergiobayona authored Jan 25, 2024
2 parents 6585019 + c9053cb commit f406f92
Show file tree
Hide file tree
Showing 14 changed files with 153 additions and 510 deletions.
40 changes: 36 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,58 @@ PATH
remote: .
specs:
instructor-rb (0.1.0)
activemodel (~> 6.1)
activesupport (~> 6.1)
activesupport (~> 6.1.3)
dry-validation (~> 1.10)
ruby-openai (~> 0.1.0)

GEM
remote: https://rubygems.org/
specs:
activemodel (6.1.7.6)
activesupport (= 6.1.7.6)
activesupport (6.1.7.6)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
ast (2.4.2)
bigdecimal (3.1.6)
coderay (1.1.3)
concurrent-ruby (1.2.3)
diff-lcs (1.5.0)
dotenv (2.7.6)
dry-configurable (1.1.0)
dry-core (~> 1.0, < 2)
zeitwerk (~> 2.6)
dry-core (1.0.1)
concurrent-ruby (~> 1.0)
zeitwerk (~> 2.6)
dry-inflector (1.0.0)
dry-initializer (3.1.1)
dry-logic (1.5.0)
concurrent-ruby (~> 1.0)
dry-core (~> 1.0, < 2)
zeitwerk (~> 2.6)
dry-schema (1.13.3)
concurrent-ruby (~> 1.0)
dry-configurable (~> 1.0, >= 1.0.1)
dry-core (~> 1.0, < 2)
dry-initializer (~> 3.0)
dry-logic (>= 1.4, < 2)
dry-types (>= 1.7, < 2)
zeitwerk (~> 2.6)
dry-types (1.7.2)
bigdecimal (~> 3.0)
concurrent-ruby (~> 1.0)
dry-core (~> 1.0)
dry-inflector (~> 1.0)
dry-logic (~> 1.4)
zeitwerk (~> 2.6)
dry-validation (1.10.0)
concurrent-ruby (~> 1.0)
dry-core (~> 1.0, < 2)
dry-initializer (~> 3.0)
dry-schema (>= 1.12, < 2)
zeitwerk (~> 2.6)
httparty (0.18.1)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
Expand Down
53 changes: 22 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,28 @@ Dive into the world of Ruby-based structured extraction, by OpenAI's function ca

## Usage

```rb
# Todo, change to ruby
import Instructor from "@instructor-ai/instructor";
import OpenAI from "openai"
import { z } from "zod"

const UserSchema = z.object({
age: z.number(),
name: z.string()
})

type User = z.infer<typeof UserSchema>

const oai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY ?? undefined,
organization: process.env.OPENAI_ORG_ID ?? undefined
})

const client = Instructor({
client: oai,
mode: "FUNCTIONS" # or "TOOLS" or "MD_JSON" or "JSON"
})

const user = await client.chat.completions.create({
messages: [{ role: "user", content: "Jason Liu is 30 years old" }],
model: "gpt-3.5-turbo",
response_model: { schema: UserSchema }
})

console.log(user)
// { age: 30, name: "Jason Liu" }
```ruby
require 'instructor'

class UserDetail < Instructor::Model
params do
required(:name).filled(:string)
required(:age).filled(:integer)
end
end

client = Instructor::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
)

puts(user.inspect)
#=> #<Dry::Validation::Result{:name=>"Jason", :age=>25} errors={}>
```

## Why use Instructor?
Expand Down
32 changes: 16 additions & 16 deletions instructor-rb.gemspec
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# frozen_string_literal: true

require_relative "lib/instructor/version"
require_relative 'lib/instructor/version'

Gem::Specification.new do |spec|
spec.name = "instructor-rb"
spec.name = 'instructor-rb'
spec.version = Instructor::VERSION
spec.authors = ["Jason Liu", "Sergio Bayona"]
spec.email = ["[email protected]", "[email protected]"]
spec.authors = ['Jason Liu', 'Sergio Bayona']
spec.email = ['[email protected]', '[email protected]']

spec.summary = "Structured extraction in Ruby, powered by llms."
spec.summary = 'Structured extraction in Ruby, powered by llms.'
spec.description = "Explore the power of structured extraction in Ruby with the Instructor gem. Leveraging OpenAI's function calling API."
spec.homepage = "https://github.com/instructor-ai/instructor-rb"
spec.license = "MIT"
spec.required_ruby_version = ">= 2.7.0"
spec.homepage = 'https://github.com/instructor-ai/instructor-rb'
spec.license = 'MIT'
spec.required_ruby_version = '>= 2.7.0'

spec.metadata["allowed_push_host"] = "https://rubygems.pkg.github.com/instructor-ai"
spec.metadata['allowed_push_host'] = 'https://rubygems.pkg.github.com/instructor-ai'

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "https://github.com/instructor-ai/instructor-rb"
spec.metadata["changelog_uri"] = "https://github.com/instructor-ai/instructor-rb/blob/main/CHANGELOG.md"
spec.metadata['homepage_uri'] = spec.homepage
spec.metadata['source_code_uri'] = 'https://github.com/instructor-ai/instructor-rb'
spec.metadata['changelog_uri'] = 'https://github.com/instructor-ai/instructor-rb/blob/main/CHANGELOG.md'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
Expand All @@ -29,9 +29,9 @@ Gem::Specification.new do |spec|
end
end

spec.require_paths = ["lib"]
spec.require_paths = ['lib']

spec.add_dependency "activemodel", "~> 6.1"
spec.add_dependency "activesupport", "~> 6.1"
spec.add_dependency "ruby-openai", "~> 0.1.0"
spec.add_dependency 'activesupport', '~> 6.1.3'
spec.add_dependency 'dry-validation', '~> 1.10'
spec.add_dependency 'ruby-openai', '~> 0.1.0'
end
14 changes: 5 additions & 9 deletions lib/instructor.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# frozen_string_literal: true

require "active_model"
require "active_support"
require_relative "instructor/version"
require_relative "instructor/type/array"
require_relative "instructor/model_serializer"
require_relative "instructor/base_model"
require 'openai'
require 'active_support/all'
require_relative 'instructor/version'
require_relative 'instructor/model'
require_relative 'instructor/openai/client'

module Instructor
class Error < StandardError; end

# Register the custom array type with ActiveModel
ActiveModel::Type.register(:array, Instructor::Type::Array)
end
11 changes: 0 additions & 11 deletions lib/instructor/base_model.rb

This file was deleted.

55 changes: 0 additions & 55 deletions lib/instructor/dsl/conditional_require.rb

This file was deleted.

5 changes: 5 additions & 0 deletions lib/instructor/model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'dry-validation'

Dry::Schema.load_extensions(:json_schema)
class Instructor::Model < Dry::Validation::Contract
end
110 changes: 0 additions & 110 deletions lib/instructor/model_serializer.rb

This file was deleted.

Loading

0 comments on commit f406f92

Please sign in to comment.