Class: AiAgent::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ai_agent/base.rb

Direct Known Subclasses

Claude

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



11
12
13
# File 'lib/ai_agent/base.rb', line 11

def initialize
  # be sure to set agent in the subclass initialize method
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



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

def agent
  @agent
end

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'lib/ai_agent/base.rb', line 7

def api_key
  @api_key
end

#endpointObject

Returns the value of attribute endpoint.



8
9
10
# File 'lib/ai_agent/base.rb', line 8

def endpoint
  @endpoint
end

#timeoutObject

Returns the value of attribute timeout.



9
10
11
# File 'lib/ai_agent/base.rb', line 9

def timeout
  @timeout
end

Instance Method Details

#analyze_sentiment(text, strict: true, options: {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ai_agent/base.rb', line 27

def analyze_sentiment(text, strict: true, options: {})
  prompt = "Analyze the sentiment of the following text and classify it as positive, negative, or neutral:\n\n#{text}\n\nSentiment: "
  if strict
    system = "If you are asked to return a word, then return only that word with no preamble or postamble. " if strict
    max_tokens = 2
  else
    max_tokens = 100
  end

  send_messages([ { 'role': 'user', 'content': prompt } ],
                options.reverse_merge( system: system, max_tokens: max_tokens ))
end

#clientObject

Raises:

  • (NotImplementedError)


15
16
17
# File 'lib/ai_agent/base.rb', line 15

def client
  raise NotImplementedError, "Subclasses must implement the client method"
end

#format_response(response) ⇒ Object

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/ai_agent/base.rb', line 23

def format_response(response)
  raise NotImplementedError, "Subclasses must implement the format_response method"
end

#recognize_entities(text, strict: true, options: {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ai_agent/base.rb', line 40

def recognize_entities(text, strict: true, options: {})
  prompt = "Identify and list the named entities in the following text:\n\n#{text}\n\nEntities: "
  if strict
    system = "Be specific in your answer, with no preamble or postamble. If you are asked to list some names, then return only a list of those names, nothing else. "
    max_tokens = 100
  else
    max_tokens = 500
  end

  send_messages([ { 'role': 'user', 'content': prompt } ],
                options.reverse_merge( system: system, max_tokens: max_tokens ))
end

#send_messages(messages, options) ⇒ Object

Raises:

  • (NotImplementedError)


19
20
21
# File 'lib/ai_agent/base.rb', line 19

def send_messages(messages, options)
  raise NotImplementedError, "Subclasses must implement the send_message method"
end

#summarize_text(text, strict: true, options: {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ai_agent/base.rb', line 53

def summarize_text(text, strict: true, options: {})
  prompt = "Summarize the following text:\n\n#{text}\n\nSummary: "
  if strict
    system = "Be specific in your answer, with no preamble or postamble. I.e. return only what the user asks for, nothing else. "
    max_tokens = 100
  else
    max_tokens = 500
  end

  send_messages([ { 'role': 'user', 'content': prompt } ],
                options.reverse_merge( system: system, max_tokens: max_tokens ))
end