Class: Hellm::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/hellm/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#agentsObject

Returns the value of attribute agents.



7
8
9
# File 'lib/hellm/agent.rb', line 7

def agents
  @agents
end

#descriptionObject

Returns the value of attribute description.



7
8
9
# File 'lib/hellm/agent.rb', line 7

def description
  @description
end

#instructionsObject

instructions (string) for the agent.



13
14
15
# File 'lib/hellm/agent.rb', line 13

def instructions
  @instructions
end

#llmObject

Returns the value of attribute llm.



7
8
9
# File 'lib/hellm/agent.rb', line 7

def llm
  @llm
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/hellm/agent.rb', line 7

def name
  @name
end

#stream_adapterObject

Parameters:

  • stream_adapter (Hellm::StreamAdapter)

    an object that responds to message and tool_call for streaming messages and tool calls



10
11
12
# File 'lib/hellm/agent.rb', line 10

def stream_adapter
  @stream_adapter
end

#toolsObject

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?
  options = {}
  options[:content] = content if !content.nil? && !content.empty?
  options[:image_url] = normalize_image_input(image) if !image.nil? && !image.empty?
  assistant.add_message(**options)
end

#new_sessionObject



68
69
70
# File 'lib/hellm/agent.rb', line 68

def new_session
  @assistant = nil
end

#responseObject



63
64
65
66
# File 'lib/hellm/agent.rb', line 63

def response
  assistant_message = assistant.messages.reverse.find(&:llm?)
  assistant_message&.content.to_s.strip
end

#runObject



57
58
59
60
61
# File 'lib/hellm/agent.rb', line 57

def run
  with_rate_limit do
    assistant.run!
  end
end