Skip to content

Commit 623d7fa

Browse files
Add files via upload
1 parent a7b4cb8 commit 623d7fa

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

Gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# frozen_string_literal: true
2+
3+
ruby "3.3.4"
4+
5+
source "https://rubygems.org"
6+
7+
gem "langchainrb"
8+
gem "ruby-openai"
9+
gem "pry-byebug"
10+
gem "dotenv"

Gemfile.lock

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
addressable (2.8.7)
5+
public_suffix (>= 2.0.2, < 7.0)
6+
baran (0.1.12)
7+
byebug (11.1.3)
8+
coderay (1.1.3)
9+
dotenv (3.1.4)
10+
event_stream_parser (1.0.0)
11+
faraday (2.12.0)
12+
faraday-net_http (>= 2.0, < 3.4)
13+
json
14+
logger
15+
faraday-multipart (1.0.4)
16+
multipart-post (~> 2)
17+
faraday-net_http (3.3.0)
18+
net-http
19+
json (2.7.2)
20+
json-schema (4.3.1)
21+
addressable (>= 2.8)
22+
langchainrb (0.17.0)
23+
baran (~> 0.1.9)
24+
json-schema (~> 4)
25+
matrix
26+
pragmatic_segmenter (~> 0.3.0)
27+
zeitwerk (~> 2.5)
28+
logger (1.6.1)
29+
matrix (0.4.2)
30+
method_source (1.1.0)
31+
multipart-post (2.4.1)
32+
net-http (0.4.1)
33+
uri
34+
pragmatic_segmenter (0.3.24)
35+
pry (0.14.2)
36+
coderay (~> 1.1)
37+
method_source (~> 1.0)
38+
pry-byebug (3.10.1)
39+
byebug (~> 11.0)
40+
pry (>= 0.13, < 0.15)
41+
public_suffix (6.0.1)
42+
ruby-openai (7.1.0)
43+
event_stream_parser (>= 0.3.0, < 2.0.0)
44+
faraday (>= 1)
45+
faraday-multipart (>= 1)
46+
uri (0.13.1)
47+
zeitwerk (2.6.18)
48+
49+
PLATFORMS
50+
arm64-darwin-23
51+
ruby
52+
53+
DEPENDENCIES
54+
dotenv
55+
langchainrb
56+
pry-byebug
57+
ruby-openai
58+
59+
RUBY VERSION
60+
ruby 3.3.4p94
61+
62+
BUNDLED WITH
63+
2.5.17

image-classifier.rb

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require "langchain"
2+
3+
class ImageClassifier
4+
extend Langchain::ToolDefinition
5+
6+
define_function :classify, description: "Classify photo" do
7+
property :main_subject,
8+
description: "Primary subject of the photo",
9+
type: "string",
10+
required: true
11+
property :scene_type,
12+
description: "Scene setting in the photo",
13+
type: "string",
14+
required: true,
15+
enum: ["urban", "rural", "indoor", "outdoor"]
16+
property :colors,
17+
description: "Dominant colors in the photo",
18+
type: "array",
19+
required: true do
20+
item type: "string"
21+
end
22+
property :objects,
23+
description: "List of key objects visible in the image",
24+
type: "array",
25+
required: true do
26+
item type: "string"
27+
end
28+
property :actions,
29+
description: "Activities or movements captured",
30+
type: "array",
31+
required: true do
32+
item type: "string"
33+
end
34+
property :number_of_humans,
35+
description: "Number of people if present",
36+
type: "number",
37+
required: true
38+
property :weather_conditions,
39+
description: "Weather conditions",
40+
type: "string",
41+
required: true,
42+
enum: ["sunny", "cloudy", "rainy", "snowy", "foggy"]
43+
property :time_of_day,
44+
description: "Time of day",
45+
type: "string",
46+
required: true,
47+
enum: ["morning", "afternoon", "evening", "night"]
48+
property :mood,
49+
description: "Overall feeling conveyed by the photo",
50+
type: "string",
51+
required: true
52+
end
53+
end
54+

main.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "langchain"
2+
require "./image-classifier.rb"
3+
require "bundler/setup"
4+
Bundler.require
5+
require "dotenv/load"
6+
7+
#Langchain.logger.level = Logger::ERROR
8+
9+
llm = Langchain::LLM::OpenAI.new(api_key: ENV["OPENAI_API_KEY"])
10+
11+
assistant = Langchain::Assistant.new(
12+
llm: llm,
13+
instructions: "You're a helpful AI assistant",
14+
tools: [ImageClassifier.new],
15+
tool_choice: "image_classifier__classify"
16+
)
17+
18+
puts "Processing image..."
19+
20+
messages = assistant.add_message_and_run image_url: "https://gist.githubusercontent.com/andreibondarev/b6f444194d0ee7ab7302a4d83184e53e/raw/db2e55ce2d29f3f6a7e3540e5abfd451c6a3d2a8/sf-cable-car.jpg"
21+
22+
puts messages.last.tool_calls

0 commit comments

Comments
 (0)