Class: AiAgent::Base
- Inherits:
-
Object
- Object
- AiAgent::Base
- Defined in:
- lib/ai_agent/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#agent ⇒ Object
Returns the value of attribute agent.
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#endpoint ⇒ Object
Returns the value of attribute endpoint.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
Instance Method Summary collapse
- #analyze_sentiment(text, strict: true, options: {}) ⇒ Object
- #client ⇒ Object
- #format_response(response) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #recognize_entities(text, strict: true, options: {}) ⇒ Object
- #send_messages(messages, options) ⇒ Object
- #summarize_text(text, strict: true, options: {}) ⇒ Object
Constructor Details
#initialize ⇒ Base
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
#agent ⇒ Object
Returns the value of attribute agent.
6 7 8 |
# File 'lib/ai_agent/base.rb', line 6 def agent @agent end |
#api_key ⇒ Object
Returns the value of attribute api_key.
7 8 9 |
# File 'lib/ai_agent/base.rb', line 7 def api_key @api_key end |
#endpoint ⇒ Object
Returns the value of attribute endpoint.
8 9 10 |
# File 'lib/ai_agent/base.rb', line 8 def endpoint @endpoint end |
#timeout ⇒ Object
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 ([ { 'role': 'user', 'content': prompt } ], .reverse_merge( system: system, max_tokens: max_tokens )) end |
#client ⇒ Object
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
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 ([ { 'role': 'user', 'content': prompt } ], .reverse_merge( system: system, max_tokens: max_tokens )) end |
#send_messages(messages, options) ⇒ Object
19 20 21 |
# File 'lib/ai_agent/base.rb', line 19 def (, ) 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 ([ { 'role': 'user', 'content': prompt } ], .reverse_merge( system: system, max_tokens: max_tokens )) end |