Class: Vectorsearch::Weaviate

Inherits:
Base
  • Object
show all
Defined in:
lib/vectorsearch/weaviate.rb

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

Methods inherited from Base

#generate_completion, #generate_embedding, #generate_prompt

Constructor Details

#initialize(url:, api_key:, index_name:, llm:, llm_api_key:) ⇒ Weaviate



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/vectorsearch/weaviate.rb', line 7

def initialize(
  url:,
  api_key:,
  index_name:,
  llm:,
  llm_api_key:
)
  @client = ::Weaviate::Client.new(
    url: url,
    api_key: api_key,
    model_service: llm,
    model_service_api_key: llm_api_key
  )
  @index_name = index_name

  super(llm: llm, llm_api_key: llm_api_key)
end

Instance Method Details

#add_texts(texts:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/vectorsearch/weaviate.rb', line 25

def add_texts(
  texts:
)
  objects = []  
  texts.each do |text|
    objects.push({
      class_name: index_name,
      properties: {
        content: text
      }
    })
  end

  client.objects.batch_create(
    objects: objects
  )
end

#ask(question:) ⇒ Hash

Ask a question and return the answer



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/vectorsearch/weaviate.rb', line 95

def ask(
  question:
)
  # Weaviate currently supports the `ask:` parameter only for the OpenAI LLM (with `qna-openai` module enabled).
  if llm == :openai
    ask_object = "{ question: \"#{question}\" }"

    client.query.get(
      class_name: index_name,
      ask: ask_object,
      limit: "1",
      fields: "_additional { answer { result } }"
    )
  elsif llm == :cohere
    search_results = similarity_search(query: question)

    context = search_results.map do |result|
      result.dig("content").to_s
    end
    context = context.join("\n---\n")

    prompt = generate_prompt(question: question, context: context)

    generate_completion(prompt: prompt)
  end
end

#create_default_schemaObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/vectorsearch/weaviate.rb', line 43

def create_default_schema
  client.schema.create(
    class_name: index_name,
    vectorizer: "text2vec-#{llm.to_s}",
    properties: [
      {
        dataType: ["text"],
        name: "content"
      }
    ]
  )
end

#similarity_search(query:, k: 4) ⇒ Hash

Return documents similar to the query



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/vectorsearch/weaviate.rb', line 60

def similarity_search(
  query:,
  k: 4
)
  near_text = "{ concepts: [\"#{query}\"] }"

  client.query.get(
    class_name: index_name,
    near_text: near_text,
    limit: k.to_s,
    fields: "content _additional { id }"
  )
end

#similarity_search_by_vector(embedding:, k: 4) ⇒ Hash

Return documents similar to the vector



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/vectorsearch/weaviate.rb', line 78

def similarity_search_by_vector(
  embedding:,
  k: 4
)
  near_vector = "{ vector: #{embedding} }"

  client.query.get(
    class_name: index_name,
    near_vector: near_vector,
    limit: k.to_s,
    fields: "content recipe_id"
  )
end