Class: LLM::OpenAI::VectorStores
- Inherits:
-
Object
- Object
- LLM::OpenAI::VectorStores
- Defined in:
- lib/llm/providers/openai/vector_stores.rb
Overview
The LLM::OpenAI::VectorStores class provides an interface for [OpenAI’s vector stores API](platform.openai.com/docs/api-reference/vector_stores/create).
Constant Summary collapse
Instance Method Summary collapse
-
#add_file(vector:, file:, attributes: nil, **params) ⇒ LLM::Response
(also: #create_file)
Add a file to a vector store.
-
#add_file_and_poll(vector:, file:, interval: INTERVAL, **rest) ⇒ LLM::Response
(also: #create_file_and_poll)
Add a file to a vector store and poll until its status is “completed”.
-
#all(**params) ⇒ LLM::Response
List all vector stores.
-
#all_files(vector:, **params) ⇒ LLM::Response
List all files in a vector store.
-
#create(name:, file_ids: nil, **params) ⇒ LLM::Response
Create a vector store.
-
#create_and_poll(interval: INTERVAL, **rest) ⇒ LLM::Response
Create a vector store and poll until its status is “completed”.
-
#delete(vector:) ⇒ LLM::Response
Delete a vector store.
-
#delete_file(vector:, file:) ⇒ LLM::Response
Delete a file from a vector store.
-
#get(vector:) ⇒ LLM::Response
Get a vector store.
-
#get_file(vector:, file:, **params) ⇒ LLM::Response
Get a file from a vector store.
-
#initialize(provider) ⇒ VectorStores
constructor
A new instance of VectorStores.
-
#modify(vector:, name: nil, **params) ⇒ LLM::Response
Modify an existing vector store.
-
#poll(vector:, file: nil, attempts: 0, max: 50, interval: INTERVAL) ⇒ LLM::Response
Poll a vector store or file until its status is “completed”.
-
#search(vector:, query:, **params) ⇒ LLM::Response
Search a vector store.
-
#update_file(vector:, file:, attributes:, **params) ⇒ LLM::Response
Update a file in a vector store.
Constructor Details
#initialize(provider) ⇒ VectorStores
Returns a new instance of VectorStores.
25 26 27 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 25 def initialize(provider) @provider = provider end |
Instance Method Details
#add_file(vector:, file:, attributes: nil, **params) ⇒ LLM::Response Also known as: create_file
Add a file to a vector store
146 147 148 149 150 151 152 153 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 146 def add_file(vector:, file:, attributes: nil, **params) vector_id = vector.respond_to?(:id) ? vector.id : vector file_id = file.respond_to?(:id) ? file.id : file req = Net::HTTP::Post.new("/v1/vector_stores/#{vector_id}/files", headers) req.body = JSON.dump(params.merge({file_id:, attributes:}).compact) res = execute(request: req) LLM::Response.new(res) end |
#add_file_and_poll(vector:, file:, interval: INTERVAL, **rest) ⇒ LLM::Response Also known as: create_file_and_poll
Add a file to a vector store and poll until its status is “completed”
161 162 163 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 161 def add_file_and_poll(vector:, file:, interval: INTERVAL, **rest) poll(vector:, interval:, file: add_file(vector:, file:, **rest)) end |
#all(**params) ⇒ LLM::Response
List all vector stores
33 34 35 36 37 38 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 33 def all(**params) query = URI.encode_www_form(params) req = Net::HTTP::Get.new("/v1/vector_stores?#{query}", headers) res = execute(request: req) LLM::Response.new(res).extend(LLM::OpenAI::Response::Enumerable) end |
#all_files(vector:, **params) ⇒ LLM::Response
List all files in a vector store
129 130 131 132 133 134 135 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 129 def all_files(vector:, **params) vector_id = vector.respond_to?(:id) ? vector.id : vector query = URI.encode_www_form(params) req = Net::HTTP::Get.new("/v1/vector_stores/#{vector_id}/files?#{query}", headers) res = execute(request: req) LLM::Response.new(res).extend(LLM::OpenAI::Response::Enumerable) end |
#create(name:, file_ids: nil, **params) ⇒ LLM::Response
Create a vector store
48 49 50 51 52 53 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 48 def create(name:, file_ids: nil, **params) req = Net::HTTP::Post.new("/v1/vector_stores", headers) req.body = JSON.dump(params.merge({name:, file_ids:}).compact) res = execute(request: req) LLM::Response.new(res) end |
#create_and_poll(interval: INTERVAL, **rest) ⇒ LLM::Response
Create a vector store and poll until its status is “completed”
60 61 62 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 60 def create_and_poll(interval: INTERVAL, **rest) poll(interval:, vector: create(**rest)) end |
#delete(vector:) ⇒ LLM::Response
Delete a vector store
99 100 101 102 103 104 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 99 def delete(vector:) vector_id = vector.respond_to?(:id) ? vector.id : vector req = Net::HTTP::Delete.new("/v1/vector_stores/#{vector_id}", headers) res = execute(request: req) LLM::Response.new(res) end |
#delete_file(vector:, file:) ⇒ LLM::Response
Delete a file from a vector store
207 208 209 210 211 212 213 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 207 def delete_file(vector:, file:) vector_id = vector.respond_to?(:id) ? vector.id : vector file_id = file.respond_to?(:id) ? file.id : file req = Net::HTTP::Delete.new("/v1/vector_stores/#{vector_id}/files/#{file_id}", headers) res = execute(request: req) LLM::Response.new(res) end |
#get(vector:) ⇒ LLM::Response
Get a vector store
70 71 72 73 74 75 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 70 def get(vector:) vector_id = vector.respond_to?(:id) ? vector.id : vector req = Net::HTTP::Get.new("/v1/vector_stores/#{vector_id}", headers) res = execute(request: req) LLM::Response.new(res) end |
#get_file(vector:, file:, **params) ⇒ LLM::Response
Get a file from a vector store
191 192 193 194 195 196 197 198 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 191 def get_file(vector:, file:, **params) vector_id = vector.respond_to?(:id) ? vector.id : vector file_id = file.respond_to?(:id) ? file.id : file query = URI.encode_www_form(params) req = Net::HTTP::Get.new("/v1/vector_stores/#{vector_id}/files/#{file_id}?#{query}", headers) res = execute(request: req) LLM::Response.new(res) end |
#modify(vector:, name: nil, **params) ⇒ LLM::Response
Modify an existing vector store
85 86 87 88 89 90 91 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 85 def modify(vector:, name: nil, **params) vector_id = vector.respond_to?(:id) ? vector.id : vector req = Net::HTTP::Post.new("/v1/vector_stores/#{vector_id}", headers) req.body = JSON.dump(params.merge({name:}).compact) res = execute(request: req) LLM::Response.new(res) end |
#poll(vector:, file: nil, attempts: 0, max: 50, interval: INTERVAL) ⇒ LLM::Response
Poll a vector store or file until its status is “completed”
224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 224 def poll(vector:, file: nil, attempts: 0, max: 50, interval: INTERVAL) target = file || vector if attempts == max raise LLM::PollError, "'#{target.id}' has status '#{target.status}' after #{max} attempts" elsif target.status == "expired" raise LLM::PollError, "#{target.id}' has expired" elsif target.status != "completed" file ? (file = get_file(vector:, file:)) : (vector = get(vector:)) sleep(interval * (2**attempts)) unless interval.zero? poll(vector:, file:, attempts: attempts + 1, max:, interval:) else target end end |
#search(vector:, query:, **params) ⇒ LLM::Response
Search a vector store
114 115 116 117 118 119 120 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 114 def search(vector:, query:, **params) vector_id = vector.respond_to?(:id) ? vector.id : vector req = Net::HTTP::Post.new("/v1/vector_stores/#{vector_id}/search", headers) req.body = JSON.dump(params.merge({query:}).compact) res = execute(request: req) LLM::Response.new(res).extend(LLM::OpenAI::Response::Enumerable) end |
#update_file(vector:, file:, attributes:, **params) ⇒ LLM::Response
Update a file in a vector store
175 176 177 178 179 180 181 182 |
# File 'lib/llm/providers/openai/vector_stores.rb', line 175 def update_file(vector:, file:, attributes:, **params) vector_id = vector.respond_to?(:id) ? vector.id : vector file_id = file.respond_to?(:id) ? file.id : file req = Net::HTTP::Post.new("/v1/vector_stores/#{vector_id}/files/#{file_id}", headers) req.body = JSON.dump(params.merge({attributes:}).compact) res = execute(request: req) LLM::Response.new(res) end |