openai_101 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +20 -1
  3. data/CHANGELOG.md +14 -0
  4. data/README.md +63 -39
  5. data/bin/automate-chatgpt.js +60 -0
  6. data/bin/automate-midjourney.js +75 -0
  7. data/bin/convert_webp_to_png.rb +86 -0
  8. data/bin/gpt_context_gatherer.rb +63 -0
  9. data/course/course.md +64 -0
  10. data/course/images/beautiful-llm-models.png +0 -0
  11. data/course/images/prompts/beautiful-llm-models.txt +1 -0
  12. data/course/images/prompts/series-2-appydave-gpt-summit.txt +1 -0
  13. data/course/images/series-2-appydave-gpt-summit.png +0 -0
  14. data/gpt-context/openai-documentation.md +498 -0
  15. data/gpt-context/ruby-openai-documenation.md +747 -0
  16. data/gpt-context/theme-prompts.csv +21 -0
  17. data/lib/openai_101/config/openai.rb +15 -0
  18. data/lib/openai_101/tools/automate-images-chatgpt.js +60 -0
  19. data/lib/openai_101/tools/automate-images-midjourney.js +75 -0
  20. data/lib/openai_101/tools/bulk_image_bot/base_automator.js +53 -0
  21. data/lib/openai_101/tools/bulk_image_bot/chatgpt_automator.js +27 -0
  22. data/lib/openai_101/tools/bulk_image_bot/midjourney_automator.js +49 -0
  23. data/lib/openai_101/tools/clean_ruby_errors.rb +274 -0
  24. data/lib/openai_101/tools/edl_to_chapters.rb +56 -0
  25. data/lib/openai_101/tools/file_content_gatherer.rb +36 -0
  26. data/lib/openai_101/tools/webp_to_png.rb +124 -0
  27. data/lib/openai_101/version.rb +1 -1
  28. data/lib/openai_101.rb +9 -0
  29. data/package-lock.json +1154 -159
  30. data/package.json +4 -1
  31. metadata +83 -6
  32. data/.builders/_.rb +0 -1
  33. data/.builders/boot.rb +0 -39
  34. data/.builders/generators/01-bootstrap.rb +0 -134
