Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Ruby OpenAI

[![Gem Version](https://img.shields.io/gem/v/ruby-openai.svg)](https://rubygems.org/gems/ruby-openai)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/alexrudall/ruby-openai/blob/main/LICENSE.txt)
[![CircleCI Build Status](https://circleci.com/gh/alexrudall/ruby-openai.svg?style=shield)](https://circleci.com/gh/alexrudall/ruby-openai)
Expand All @@ -16,7 +17,7 @@ Stream chats with the Responses API, transcribe and translate audio with Whisper
## Contents

- [Ruby OpenAI](#ruby-openai)
- [Table of Contents](#table-of-contents)
- [Contents](#contents)
- [Installation](#installation)
- [Bundler](#bundler)
- [Gem install](#gem-install)
Expand All @@ -39,6 +40,13 @@ Stream chats with the Responses API, transcribe and translate audio with Whisper
- [Vision](#vision)
- [JSON Mode](#json-mode)
- [Responses API](#responses-api)
- [Create a Response](#create-a-response)
- [Follow-up Messages](#follow-up-messages)
- [Tool Calls](#tool-calls)
- [Streaming](#streaming)
- [Retrieve a Response](#retrieve-a-response)
- [Delete a Response](#delete-a-response)
- [List Input Items](#list-input-items)
- [Functions](#functions)
- [Completions](#completions)
- [Embeddings](#embeddings)
Expand Down Expand Up @@ -70,6 +78,7 @@ Stream chats with the Responses API, transcribe and translate audio with Whisper
- [Usage](#usage)
- [Errors](#errors-1)
- [Development](#development)
- [To check for deprecations](#to-check-for-deprecations)
- [Release](#release)
- [Contributing](#contributing)
- [License](#license)
Expand All @@ -88,15 +97,15 @@ gem "ruby-openai"
And then execute:

```bash
$ bundle install
bundle install
```

### Gem install

Or install with:

```bash
$ gem install ruby-openai
gem install ruby-openai
```

and require with:
Expand Down Expand Up @@ -472,9 +481,11 @@ You can stream it as well!
```

### Responses API

[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.

#### Create a Response

```ruby
response = client.responses.create(parameters: {
model: "gpt-4o",
Expand All @@ -485,6 +496,7 @@ puts response.dig("output", 0, "content", 0, "text")
```

#### Follow-up Messages

```ruby
followup = client.responses.create(parameters: {
model: "gpt-4o",
Expand All @@ -496,6 +508,7 @@ puts followup.dig("output", 0, "content", 0, "text")
```

#### Tool Calls

```ruby
response = client.responses.create(parameters: {
model: "gpt-4o",
Expand Down Expand Up @@ -523,6 +536,7 @@ puts response.dig("output", 0, "name")
```

#### Streaming

```ruby
client.responses.create(
parameters: {
Expand All @@ -540,20 +554,23 @@ client.responses.create(
```

#### Retrieve a Response

```ruby
retrieved_response = client.responses.retrieve(response_id: response["id"])
puts retrieved_response["object"]
# => "response"
```

#### Delete a Response

```ruby
deletion = client.responses.delete(response_id: response["id"])
puts deletion["deleted"]
# => true
```

#### List Input Items

```ruby
input_items = client.responses.input_items(response_id: response["id"])
puts input_items["object"] # => "list"
Expand Down Expand Up @@ -910,6 +927,27 @@ response = client.vector_stores.modify(
)
```

You can search a vector store for relevant chunks based on a query:

```ruby
response = client.vector_stores.search(
id: vector_store_id,
parameters: {
query: "What is the return policy?",
max_num_results: 20,
ranking_options: {
# Add any ranking options here in line with the API documentation
},
rewrite_query: true,
filters: {
type: "eq",
property: "region",
value: "us"
}
}
)
```

You can delete vector stores:

```ruby
Expand Down Expand Up @@ -1588,6 +1626,7 @@ File.binwrite('demo.mp3', response)
```

### Usage

The Usage API provides information about the cost of various OpenAI services within your organization.
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).

Expand Down
4 changes: 4 additions & 0 deletions lib/openai/vector_stores.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@ def modify(id:, parameters: {})
def delete(id:)
@client.delete(path: "/vector_stores/#{id}")
end

def search(id:, parameters: {})
@client.json_post(path: "/vector_stores/#{id}/search", parameters: parameters)
end
end
end
100 changes: 100 additions & 0 deletions spec/fixtures/cassettes/vector_stores_search.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions spec/fixtures/cassettes/vector_stores_search_setup.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions spec/openai/client/vector_stores_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,25 @@
end
end
end

describe "#search" do
let(:cassette) { "vector_stores search" }
let(:response) do
OpenAI::Client.new.vector_stores.search(
id: vector_store_id,
parameters: {
query: "Test search query",
max_num_results: 5,
rewrite_query: false
}
)
end

it "succeeds" do
VCR.use_cassette(cassette) do
expect(response["object"]).to eq("vector_store.search_results.page")
end
end
end
end
end