Class: Vectorsearch::Pinecone
Constant Summary
Constants inherited from Base
Base::DEFAULT_COHERE_DIMENSION, Base::DEFAULT_METRIC, Base::DEFAULT_OPENAI_DIMENSION, Base::LLMS
Instance Attribute Summary
Attributes inherited from Base
#client, #index_name, #llm, #llm_api_key
Instance Method Summary collapse
-
#add_texts(texts:) ⇒ Hash
Add a list of texts to the index.
- #ask(question:) ⇒ Object
-
#create_default_schema ⇒ Hash
Create the index with the default schema.
-
#initialize(environment:, api_key:, index_name:, llm:, llm_api_key:) ⇒ Pinecone
constructor
A new instance of Pinecone.
- #similarity_search(query:, k: 4) ⇒ Object
- #similarity_search_by_vector(embedding:, k: 4) ⇒ Object
Methods inherited from Base
#generate_completion, #generate_embedding, #generate_prompt
Constructor Details
#initialize(environment:, api_key:, index_name:, llm:, llm_api_key:) ⇒ Pinecone
Returns a new instance of Pinecone.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/vectorsearch/pinecone.rb', line 7 def initialize( environment:, api_key:, index_name:, llm:, llm_api_key: ) ::Pinecone.configure do |config| config.api_key = api_key config.environment = environment end @client = ::Pinecone::Client.new @index_name = index_name super(llm: llm, llm_api_key: llm_api_key) end |
Instance Method Details
#add_texts(texts:) ⇒ Hash
Add a list of texts to the index
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vectorsearch/pinecone.rb', line 28 def add_texts( texts: ) vectors = texts.map do |text| { # TODO: Allows passing in your own IDs id: SecureRandom.uuid, metadata: { content: text }, values: (text: text) } end index = client.index(index_name) index.upsert(vectors: vectors) end |
#ask(question:) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/vectorsearch/pinecone.rb', line 82 def ask(question:) search_results = similarity_search(query: question) context = search_results.dig("matches").map do |result| result.dig("metadata").to_s end context = context.join("\n---\n") prompt = generate_prompt(question: question, context: context) generate_completion(prompt: prompt) end |
#create_default_schema ⇒ Hash
Create the index with the default schema
47 48 49 50 51 52 53 |
# File 'lib/vectorsearch/pinecone.rb', line 47 def create_default_schema client.create_index( metric: DEFAULT_METRIC, name: index_name, dimension: default_dimension ) end |
#similarity_search(query:, k: 4) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vectorsearch/pinecone.rb', line 55 def similarity_search( query:, k: 4 ) = (text: query) similarity_search_by_vector( embedding: , k: k ) end |
#similarity_search_by_vector(embedding:, k: 4) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/vectorsearch/pinecone.rb', line 67 def similarity_search_by_vector( embedding:, k: 4 ) index = client.index(index_name) response = index.query( vector: , top_k: k, include_values: true, include_metadata: true ) response.dig("matches") end |