Class: Plain::Conversation

Inherits:
ApplicationRecord show all
Defined in:
app/models/plain/conversation.rb

Instance Method Summary collapse

Instance Method Details

#add_assistant_response(question) ⇒ Object



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

  # plain_answer = vector_search.ask(question: @question)
  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.set_functions(Plain::AiDocs.functions())
  chat.message(question)
end

#add_assistant_response_async(question) ⇒ Object



44
45
46
# File 'app/models/plain/conversation.rb', line 44

def add_assistant_response_async(question)
  Plain::MessageProcessorJob.perform_later(self, question: question)
end

#add_subject_from_summaryObject



48
49
50
# File 'app/models/plain/conversation.rb', line 48

def add_subject_from_summary
  Plain::AiDocs.set_conversation_title(self)
end