Class: Prescient::Provider::Anthropic
- Includes:
- HTTParty
- Defined in:
- lib/prescient/provider/anthropic.rb
Overview
Anthropic Messages API provider adapter.
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#generate_embedding(_text, **_options) ⇒ Object
Anthropic does not provide embeddings through this adapter.
-
#generate_response(prompt, context_items = [], **options) ⇒ Hash
Generate a response using Anthropic's Messages API.
-
#health_check ⇒ Hash
Check Anthropic API availability using the non-generating
/v1/modelsendpoint. -
#initialize(**options) ⇒ Anthropic
constructor
A new instance of Anthropic.
-
#list_models ⇒ Array<Hash>
Return models available to the configured Anthropic account.
- #validate_configuration! ⇒ Object protected
Methods inherited from Base
#available?, #build_prompt, #clean_text, #default_context_configs, #default_prompt_templates, #extract_embedding_text, #extract_text_values, #format_context_item, #handle_errors, #validate_embedding_dimensions
Constructor Details
#initialize(**options) ⇒ Anthropic
Returns a new instance of Anthropic.
11 12 13 14 |
# File 'lib/prescient/provider/anthropic.rb', line 11 def initialize(**) super self.class.default_timeout(@options[:timeout] || 60) end |
Instance Method Details
#generate_embedding(_text, **_options) ⇒ Object
Anthropic does not provide embeddings through this adapter.
19 20 21 22 23 |
# File 'lib/prescient/provider/anthropic.rb', line 19 def (_text, **) # Anthropic doesn't provide embedding API, raise error raise Prescient::Error, 'Anthropic provider does not support embeddings. Use OpenAI or HuggingFace for embeddings.' end |
#generate_response(prompt, context_items = [], **options) ⇒ Hash
Generate a response using Anthropic's Messages API.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/prescient/provider/anthropic.rb', line 29 def generate_response(prompt, context_items = [], **) handle_errors do formatted_prompt = build_prompt(prompt, context_items) response = self.class.post('/v1/messages', headers: { 'Content-Type' => 'application/json', 'x-api-key' => @options[:api_key], 'anthropic-version' => '2023-06-01', }, body: { model: [:model] || @options[:model], max_tokens: [:max_tokens] || 2000, temperature: [:temperature] || 0.7, messages: [ { role: 'user', content: formatted_prompt, }, ], }.to_json) validate_response!(response, 'text generation') content = response.parsed_response.dig('content', 0, 'text') raise Prescient::InvalidResponseError, 'No response generated' unless content { response: content.strip, model: [:model] || @options[:model], provider: 'anthropic', processing_time: nil, metadata: { usage: response.parsed_response['usage'], }, } end end |
#health_check ⇒ Hash
Check Anthropic API availability using the non-generating /v1/models endpoint.
reachable indicates the API answered successfully. ready indicates that
the configured model appears in the returned model list.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/prescient/provider/anthropic.rb', line 74 def health_check handle_errors do response = self.class.get('/v1/models', headers: api_headers) if response.success? models = response.parsed_response['data'] || [] model_available = models.any? { |model| model['id'] == @options[:model] } { status: 'healthy', provider: 'anthropic', reachable: true, models_available: models.map { |model| model['id'] }, model: { name: @options[:model], available: model_available }, ready: model_available, } else { status: 'unhealthy', provider: 'anthropic', reachable: true, error: "HTTP #{response.code}", message: response., ready: false } end end rescue Prescient::Error => e { status: 'unavailable', provider: 'anthropic', reachable: false, error: e.class.name, message: e., ready: false, } end |
#list_models ⇒ Array<Hash>
Return models available to the configured Anthropic account.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/prescient/provider/anthropic.rb', line 109 def list_models handle_errors do response = self.class.get('/v1/models', headers: api_headers) validate_response!(response, 'model listing') (response.parsed_response['data'] || []).map do |model| { name: model['id'], type: 'text', display_name: model['display_name'], created_at: model['created_at'], max_input_tokens: model['max_input_tokens'], max_tokens: model['max_tokens'], }.compact end end end |
#validate_configuration! ⇒ Object (protected)
129 130 131 132 133 134 135 136 |
# File 'lib/prescient/provider/anthropic.rb', line 129 def validate_configuration! = [:api_key, :model] = .select { |opt| @options[opt].nil? } return unless .any? raise Prescient::Error, "Missing required options: #{.join(', ')}" end |