Class: ActivePinecone::Assistant

Inherits:
Object
  • Object
show all
Defined in:
lib/active_pinecone/assistant.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, instruction: 'Context:') ⇒ Assistant

Returns a new instance of Assistant.



5
6
7
8
9
# File 'lib/active_pinecone/assistant.rb', line 5

def initialize(model, instruction: 'Context:')
  @model = model
  @instruction = instruction
  @messages_with_system = [{ role: 'system', content: '' }]
end

Instance Attribute Details

#instructionObject (readonly)

Returns the value of attribute instruction.



3
4
5
# File 'lib/active_pinecone/assistant.rb', line 3

def instruction
  @instruction
end

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/active_pinecone/assistant.rb', line 3

def model
  @model
end

Class Method Details

.openaiObject



11
12
13
# File 'lib/active_pinecone/assistant.rb', line 11

def self.openai
  @openai ||= ActivePinecone::Openai.new
end

Instance Method Details

#messagesObject



15
16
17
# File 'lib/active_pinecone/assistant.rb', line 15

def messages
  @messages_with_system.drop(1)
end

#reply(message) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/active_pinecone/assistant.rb', line 19

def reply(message)
  @messages_with_system << { role: 'user', content: message }

  records = model.search(messages.pluck(:content))

  @messages_with_system[0][:content] = [instruction, records.map(&:attributes).to_json].join("\n")

  result = self.class.openai.chat(
    parameters: { model: ActivePinecone.configuration.chat_model, messages: messages, temperature: 0.7 }
  )
  rep = ActivePinecone::Message.new(role: 'assistant', content: result['choices'][0]['message']['content'], references: messages)
  @messages_with_system << rep
  rep
end