File tree Expand file tree Collapse file tree 4 files changed +53
-1
lines changed Expand file tree Collapse file tree 4 files changed +53
-1
lines changed Original file line number Diff line number Diff line change 56
56
/.idea /*
57
57
/client /.idea /*
58
58
vendor /bundle
59
+
60
+ # Ignore downloaded images
61
+ /public /dalle_images
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ def self.descendants
3
3
[
4
4
Toolbox ::HelloWorld ,
5
5
Toolbox ::OpenMeteo ,
6
+ Toolbox ::Dalle
6
7
]
7
8
end
8
9
Original file line number Diff line number Diff line change
1
+ class Toolbox ::Dalle < Toolbox
2
+
3
+ describe :generate_an_image , <<~S
4
+ Generate an image based on what the user asks you to generate. You will pass the user's prompt and will get back a URL to an image.
5
+ S
6
+
7
+ def self . generate_an_image ( image_generation_prompt_s :)
8
+
9
+ response = client . images . generate (
10
+ parameters : {
11
+ prompt : image_generation_prompt_s ,
12
+ model : "dall-e-3" ,
13
+ size : "1024x1792" ,
14
+ quality : "standard"
15
+ }
16
+ )
17
+
18
+ dalle_url = response . dig ( "data" , 0 , "url" )
19
+ download ( dalle_url )
20
+
21
+ {
22
+ url : dalle_url
23
+ }
24
+ end
25
+
26
+ class << self
27
+ private
28
+
29
+ def download ( image_url )
30
+ filename = File . basename ( URI . parse ( image_url ) . path )
31
+ file_path = Rails . root . join ( 'public' , 'dalle_images' , filename )
32
+
33
+ FileUtils . mkdir_p ( File . dirname ( file_path ) )
34
+
35
+ File . open ( file_path , 'wb' ) do |file |
36
+ file << URI . open ( image_url ) . read
37
+ end
38
+
39
+ file_path
40
+ end
41
+
42
+ def client
43
+ OpenAI ::Client . new (
44
+ access_token : Current . user . openai_key ,
45
+ )
46
+ end
47
+ end
48
+ end
Original file line number Diff line number Diff line change 2
2
3
3
class ToolboxTest < ActiveSupport ::TestCase
4
4
test "tools" do
5
- assert_equal 4 , Toolbox . tools . length
5
+ assert_equal 5 , Toolbox . tools . length
6
6
assert Toolbox ::OpenMeteo . tools . first [ :function ] . values . all? { |value | value . present? }
7
7
assert Toolbox ::OpenMeteo . tools . first [ :function ] [ :description ] . length > 100
8
8
tools = [
You can’t perform that action at this time.
0 commit comments