-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.clj
160 lines (128 loc) · 5.36 KB
/
repl.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
(require '[me.pmatiello.openai-api.api :as openai])
(require '[clojure.java.io :as io])
(require '[clojure.spec.alpha :as s])
(require '[clojure.java.browse :refer [browse-url]])
(require '[clojure.spec.test.alpha :as stest])
(stest/instrument)
; Config
(def ^:private api-key
(or (System/getenv "OPENAI_API_KEY") (read-line)))
(def config
(openai/config :api-key api-key))
(s/valid? :me.pmatiello.openai-api.specs.config/config config)
; Models
(openai/models config)
(s/valid? :me.pmatiello.openai-api.specs.model/description-list *1)
(openai/model "gpt-3.5-turbo" config)
(s/valid? :me.pmatiello.openai-api.specs.model/description *1)
; Completions
(openai/completion {:model "ada"
:prompt "hello"}
config)
(s/valid? :me.pmatiello.openai-api.specs.completion/result *1)
(openai/completion {:model "ada"
:prompt "hello"
:stream true}
config)
(s/valid? :me.pmatiello.openai-api.specs.completion/result *1)
; Chat
(openai/chat {:model "gpt-3.5-turbo"
:messages [{:role "user" :content "(println \"hello"}]}
config)
(s/valid? :me.pmatiello.openai-api.specs.chat/result *1)
(openai/chat {:model "gpt-3.5-turbo"
:stream true
:messages [{:role "user" :content "hello world program in clojure."}]}
config)
(s/valid? :me.pmatiello.openai-api.specs.chat/result *1)
; Edits
(openai/edit {:model "code-davinci-edit-001"
:instruction "Fix clojure code."
:input "(println \"hello)"}
config)
(s/valid? :me.pmatiello.openai-api.specs.edit/result *1)
; Images
(openai/image-generation {:prompt "brick wall"
:response-format "url"}
config)
(s/valid? :me.pmatiello.openai-api.specs.image/result *1)
(openai/image-edit {:image (io/file "test/fixtures/image.png")
:mask (io/file "test/fixtures/mask.png")
:prompt "brick wall with a graffiti"
:response-format "url"}
config)
(s/valid? :me.pmatiello.openai-api.specs.image/result *1)
(openai/image-variation {:image (io/file "test/fixtures/image.png")}
config)
(s/valid? :me.pmatiello.openai-api.specs.image/result *1)
; Embeddings
(openai/embedding {:model "text-embedding-ada-002"
:input "brick wall"}
config)
(s/valid? :me.pmatiello.openai-api.specs.embedding/result *1)
; Audio
(openai/audio-transcription {:model "whisper-1"
:file (io/file "test/fixtures/audio.m4a")}
config)
(s/valid? :me.pmatiello.openai-api.specs.audio/result *1)
(openai/audio-translation {:model "whisper-1"
:file (io/file "test/fixtures/audio-pt.m4a")}
config)
(s/valid? :me.pmatiello.openai-api.specs.audio/result *1)
; Files
(openai/file-upload! {:file (io/file "test/fixtures/colors.txt")
:purpose "fine-tune"}
config)
(s/valid? :me.pmatiello.openai-api.specs.file/upload-result *1)
(openai/files config)
(s/valid? :me.pmatiello.openai-api.specs.file/description-list *1)
(openai/file (-> config openai/files :data first :id) config)
(s/valid? :me.pmatiello.openai-api.specs.file/result *1)
(openai/file-content (-> config openai/files :data first :id) config)
(s/valid? string? *1)
(openai/file-delete! (-> config openai/files :data first :id) config)
(s/valid? :me.pmatiello.openai-api.specs.file/delete-result *1)
; Fine-tunes
(openai/fine-tune-create!
{:training-file (->> config openai/files :data
(filter #(-> % :filename #{"colors.txt"}))
first :id)
:model "ada"}
config)
(s/valid? :me.pmatiello.openai-api.specs.fine-tune/description *1)
(openai/fine-tunes config)
(s/valid? :me.pmatiello.openai-api.specs.fine-tune/description-list *1)
(openai/fine-tune
(-> config openai/fine-tunes :data last :id)
config)
(s/valid? :me.pmatiello.openai-api.specs.fine-tune/description *1)
(openai/fine-tune-events
(-> config openai/fine-tunes :data last :id)
config)
(s/valid? :me.pmatiello.openai-api.specs.fine-tune/event-list *1)
(openai/fine-tune-cancel!
(-> config openai/fine-tunes :data last :id)
config)
(s/valid? :me.pmatiello.openai-api.specs.fine-tune/description *1)
(openai/fine-tune-delete!
(->> config openai/models :data
(filter #(->> % :owned-by (re-matches #"user-.*"))) last :id)
config)
(s/valid? :me.pmatiello.openai-api.specs.fine-tune/delete-result *1)
; Moderation
(openai/moderation {:input "kittens"} config)
(s/valid? :me.pmatiello.openai-api.specs.moderation/classification *1)
; Timeout
(openai/models (merge config {:http-opts {:connection-timeout 2500
:socket-timeout 2500}}))
(s/valid? :me.pmatiello.openai-api.specs.model/description-list *1)
(comment
"cleanup"
"WARNING: deletes all files in the account"
(doseq [each (->> config openai/files :data (map :id))]
(openai/file-delete! each config))
"WARNING: deletes all user's fine-tuned models in the account"
(doseq [each (->> config openai/models :data
(filter #(->> % :owned-by (re-matches #"user-.*")))
(map :id))]
(openai/fine-tune-delete! each config)))