Skip to content

Instantly share code, notes, and snippets.

@peterc
Last active April 19, 2024 17:45
Show Gist options
  • Save peterc/60bdd47382318dd72cc790003f1e8cdb to your computer and use it in GitHub Desktop.
Save peterc/60bdd47382318dd72cc790003f1e8cdb to your computer and use it in GitHub Desktop.
Basic Groq API client for Ruby
require 'http'
require 'json'
class GroqClient
def initialize(api_key: nil, api_url: "https://api.groq.com/openai/v1/chat/completions", model: "mixtral-8x7b-32768")
@api_key = api_key || ENV['GROQ_API_KEY']
@api_url = api_url
@model = model
end
def ask(query = "Return an array of the names of the top ten most popular spoken languages")
response = HTTP.headers(headers).post(@api_url, json: request_body(query))
if response.status != 200
STDERR.puts "Groq error: #{response.status} - #{response.headers.to_h} - #{response.body}"
return
end
begin
r = JSON.parse(response.body.to_s)['choices'].first['message']['content']
JSON.parse(r)
rescue
"Response was not valid JSON"
end
end
def self.ask(*args)
self.new.ask(*args)
end
private
def headers
{"Authorization" => "Bearer #{@api_key}","Content-Type" => "application/json"}
end
def request_body(query)
{
messages: [
{ role: "system", content: "You only reply with one JSON object. No plain text before or after. ONLY JSON." },
{ role: "user", content: query }
],
model: @model
}
end
end
p GroqClient.ask if __FILE__ == $0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment