active_pinecone 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 810ca56ec86165e1c43932c6f26a7e0909f75ba68e3242d16e48d443de91a69a
4
- data.tar.gz: 35e89c03e5e0d13b68e0aa0c43082669dc7c094fdbb8a1471eebee3c16df4772
3
+ metadata.gz: ac83a40bff569558fe83e8b92eef9f9f81fc152a92f69a101dfde763f633c108
4
+ data.tar.gz: 0cf15188e17fae9e367024c4197168f3620205fe24719ab095c41a150cca6176
5
5
  SHA512:
6
- metadata.gz: 905d43792ce720898bde25b3f599ecb18286b250d53ae50354aae86c1ddbba6d07ca7210a6ee2a3719bb8b8b00f84c0b6d35f5a26de5a5f42781c5a56e569c5c
7
- data.tar.gz: d1357320f74503960717be02a51e1aae21ceaf9d7326d7b9cc41f920a76234683b25a63a20f60a0623cb158ba06eb83b5839b7839fddec76c37f37c26d40ae08
6
+ metadata.gz: efafb2f1c7a68b2e4fa3a9f08668038595f126cbf41f191e929f2645cb413968e0c6786681fe5d65298dcf5b3efda20b876875fd6ce889a51c546c71f6ddf8e2
7
+ data.tar.gz: c01271f4b41521e3a259666c8937c60dd16a204ea663fd3fa4a73b0fc87a1fa90d75dad941ff251e5fd84097b3a7597b168b6c99555499d302f7de321f5c65d0
data/CHANGELOG.md CHANGED
@@ -3,3 +3,8 @@
3
3
  ## [0.1.0] - 2023-06-24
4
4
 
5
5
  - Initial release
6
+
7
+ ## [0.1.1] - 2023-05-24
8
+
9
+ - Bug fix
10
+ - Add example
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- active_pinecone (0.1.0)
4
+ active_pinecone (0.1.1)
5
5
  activesupport (>= 5.0.0)
6
6
  pinecone (~> 0.1.5)
7
7
  ruby-openai (~> 4.1.0)
data/README.md CHANGED
@@ -37,6 +37,15 @@ class Recipe < ActivePinecone::Base
37
37
  end
38
38
  ```
39
39
 
40
+ ### Initialize
41
+
42
+ Create Pinecone index. Index initialization takes a few minutes.
43
+
44
+ ```ruby
45
+ Post.init
46
+ ```
47
+
48
+
40
49
  ### Create
41
50
 
42
51
  Pinecone index `Recipe` is created automatically.
data/examples/main.rb ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'post'
4
+
5
+ Post.init
6
+
7
+ post = Post.create(title: 'This is An Example', body: 'This is an example', author: 'Kevin')
8
+
9
+ p post.title #=> 'This is An Example'
10
+
11
+ p post.author #=> 'Kevin'
12
+
13
+ post.update(body: 'This will be changed')
14
+
15
+ posts = Post.search('example')
16
+
17
+ posts.each do |pt|
18
+ p pt.title #=> 'This is An Example'
19
+ end
20
+
21
+ assistant = Post.assistant
22
+
23
+ p assistant.reply('hoge')
24
+
25
+ p assistant.messages
data/examples/post.rb ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_pinecone'
4
+
5
+ ActivePinecone.configure do |config|
6
+ config.openai_access_token = ENV.fetch('OPENAI_ACCESS_TOKEN')
7
+ config.pinecone_api_key = ENV.fetch('PINECONE_API_KEY')
8
+ config.pinecone_environment = ENV.fetch('PINECONE_ENVIRONMENT')
9
+ end
10
+
11
+ class Post < ActivePinecone::Base
12
+ vectorizes :title, :body
13
+ end
@@ -17,7 +17,7 @@ module ActivePinecone
17
17
  end
18
18
 
19
19
  def self.index_name
20
- to_s
20
+ to_s.downcase.pluralize
21
21
  end
22
22
 
23
23
  def self.pinecone
@@ -33,7 +33,8 @@ module ActivePinecone
33
33
  end
34
34
 
35
35
  def self.embed(input)
36
- openai.embeddings(parameters: { model: ActivePinecone.configuration.embedding_model, input: input })
36
+ embeded = openai.embeddings(parameters: { model: ActivePinecone.configuration.embedding_model, input: input })
37
+ embeded['data'][0]['embedding']
37
38
  end
38
39
 
39
40
  def self.vectorized_attributes
@@ -49,6 +50,10 @@ module ActivePinecone
49
50
  @@vectorized_attributes += attr_names
50
51
  end
51
52
 
53
+ def self.init
54
+ pinecone.create_index({ name: index_name, dimension: ActivePinecone.configuration.dimension, metrics: ActivePinecone.configuration.metrics })
55
+ end
56
+
52
57
  def self.create(**attributes)
53
58
  id = attributes[:id] || SecureRandom.uuid
54
59
  index.upsert(vectors: [{ id: id, metadata: attributes, values: embed(attributes.slice(vectorized_attributes).to_json) }])
@@ -57,15 +62,11 @@ module ActivePinecone
57
62
 
58
63
  def self.delete(id)
59
64
  index.delete(ids: [id])
60
- rescue ::Pinecone::IndexNotFoundError
61
- pinecone.create_index({ name: index_name, dimension: ActivePinecone.configuration.dimension, metrics: ActivePinecone.configuration.metrics })
62
65
  end
63
66
 
64
67
  def self.find(id)
65
68
  fetched = index.fetch(ids: [id])
66
69
  new(fetched['vectors'][id]['metadata'].merge(id: id).symbolize_keys)
67
- rescue ::Pinecone::IndexNotFoundError
68
- pinecone.create_index({ name: index_name, dimension: ActivePinecone.configuration.dimension, metrics: ActivePinecone.configuration.metrics })
69
70
  end
70
71
 
71
72
  def self.search(text, filter: {})
@@ -73,15 +74,11 @@ module ActivePinecone
73
74
  searched['matches'].map do |match|
74
75
  new(match['metadata'].merge(id: match['id']).symbolize_keys)
75
76
  end
76
- rescue ::Pinecone::IndexNotFoundError
77
- pinecone.create_index({ name: index_name, dimension: ActivePinecone.configuration.dimension, metrics: ActivePinecone.configuration.metrics })
78
77
  end
79
78
 
80
79
  def update(**attributes)
81
80
  self.class.index.upsert(vectors: [{ id: id, metadata: attributes, values: self.class.embed(attributes.slice(self.class.vectorized_attributes).to_json) }])
82
81
  @attributes = attributes
83
- rescue ::Pinecone::IndexNotFoundError
84
- pinecone.create_index({ name: index_name, dimension: ActivePinecone.configuration.dimension, metrics: ActivePinecone.configuration.metrics })
85
82
  end
86
83
  end
87
84
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActivePinecone
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_pinecone
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moeki Kawakami
@@ -68,6 +68,8 @@ files:
68
68
  - README.md
69
69
  - Rakefile
70
70
  - active_pinecone.gemspec
71
+ - examples/main.rb
72
+ - examples/post.rb
71
73
  - lib/active_pinecone.rb
72
74
  - lib/active_pinecone/assistant.rb
73
75
  - lib/active_pinecone/base.rb