Skip to content

Commit

Permalink
Add S()/System() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic committed Apr 20, 2024
1 parent 847868e commit c2ddcd2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 24 deletions.
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,24 @@ Or you can use this convenience RubyGem with some nice helpers to get you starte

include Groq::Helpers
@client.chat([
U("Hi"),
A("Hello back. Ask me anything. I'll reply with 'cat'"),
U("Favourite food?")
User("Hi"),
Assistant("Hello back. Ask me anything. I'll reply with 'cat'"),
User("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"}

@client.chat([
System("I am an obedient AI"),
U("Hi"),
A("Hello back. Ask me anything. I'll reply with 'cat'"),
U("Favourite food?")
])
# => {"role"=>"assistant", "content"=>"Cat"}
# => {"role"=>"assistant", "content"=>"cat"}
# => {"role"=>"assistant", "content"=>"Cat"}
```

## Installation
Expand Down Expand Up @@ -98,18 +108,16 @@ The remaining examples below will use `@client` variable to allow you to copy+pa

### Message helpers

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

```ruby
include Groq::Helpers
@client.chat([
U("Hi"),
A("Hello back. Ask me anything. I'll reply with 'cat'"),
U("Favourite food?")
S("I am an obedient AI"),
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"}
```

Expand All @@ -124,6 +132,7 @@ There are also aliases for each helper function:

* `U(content)` is also `User(content)`
* `A(content)` is also `Assistant(content)`
* `S(content)` is also `System(content)`
* `T(content, ...)` is also `Tool`, `ToolReply`, `Function`, `F`

### Specifying an LLM model
Expand Down
1 change: 1 addition & 0 deletions lib/groq/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def initialize(config = {}, &faraday_middleware)
@faraday_middleware = faraday_middleware
end

# TODO: support stream: true; or &stream block
def chat(messages, model_id: nil, tools: nil)
unless messages.is_a?(Array) || messages.is_a?(String)
raise ArgumentError, "require messages to be an Array or String"
Expand Down
5 changes: 5 additions & 0 deletions lib/groq/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def A(content)
end
alias_method :Assistant, :A

def S(content)
{role: "system", content: content}
end
alias_method :System, :S

def T(content, tool_call_id:, name:)
{role: "function", tool_call_id: tool_call_id, name: name, content: content}
end
Expand Down
28 changes: 14 additions & 14 deletions test/fixtures/vcr_cassettes/llama3-8b-8192/chat_messages.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/groq/test_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def test_chat_messages_with_U_A_helpers
client = Groq::Client.new
# and with U/A helper methods
response = client.chat([
S("I am an obedient AI."),
U("What's the next day after Wednesday?"),
A("The next day after Wednesday is Thursday."),
U("What's the next day after that?")
Expand Down

0 comments on commit c2ddcd2

Please sign in to comment.