Class: LLM::OpenAI::VectorStores

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/providers/openai/vector_stores.rb

Overview

The LLM::OpenAI::VectorStores class provides an interface for OpenAI’s vector stores API.

Examples:

llm = LLM.openai(key: ENV["OPENAI_SECRET"])
files = %w(foo.pdf bar.pdf).map { llm.files.create(file: _1) }
store = llm.vector_stores.create(name: "PDF Store", file_ids: files.map(&:id))
store = llm.vector_stores.poll(vector: store)
print "[-] store is ready", "\n"
chunks = llm.vector_stores.search(vector: store, query: "What is Ruby?")
chunks.each { |chunk| puts chunk }

Constant Summary collapse

PollError =
Class.new(LLM::Error)

Instance Method Summary collapse

Constructor Details

#initialize(provider) ⇒ VectorStores

Returns a new instance of VectorStores.

Parameters:



23
24
25
# File 'lib/llm/providers/openai/vector_stores.rb', line 23

def initialize(provider)
  @provider = provider
end

Instance Method Details

#poll(vector:, attempts: 0, max: 50) ⇒ LLM::Response

Poll a vector store until its status is “completed”

Parameters:

  • vector (String, #id)

    The ID of the vector store

  • attempts (Integer) (defaults to: 0)

    The current number of attempts (default: 0)

  • max (Integer) (defaults to: 50)

    The maximum number of iterations (default: 50)

Returns:

Raises:

  • (LLM::PollError)

    When the maximum number of iterations is reached



201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/llm/providers/openai/vector_stores.rb', line 201

def poll(vector:, attempts: 0, max: 50)
  if attempts == max
    raise LLM::PollError, "vector store '#{vector.id}' has status '#{vector.status}' after #{max} attempts"
  elsif vector.status == "expired"
    raise LLM::PollError, "vector store '#{vector.id}' has expired"
  elsif vector.status != "completed"
    vector = get(vector:)
    sleep(0.1 * (2**attempts))
    poll(vector:, attempts: attempts + 1, max:)
  else
    vector
  end
end

#all(**params) ⇒ LLM::Response

List all vector stores

Parameters:

  • params (Hash)

    Other parameters (see OpenAI docs)

Returns:



31
32
33
34
35
36
# File 'lib/llm/providers/openai/vector_stores.rb', line 31

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

#create(name:, file_ids: [], **params) ⇒ LLM::Response

Create a vector store

Parameters:

  • name (String)

    The name of the vector store

  • file_ids (Array<String>) (defaults to: [])

    The IDs of the files to include in the vector store

  • params (Hash)

    Other parameters (see OpenAI docs)

Returns:

See Also:



46
47
48
49
50
51
# File 'lib/llm/providers/openai/vector_stores.rb', line 46

def create(name:, file_ids: [], **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

#get(vector:) ⇒ LLM::Response

Get a vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

Returns:

See Also:



59
60
61
62
63
64
# File 'lib/llm/providers/openai/vector_stores.rb', line 59

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

#modify(vector:, name: nil, **params) ⇒ LLM::Response

Modify an existing vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

  • name (String) (defaults to: nil)

    The new name of the vector store

  • params (Hash)

    Other parameters (see OpenAI docs)

Returns:

See Also:



74
75
76
77
78
79
80
# File 'lib/llm/providers/openai/vector_stores.rb', line 74

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

#delete(vector:) ⇒ LLM::Response

Delete a vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

Returns:

See Also:



88
89
90
91
92
93
# File 'lib/llm/providers/openai/vector_stores.rb', line 88

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

#search(vector:, query:, **params) ⇒ LLM::Response

Search a vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

  • query (String)

    The query to search for

  • params (Hash)

    Other parameters (see OpenAI docs)

Returns:

See Also:



103
104
105
106
107
108
109
# File 'lib/llm/providers/openai/vector_stores.rb', line 103

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

#all_files(vector:, **params) ⇒ LLM::Response

List all files in a vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

  • params (Hash)

    Other parameters (see OpenAI docs)

Returns:

See Also:



118
119
120
121
122
123
124
# File 'lib/llm/providers/openai/vector_stores.rb', line 118

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

#add_file(vector:, file:, attributes: nil, **params) ⇒ LLM::Response Also known as: create_file

Add a file to a vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

  • file (String, #id)

    The ID of the file to add

  • attributes (Hash) (defaults to: nil)

    Attributes to associate with the file (optional)

  • params (Hash)

    Other parameters (see OpenAI docs)

Returns:

See Also:



135
136
137
138
139
140
141
142
# File 'lib/llm/providers/openai/vector_stores.rb', line 135

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

#update_file(vector:, file:, attributes:, **params) ⇒ LLM::Response

Update a file in a vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

  • file (String, #id)

    The ID of the file to update

  • attributes (Hash)

    Attributes to associate with the file

  • params (Hash)

    Other parameters (see OpenAI docs)

Returns:

See Also:



154
155
156
157
158
159
160
161
# File 'lib/llm/providers/openai/vector_stores.rb', line 154

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

#get_file(vector:, file:, **params) ⇒ LLM::Response

Get a file from a vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

  • file (String, #id)

    The ID of the file to retrieve

Returns:

See Also:



170
171
172
173
174
175
176
177
# File 'lib/llm/providers/openai/vector_stores.rb', line 170

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

#delete_file(vector:, file:) ⇒ LLM::Response

Delete a file from a vector store

Parameters:

  • vector (String, #id)

    The ID of the vector store

  • file (String, #id)

    The ID of the file to delete

Returns:

See Also:



186
187
188
189
190
191
192
# File 'lib/llm/providers/openai/vector_stores.rb', line 186

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