@@ -0,0 +1,747 @@
1
+
2
+ - Get your API key from [https://platform.openai.com/account/api-keys](https://platform.openai.com/account/api-keys)
3
+ - If you belong to multiple organizations, you can get your Organization ID from [https://platform.openai.com/account/org-settings](https://platform.openai.com/account/org-settings)
4
+
5
+ ### Quickstart
6
+
7
+ For a quick test you can pass your token directly to a new client:
8
+
9
+ ```ruby
10
+ client = OpenAI::Client.new(access_token: "access_token_goes_here")
11
+ ```
12
+
13
+ ### With Config
14
+
15
+ For a more robust setup, you can configure the gem with your API keys, for example in an `openai.rb` initializer file. Never hardcode secrets into your codebase - instead use something like [dotenv](https://github.com/motdotla/dotenv) to pass the keys safely into your environments.
16
+
17
+ ```ruby
18
+ OpenAI.configure do |config|
19
+ config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
20
+ config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional.
21
+ end
22
+ ```
23
+
24
+ Then you can create a client like this:
25
+
26
+ ```ruby
27
+ client = OpenAI::Client.new
28
+ ```
29
+
30
+ You can still override the config defaults when making new clients; any options not included will fall back to any global config set with OpenAI.configure. e.g. in this example the organization_id, request_timeout, etc. will fallback to any set globally using OpenAI.configure, with only the access_token overridden:
31
+
32
+ ```ruby
33
+ client = OpenAI::Client.new(access_token: "access_token_goes_here")
34
+ ```
35
+
36
+ #### Custom timeout or base URI
37
+
38
+ The default timeout for any request using this library is 120 seconds. You can change that by passing a number of seconds to the `request_timeout` when initializing the client. You can also change the base URI used for all requests, eg. to use observability tools like [Helicone](https://docs.helicone.ai/quickstart/integrate-in-one-line-of-code), and add arbitrary other headers e.g. for [openai-caching-proxy-worker](https://github.com/6/openai-caching-proxy-worker):
39
+
40
+ ```ruby
41
+ client = OpenAI::Client.new(
42
+ access_token: "access_token_goes_here",
43
+ uri_base: "https://oai.hconeai.com/",
44
+ request_timeout: 240,
45
+ extra_headers: {
46
+ "X-Proxy-TTL" => "43200", # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
47
+ "X-Proxy-Refresh": "true", # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
48
+ "Helicone-Auth": "Bearer HELICONE_API_KEY", # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
49
+ "helicone-stream-force-format" => "true", # Use this with Helicone otherwise streaming drops chunks # https://github.com/alexrudall/ruby-openai/issues/251
50
+ }
51
+ )
52
+ ```
53
+
54
+ or when configuring the gem:
55
+
56
+ ```ruby
57
+ OpenAI.configure do |config|
58
+ config.access_token = ENV.fetch("OPENAI_ACCESS_TOKEN")
59
+ config.organization_id = ENV.fetch("OPENAI_ORGANIZATION_ID") # Optional
60
+ config.uri_base = "https://oai.hconeai.com/" # Optional
61
+ config.request_timeout = 240 # Optional
62
+ config.extra_headers = {
63
+ "X-Proxy-TTL" => "43200", # For https://github.com/6/openai-caching-proxy-worker#specifying-a-cache-ttl
64
+ "X-Proxy-Refresh": "true", # For https://github.com/6/openai-caching-proxy-worker#refreshing-the-cache
65
+ "Helicone-Auth": "Bearer HELICONE_API_KEY" # For https://docs.helicone.ai/getting-started/integration-method/openai-proxy
66
+ } # Optional
67
+ end
68
+ ```
69
+
70
+ #### Extra Headers per Client
71
+
72
+ You can dynamically pass headers per client object, which will be merged with any headers set globally with OpenAI.configure:
73
+
74
+ ```ruby
75
+ client = OpenAI::Client.new(access_token: "access_token_goes_here")
76
+ client.add_headers("X-Proxy-TTL" => "43200")
77
+ ```
78
+
79
+ #### Verbose Logging
80
+
81
+ You can pass [Faraday middleware](https://lostisland.github.io/faraday/#/middleware/index) to the client in a block, eg. to enable verbose logging with Ruby's [Logger](https://ruby-doc.org/3.2.2/stdlibs/logger/Logger.html):
82
+
83
+ ```ruby
84
+ client = OpenAI::Client.new do |f|
85
+ f.response :logger, Logger.new($stdout), bodies: true
86
+ end
87
+ ```
88
+
89
+ #### Azure
90
+
91
+ To use the [Azure OpenAI Service](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/) API, you can configure the gem like this:
92
+
93
+ ```ruby
94
+ OpenAI.configure do |config|
95
+ config.access_token = ENV.fetch("AZURE_OPENAI_API_KEY")
96
+ config.uri_base = ENV.fetch("AZURE_OPENAI_URI")
97
+ config.api_type = :azure
98
+ config.api_version = "2023-03-15-preview"
99
+ end
100
+ ```
101
+
102
+ where `AZURE_OPENAI_URI` is e.g. `https://custom-domain.openai.azure.com/openai/deployments/gpt-35-turbo`
103
+
104
+ ### Counting Tokens
105
+
106
+ OpenAI parses prompt text into [tokens](https://help.openai.com/en/articles/4936856-what-are-tokens-and-how-to-count-them), which are words or portions of words. (These tokens are unrelated to your API access_token.) Counting tokens can help you estimate your [costs](https://openai.com/pricing). It can also help you ensure your prompt text size is within the max-token limits of your model's context window, and choose an appropriate [`max_tokens`](https://platform.openai.com/docs/api-reference/chat/create#chat/create-max_tokens) completion parameter so your response will fit as well.
107
+
108
+ To estimate the token-count of your text:
109
+
110
+ ```ruby
111
+ OpenAI.rough_token_count("Your text")
112
+ ```
113
+
114
+ If you need a more accurate count, try [tiktoken_ruby](https://github.com/IAPark/tiktoken_ruby).
115
+
116
+ ### Models
117
+
118
+ There are different models that can be used to generate text. For a full list and to retrieve information about a single model:
119
+
120
+ ```ruby
121
+ client.models.list
122
+ client.models.retrieve(id: "text-ada-001")
123
+ ```
124
+
125
+ #### Examples
126
+
127
+ - [GPT-4 (limited beta)](https://platform.openai.com/docs/models/gpt-4)
128
+ - gpt-4 (uses current version)
129
+ - gpt-4-0314
130
+ - gpt-4-32k
131
+ - [GPT-3.5](https://platform.openai.com/docs/models/gpt-3-5)
132
+ - gpt-3.5-turbo
133
+ - gpt-3.5-turbo-0301
134
+ - text-davinci-003
135
+ - [GPT-3](https://platform.openai.com/docs/models/gpt-3)
136
+ - text-ada-001
137
+ - text-babbage-001
138
+ - text-curie-001
139
+
140
+ ### Chat
141
+
142
+ GPT is a model that can be used to generate text in a conversational style. You can use it to [generate a response](https://platform.openai.com/docs/api-reference/chat/create) to a sequence of [messages](https://platform.openai.com/docs/guides/chat/introduction):
143
+
144
+ ```ruby
145
+ response = client.chat(
146
+ parameters: {
147
+ model: "gpt-3.5-turbo", # Required.
148
+ messages: [{ role: "user", content: "Hello!"}], # Required.
149
+ temperature: 0.7,
150
+ })
151
+ puts response.dig("choices", 0, "message", "content")
152
+ # => "Hello! How may I assist you today?"
153
+ ```
154
+
155
+ #### Streaming Chat
156
+
157
+ [Quick guide to streaming Chat with Rails 7 and Hotwire](https://gist.github.com/alexrudall/cb5ee1e109353ef358adb4e66631799d)
158
+
159
+ You can stream from the API in realtime, which can be much faster and used to create a more engaging user experience. Pass a [Proc](https://ruby-doc.org/core-2.6/Proc.html) (or any object with a `#call` method) to the `stream` parameter to receive the stream of completion chunks as they are generated. Each time one or more chunks is received, the proc will be called once with each chunk, parsed as a Hash. If OpenAI returns an error, `ruby-openai` will raise a Faraday error.
160
+
161
+ ```ruby
162
+ client.chat(
163
+ parameters: {
164
+ model: "gpt-3.5-turbo", # Required.
165
+ messages: [{ role: "user", content: "Describe a character called Anna!"}], # Required.
166
+ temperature: 0.7,
167
+ stream: proc do |chunk, _bytesize|
168
+ print chunk.dig("choices", 0, "delta", "content")
169
+ end
170
+ })
171
+ # => "Anna is a young woman in her mid-twenties, with wavy chestnut hair that falls to her shoulders..."
172
+ ```
173
+
174
+ Note: OpenAPI currently does not report token usage for streaming responses. To count tokens while streaming, try `OpenAI.rough_token_count` or [tiktoken_ruby](https://github.com/IAPark/tiktoken_ruby). We think that each call to the stream proc corresponds to a single token, so you can also try counting the number of calls to the proc to get the completion token count.
175
+
176
+ #### Vision
177
+
178
+ You can use the GPT-4 Vision model to generate a description of an image:
179
+
180
+ ```ruby
181
+ messages = [
182
+ { "type": "text", "text": "What’s in this image?"},
183
+ { "type": "image_url",
184
+ "image_url": {
185
+ "url": "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Gfp-wisconsin-madison-the-nature-boardwalk.jpg/2560px-Gfp-wisconsin-madison-the-nature-boardwalk.jpg",
186
+ },
187
+ }
188
+ ]
189
+ response = client.chat(
190
+ parameters: {
191
+ model: "gpt-4-vision-preview", # Required.
192
+ messages: [{ role: "user", content: messages}], # Required.
193
+ })
194
+ puts response.dig("choices", 0, "message", "content")
195
+ # => "The image depicts a serene natural landscape featuring a long wooden boardwalk extending straight ahead"
196
+ ```
197
+
198
+ #### JSON Mode
199
+
200
+ You can set the response_format to ask for responses in JSON (at least for `gpt-3.5-turbo-1106`):
201
+
202
+ ```ruby
203
+ response = client.chat(
204
+ parameters: {
205
+ model: "gpt-3.5-turbo-1106",
206
+ response_format: { type: "json_object" },
207
+ messages: [{ role: "user", content: "Hello! Give me some JSON please."}],
208
+ temperature: 0.7,
209
+ })
210
+ puts response.dig("choices", 0, "message", "content")
211
+ {
212
+ "name": "John",
213
+ "age": 30,
214
+ "city": "New York",
215
+ "hobbies": ["reading", "traveling", "hiking"],
216
+ "isStudent": false
217
+ }
218
+ ```
219
+
220
+ You can stream it as well!
221
+
222
+ ```ruby
223
+ response = client.chat(
224
+ parameters: {
225
+ model: "gpt-3.5-turbo-1106",
226
+ messages: [{ role: "user", content: "Can I have some JSON please?"}],
227
+ response_format: { type: "json_object" },
228
+ stream: proc do |chunk, _bytesize|
229
+ print chunk.dig("choices", 0, "delta", "content")
230
+ end
231
+ })
232
+ {
233
+ "message": "Sure, please let me know what specific JSON data you are looking for.",
234
+ "JSON_data": {
235
+ "example_1": {
236
+ "key_1": "value_1",
237
+ "key_2": "value_2",
238
+ "key_3": "value_3"
239
+ },
240
+ "example_2": {
241
+ "key_4": "value_4",
242
+ "key_5": "value_5",
243
+ "key_6": "value_6"
244
+ }
245
+ }
246
+ }
247
+ ```
248
+
249
+ ### Functions
250
+
251
+ You can describe and pass in functions and the model will intelligently choose to output a JSON object containing arguments to call those them. For example, if you want the model to use your method `get_current_weather` to get the current weather in a given location:
252
+
253
+ ```ruby
254
+ def get_current_weather(location:, unit: "fahrenheit")
255
+ # use a weather api to fetch weather
256
+ end
257
+
258
+ response =
259
+ client.chat(
260
+ parameters: {
261
+ model: "gpt-3.5-turbo-0613",
262
+ messages: [
263
+ {
264
+ "role": "user",
265
+ "content": "What is the weather like in San Francisco?",
266
+ },
267
+ ],
268
+ functions: [
269
+ {
270
+ name: "get_current_weather",
271
+ description: "Get the current weather in a given location",
272
+ parameters: {
273
+ type: :object,
274
+ properties: {
275
+ location: {
276
+ type: :string,
277
+ description: "The city and state, e.g. San Francisco, CA",
278
+ },
279
+ unit: {
280
+ type: "string",
281
+ enum: %w[celsius fahrenheit],
282
+ },
283
+ },
284
+ required: ["location"],
285
+ },
286
+ },
287
+ ],
288
+ },
289
+ )
290
+
291
+ message = response.dig("choices", 0, "message")
292
+
293
+ if message["role"] == "assistant" && message["function_call"]
294
+ function_name = message.dig("function_call", "name")
295
+ args =
296
+ JSON.parse(
297
+ message.dig("function_call", "arguments"),
298
+ { symbolize_names: true },
299
+ )
300
+
301
+ case function_name
302
+ when "get_current_weather"
303
+ get_current_weather(**args)
304
+ end
305
+ end
306
+ # => "The weather is nice 🌞"
307
+ ```
308
+
309
+ ### Edits
310
+
311
+ Send a string and some instructions for what to do to the string:
312
+
313
+ ```ruby
314
+ response = client.edits(
315
+ parameters: {
316
+ model: "text-davinci-edit-001",
317
+ input: "What day of the wek is it?",
318
+ instruction: "Fix the spelling mistakes"
319
+ }
320
+ )
321
+ puts response.dig("choices", 0, "text")
322
+ # => What day of the week is it?
323
+ ```
324
+
325
+ ### Embeddings
326
+
327
+ You can use the embeddings endpoint to get a vector of numbers representing an input. You can then compare these vectors for different inputs to efficiently check how similar the inputs are.
328
+
329
+ ```ruby
330
+ response = client.embeddings(
331
+ parameters: {
332
+ model: "text-embedding-ada-002",
333
+ input: "The food was delicious and the waiter..."
334
+ }
335
+ )
336
+
337
+ puts response.dig("data", 0, "embedding")
338
+ # => Vector representation of your embedding
339
+ ```
340
+
341
+ ### Files
342
+
343
+ Put your data in a `.jsonl` file like this:
344
+
345
+ ```json
346
+ {"prompt":"Overjoyed with my new phone! ->", "completion":" positive"}
347
+ {"prompt":"@lakers disappoint for a third straight night ->", "completion":" negative"}
348
+ ```
349
+
350
+ and pass the path to `client.files.upload` to upload it to OpenAI, and then interact with it:
351
+
352
+ ```ruby
353
+ client.files.upload(parameters: { file: "path/to/sentiment.jsonl", purpose: "fine-tune" })
354
+ client.files.list
355
+ client.files.retrieve(id: "file-123")
356
+ client.files.content(id: "file-123")
357
+ client.files.delete(id: "file-123")
358
+ ```
359
+
360
+ ### Finetunes
361
+
362
+ Upload your fine-tuning data in a `.jsonl` file as above and get its ID:
363
+
364
+ ```ruby
365
+ response = client.files.upload(parameters: { file: "path/to/sarcasm.jsonl", purpose: "fine-tune" })
366
+ file_id = JSON.parse(response.body)["id"]
367
+ ```
368
+
369
+ You can then use this file ID to create a fine tuning job:
370
+
371
+ ```ruby
372
+ response = client.finetunes.create(
373
+ parameters: {
374
+ training_file: file_id,
375
+ model: "gpt-3.5-turbo-0613"
376
+ })
377
+ fine_tune_id = response["id"]
378
+ ```
379
+
380
+ That will give you the fine-tune ID. If you made a mistake you can cancel the fine-tune model before it is processed:
381
+
382
+ ```ruby
383
+ client.finetunes.cancel(id: fine_tune_id)
384
+ ```
385
+
386
+ You may need to wait a short time for processing to complete. Once processed, you can use list or retrieve to get the name of the fine-tuned model:
387
+
388
+ ```ruby
389
+ client.finetunes.list
390
+ response = client.finetunes.retrieve(id: fine_tune_id)
391
+ fine_tuned_model = response["fine_tuned_model"]
392
+ ```
393
+
394
+ This fine-tuned model name can then be used in completions:
395
+
396
+ ```ruby
397
+ response = client.completions(
398
+ parameters: {
399
+ model: fine_tuned_model,
400
+ prompt: "I love Mondays!"
401
+ }
402
+ )
403
+ response.dig("choices", 0, "text")
404
+ ```
405
+
406
+ You can also capture the events for a job:
407
+
408
+ ```
409
+ client.finetunes.list_events(id: fine_tune_id)
410
+ ```
411
+
412
+ ### Assistants
413
+
414
+ Assistants can call models to interact with threads and use tools to perform tasks (see [Assistant Overview](https://platform.openai.com/docs/assistants/overview)).
415
+
416
+ To create a new assistant (see [API documentation](https://platform.openai.com/docs/api-reference/assistants/createAssistant)):
417
+
418
+ ```ruby
419
+ response = client.assistants.create(
420
+ parameters: {
421
+ model: "gpt-3.5-turbo-1106", # Retrieve via client.models.list. Assistants need 'gpt-3.5-turbo-1106' or later.
422
+ name: "OpenAI-Ruby test assistant",
423
+ description: nil,
424
+ instructions: "You are a helpful assistant for coding a OpenAI API client using the OpenAI-Ruby gem.",
425
+ tools: [
426
+ { type: 'retrieval' }, # Allow access to files attached using file_ids
427
+ { type: 'code_interpreter' }, # Allow access to Python code interpreter
428
+ ],
429
+ "file_ids": ["file-123"], # See Files section above for how to upload files
430
+ "metadata": { my_internal_version_id: '1.0.0' }
431
+ })
432
+ assistant_id = response["id"]
433
+ ```
434
+
435
+ Given an `assistant_id` you can `retrieve` the current field values:
436
+
437
+ ```ruby
438
+ client.assistants.retrieve(id: assistant_id)
439
+ ```
440
+
441
+ You can get a `list` of all assistants currently available under the organization:
442
+
443
+ ```ruby
444
+ client.assistants.list
445
+ ```
446
+
447
+ You can modify an existing assistant using the assistant's id (see [API documentation](https://platform.openai.com/docs/api-reference/assistants/modifyAssistant)):
448
+
449
+ ```ruby
450
+ response = client.assistants.modify(
451
+ id: assistant_id,
452
+ parameters: {
453
+ name: "Modified Test Assistant for OpenAI-Ruby",
454
+ metadata: { my_internal_version_id: '1.0.1' }
455
+ })
456
+ ```
457
+
458
+ You can delete assistants:
459
+
460
+ ```
461
+ client.assistants.delete(id: assistant_id)
462
+ ```
463
+
464
+ ### Threads and Messages
465
+
466
+ Once you have created an assistant as described above, you need to prepare a `Thread` of `Messages` for the assistant to work on (see [introduction on Assistants](https://platform.openai.com/docs/assistants/how-it-works)). For example, as an initial setup you could do:
467
+
468
+ ```ruby
469
+ # Create thread
470
+ response = client.threads.create # Note: Once you create a thread, there is no way to list it
471
+ # or recover it currently (as of 2023-12-10). So hold onto the `id`
472
+ thread_id = response["id"]
473
+
474
+ # Add initial message from user (see https://platform.openai.com/docs/api-reference/messages/createMessage)
475
+ message_id = client.messages.create(
476
+ thread_id: thread_id,
477
+ parameters: {
478
+ role: "user", # Required for manually created messages
479
+ content: "Can you help me write an API library to interact with the OpenAI API please?"
480
+ })["id"]
481
+
482
+ # Retrieve individual message
483
+ message = client.messages.retrieve(thread_id: thread_id, id: message_id)
484
+
485
+ # Review all messages on the thread
486
+ messages = client.messages.list(thread_id: thread_id)
487
+ ```
488
+
489
+ To clean up after a thread is no longer needed:
490
+
491
+ ```ruby
492
+ # To delete the thread (and all associated messages):
493
+ client.threads.delete(id: thread_id)
494
+
495
+ client.messages.retrieve(thread_id: thread_id, id: message_id) # -> Fails after thread is deleted
496
+ ```
497
+
498
+
499
+ ### Runs
500
+
501
+ To submit a thread to be evaluated with the model of an assistant, create a `Run` as follows (Note: This is one place where OpenAI will take your money):
502
+
503
+ ```ruby
504
+ # Create run (will use instruction/model/tools from Assistant's definition)
505
+ response = client.runs.create(thread_id: thread_id,
506
+ parameters: {
507
+ assistant_id: assistant_id
508
+ })
509
+ run_id = response['id']
510
+
511
+ # Retrieve/poll Run to observe status
512
+ response = client.runs.retrieve(id: run_id, thread_id: thread_id)
513
+ status = response['status']
514
+ ```
515
+
516
+ The `status` response can include the following strings `queued`, `in_progress`, `requires_action`, `cancelling`, `cancelled`, `failed`, `completed`, or `expired` which you can handle as follows:
517
+
518
+ ```ruby
519
+ while true do
520
+
521
+ response = client.runs.retrieve(id: run_id, thread_id: thread_id)
522
+ status = response['status']
523
+
524
+ case status
525
+ when 'queued', 'in_progress', 'cancelling'
526
+ puts 'Sleeping'
527
+ sleep 1 # Wait one second and poll again
528
+ when 'completed'
529
+ break # Exit loop and report result to user
530
+ when 'requires_action'
531
+ # Handle tool calls (see below)
532
+ when 'cancelled', 'failed', 'expired'
533
+ puts response['last_error'].inspect
534
+ break # or `exit`
535
+ else
536
+ puts "Unknown status response: #{status}"
537
+ end
538
+ end
539
+ ```
540
+
541
+ If the `status` response indicates that the `run` is `completed`, the associated `thread` will have one or more new `messages` attached:
542
+
543
+ ```ruby
544
+ # Either retrieve all messages in bulk again, or...
545
+ messages = client.messages.list(thread_id: thread_id) # Note: as of 2023-12-11 adding limit or order options isn't working, yet
546
+
547
+ # Alternatively retrieve the `run steps` for the run which link to the messages:
548
+ run_steps = client.run_steps.list(thread_id: thread_id, run_id: run_id)
549
+ new_message_ids = run_steps['data'].filter_map { |step|
550
+ if step['type'] == 'message_creation'
551
+ step.dig('step_details', "message_creation", "message_id")
552
+ end # Ignore tool calls, because they don't create new messages.
553
+ }
554
+
555
+ # Retrieve the individual messages
556
+ new_messages = new_message_ids.map { |msg_id|
557
+ client.messages.retrieve(id: msg_id, thread_id: thread_id)
558
+ }
559
+
560
+ # Find the actual response text in the content array of the messages
561
+ new_messages.each { |msg|
562
+ msg['content'].each { |content_item|
563
+ case content_item['type']
564
+ when 'text'
565
+ puts content_item.dig('text', 'value')
566
+ # Also handle annotations
567
+ when 'image_file'
568
+ # Use File endpoint to retrieve file contents via id
569
+ id = content_item.dig('image_file', 'file_id')
570
+ end
571
+ }
572
+ }
573
+ ```
574
+
575
+ At any time you can list all runs which have been performed on a particular thread or are currently running (in descending/newest first order):
576
+
577
+ ```ruby
578
+ client.runs.list(thread_id: thread_id)
579
+ ```
580
+
581
+ #### Runs involving function tools
582
+
583
+ In case you are allowing the assistant to access `function` tools (they are defined in the same way as functions during chat completion), you might get a status code of `requires_action` when the assistant wants you to evaluate one or more function tools:
584
+
585
+ ```ruby
586
+ def get_current_weather(location:, unit: "celsius")
587
+ # Your function code goes here
588
+ if location =~ /San Francisco/i
589
+ return unit == "celsius" ? "The weather is nice 🌞 at 27°C" : "The weather is nice 🌞 at 80°F"
590
+ else
591
+ return unit == "celsius" ? "The weather is icy 🥶 at -5°C" : "The weather is icy 🥶 at 23°F"
592
+ end
593
+ end
594
+
595
+ if status == 'requires_action'
596
+
597
+ tools_to_call = response.dig('required_action', 'submit_tool_outputs', 'tool_calls')
598
+
599
+ my_tool_outputs = tools_to_call.map { |tool|
600
+ # Call the functions based on the tool's name
601
+ function_name = tool.dig('function', 'name')
602
+ arguments = JSON.parse(
603
+ tool.dig("function", "arguments"),
604
+ { symbolize_names: true },
605
+ )
606
+
607
+ tool_output = case function_name
608
+ when "get_current_weather"
609
+ get_current_weather(**arguments)
610
+ end
611
+
612
+ { tool_call_id: tool['id'], output: tool_output }
613
+ }
614
+
615
+ client.runs.submit_tool_outputs(thread_id: thread_id, run_id: run_id, parameters: { tool_outputs: my_tool_outputs })
616
+ end
617
+ ```
618
+
619
+ Note that you have 10 minutes to submit your tool output before the run expires.
620
+
621
+ ### Image Generation
622
+
623
+ Generate an image using DALL·E! The size of any generated images must be one of `256x256`, `512x512` or `1024x1024` -
624
+ if not specified the image will default to `1024x1024`.
625
+
626
+ ```ruby
627
+ response = client.images.generate(parameters: { prompt: "A baby sea otter cooking pasta wearing a hat of some sort", size: "256x256" })
628
+ puts response.dig("data", 0, "url")
629
+ # => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
630
+ ```
631
+
632
+ ![Ruby](https://i.ibb.co/6y4HJFx/img-d-Tx-Rf-RHj-SO5-Gho-Cbd8o-LJvw3.png)
633
+
634
+ ### Image Edit
635
+
636
+ Fill in the transparent part of an image, or upload a mask with transparent sections to indicate the parts of an image that can be changed according to your prompt...
637
+
638
+ ```ruby
639
+ response = client.images.edit(parameters: { prompt: "A solid red Ruby on a blue background", image: "image.png", mask: "mask.png" })
640
+ puts response.dig("data", 0, "url")
641
+ # => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
642
+ ```
643
+
644
+ ![Ruby](https://i.ibb.co/sWVh3BX/dalle-ruby.png)
645
+
646
+ ### Image Variations
647
+
648
+ Create n variations of an image.
649
+
650
+ ```ruby
651
+ response = client.images.variations(parameters: { image: "image.png", n: 2 })
652
+ puts response.dig("data", 0, "url")
653
+ # => "https://oaidalleapiprodscus.blob.core.windows.net/private/org-Rf437IxKhh..."
654
+ ```
655
+
656
+ ![Ruby](https://i.ibb.co/TWJLP2y/img-miu-Wk-Nl0-QNy-Xtj-Lerc3c0l-NW.png)
657
+ ![Ruby](https://i.ibb.co/ScBhDGB/img-a9-Be-Rz-Au-Xwd-AV0-ERLUTSTGdi.png)
658
+
659
+ ### Moderations
660
+
661
+ Pass a string to check if it violates OpenAI's Content Policy:
662
+
663
+ ```ruby
664
+ response = client.moderations(parameters: { input: "I'm worried about that." })
665
+ puts response.dig("results", 0, "category_scores", "hate")
666
+ # => 5.505014632944949e-05
667
+ ```
668
+
669
+ ### Whisper
670
+
671
+ Whisper is a speech to text model that can be used to generate text based on audio files:
672
+
673
+ #### Translate
674
+
675
+ The translations API takes as input the audio file in any of the supported languages and transcribes the audio into English.
676
+
677
+ ```ruby
678
+ response = client.audio.translate(
679
+ parameters: {
680
+ model: "whisper-1",
681
+ file: File.open("path_to_file", "rb"),
682
+ })
683
+ puts response["text"]
684
+ # => "Translation of the text"
685
+ ```
686
+
687
+ #### Transcribe
688
+
689
+ The transcriptions API takes as input the audio file you want to transcribe and returns the text in the desired output file format.
690
+
691
+ ```ruby
692
+ response = client.audio.transcribe(
693
+ parameters: {
694
+ model: "whisper-1",
695
+ file: File.open("path_to_file", "rb"),
696
+ })
697
+ puts response["text"]
698
+ # => "Transcription of the text"
699
+ ```
700
+
701
+ #### Speech
702
+
703
+ The speech API takes as input the text and a voice and returns the content of an audio file you can listen to.
704
+
705
+ ```ruby
706
+ response = client.audio.speech(
707
+ parameters: {
708
+ model: "tts-1",
709
+ input: "This is a speech test!",
710
+ voice: "alloy"
711
+ }
712
+ )
713
+ File.binwrite('demo.mp3', response)
714
+ # => mp3 file that plays: "This is a speech test!"
715
+ ```
716
+
717
+ ### Errors
718
+
719
+ HTTP errors can be caught like this:
720
+
721
+ ```
722
+ begin
723
+ OpenAI::Client.new.models.retrieve(id: "text-ada-001")
724
+ rescue Faraday::Error => e
725
+ raise "Got a Faraday error: #{e}"
726
+ end
727
+ ```
728
+
729
+ ## Development
730
+
731
+ After checking out the repo, run `bin/setup` to install dependencies. You can run `bin/console` for an interactive prompt that will allow you to experiment.
732
+
733
+ To install this gem onto your local machine, run `bundle exec rake install`.
734
+
735
+ ### Warning
736
+
737
+ If you have an `OPENAI_ACCESS_TOKEN` in your `ENV`, running the specs will use this to run the specs against the actual API, which will be slow and cost you money - 2 cents or more! Remove it from your environment with `unset` or similar if you just want to run the specs against the stored VCR responses.
738
+
739
+ ## Release
740
+
741
+ First run the specs without VCR so they actually hit the API. This will cost 2 cents or more. Set OPENAI_ACCESS_TOKEN in your environment or pass it in like this:
742
+
743
+ ```
744
+ OPENAI_ACCESS_TOKEN=123abc bundle exec rspec
745
+ ```
746
+
747
+ Then update the version number in `version.rb`, update `CHANGELOG.md`, run `bundle install` to update Gemfile.lock, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).