Class: LlmScraper::LlmClients::Anthropic
- Defined in:
- lib/llm_scraper/llm_clients/anthropic.rb
Constant Summary collapse
- BASE_URL =
"https://api.anthropic.com"- API_VERSION =
"2023-06-01"
Constants inherited from Base
Instance Method Summary collapse
-
#complete(prompt) ⇒ Hash
{ content:, tokens:, cost_usd: }.
-
#initialize(config = LlmScraper.configuration) ⇒ Anthropic
constructor
A new instance of Anthropic.
Constructor Details
#initialize(config = LlmScraper.configuration) ⇒ Anthropic
Returns a new instance of Anthropic.
9 10 11 12 |
# File 'lib/llm_scraper/llm_clients/anthropic.rb', line 9 def initialize(config = LlmScraper.configuration) @config = config @conn = build_connection(base_url: BASE_URL, timeout: config.llm_timeout) end |
Instance Method Details
#complete(prompt) ⇒ Hash
Returns { content:, tokens:, cost_usd: }.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/llm_scraper/llm_clients/anthropic.rb', line 16 def complete(prompt) response = @conn.post("v1/messages") do |req| req.headers["x-api-key"] = @config.llm_api_key req.headers["anthropic-version"] = API_VERSION req.headers["Content-Type"] = "application/json" req.body = JSON.generate( model: @config.llm_model, max_tokens: 1024, messages: [{ role: "user", content: prompt }] ) end handle_response(response) rescue Faraday::Error => e raise LlmScraper::LlmError, "Anthropic request failed: #{e.}" end |