Class: AiAgent::Base

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

Direct Known Subclasses

Claude, OpenAI

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



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/ai_agent/base.rb', line 30

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: "
  messages = [{ 'role': 'user', 'content': prompt }]
  system = options.delete(:system)
  if strict
    system = ["If you are asked to return a word, then return only that word with no preamble or postamble.", system].compact.join(' ')
    messages.prepend({ 'role': 'system', 'content': system })
    max_tokens = 2
  else
    max_tokens = 100
  end

  chat(messages, options: options.reverse_merge( max_tokens: max_tokens ))
end

#chat(messages, options: {}) ⇒ Object

messages should be an array of hashes, where each hash contains role and content. role can be ‘system’, ‘assistant’, or ‘user’. e.g. [{ ‘role’: ‘system’, ‘content’: ‘You are a helpful assistant’ }, { ‘role’: ‘user’, ‘content’: ‘Tell me a joke’ }]

Raises:

  • (NotImplementedError)


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

def chat(messages, options: {})
  raise NotImplementedError, "Subclasses must implement the chat (completions) method"
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)


26
27
28
# File 'lib/ai_agent/base.rb', line 26

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

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



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ai_agent/base.rb', line 45

def recognize_entities(text, strict: true, options: {})
  prompt = "Identify and list the named entities in the following text:\n\n#{text}\n\nEntities: "
  messages = [{ 'role': 'user', 'content': prompt }]
  system = options.delete(:system)
  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. ", system].compact.join(' ')
    messages.prepend({ 'role': 'system', 'content': system })
    max_tokens = 100
  else
    max_tokens = 500
  end

  chat(messages, options: options.reverse_merge( max_tokens: max_tokens ))
end

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



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ai_agent/base.rb', line 60

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

  chat(messages, options: options.reverse_merge( max_tokens: max_tokens ))
end