Skip to content

Commit

Permalink
U() and A() helpers to create messages
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic committed Apr 20, 2024
1 parent 53cbae4 commit c1095a4
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 5 deletions.
20 changes: 20 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,46 @@ PATH
remote: .
specs:
groq (0.1.0)
activesupport (> 5)
faraday (~> 2.0)
json

GEM
remote: https://rubygems.org/
specs:
activesupport (7.1.3.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.7)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crack (1.0.0)
bigdecimal
rexml
drb (2.2.1)
faraday (2.9.0)
faraday-net_http (>= 2.0, < 3.2)
faraday-net_http (3.1.0)
net-http
hashdiff (1.1.0)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
lint_roller (1.1.0)
minitest (5.22.3)
mutex_m (0.2.0)
net-http (0.4.1)
uri
parallel (1.24.0)
Expand Down Expand Up @@ -67,6 +85,8 @@ GEM
rubocop-performance (~> 1.20.2)
standardrb (1.0.1)
standard
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
uri (0.13.0)
vcr (6.2.0)
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ client.chat([
# => {"role" => "assistant", "content" => "The next day after Thursday is Friday."}
```

We also have some handy `U` and `A` methods to produce the `{role:, content:}` hash:

```ruby
include Groq::Helpers
client.chat([
U("Hi"),
A("Hello back. Ask me anything. I'll reply with 'cat'"),
U("Favourite food?")
])
# => {"role"=>"assistant", "content"=>"Um... CAT"}
# => {"role"=>"assistant", "content"=>"Not a cat! It's a pizza!"}
# => {"role"=>"assistant", "content"=>"Pizza"}
# => {"role"=>"assistant", "content"=>"Cat"}
```

## Development

After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
Expand Down
1 change: 1 addition & 0 deletions groq.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "faraday", "~> 2.0"
spec.add_dependency "json"
spec.add_dependency "activesupport", "> 5" # for Concerns

spec.add_development_dependency "vcr", "~> 6.0"
spec.add_development_dependency "webmock", "~> 3.0"
Expand Down
13 changes: 8 additions & 5 deletions lib/groq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ module Groq
autoload :Configuration, "groq/configuration"
autoload :Client, "groq/client"
autoload :Model, "groq/model"
autoload :Helpers, "groq/helpers"

def self.configuration
@configuration ||= Configuration.new
end
class << self
def configuration
@configuration ||= Configuration.new
end

def self.configure
yield configuration
def configure
yield configuration
end
end
end
14 changes: 14 additions & 0 deletions lib/groq/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "active_support/concern"

module Groq::Helpers
extend ActiveSupport::Concern
included do
def U(content)
{role: "user", content: content}
end

def A(content)
{role: "assistant", content: content}
end
end
end
16 changes: 16 additions & 0 deletions test/groq/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,20 @@ def test_chat_messages
}
end
end

include Groq::Helpers
def test_chat_messages_with_U_A_helpers
VCR.use_cassette("llama3-8b-8192/chat_messages") do
client = Groq::Client.new(model_id: "llama3-8b-8192")
# and with U/A helper methods
response = client.chat([
U("What's the next day after Wednesday?"),
A("The next day after Wednesday is Thursday."),
U("What's the next day after that?")
])
assert_equal response, {
"role" => "assistant", "content" => "The next day after Thursday is Friday."
}
end
end
end

0 comments on commit c1095a4

Please sign in to comment.