Class: SmartPrompt::OllamaAdapter
- Inherits:
-
LLMAdapter
- Object
- LLMAdapter
- SmartPrompt::OllamaAdapter
- Defined in:
- lib/smart_prompt/ollama_adapter.rb
Instance Method Summary collapse
- #embeddings(text, model) ⇒ Object
-
#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::Errors => 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
#embeddings(text, model) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/smart_prompt/ollama_adapter.rb', line 55 def (text, model) SmartPrompt.logger.info "OllamaAdapter: get embeddings from Ollama" if model model_name = model else model_name = @config['model'] end SmartPrompt.logger.info "OllamaAdapter: Using model #{model_name}" begin response = @client.( { model: model_name, prompt: text.to_s } ) 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 return response.dig(0,"embedding") end |
#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 |
# 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::Errors => e SmartPrompt.logger.error "Ollama API error: #{e.}" raise LLMAPIError, "Ollama API error: #{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.dig(0,"response") end |