Method: Langchain::Vectorsearch::Milvus#ask

Defined in:
lib/langchain/vectorsearch/milvus.rb

#ask(question:, k: 4) {|String| ... } ⇒ String

Ask a question and return the answer

Parameters:

  • question (String)

    The question to ask

  • k (Integer) (defaults to: 4)

    The number of results to have in context

Yields:

  • (String)

    Stream responses back one String at a time

Returns:

  • (String)

    The answer to the question



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/langchain/vectorsearch/milvus.rb', line 144

def ask(question:, k: 4, &block)
  search_results = similarity_search(query: question, k: k)

  content_data = search_results.dig("data").map { |result| result.dig("content") }

  context = content_data.join("\n---\n")

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

  messages = [{role: "user", content: prompt}]
  response = llm.chat(messages: messages, &block)

  response.context = context
  response
end