Class: Hellm::Agent
- Inherits:
-
Object
- Object
- Hellm::Agent
- Defined in:
- lib/hellm/agent.rb
Instance Attribute Summary collapse
-
#agents ⇒ Object
Returns the value of attribute agents.
-
#description ⇒ Object
Returns the value of attribute description.
-
#instructions ⇒ Object
instructions (string) for the agent.
-
#llm ⇒ Object
Returns the value of attribute llm.
-
#name ⇒ Object
Returns the value of attribute name.
- #stream_adapter ⇒ Object
-
#tools ⇒ Object
array of tools (Langchain::Tool) for the agent to use.
Instance Method Summary collapse
- #add(content: nil, image: nil) ⇒ Object
-
#initialize(model: nil, name: nil, description: nil, stream_adapter: nil, openai_api_key: nil, agents: nil, tools: nil, instructions: nil) ⇒ Agent
constructor
A new instance of Agent.
- #new_session ⇒ Object
- #response ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize(model: nil, name: nil, description: nil, stream_adapter: nil, openai_api_key: nil, agents: nil, tools: nil, instructions: nil) ⇒ Agent
Returns a new instance of Agent.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/hellm/agent.rb', line 31 def initialize(model: nil, name: nil, description: nil, stream_adapter: nil, openai_api_key: nil, agents: nil, tools: nil, instructions: nil) model ||= "gpt-5-nano" Hellm.logger.debug("Initializing agent #{name} (#{model})") @llm = Langchain::LLM::OpenAI.new( api_key: openai_api_key || ::Hellm.openai_api_key, default_options: {chat_model: model} ) self.name = name || self.class.name self.description = description || "" self.instructions = instructions || "" self.stream_adapter = stream_adapter || Hellm::StreamAdapter.new self.agents = agents || [] self.tools = tools || [] # remove "agent" from tools, this ability is controlled by the presence of agents self.tools.reject!{|t| t == "agent"} end |
Instance Attribute Details
#agents ⇒ Object
Returns the value of attribute agents.
7 8 9 |
# File 'lib/hellm/agent.rb', line 7 def agents @agents end |
#description ⇒ Object
Returns the value of attribute description.
7 8 9 |
# File 'lib/hellm/agent.rb', line 7 def description @description end |
#instructions ⇒ Object
instructions (string) for the agent.
13 14 15 |
# File 'lib/hellm/agent.rb', line 13 def instructions @instructions end |
#llm ⇒ Object
Returns the value of attribute llm.
7 8 9 |
# File 'lib/hellm/agent.rb', line 7 def llm @llm end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/hellm/agent.rb', line 7 def name @name end |
#stream_adapter ⇒ Object
10 11 12 |
# File 'lib/hellm/agent.rb', line 10 def stream_adapter @stream_adapter end |
#tools ⇒ Object
array of tools (Langchain::Tool) for the agent to use
langchain.rb Built-in Tools: - Langchain::Tool::Calculator: Useful for evaluating math expressions. Requires gem "eqn". - Langchain::Tool::Database: Connect your SQL database. Requires gem "sequel". - Langchain::Tool::FileSystem: Interact with the file system (read & write). - Langchain::Tool::GoogleSearch: Wrapper around SerpApi's Google Search API. Requires gem "google_search_results". - Langchain::Tool::NewsRetriever: A wrapper around NewsApi.org to fetch news articles. - Langchain::Tool::RubyCodeInterpreter: Useful for evaluating generated Ruby code. Requires gem "safe_ruby" (In need of a better solution). - Langchain::Tool::Tavily: A wrapper around Tavily AI. - Langchain::Tool::Vectorsearch: A wrapper for vector search classes. - Langchain::Tool::Weather: Calls Open Weather API to retrieve the current weather. - Langchain::Tool::Wikipedia: Calls Wikipedia API. Requires gem "wikipedia-client".
29 30 31 |
# File 'lib/hellm/agent.rb', line 29 def tools @tools end |
Instance Method Details
#add(content: nil, image: nil) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/hellm/agent.rb', line 49 def add(content: nil, image: nil) return if content.nil? && image.nil? = {} [:content] = content if !content.nil? && !content.empty? [:image_url] = normalize_image_input(image) if !image.nil? && !image.empty? assistant.(**) end |
#new_session ⇒ Object
68 69 70 |
# File 'lib/hellm/agent.rb', line 68 def new_session @assistant = nil end |
#response ⇒ Object
63 64 65 66 |
# File 'lib/hellm/agent.rb', line 63 def response = assistant..reverse.find(&:llm?) &.content.to_s.strip end |
#run ⇒ Object
57 58 59 60 61 |
# File 'lib/hellm/agent.rb', line 57 def run with_rate_limit do assistant.run! end end |