Skip to content

Ruby library for interacting with the Pinecone Vector Database

License

Notifications You must be signed in to change notification settings

hornet-network/ruby-pinecone

Repository files navigation

Ruby Pinecone

Rather use the more full featured libary

This was built to hit a deadline.
Please rather use https://github.com/ScotterC/pinecone It's well maintained and full featured.

Description

Ruby library to make interacting with the Pinecone Vector Database API easier. Format and workings inspired by https://github.com/alexrudall/ruby-openai

Installation

Add this line to your application's Gemfile:

gem 'ruby-pinecone'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install ruby-pinecone

Usage

Quickstart

For a quick test you can pass your token directly to a new client:

client = Pinecone::Client.new(api_key: "access_token_goes_here", environment: "us-east1-gcp")

With Config

For a more robust setup, you can configure the gem with your API keys, for example in a pinecone.rb initializer. Never hardcode secrets into your codebase - instead use something like dotenv to pass the keys safely into your environments.

Pinecone.configuration.api_key = ENV.fetch('PINECONE_API_KEY')
Pinecone.configuration.environment = ENV.fetch('PINECONE_ENVIRONMENT')

Then you can create a client like this:

client = Pinecone::Client.new

Indexes

View Indexes

client = Pinecone::Client.new

# Load all indexes
indexes = client.indexes.list

# Load specific index
index = client.indexes['my-index']

index.describe
# => {"database"=>{"name"=>"my-index", "metric"=>"cosine"...}}

index.describe_index_stats
#=> {"namespaces"=>{}, "dimension"=>1536, "indexFullness"=>0, "totalVectorCount"=>0}

Create Index

index = client.indexes.create(name: 'my-new-index', params: {dimension: 1536})

Delete Index

index = client.indexes['my-index']
index.delete

Update an Index

index = client.indexes['my-index']
index.configure(params: {pod_type: 's1.x2'})

Manage Vectors

Upsert
client = Pinecone::Client.new
index = client.indexes['my-index']

body = {
  vectors: [
    {
      id: 'id-1',
      values: [0.1, 0.1, 0.1, 0.1, 0.1],
      metadata: {genre: 'comedy', year: 2020}
    },
    {
      id: 'id-2',
      values: [0.2, 0.2, 0.2, 0.2, 0.2],
      metadata: {genre: 'drama'}
    },
  ]
}

index.vectors.upsert(body)
Delete
client = Pinecone::Client.new
index = client.indexes['my-index']
index.vectors.delete(ids: ['id-1', 'id-2'])

Query

client = Pinecone::Client.new
index = client.indexes['my-index']

options = {
  "includeValues": false,
  "includeMetadata": true,
  "topK": 10,
}
index.query([1.0, 1.0], options: options)
# => {"results"=>[], "matches"=>[{"id"=>"id-1", "score"=>1, "values"=>[], "metadata"=>{"type"=>"Thing"}}, {"id"=>"id-2", "score"=>0.89825207, "values"=>[], "metadata"=>{"type"=>"Thing"}}], "namespace"=>""}

index.query(nil, options: options)
# => raise QueryError (Query failed with 400 - {"code"=>3, "message"=>"No query provided", "details"=>[]})

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/ruby-pinecone. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

The gem is available as open source under the terms of the MIT License.

Code of Conduct

Everyone interacting in the Ruby Pinecone project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

About

Ruby library for interacting with the Pinecone Vector Database

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published