Class: AiAgent::Base
- Inherits:
-
Object
- Object
- AiAgent::Base
- Defined in:
- lib/ai_agent/base.rb
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
-
#chat(messages, options: {}) ⇒ Object
messages should be an array of hashes, where each hash contains role and content.
- #client ⇒ Object
- #format_response(response) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #recognize_entities(text, strict: true, 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
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: " = [{ 'role': 'user', 'content': prompt }] system = .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(' ') .prepend({ 'role': 'system', 'content': system }) max_tokens = 2 else max_tokens = 100 end chat(, 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’ }]
22 23 24 |
# File 'lib/ai_agent/base.rb', line 22 def chat(, options: {}) raise NotImplementedError, "Subclasses must implement the chat (completions) method" 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
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: " = [{ 'role': 'user', 'content': prompt }] system = .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(' ') .prepend({ 'role': 'system', 'content': system }) max_tokens = 100 else max_tokens = 500 end chat(, 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: " = [{ 'role': 'user', 'content': prompt }] system = .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(' ') .prepend({ 'role': 'system', 'content': system }) max_tokens = 100 else max_tokens = 500 end chat(, options: .reverse_merge( max_tokens: max_tokens )) end |