Class: SmartPrompt::OllamaAdapter
- Inherits:
-
LLMAdapter
- Object
- LLMAdapter
- SmartPrompt::OllamaAdapter
- Defined in:
- lib/smart_prompt/ollama_adapter.rb
Instance Method Summary collapse
-
#initialize(config) ⇒ OllamaAdapter
constructor
A new instance of OllamaAdapter.
- #send_request(messages, model = nil) ⇒ Object
Constructor Details
#initialize(config) ⇒ OllamaAdapter
Returns a new instance of OllamaAdapter.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/smart_prompt/ollama_adapter.rb', line 5 def initialize(config) super begin @client = Ollama.new(credentials: { address: @config['url'] }) rescue Ollama::Error => e SmartPrompt.logger.error "Failed to initialize Ollama client: #{e.}" raise LLMAPIError, "Invalid Ollama configuration: #{e.}" rescue SocketError => e SmartPrompt.logger.error "Failed to initialize Ollama client: #{e.}" raise LLMAPIError, "Network error: Unable to connect to Ollama API" rescue => e SmartPrompt.logger.error "Failed to initialize Ollama client: #{e.}" raise Error, "Unexpected error initializing Ollama client: #{e.}" ensure SmartPrompt.logger.info "Successful creation an Ollama client." end end |
Instance Method Details
#send_request(messages, model = nil) ⇒ Object
23 24 25 26 27 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 |
# File 'lib/smart_prompt/ollama_adapter.rb', line 23 def send_request(, model=nil) SmartPrompt.logger.info "OllamaAdapter: Sending request to Ollama" if model model_name = model else model_name = @config['model'] end SmartPrompt.logger.info "OllamaAdapter: Using model #{model_name}" begin response = @client.generate( { model: model_name, prompt: .to_s, stream: false } ) rescue Ollama::Error => e SmartPrompt.logger.error "Ollama API error: #{e.}" raise LLMAPIError, "Ollama API error: #{e.}" rescue Ollama::ConnectionError => e SmartPrompt.logger.error "Connection error: Unable to reach Ollama API" raise LLMAPIError, "Connection error: Unable to reach Ollama API" rescue Ollama::TimeoutError => e SmartPrompt.logger.error "Request to Ollama API timed out" raise LLMAPIError, "Request to Ollama API timed out" rescue Ollama::InvalidRequestError => e SmartPrompt.logger.error "Invalid request to Ollama API: #{e.}" raise LLMAPIError, "Invalid request to Ollama API: #{e.}" rescue JSON::ParserError => e SmartPrompt.logger.error "Failed to parse Ollama API response" raise LLMAPIError, "Failed to parse Ollama API response" rescue => e SmartPrompt.logger.error "Unexpected error during Ollama request: #{e.}" raise Error, "Unexpected error during Ollama request: #{e.}" ensure SmartPrompt.logger.info "Successful send a message" end SmartPrompt.logger.info "OllamaAdapter: Received response from Ollama" return response[0]["response"] end |