Class: RubyLLM::Registry::Adapters::MongoDB
- Defined in:
- lib/ruby_llm/registry/adapters/mongo.rb
Overview
MongoDB-backed prompt repository.
Instance Method Summary collapse
- #available_versions(path) ⇒ Object
- #get(path, version: nil, label: nil) ⇒ Object
-
#initialize(collection: nil, database: nil, collection_name: "ruby_llm_registry_prompts") ⇒ MongoDB
constructor
A new instance of MongoDB.
- #store(prompt, overwrite: false) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(collection: nil, database: nil, collection_name: "ruby_llm_registry_prompts") ⇒ MongoDB
Returns a new instance of MongoDB.
10 11 12 |
# File 'lib/ruby_llm/registry/adapters/mongo.rb', line 10 def initialize(collection: nil, database: nil, collection_name: "ruby_llm_registry_prompts") @collection = collection || resolve_collection(database, collection_name) end |
Instance Method Details
#available_versions(path) ⇒ Object
32 33 34 |
# File 'lib/ruby_llm/registry/adapters/mongo.rb', line 32 def available_versions(path) collection.find(path: path).map { |doc| Version.parse(doc["version"]) }.sort end |
#get(path, version: nil, label: nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/ruby_llm/registry/adapters/mongo.rb', line 14 def get(path, version: nil, label: nil) docs = collection.find(path: path).to_a raise PromptNotFoundError, "Prompt path not found: #{path}" if docs.empty? doc = if version docs.find { |record| record["version"] == Version.parse(version).to_s } elsif label label = label.to_sym docs.find { |record| labels_for(record).include?(label) } || docs.find { |record| record["version"] == label.to_s } else docs.max_by { |record| Version.parse(record["version"]) } end raise PromptNotFoundError, "Prompt not found: #{path}" unless doc prompt_from_document(doc) end |
#store(prompt, overwrite: false) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/ruby_llm/registry/adapters/mongo.rb', line 36 def store(prompt, overwrite: false) existing = collection.find(path: prompt.path, version: prompt.version.to_s).first if existing && !overwrite raise Error, "Prompt #{prompt.path}@#{prompt.version} already exists" end doc = serialize_prompt(prompt) existing ? collection.replace_one({ path: prompt.path, version: prompt.version.to_s }, doc) : collection.insert_one(doc) prompt end |