5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
# File 'app/models/plain/conversation.rb', line 5
def add_assistant_response(question)
ai_docs = Plain::AiDocs.new
vector_search = ai_docs.client
complete_response = ""
message = messages.new(role: "assistant", content: "")
chat = Plain::AiDocs.new.conversation_client do |chunk|
puts "****"
p chunk
puts "****"
next if chunk.content.nil?
complete_response << chunk.content
message.content = complete_response
message.save
end
search_results = vector_search.similarity_search(query: question)
context = search_results.map do |result|
result.dig("payload", "content").to_s
end
plain_answer = context.join("\n---\n")
chat.add_examples(messages) if messages.any?
Plain::AiDocs.set_conversation_title(self) if messages.size >= 4 && subject.blank?
puts "CONTEXT"
context = "The following is the context of my application, please respond accordingly with this context: \r\n" << plain_answer << "end of context"
puts context
chat.set_context(context)
chat.message(question)
end
|