Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sebyx07 committed Aug 2, 2024
1 parent 27d92ce commit c5a7047
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 82 deletions.
62 changes: 28 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,60 +134,58 @@ class MyProductsIndexer < AiAgent::Embeddings::Indexers::BaseIndexer
end
```

# External tools usage(DEVIN)
# External tools usage(DEVIN like)
```ruby
class EvaluateRubyCodeAgent < AiAgent::Agents::BasicAgentWithTools
config do |c|
c.who_am_i = <<~WHO_AM_I
I am a ruby programming agent agent, i use my tools to evaluate ruby code
I have multiple roles, here are some of them:
- #{AiAgent::Tools::Ruby::EvaluateRubyTool.who_am_i_description}
WHO_AM_I

c.ollama_model = 'llama3.1' # you need a model with tools
end

def tools
@tools ||= {
@tools ||= [
evaluate_ruby_tool.tool_definition,
].inject({}) do |h, tool|
h.merge(tool)
end.deep_merge({
evaluate_ruby: {
execute: ->(params) do
code = params['code']
output = evaluate_ruby_tool.evaluate(code)
AiAgent::Conversation::Message.new(:tool, output) # you need to return a message
end,
description: 'Evaluates Ruby code',
parameters: {
type: 'string',
description: 'The Ruby code to evaluate'
},
required: ['code']
},
ask_user: {
execute: ->(params) do
question = params['question']
puts question
answer = gets.chomp
stop if answer == 'exit'
answer
end,
description: 'Asks the user a question',
parameters: {
type: 'string',
description: 'The question to ask the user'
},
required: ['question']
before_execute: ->(params) do
puts params['code'].colorize(:green)
end
}
}
})
end

private
def evaluate_ruby_tool
@evaluate_ruby_tool ||= AiAgent::Tools::Ruby::EvaluateRubyTool.new(self)
end
def show_normal_response(content)
puts content
end

def ask_for_user_input
print '> '
response = gets.chomp
exit if response == 'exit'
AiAgent::Conversation::Message.new(:user, response)
end

def show_tool_call_explanation(tool_call)
puts 'Ai: '.colorize(:magenta) + tool_call.explanation
end
end

agent = EvaluateRubyCodeAgent.new([]) # [] is the history of messages
agent.run_in_loop
```

Check for more tools in `lib/ai_agent/tools`

# Rails Integration
#### Example 1) Standalone Chat Agent
```ruby
Expand Down Expand Up @@ -242,10 +240,6 @@ class AiConversationsController < ApplicationController
end
```

# 🔮 Future Plans

- 🛠️ Ability for agents to execute tasks independently

# 🛠️ Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install.
Expand Down
4 changes: 4 additions & 0 deletions lib/ai_agent/agents/basic_agent_with_tools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ def serialize_tools
type: :string,
description: 'Explain the reason for the tool call'
}

t_options[:parameters][:required].tap do |required|
required << '__explain' if required && required.include?('__explain')
end
end

{
Expand Down
10 changes: 8 additions & 2 deletions lib/ai_agent/tools/os/execute_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ def self.who_am_i_description
"I have access to the system's shell and can execute commands."
end

def initialize(agent, sudo: false, root: false)
super(agent)
@sudo = sudo
@root = root
end

def execute_command(command, path)
command = "cd #{path} && #{command}"
result = Open3.capture3(command)
Expand Down Expand Up @@ -36,7 +42,7 @@ def tool_definition
rescue RuntimeError => e
AiAgent::Conversation::Message.new(:tool, "Error executing command: #{e.inspect}")
end,
description: "Executes a command in the system's shell.",
description: "Executes a command in the system's shell. Use this tool to execute commands in the system's shell. Sudo access: #{@sudo}, root access: #{@root}.",
parameters: {
type: 'object',
properties: {
Expand All @@ -58,7 +64,7 @@ def tool_definition
path = params['path'] || Dir.pwd
AiAgent::Conversation::Message.new(:tool, execute_command_in_background(command, path))
end,
description: "Executes a command in the system's shell in the background.",
description: "Executes a command in the system's shell in the background. Use this tool to execute commands in the system's shell in the background. Sudo access: #{@sudo}, root access: #{@root}",
parameters: {
type: 'object',
properties: {
Expand Down
Loading

0 comments on commit c5a7047

Please sign in to comment.