Class: SmartPrompt::LlamacppAdapter
- Inherits:
-
LLMAdapter
- Object
- LLMAdapter
- SmartPrompt::LlamacppAdapter
- Defined in:
- lib/smart_prompt/llamacpp_adapter.rb
Instance Attribute Summary
Attributes inherited from LLMAdapter
Instance Method Summary collapse
-
#initialize(config) ⇒ LlamacppAdapter
constructor
A new instance of LlamacppAdapter.
- #send_request(messages, model = nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ LlamacppAdapter
Returns a new instance of LlamacppAdapter.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/smart_prompt/llamacpp_adapter.rb', line 5 def initialize(config) super begin @client = OpenAI::Client.new( uri_base: @config['url'] ) rescue OpenAI::ConfigurationError => e SmartPrompt.logger.error "Failed to initialize Llamacpp client: #{e.}" raise LLMAPIError, "Invalid Llamacpp configuration: #{e.}" rescue OpenAI::AuthenticationError => e SmartPrompt.logger.error "Failed to initialize Llamacpp client: #{e.}" raise LLMAPIError, "Llamacpp authentication failed: #{e.}" rescue SocketError => e SmartPrompt.logger.error "Failed to initialize Llamacpp client: #{e.}" raise LLMAPIError, "Network error: Unable to connect to Llamacpp API" rescue => e SmartPrompt.logger.error "Failed to initialize Llamacpp client: #{e.}" raise Error, "Unexpected error initializing Llamacpp client: #{e.}" ensure SmartPrompt.logger.info "Successful creation an Llamacpp client." end end |
Instance Method Details
#send_request(messages, model = nil) ⇒ Object
28 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/smart_prompt/llamacpp_adapter.rb', line 28 def send_request(, model=nil) SmartPrompt.logger.info "LlamacppAdapter: Sending request to Llamacpp" begin response = @client.chat( parameters: { messages: , temperature: @config['temperature'] || 0.7 } ) rescue OpenAI::APIError => e SmartPrompt.logger.error "Llamacpp API error: #{e.}" raise LLMAPIError, "Llamacpp API error: #{e.}" rescue OpenAI::APIConnectionError => e SmartPrompt.logger.error "Connection error: Unable to reach Llamacpp API" raise LLMAPIError, "Connection error: Unable to reach Llamacpp API" rescue OpenAI::APITimeoutError => e SmartPrompt.logger.error "Request to Llamacpp API timed out" raise LLMAPIError, "Request to Llamacpp API timed out" rescue OpenAI::InvalidRequestError => e SmartPrompt.logger.error "Invalid request to Llamacpp API: #{e.}" raise LLMAPIError, "Invalid request to Llamacpp API: #{e.}" rescue OpenAI::AuthenticationError => e SmartPrompt.logger.error "Authentication error with Llamacpp API: #{e.}" raise LLMAPIError, "Authentication error with Llamacpp API: #{e.}" rescue OpenAI::RateLimitError => e SmartPrompt.logger.error "Rate limit exceeded for Llamacpp API" raise LLMAPIError, "Rate limit exceeded for Llamacpp API" rescue JSON::ParserError => e SmartPrompt.logger.error "Failed to parse Llamacpp API response" raise LLMAPIError, "Failed to parse Llamacpp API response" rescue => e SmartPrompt.logger.error "Unexpected error during Llamacpp request: #{e.}" raise Error, "Unexpected error during Llamacpp request: #{e.}" ensure SmartPrompt.logger.info "Successful send a message" end SmartPrompt.logger.info "LlamacppAdapter: Received response from Llamacpp" response.dig("choices", 0, "message", "content") end |