Class: IntelliAgent

Inherits:
Object
  • Object
show all
Extended by:
AI
Defined in:
lib/intelli_agent.rb

Constant Summary

Constants included from AI

AI::ADVANCED_MODEL, AI::BASIC_MODEL

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AI

chat, embed, models, single_chat, single_prompt, vision

Constructor Details

#initialize(assistant_id: nil, thread_id: nil, thread_instructions: nil, vector_store_id: nil) ⇒ IntelliAgent

Returns a new instance of IntelliAgent.



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

def initialize(assistant_id: nil, thread_id: nil, thread_instructions: nil, vector_store_id: nil)
  @openai_client = OpenAI::Client.new

  assistant_id ||= ENV.fetch('OPENAI_ASSISTANT_ID')
  @assistant = @openai_client.assistants.retrieve(id: assistant_id)

  thread_params = {}

  # Only one vector store can be attached, according to the OpenAI API documentation
  @vector_store_id = vector_store_id
  thread_params = { tool_resources: { file_search: { vector_store_ids: [vector_store_id] } } } if @vector_store_id

  thread_id ||= @openai_client.threads.create(parameters: thread_params)['id']
  @thread = @openai_client.threads.retrieve(id: thread_id)

  @instructions = thread_instructions || @assistant['instructions']
end

Instance Attribute Details

#assistantObject (readonly)

Returns the value of attribute assistant.



6
7
8
# File 'lib/intelli_agent.rb', line 6

def assistant
  @assistant
end

#instructionsObject (readonly)

Returns the value of attribute instructions.



6
7
8
# File 'lib/intelli_agent.rb', line 6

def instructions
  @instructions
end

#threadObject (readonly)

Returns the value of attribute thread.



6
7
8
# File 'lib/intelli_agent.rb', line 6

def thread
  @thread
end

#vector_store_idObject (readonly)

Returns the value of attribute vector_store_id.



6
7
8
# File 'lib/intelli_agent.rb', line 6

def vector_store_id
  @vector_store_id
end

Instance Method Details

#add_message(text, role: 'user') ⇒ Object



26
# File 'lib/intelli_agent.rb', line 26

def add_message(text, role: 'user') = @openai_client.messages.create(thread_id: @thread['id'], parameters: { role: role, content: text })

#last_messageObject



28
# File 'lib/intelli_agent.rb', line 28

def last_message = messages['data'].first['content'].first['text']['value']

#messagesObject



27
# File 'lib/intelli_agent.rb', line 27

def messages = @openai_client.messages.list(thread_id: @thread['id'])

#run(instructions: nil, additional_instructions: nil, additional_message: nil, model: nil, tool_choice: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/intelli_agent.rb', line 31

def run(instructions: nil, additional_instructions: nil, additional_message: nil, model: nil, tool_choice: nil)
  params = { assistant_id: @assistant['id'] }

  params[:instructions] = instructions || @instructions
  params[:additional_instructions] = additional_instructions unless additional_instructions.nil?
  params[:tool_choice] = tool_choice unless tool_choice.nil?

  params[:additional_messages] = [{ role: :user, content: additional_message }] unless additional_message.nil?

  params[:model] = model || @assistant['model']

  run_id = @openai_client.runs.create(thread_id: @thread['id'], parameters: params)['id']

  loop do
    response = @openai_client.runs.retrieve(id: run_id, thread_id: @thread['id'])

    case response['status']
    when 'queued', 'in_progress', 'cancelling'
      puts 'Status: Waiting AI Processing finish'
      sleep 1
    when 'completed'
      puts last_message
      break
    when 'requires_action'
      # Handle tool calls (see below)
    when 'cancelled', 'failed', 'expired'
      puts response['last_error'].inspect
      break # or `exit`
    else
      puts "Unknown status response: #{status}"
    end
  end
end

#runsObject



29
# File 'lib/intelli_agent.rb', line 29

def runs = @openai_client.runs.list(thread_id: @thread['id'])