Skip to content

Commit 9340981

Browse files
authored
Merge pull request #583 from jaebrownn/main
Add search method to vector stores
2 parents d1ba292 + b0341a7 commit 9340981

File tree

5 files changed

+241
-3
lines changed

5 files changed

+241
-3
lines changed

README.md

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Ruby OpenAI
2+
23
[![Gem Version](https://img.shields.io/gem/v/ruby-openai.svg)](https://rubygems.org/gems/ruby-openai)
34
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/alexrudall/ruby-openai/blob/main/LICENSE.txt)
45
[![CircleCI Build Status](https://circleci.com/gh/alexrudall/ruby-openai.svg?style=shield)](https://circleci.com/gh/alexrudall/ruby-openai)
@@ -16,7 +17,7 @@ Stream chats with the Responses API, transcribe and translate audio with Whisper
1617
## Contents
1718

1819
- [Ruby OpenAI](#ruby-openai)
19-
- [Table of Contents](#table-of-contents)
20+
- [Contents](#contents)
2021
- [Installation](#installation)
2122
- [Bundler](#bundler)
2223
- [Gem install](#gem-install)
@@ -39,6 +40,13 @@ Stream chats with the Responses API, transcribe and translate audio with Whisper
3940
- [Vision](#vision)
4041
- [JSON Mode](#json-mode)
4142
- [Responses API](#responses-api)
43+
- [Create a Response](#create-a-response)
44+
- [Follow-up Messages](#follow-up-messages)
45+
- [Tool Calls](#tool-calls)
46+
- [Streaming](#streaming)
47+
- [Retrieve a Response](#retrieve-a-response)
48+
- [Delete a Response](#delete-a-response)
49+
- [List Input Items](#list-input-items)
4250
- [Functions](#functions)
4351
- [Completions](#completions)
4452
- [Embeddings](#embeddings)
@@ -70,6 +78,7 @@ Stream chats with the Responses API, transcribe and translate audio with Whisper
7078
- [Usage](#usage)
7179
- [Errors](#errors-1)
7280
- [Development](#development)
81+
- [To check for deprecations](#to-check-for-deprecations)
7382
- [Release](#release)
7483
- [Contributing](#contributing)
7584
- [License](#license)
@@ -88,15 +97,15 @@ gem "ruby-openai"
8897
And then execute:
8998

9099
```bash
91-
$ bundle install
100+
bundle install
92101
```
93102

94103
### Gem install
95104

96105
Or install with:
97106

98107
```bash
99-
$ gem install ruby-openai
108+
gem install ruby-openai
100109
```
101110

102111
and require with:
@@ -472,9 +481,11 @@ You can stream it as well!
472481
```
473482

474483
### Responses API
484+
475485
[OpenAI's most advanced interface for generating model responses](https://platform.openai.com/docs/api-reference/responses). Supports text and image inputs, and text outputs. Create stateful interactions with the model, using the output of previous responses as input. Extend the model's capabilities with built-in tools for file search, web search, computer use, and more. Allow the model access to external systems and data using function calling.
476486

477487
#### Create a Response
488+
478489
```ruby
479490
response = client.responses.create(parameters: {
480491
model: "gpt-4o",
@@ -485,6 +496,7 @@ puts response.dig("output", 0, "content", 0, "text")
485496
```
486497

487498
#### Follow-up Messages
499+
488500
```ruby
489501
followup = client.responses.create(parameters: {
490502
model: "gpt-4o",
@@ -496,6 +508,7 @@ puts followup.dig("output", 0, "content", 0, "text")
496508
```
497509

498510
#### Tool Calls
511+
499512
```ruby
500513
response = client.responses.create(parameters: {
501514
model: "gpt-4o",
@@ -523,6 +536,7 @@ puts response.dig("output", 0, "name")
523536
```
524537

525538
#### Streaming
539+
526540
```ruby
527541
client.responses.create(
528542
parameters: {
@@ -540,20 +554,23 @@ client.responses.create(
540554
```
541555

542556
#### Retrieve a Response
557+
543558
```ruby
544559
retrieved_response = client.responses.retrieve(response_id: response["id"])
545560
puts retrieved_response["object"]
546561
# => "response"
547562
```
548563

549564
#### Delete a Response
565+
550566
```ruby
551567
deletion = client.responses.delete(response_id: response["id"])
552568
puts deletion["deleted"]
553569
# => true
554570
```
555571

556572
#### List Input Items
573+
557574
```ruby
558575
input_items = client.responses.input_items(response_id: response["id"])
559576
puts input_items["object"] # => "list"
@@ -910,6 +927,27 @@ response = client.vector_stores.modify(
910927
)
911928
```
912929

930+
You can search a vector store for relevant chunks based on a query:
931+
932+
```ruby
933+
response = client.vector_stores.search(
934+
id: vector_store_id,
935+
parameters: {
936+
query: "What is the return policy?",
937+
max_num_results: 20,
938+
ranking_options: {
939+
# Add any ranking options here in line with the API documentation
940+
},
941+
rewrite_query: true,
942+
filters: {
943+
type: "eq",
944+
property: "region",
945+
value: "us"
946+
}
947+
}
948+
)
949+
```
950+
913951
You can delete vector stores:
914952

915953
```ruby
@@ -1588,6 +1626,7 @@ File.binwrite('demo.mp3', response)
15881626
```
15891627

15901628
### Usage
1629+
15911630
The Usage API provides information about the cost of various OpenAI services within your organization.
15921631
To use Admin APIs like Usage, you need to set an OPENAI_ADMIN_TOKEN, which can be generated [here](https://platform.openai.com/settings/organization/admin-keys).
15931632

lib/openai/vector_stores.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,9 @@ def modify(id:, parameters: {})
2323
def delete(id:)
2424
@client.delete(path: "/vector_stores/#{id}")
2525
end
26+
27+
def search(id:, parameters: {})
28+
@client.json_post(path: "/vector_stores/#{id}/search", parameters: parameters)
29+
end
2630
end
2731
end

spec/fixtures/cassettes/vector_stores_search.yml

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

spec/fixtures/cassettes/vector_stores_search_setup.yml

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

spec/openai/client/vector_stores_spec.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,25 @@
7272
end
7373
end
7474
end
75+
76+
describe "#search" do
77+
let(:cassette) { "vector_stores search" }
78+
let(:response) do
79+
OpenAI::Client.new.vector_stores.search(
80+
id: vector_store_id,
81+
parameters: {
82+
query: "Test search query",
83+
max_num_results: 5,
84+
rewrite_query: false
85+
}
86+
)
87+
end
88+
89+
it "succeeds" do
90+
VCR.use_cassette(cassette) do
91+
expect(response["object"]).to eq("vector_store.search_results.page")
92+
end
93+
end
94+
end
7595
end
7696
end

0 commit comments

Comments
 (0)