Class: LlamaBotRails::LlamaBot
- Inherits:
-
Object
- Object
- LlamaBotRails::LlamaBot
- Defined in:
- lib/llama_bot_rails/llama_bot.rb
Overview
This class is responsible for initiating HTTP requests to the FastAPI backend that takes us to LangGraph.
Class Method Summary collapse
- .get_chat_history(thread_id) ⇒ Object
- .get_threads ⇒ Object
- .send_agent_message(agent_params) ⇒ Object
Class Method Details
.get_chat_history(thread_id) ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/llama_bot_rails/llama_bot.rb', line 17 def self.get_chat_history(thread_id) uri = URI("#{Rails.application.config.llama_bot_rails.llamabot_api_url}/chat-history/#{thread_id}") response = Net::HTTP.get_response(uri) JSON.parse(response.body) rescue => e Rails.logger.error "Error fetching chat history: #{e.message}" [] end |
.get_threads ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/llama_bot_rails/llama_bot.rb', line 8 def self.get_threads uri = URI("#{Rails.application.config.llama_bot_rails.llamabot_api_url}/threads") response = Net::HTTP.get_response(uri) JSON.parse(response.body) rescue => e Rails.logger.error "Error fetching threads: #{e.message}" [] end |
.send_agent_message(agent_params) ⇒ Object
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 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/llama_bot_rails/llama_bot.rb', line 26 def self.(agent_params) return enum_for(__method__, agent_params) unless block_given? uri = URI("#{Rails.application.config.llama_bot_rails.llamabot_api_url}/llamabot-chat-message") http = Net::HTTP.new(uri.host, uri.port) request = Net::HTTP::Post.new(uri) http.use_ssl = (uri.scheme == "https") request['Content-Type'] = 'application/json' request.body = agent_params.to_json # Stream the response instead of buffering it http.request(request) do |response| if response.code.to_i == 200 buffer = '' response.read_body do |chunk| buffer += chunk # Process complete lines (ended with \n) while buffer.include?("\n") line, buffer = buffer.split("\n", 2) if line.strip.present? begin yield JSON.parse(line) rescue JSON::ParserError => e Rails.logger.error "Parse error: #{e.message}" end end end end # Process any remaining data in buffer if buffer.strip.present? begin yield JSON.parse(buffer) rescue JSON::ParserError => e Rails.logger.error "Final buffer parse error: #{e.message}" end end end end rescue => e Rails.logger.error "Error sending agent message: #{e.message}" { error: e. } end |