Skip to content

Commit

Permalink
Add example pizzeria chat
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic committed Apr 21, 2024
1 parent f6fa8c8 commit 9d141fa
Showing 1 changed file with 62 additions and 12 deletions.
74 changes: 62 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,12 @@ JSON.parse(response["content"])

Install the gem and add to the application's Gemfile by executing:

```plain
bundle add groq
> bundle add groq
```

If bundler is not being used to manage dependencies, install the gem by executing:
```plain
gem install groq
> gem install groq
```

## Usage

- Get your API key from [console.groq.com/keys](https://console.groq.com/keys)
Expand Down Expand Up @@ -105,10 +101,8 @@ client.chat([

### Interactive console (IRb)

```plain
bin/console
> bin/console
```

This repository has a `bin/console` script to start an interactive console to play with the Groq API. The `@client` variable is setup using `$GROQ_API_KEY` environment variable; and the `U`, `A`, `T` helpers are already included.
```ruby
Expand Down Expand Up @@ -189,10 +183,8 @@ end

The output might looks similar to:

```plain
User message: Hello, world!
> User message: Hello, world!
Assistant reply with model llama3-8b-8192:
{"role"=>"assistant", "content"=>"Hello, world! It's great to meet you! Is there something I can help you with, or would you like to chat?"}
Assistant reply with model llama3-70b-8192:
{"role"=>"assistant", "content"=>"The classic \"Hello, world!\" It's great to see you here! Is there something I can help you with, or would you like to just chat?"}
Assistant reply with model llama2-70b-4096:
Expand Down Expand Up @@ -309,6 +301,64 @@ end
@client.chat("Hello, world!", max_tokens: 512, temperature: 0.5)
```

## Examples

Talking with a pizzeria.

```ruby
messages = [U("Is this the pizza shop? Do you sell hawaiian?")]

# Build message history, including pizzeria agent's system message.
@agent_message = <<~EOS
You are an employee at a pizza store.
You sell hawaiian, and pepperoni pizzas; in small and large sizes for $10, and $20 respectively.
Pick up only in. Ready in 10 mins. Cash on pickup.
EOS

# Build the `messages` array, including system message up front.
def pizza_messages(messages)
[
System(@agent_message),
*messages
]
end

response = @client.chat(pizza_messages(messages))
puts response["content"]
```

The output might be:

> Yeah! This is the place! Yes, we sell Hawaiian pizzas here! We've got both small and large sizes available for you. The small Hawaiian pizza is $10, and the large one is $20. Plus, because we're all about getting you your pizza fast, our pick-up time is only 10 minutes! So, what can I get for you today? Would you like to order a small or large Hawaiian pizza?
Continue with user's reply.

Note, we build the `messages` array with the previous user and assistant messages and the new user message:

```ruby
messages << response << U("Yep, give me a large.")
response = @client.chat(pizza_messages(messages))
puts response["content"]
```

Response:

> I'll get that ready for you. So, to confirm, you'd like to order a large Hawaiian pizza for $20, and I'll have it ready for you in 10 minutes. When you come to pick it up, please have the cash ready as we're a cash-only transaction. See you in 10!
Making a change:

```ruby
messages << response << U("Actually, make it two smalls.")
response = @client.chat(pizza_messages(messages))
puts response["content"]
```

Response:

> I've got it! Two small Hawaiian pizzas on the way! That'll be $20 for two small pizzas. Same deal, come back in 10 minutes to pick them up, and bring cash for the payment. See you soon!
## 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

0 comments on commit 9d141fa

Please sign in to comment.