Skip to content

Commit 51090a2

Browse files
enhancements
1 parent 3248e41 commit 51090a2

File tree

3 files changed

+47
-24
lines changed

3 files changed

+47
-24
lines changed

image-classifier.rb

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# frozen_string_literal: true
2+
3+
# Parse command line arguments
4+
url = nil
5+
ARGV.each_with_index do |arg, index|
6+
if arg.start_with?('--url=')
7+
url = arg.split('=', 2)[1]
8+
break
9+
elsif arg == '--url' && index + 1 < ARGV.length
10+
url = ARGV[index + 1]
11+
break
12+
end
13+
end
14+
15+
if url.nil?
16+
puts "Error: --url parameter is required"
17+
puts "Usage: bundle exec ruby main.rb --url <your_url>"
18+
exit 1
19+
end
20+
21+
require "langchain"
22+
require "./tools/image-classifier-tool.rb"
23+
require "bundler/setup"
24+
Bundler.require
25+
require "dotenv/load"
26+
27+
# Silence the logger
28+
Langchain.logger.level = Logger::ERROR
29+
30+
llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
31+
32+
assistant = Langchain::Assistant.new(
33+
llm: llm,
34+
instructions: "You're a helpful AI assistant",
35+
tools: [ImageClassifierTool.new],
36+
tool_choice: "image_classifier_tool__classify"
37+
)
38+
39+
puts "Processing image..."
40+
41+
messages = assistant.add_message_and_run(image_url: url)
42+
43+
_, _, _, tool_arguments = assistant.llm_adapter.extract_tool_call_args(tool_call: messages.last.tool_calls.first)
44+
45+
pp(tool_arguments)

main.rb

Lines changed: 0 additions & 22 deletions
This file was deleted.

tools/image-classifier.rb renamed to tools/image-classifier-tool.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require "langchain"
1+
# frozen_string_literal: true
22

3-
class ImageClassifier
3+
class ImageClassifierTool
44
extend Langchain::ToolDefinition
55

66
define_function :classify, description: "Classify photo" do

0 commit comments

Comments
 (0)