Module: RubyLlmMesh::ActiveRecord::ActsAsAiAgent::InstanceMethods

Defined in:
lib/ruby_llm_mesh/active_record/acts_as_ai_agent.rb

Instance Method Summary collapse

Instance Method Details

#ai_audit!(prompt, response) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby_llm_mesh/active_record/acts_as_ai_agent.rb', line 86

def ai_audit!(prompt, response)
  audits = read_ai_json(self.class.ai_agent_audit_attribute)
  audits = [] unless audits.is_a?(Array)
  audits << {
    "at" => Time.now.utc.iso8601,
    "prompt" => prompt.to_s,
    "provider" => response.provider.to_s,
    "model" => response.model,
    "latency_ms" => response.latency_ms,
    "fallback_used" => response.fallback_used
  }
  write_ai_json(self.class.ai_agent_audit_attribute, audits)
end

#ai_cache!(prompt, response) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ruby_llm_mesh/active_record/acts_as_ai_agent.rb', line 74

def ai_cache!(prompt, response)
  cache = read_ai_json(self.class.ai_agent_cache_attribute)
  cache = {} unless cache.is_a?(Hash)
  cache[prompt.to_s] = {
    "content" => response.content,
    "provider" => response.provider.to_s,
    "model" => response.model,
    "usage" => response.usage
  }
  write_ai_json(self.class.ai_agent_cache_attribute, cache)
end

#ai_clear_memory!Object



52
53
54
55
# File 'lib/ruby_llm_mesh/active_record/acts_as_ai_agent.rb', line 52

def ai_clear_memory!
  write_ai_json(self.class.ai_agent_memory_attribute, [])
  save if respond_to?(:save)
end

#ai_complete(prompt, **options) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ruby_llm_mesh/active_record/acts_as_ai_agent.rb', line 24

def ai_complete(prompt, **options)
  cached = ai_semantic_lookup(prompt)
  return cached if cached

  memory = ai_memory
  system = options.delete(:system)
  system = [system, "Conversation memory:\n#{memory.join("\n")}"].compact.join("\n\n") unless memory.empty?

  response = RubyLlmMesh.complete(prompt: prompt, system: system, **options)
  ai_remember!(role: "user", content: prompt)
  ai_remember!(role: "assistant", content: response.content, provider: response.provider)
  ai_cache!(prompt, response)
  ai_audit!(prompt, response)
  response
end

#ai_memoryObject



40
41
42
43
# File 'lib/ruby_llm_mesh/active_record/acts_as_ai_agent.rb', line 40

def ai_memory
  raw = read_ai_json(self.class.ai_agent_memory_attribute)
  Array(raw)
end

#ai_remember!(entry) ⇒ Object



45
46
47
48
49
50
# File 'lib/ruby_llm_mesh/active_record/acts_as_ai_agent.rb', line 45

def ai_remember!(entry)
  memory = ai_memory
  memory << entry.transform_keys(&:to_s)
  write_ai_json(self.class.ai_agent_memory_attribute, memory)
  save if respond_to?(:save)
end

#ai_semantic_lookup(prompt) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ruby_llm_mesh/active_record/acts_as_ai_agent.rb', line 57

def ai_semantic_lookup(prompt)
  cache = read_ai_json(self.class.ai_agent_cache_attribute)
  return nil unless cache.is_a?(Hash)

  entry = cache[prompt.to_s]
  return nil unless entry

  RubyLlmMesh::Response.new(
    content: entry["content"],
    provider: (entry["provider"] || :cache).to_sym,
    model: entry["model"],
    usage: entry["usage"] || {},
    latency_ms: 0,
    fallback_used: false
  )
end