Skip to content

Commit 6098298

Browse files
authored
Merge pull request alexrudall#472 from willywg/add_vector_store_endpoints
Add vector store endpoints
2 parents e06565a + 50f09cf commit 6098298

File tree

49 files changed

+3876
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3876
-3
lines changed

README.md

Lines changed: 159 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,12 @@ Stream text with GPT-4o, transcribe and translate audio with Whisper, or create
3939
- [Embeddings](#embeddings)
4040
- [Batches](#batches)
4141
- [Files](#files)
42+
- [For fine-tuning purposes](#for-fine-tuning-purposes)
43+
- [For assistant purposes](#for-assistant-purposes)
4244
- [Finetunes](#finetunes)
45+
- [Vector Stores](#vector-stores)
46+
- [Vector Store Files](#vector-store-files)
47+
- [Vector Store File Batches](#vector-store-file-batches)
4348
- [Assistants](#assistants)
4449
- [Threads and Messages](#threads-and-messages)
4550
- [Runs](#runs)
@@ -609,7 +614,7 @@ These files are in JSONL format, with each line representing the output or error
609614
If a request fails with a non-HTTP error, the error object will contain more information about the cause of the failure.
610615

611616
### Files
612-
617+
#### For fine-tuning purposes
613618
Put your data in a `.jsonl` file like this:
614619

615620
```json
@@ -627,6 +632,24 @@ client.files.content(id: "file-123")
627632
client.files.delete(id: "file-123")
628633
```
629634

635+
#### For assistant purposes
636+
637+
You can send a file path:
638+
639+
```ruby
640+
client.files.upload(parameters: { file: "path/to/file.pdf", purpose: "assistants" })
641+
```
642+
643+
or a File object
644+
645+
```ruby
646+
my_file = File.open("path/to/file.pdf", "rb")
647+
client.files.upload(parameters: { file: my_file, purpose: "assistants" })
648+
```
649+
650+
651+
See supported file types on [API documentation](https://platform.openai.com/docs/assistants/tools/file-search/supported-files).
652+
630653
### Finetunes
631654

632655
Upload your fine-tuning data in a `.jsonl` file as above and get its ID:
@@ -679,6 +702,135 @@ You can also capture the events for a job:
679702
client.finetunes.list_events(id: fine_tune_id)
680703
```
681704

705+
### Vector Stores
706+
Vector Store objects give the File Search tool the ability to search your files.
707+
708+
You can create a new vector store:
709+
710+
```ruby
711+
response = client.vector_stores.create(
712+
parameters: {
713+
name: "my vector store",
714+
file_ids: ["file-abc123", "file-def456"]
715+
}
716+
)
717+
718+
vector_store_id = response["id"]
719+
```
720+
721+
Given a `vector_store_id` you can `retrieve` the current field values:
722+
723+
```ruby
724+
client.vector_stores.retrieve(id: vector_store_id)
725+
```
726+
727+
You can get a `list` of all vector stores currently available under the organization:
728+
729+
```ruby
730+
client.vector_stores.list
731+
```
732+
733+
You can modify an existing vector store, except for the `file_ids`:
734+
735+
```ruby
736+
response = client.vector_stores.modify(
737+
id: vector_store_id,
738+
parameters: {
739+
name: "Modified Test Vector Store",
740+
}
741+
)
742+
```
743+
744+
You can delete vector stores:
745+
746+
```ruby
747+
client.vector_stores.delete(id: vector_store_id)
748+
```
749+
750+
### Vector Store Files
751+
Vector store files represent files inside a vector store.
752+
753+
You can create a new vector store file by attaching a File to a vector store.
754+
755+
```ruby
756+
response = client.vector_store_files.create(
757+
vector_store_id: "vector-store-abc123",
758+
parameters: {
759+
file_id: "file-abc123"
760+
}
761+
)
762+
763+
vector_store_file_id = response["id"]
764+
```
765+
766+
Given a `vector_store_file_id` you can `retrieve` the current field values:
767+
768+
```ruby
769+
client.vector_store_files.retrieve(
770+
vector_store_id: "vector-store-abc123",
771+
id: vector_store_file_id
772+
)
773+
```
774+
775+
You can get a `list` of all vector store files currently available under the vector store:
776+
777+
```ruby
778+
client.vector_store_files.list(vector_store_id: "vector-store-abc123")
779+
```
780+
781+
You can delete a vector store file:
782+
783+
```ruby
784+
client.vector_store_files.delete(
785+
vector_store_id: "vector-store-abc123",
786+
id: vector_store_file_id
787+
)
788+
```
789+
Note: This will remove the file from the vector store but the file itself will not be deleted. To delete the file, use the delete file endpoint.
790+
791+
### Vector Store File Batches
792+
Vector store file batches represent operations to add multiple files to a vector store.
793+
794+
You can create a new vector store file batch by attaching multiple Files to a vector store.
795+
796+
```ruby
797+
response = client.vector_store_file_batches.create(
798+
vector_store_id: "vector-store-abc123",
799+
parameters: {
800+
file_ids: ["file-abc123", "file-def456"]
801+
}
802+
)
803+
804+
file_batch_id = response["id"]
805+
```
806+
807+
Given a `file_batch_id` you can `retrieve` the current field values:
808+
809+
```ruby
810+
client.vector_store_file_batches.retrieve(
811+
vector_store_id: "vector-store-abc123",
812+
id: file_batch_id
813+
)
814+
```
815+
816+
You can get a `list` of all vector store files in a batch currently available under the vector store:
817+
818+
```ruby
819+
client.vector_store_file_batches.list(
820+
vector_store_id: "vector-store-abc123",
821+
id: file_batch_id
822+
)
823+
```
824+
825+
You can cancel a vector store file batch (This attempts to cancel the processing of files in this batch as soon as possible):
826+
827+
```ruby
828+
client.vector_store_file_batches.cancel(
829+
vector_store_id: "vector-store-abc123",
830+
id: file_batch_id
831+
)
832+
```
833+
682834
### Assistants
683835

684836
Assistants are stateful actors that can have many conversations and use tools to perform tasks (see [Assistant Overview](https://platform.openai.com/docs/assistants/overview)).
@@ -694,10 +846,14 @@ response = client.assistants.create(
694846
instructions: "You are a Ruby dev bot. When asked a question, write and run Ruby code to answer the question",
695847
tools: [
696848
{ type: "code_interpreter" },
849+
{ type: "file_search" }
697850
],
698851
tool_resources: {
699-
"code_interpreter": {
700-
"file_ids": [] # See Files section above for how to upload files
852+
code_interpreter: {
853+
file_ids: [] # See Files section above for how to upload files
854+
},
855+
file_search: {
856+
vector_store_ids: [] # See Vector Stores section above for how to add vector stores
701857
}
702858
},
703859
"metadata": { my_internal_version_id: "1.0.0" }

lib/openai.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
require_relative "openai/messages"
1313
require_relative "openai/runs"
1414
require_relative "openai/run_steps"
15+
require_relative "openai/vector_stores"
16+
require_relative "openai/vector_store_files"
17+
require_relative "openai/vector_store_file_batches"
1518
require_relative "openai/audio"
1619
require_relative "openai/version"
1720
require_relative "openai/batches"

lib/openai/client.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ def run_steps
7878
@run_steps ||= OpenAI::RunSteps.new(client: self)
7979
end
8080

81+
def vector_stores
82+
@vector_stores ||= OpenAI::VectorStores.new(client: self)
83+
end
84+
85+
def vector_store_files
86+
@vector_store_files ||= OpenAI::VectorStoreFiles.new(client: self)
87+
end
88+
89+
def vector_store_file_batches
90+
@vector_store_file_batches ||= OpenAI::VectorStoreFileBatches.new(client: self)
91+
end
92+
8193
def batches
8294
@batches ||= OpenAI::Batches.new(client: self)
8395
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module OpenAI
2+
class VectorStoreFileBatches
3+
def initialize(client:)
4+
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
5+
end
6+
7+
def list(vector_store_id:, id:, parameters: {})
8+
@client.get(
9+
path: "/vector_stores/#{vector_store_id}/file_batches/#{id}/files",
10+
parameters: parameters
11+
)
12+
end
13+
14+
def retrieve(vector_store_id:, id:)
15+
@client.get(path: "/vector_stores/#{vector_store_id}/file_batches/#{id}")
16+
end
17+
18+
def create(vector_store_id:, parameters: {})
19+
@client.json_post(
20+
path: "/vector_stores/#{vector_store_id}/file_batches",
21+
parameters: parameters
22+
)
23+
end
24+
25+
def cancel(vector_store_id:, id:)
26+
@client.post(path: "/vector_stores/#{vector_store_id}/file_batches/#{id}/cancel")
27+
end
28+
end
29+
end

lib/openai/vector_store_files.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module OpenAI
2+
class VectorStoreFiles
3+
def initialize(client:)
4+
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
5+
end
6+
7+
def list(vector_store_id:, parameters: {})
8+
@client.get(path: "/vector_stores/#{vector_store_id}/files", parameters: parameters)
9+
end
10+
11+
def retrieve(vector_store_id:, id:)
12+
@client.get(path: "/vector_stores/#{vector_store_id}/files/#{id}")
13+
end
14+
15+
def create(vector_store_id:, parameters: {})
16+
@client.json_post(path: "/vector_stores/#{vector_store_id}/files", parameters: parameters)
17+
end
18+
19+
def delete(vector_store_id:, id:)
20+
@client.delete(path: "/vector_stores/#{vector_store_id}/files/#{id}")
21+
end
22+
end
23+
end

lib/openai/vector_stores.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
module OpenAI
2+
class VectorStores
3+
def initialize(client:)
4+
@client = client.beta(assistants: OpenAI::Assistants::BETA_VERSION)
5+
end
6+
7+
def list(parameters: {})
8+
@client.get(path: "/vector_stores", parameters: parameters)
9+
end
10+
11+
def retrieve(id:)
12+
@client.get(path: "/vector_stores/#{id}")
13+
end
14+
15+
def create(parameters: {})
16+
@client.json_post(path: "/vector_stores", parameters: parameters)
17+
end
18+
19+
def modify(id:, parameters: {})
20+
@client.json_post(path: "/vector_stores/#{id}", parameters: parameters)
21+
end
22+
23+
def delete(id:)
24+
@client.delete(path: "/vector_stores/#{id}")
25+
end
26+
end
27+
end

spec/fixtures/cassettes/vector_store_file_batches_cancel.yml

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)