Skip to content

Commit

Permalink
bin/console --debug + explained in README
Browse files Browse the repository at this point in the history
  • Loading branch information
drnic committed Apr 22, 2024
1 parent a8d7e6c commit 2a33a33
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,29 @@ end
@client.chat("Hello, world!", max_tokens: 512, temperature: 0.5)
```

### Debugging API calls

The underlying HTTP library being used is faraday, and you can enabled debugging, or configure other faraday internals by passing a block to the `Groq::Client.new` constructor.

```ruby
require 'logger'

# Create a logger instance
logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG

@client = Groq::Client.new do |faraday|
# Log request and response bodies
faraday.response :logger, logger, bodies: true
end
```

If you pass `--debug` to `bin/console` you will have this logger setup for you.

```plain
bin/console --debug
```

## Examples

### Pizzeria agent
Expand Down
32 changes: 29 additions & 3 deletions bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@

require "bundler/setup"
require "groq"
require "logger"
require "optparse"

@options = {
debug: false
}

OptionParser.new do |opts|
opts.banner = "Usage: bin/console [--debug]"

opts.on("--debug", "Enable debug mode") do
@options[:debug] = true
end
end.parse!

def debug?
@options[:debug]
end

@client = Groq::Client.new do |faraday|
if debug?
# Create a logger instance
logger = Logger.new(STDOUT)
logger.level = Logger::DEBUG

# Log request and response bodies
faraday.response :logger, logger, bodies: true
end
end

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
@client = Groq::Client.new
include Groq::Helpers

require "dry-schema"
Expand Down
13 changes: 9 additions & 4 deletions lib/groq/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ def post(path:, body:)
end

def client
@client ||= Faraday.new(url: @api_url) do |f|
f.request :json # automatically encode the request body as JSON
f.response :json # automatically decode JSON responses
f.adapter Faraday.default_adapter
@client ||= begin
connection = Faraday.new(url: @api_url) do |f|
f.request :json # automatically encode the request body as JSON
f.response :json # automatically decode JSON responses
f.adapter Faraday.default_adapter
end
@faraday_middleware&.call(connection)

connection
end
end
end

0 comments on commit 2a33a33

Please sign in to comment.