Class: ZenAndMusashi::Client
- Inherits:
-
Object
- Object
- ZenAndMusashi::Client
- Defined in:
- lib/zen_and_musashi/client.rb
Constant Summary collapse
- MODEL =
"claude-haiku-4-5"- API_URL =
"https://api.anthropic.com/v1/messages"
Instance Method Summary collapse
-
#initialize(api_key: nil) ⇒ Client
constructor
A new instance of Client.
- #invoke(mode, input) ⇒ Object
Constructor Details
#initialize(api_key: nil) ⇒ Client
Returns a new instance of Client.
12 13 14 15 |
# File 'lib/zen_and_musashi/client.rb', line 12 def initialize(api_key: nil) @api_key = api_key || ENV["ANTHROPIC_API_KEY"] validate_api_key! end |
Instance Method Details
#invoke(mode, input) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/zen_and_musashi/client.rb', line 17 def invoke(mode, input) uri = URI(API_URL) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true body = { model: MODEL, max_tokens: 300, system: system_prompt(mode), messages: [{ role: "user", content: input }] }.to_json request = Net::HTTP::Post.new(uri) request["anthropic-version"] = "2023-06-01" request["content-type"] = "application/json" request["x-api-key"] = @api_key request.body = body response = http.request(request) unless response.is_a?(Net::HTTPSuccess) raise "HTTP Error: #{response.code}" end parsed = JSON.parse(response.body) parsed["content"][0]["text"] end |