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 +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/README.md +9 -0
- data/examples/main.rb +25 -0
- data/examples/post.rb +13 -0
- data/lib/active_pinecone/base.rb +7 -10
- data/lib/active_pinecone/version.rb +1 -1
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac83a40bff569558fe83e8b92eef9f9f81fc152a92f69a101dfde763f633c108
|
4
|
+
data.tar.gz: 0cf15188e17fae9e367024c4197168f3620205fe24719ab095c41a150cca6176
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: efafb2f1c7a68b2e4fa3a9f08668038595f126cbf41f191e929f2645cb413968e0c6786681fe5d65298dcf5b3efda20b876875fd6ce889a51c546c71f6ddf8e2
|
7
|
+
data.tar.gz: c01271f4b41521e3a259666c8937c60dd16a204ea663fd3fa4a73b0fc87a1fa90d75dad941ff251e5fd84097b3a7597b168b6c99555499d302f7de321f5c65d0
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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
|
data/lib/active_pinecone/base.rb
CHANGED
@@ -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
|
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.
|
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
|